Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
despiegk committed Feb 9, 2025
1 parent 1d631fe commit 7b69719
Show file tree
Hide file tree
Showing 38 changed files with 818 additions and 938 deletions.
Binary file added doc
Binary file not shown.
Binary file removed examples/clients/mycelium
Binary file not shown.
21 changes: 10 additions & 11 deletions examples/clients/mycelium.vsh
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run

import freeflowuniverse.herolib.clients.mycelium
import freeflowuniverse.herolib.clients.mycelium

mycelium.delete()!

mut r:=mycelium.inspect()!
println("My pub key: ${r.public_key}")
mut r := mycelium.inspect()!
println('My pub key: ${r.public_key}')

mut client := mycelium.get()!
println(client)

// Send a message to a node by public key
// Parameters: public_key, payload, topic, wait_for_reply
msg := client.send_msg(
r.public_key, // destination public key
'Hello World', // message payload
'greetings', // optional topic
false // wait for reply
)!
msg := client.send_msg(r.public_key, // destination public key
'Hello World', // message payload
'greetings', // optional topic
false // wait for reply
)!

println('Sent message ID: ${msg.id}')
println("send succeeded")
println('send succeeded')

// Receive messages
// Parameters: wait_for_message, peek_only, topic_filter
Expand All @@ -40,4 +39,4 @@ println('Message payload: ${received.payload}')
// status := client.get_msg_status(msg.id)!
// println('Message status: ${status.state}')
// println('Created at: ${status.created}')
// println('Expires at: ${status.deadline}')
// println('Expires at: ${status.deadline}')
10 changes: 4 additions & 6 deletions examples/develop/gittools/gittools_path_get.vsh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import freeflowuniverse.herolib.develop.gittools
import freeflowuniverse.herolib.osal
import time


mut gs := gittools.new()!
mydocs_path:=gs.get_path(
pull:true,
reset:false,
url:'https://git.ourworld.tf/tfgrid/info_docs_depin/src/branch/main/docs'
mydocs_path := gs.get_path(
pull: true
reset: false
url: 'https://git.ourworld.tf/tfgrid/info_docs_depin/src/branch/main/docs'
)!

println(mydocs_path)

32 changes: 14 additions & 18 deletions examples/installers/mycelium.vsh
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run

import freeflowuniverse.herolib.installers.net.mycelium as mycelium_installer
import freeflowuniverse.herolib.clients.mycelium
import freeflowuniverse.herolib.clients.mycelium

mut installer:=mycelium_installer.get()!
mut installer := mycelium_installer.get()!
installer.start()!

mut r:=mycelium.inspect()!
mut r := mycelium.inspect()!
println(r)



mut client := mycelium.get()!

// Send a message to a node by public key
// Parameters: public_key, payload, topic, wait_for_reply
msg := client.send_msg(
'abc123...', // destination public key
'Hello World', // message payload
'greetings', // optional topic
true // wait for reply
)!
msg := client.send_msg('abc123...', // destination public key
'Hello World', // message payload
'greetings', // optional topic
true // wait for reply
)!
println('Sent message ID: ${msg.id}')

// Receive messages
Expand All @@ -30,15 +27,14 @@ println('Received message from: ${received.src_pk}')
println('Message payload: ${received.payload}')

// Reply to a message
client.reply_msg(
received.id, // original message ID
received.src_pk, // sender's public key
'Got your message!', // reply payload
'greetings' // topic
)!
client.reply_msg(received.id, // original message ID
received.src_pk, // sender's public key
'Got your message!', // reply payload
'greetings' // topic
)!

// Check message status
status := client.get_msg_status(msg.id)!
println('Message status: ${status.state}')
println('Created at: ${status.created}')
println('Expires at: ${status.deadline}')
println('Expires at: ${status.deadline}')
2 changes: 1 addition & 1 deletion examples/installers/zinit_installer.vsh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

import freeflowuniverse.herolib.installers.sysadmintools.zinit as zinit_installer

mut installer:=zinit_installer.get()!
mut installer := zinit_installer.get()!
installer.start()!
33 changes: 16 additions & 17 deletions examples/osal/tun.vsh
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@

import freeflowuniverse.herolib.osal.tun


// Check if TUN is available
if available := tun.available() {
if available {
println('TUN is available on this system')
// Get a free TUN interface name
if interface_name := tun.free() {
println('Found free TUN interface: ${interface_name}')
// Example: Now you could use this interface name
// to set up your tunnel
} else {
println('Error finding free interface: ${err}')
}
} else {
println('TUN is not available on this system')
}
if available {
println('TUN is available on this system')

// Get a free TUN interface name
if interface_name := tun.free() {
println('Found free TUN interface: ${interface_name}')

// Example: Now you could use this interface name
// to set up your tunnel
} else {
println('Error finding free interface: ${err}')
}
} else {
println('TUN is not available on this system')
}
} else {
println('Error checking TUN availability: ${err}')
println('Error checking TUN availability: ${err}')
}
6 changes: 3 additions & 3 deletions examples/virt/podman_buildah/buildah_run.vsh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import freeflowuniverse.herolib.core.base
import time
import os

//herocompile means we do it for the host system
// herocompile means we do it for the host system
mut pm := herocontainers.new(herocompile: false, install: false)!

//pm.builder_base(reset:true)!
// pm.builder_base(reset:true)!

mut builder := pm.builder_get('base')!
builder.shell()!
Expand All @@ -22,7 +22,7 @@ println(builder)
// bash & python can be executed directly in build container

// any of the herocommands can be executed like this
//mybuildcontainer.run(cmd: 'installers -n heroweb', runtime: .herocmd)!
// mybuildcontainer.run(cmd: 'installers -n heroweb', runtime: .herocmd)!

// //following will execute heroscript in the buildcontainer
// mybuildcontainer.run(
Expand Down
11 changes: 5 additions & 6 deletions examples/webtools/docusaurus/docusaurus_example.vsh
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import freeflowuniverse.herolib.web.docusaurus

// Create a new docusaurus factory
mut docs := docusaurus.new(
// build_path: '/tmp/docusaurus_build'
build_path: '/tmp/docusaurus_build'
)!

// Create a new docusaurus site
mut site := docs.dev(
url:'https://git.ourworld.tf/despiegk/docs_kristof'
url: 'https://git.ourworld.tf/despiegk/docs_kristof'
)!


//FOR FUTURE TO ADD CONTENT FROM DOCTREE
// FOR FUTURE TO ADD CONTENT FROM DOCTREE

// Create a doctree for content
// mut tree := doctree.new(name: 'content')!
Expand All @@ -34,10 +33,10 @@ mut site := docs.dev(
// )!

// Build the docusaurus site
//site.build()!
// site.build()!

// Generate the static site
//site.generate()!
// site.generate()!

// Optionally open the site in a browser
// site.open()!
68 changes: 35 additions & 33 deletions lib/clients/mycelium/mycelium.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import freeflowuniverse.herolib.core.httpconnection
// Represents a destination for a message, can be either IP or public key
pub struct MessageDestination {
pub:
ip string @[omitempty] // IP in the subnet of the receiver node
pk string @[omitempty] // hex encoded public key of the receiver node
ip string @[omitempty] // IP in the subnet of the receiver node
pk string @[omitempty] // hex encoded public key of the receiver node
}

// Body of a message to be sent
Expand All @@ -29,12 +29,12 @@ pub:
pub struct InboundMessage {
pub:
id string
src_ip string @[json: 'srcIp'] // Sender overlay IP address
src_pk string @[json: 'srcPk'] // Sender public key, hex encoded
dst_ip string @[json: 'dstIp'] // Receiver overlay IP address
dst_pk string @[json: 'dstPk'] // Receiver public key, hex encoded
topic string // Optional message topic
payload string // Message payload, base64 encoded
src_ip string @[json: 'srcIp'] // Sender overlay IP address
src_pk string @[json: 'srcPk'] // Sender public key, hex encoded
dst_ip string @[json: 'dstIp'] // Receiver overlay IP address
dst_pk string @[json: 'dstPk'] // Receiver public key, hex encoded
topic string // Optional message topic
payload string // Message payload, base64 encoded
}

// Information about an outbound message
Expand All @@ -44,7 +44,7 @@ pub:
state string // pending, received, read, aborted or sending object
created i64 // Unix timestamp of creation
deadline i64 // Unix timestamp of expiry
msg_len int @[json: 'msgLen'] // Length in bytes
msg_len int @[json: 'msgLen'] // Length in bytes
}

// General information about a node
Expand All @@ -63,36 +63,37 @@ pub:
pub fn (mut self Mycelium) connection() !&httpconnection.HTTPConnection {
mut c := self.conn or {
mut c2 := httpconnection.new(
name: 'mycelium'
url: self.server_url
name: 'mycelium'
url: self.server_url
retry: 3
)!
c2
}

return c
}

// Send a message to a node identified by public key
pub fn (mut self Mycelium) send_msg(pk string, payload string, topic string, wait bool) !InboundMessage {
mut conn := self.connection()!
mut body := PushMessageBody{
dst: MessageDestination{
dst: MessageDestination{
pk: pk
ip: ''
}
payload: base64.encode_str(payload)
topic: base64.encode_str(topic)
topic: base64.encode_str(topic)
}
mut prefix := '/api/v1/messages'
if wait {
prefix += '?reply_timeout=120'
prefix += '?reply_timeout=120'
}
return conn.post_json_generic[InboundMessage](
method: .post
prefix: prefix
data: json.encode(body)
method: .post
prefix: prefix
data: json.encode(body)
dataformat: .json
)!
)!
}

// Receive a message from the queue
Expand All @@ -109,8 +110,8 @@ pub fn (mut self Mycelium) receive_msg(wait bool, peek bool, topic string) !Inbo
prefix += 'topic=${base64.encode_str(topic)}'
}
return conn.get_json_generic[InboundMessage](
method: .get
prefix: prefix
method: .get
prefix: prefix
dataformat: .json
)!
}
Expand All @@ -129,8 +130,8 @@ pub fn (mut self Mycelium) receive_msg_opt(wait bool, peek bool, topic string) ?
prefix += 'topic=${base64.encode_str(topic)}'
}
res := conn.get_json_generic[InboundMessage](
method: .get
prefix: prefix
method: .get
prefix: prefix
dataformat: .json
) or {
if err.msg().contains('204') {
Expand All @@ -145,8 +146,8 @@ pub fn (mut self Mycelium) receive_msg_opt(wait bool, peek bool, topic string) ?
pub fn (mut self Mycelium) get_msg_status(id string) !MessageStatusResponse {
mut conn := self.connection()!
return conn.get_json_generic[MessageStatusResponse](
method: .get
prefix: '/api/v1/messages/status/${id}'
method: .get
prefix: '/api/v1/messages/status/${id}'
dataformat: .json
)!
}
Expand All @@ -155,28 +156,29 @@ pub fn (mut self Mycelium) get_msg_status(id string) !MessageStatusResponse {
pub fn (mut self Mycelium) reply_msg(id string, pk string, payload string, topic string) ! {
mut conn := self.connection()!
mut body := PushMessageBody{
dst: MessageDestination{
dst: MessageDestination{
pk: pk
ip: ''
}
payload: base64.encode_str(payload)
topic: base64.encode_str(topic)
topic: base64.encode_str(topic)
}
_ := conn.post_json_generic[MessageDestination](
method: .post
prefix: '/api/v1/messages/reply/${id}'
data: json.encode(body)
method: .post
prefix: '/api/v1/messages/reply/${id}'
data: json.encode(body)
dataformat: .json
)!
}

// curl -v -H 'Content-Type: application/json' -d '{"dst": {"pk": "be4bf135d60b7e43a46be1ad68f955cdc1209a3c55dc30d00c4463b1dace4377"}, "payload": "xuV+"}' http://localhost:8989/api/v1/messages\

// Get node info
pub fn (mut self Mycelium) get_info() !Info {
mut conn := self.connection()!
return conn.get_json_generic[Info](
method: .get
prefix: '/api/v1/admin'
method: .get
prefix: '/api/v1/admin'
dataformat: .json
)!
}
Expand All @@ -185,8 +187,8 @@ pub fn (mut self Mycelium) get_info() !Info {
pub fn (mut self Mycelium) get_pubkey_from_ip(ip string) !PublicKeyResponse {
mut conn := self.connection()!
return conn.get_json_generic[PublicKeyResponse](
method: .get
prefix: '/api/v1/pubkey/${ip}'
method: .get
prefix: '/api/v1/pubkey/${ip}'
dataformat: .json
)!
}
Loading

0 comments on commit 7b69719

Please sign in to comment.