Skip to content

Commit

Permalink
feat: Add example scripts for Mycelium inter-node communication
Browse files Browse the repository at this point in the history
- Added `deduped_mycelium_master.vsh` to demonstrate a master node
  sending data.
- Added `deduped_mycelium_slave.vsh` to demonstrate a slave node
  receiving data.  These scripts showcase basic inter-node
  communication using the Mycelium library.
  • Loading branch information
Mahmoud-Emad committed Feb 27, 2025
1 parent 38cd933 commit a0b5312
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
72 changes: 72 additions & 0 deletions examples/data/deduped_mycelium_master.vsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/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.installers.net.mycelium_installer
import freeflowuniverse.herolib.data.ourdb
import freeflowuniverse.herolib.osal
import time
import os
import encoding.base64
import json

// NOTE: Before running this script, ensure that the mycelium binary is installed and in the PATH

const master_port = 9000
const slave_public_key = '46a9f9cee1ce98ef7478f3dea759589bbf6da9156533e63fed9f233640ac072c'
const slave_address = '59c:28ee:8597:6c20:3b2f:a9ee:2e18:9d4f'

// Struct to hold data for syncing
struct SyncData {
id u32
data string
topic string = 'db_sync'
}

mycelium.delete()!

// Initialize mycelium clients
mut master := mycelium.get()!
master.server_url = 'http://localhost:${master_port}'
master.name = 'master_node'

// Get public keys for communication
master_inspect := mycelium.inspect(key_file_path: '/tmp/mycelium_server1/priv_key.bin')!

println('Server 1 (Master Node) public key: ${master_inspect.public_key}')

// Initialize ourdb instances
mut db := ourdb.new(
record_nr_max: 16777216 - 1
record_size_max: 1024
path: '/tmp/ourdb1'
reset: true
)!

defer {
db.destroy() or { panic('failed to destroy db1: ${err}') }
}

// Store in master db
println('\nStoring data in master node DB...')
data := 'Test data for sync - ' + time.now().str()
id := db.set(data: data.bytes())!
println('Successfully stored data in master node DB with ID: ${id}')

// Create sync data
sync_data := SyncData{
id: id
data: data
}

// Convert to JSON
json_data := json.encode(sync_data)

// Send sync message to slave
println('\nSending sync message to slave...')
msg := master.send_msg(
public_key: '46a9f9cee1ce98ef7478f3dea759589bbf6da9156533e63fed9f233640ac072c'
payload: json_data
topic: 'db_sync'
)!

println('Sync message sent with ID: ${msg.id} to slave with public key: 46a9f9cee1ce98ef7478f3dea759589bbf6da9156533e63fed9f233640ac072c')
78 changes: 78 additions & 0 deletions examples/data/deduped_mycelium_slave.vsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/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.installers.net.mycelium_installer
import freeflowuniverse.herolib.data.ourdb
import freeflowuniverse.herolib.osal
import time
import os
import encoding.base64
import json

// NOTE: Before running this script, ensure that the mycelium binary is installed and in the PATH

const slave_port = 9001
const master_public_key = '89c2eeb24bcdfaaac78c0023a166d88f760c097c1a57748770e432ba10757179'
const master_address = '458:90d4:a3ef:b285:6d32:a22d:9e73:697f'

// Struct to hold data for syncing
struct SyncData {
id u32
data string
topic string = 'db_sync'
}

mycelium.delete()!

// Initialize mycelium clients
mut slave := mycelium.get()!
slave.server_url = 'http://localhost:${slave_port}'
slave.name = 'slave_node'

// Get public keys for communication
slave_inspect := mycelium.inspect(key_file_path: '/tmp/mycelium_server1/priv_key.bin')!

println('Server 2 (slave Node) public key: ${slave_inspect.public_key}')

// Initialize ourdb instances
mut db := ourdb.new(
record_nr_max: 16777216 - 1
record_size_max: 1024
path: '/tmp/ourdb1'
reset: true
)!

defer {
db.destroy() or { panic('failed to destroy db1: ${err}') }
}

// Receive messages
// Parameters: wait_for_message, peek_only, topic_filter
received := slave.receive_msg(wait: true, peek: false, topic: 'db_sync')!
println('Received message from: ${received.src_pk}')
println('Message payload: ${base64.decode_str(received.payload)}')

// // Store in slave db
// println('\nStoring data in slave node DB...')
// data := 'Test data for sync - ' + time.now().str()
// id := db.set(data: data.bytes())!
// println('Successfully stored data in slave node DB with ID: ${id}')

// // Create sync data
// sync_data := SyncData{
// id: id
// data: data
// }

// // Convert to JSON
// json_data := json.encode(sync_data)

// // Send sync message to slave
// println('\nSending sync message to slave...')
// msg := slave.send_msg(
// public_key: '46a9f9cee1ce98ef7478f3dea759589bbf6da9156533e63fed9f233640ac072c'
// payload: json_data
// topic: 'db_sync'
// )!

// println('Sync message sent with ID: ${msg.id} to slave with public key: 46a9f9cee1ce98ef7478f3dea759589bbf6da9156533e63fed9f233640ac072c')

0 comments on commit a0b5312

Please sign in to comment.