...
This commit is contained in:
@@ -306,73 +306,3 @@ fn (mut e Election) heartbeat_loop() {
|
||||
time.sleep(heartbeat_interval_ms * time.millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
// --- MAIN ---
|
||||
|
||||
fn main() {
|
||||
if os.args.len < 3 {
|
||||
eprintln('Usage: ./prog <node_id> <status>')
|
||||
eprintln(' status: active|buffer')
|
||||
return
|
||||
}
|
||||
node_id := os.args[1]
|
||||
status_str := os.args[2]
|
||||
|
||||
status := match status_str {
|
||||
'active' { NodeStatus.active }
|
||||
'buffer' { NodeStatus.buffer }
|
||||
else {
|
||||
eprintln('Invalid status. Use: active|buffer')
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// --- Generate ephemeral keys for demo ---
|
||||
// In real use: load from PEM files
|
||||
priv, pub := ed25519.generate_key(rand.reader) or { panic(err) }
|
||||
|
||||
mut pubkeys := map[string]ed25519.PublicKey{}
|
||||
pubkeys[node_id] = pub
|
||||
// TODO: load all pubkeys from config file so every node knows others
|
||||
|
||||
// Initialize all nodes (in real scenario, load from config)
|
||||
mut all_nodes := map[string]Node{}
|
||||
all_nodes['node1'] = Node{id: 'node1', status: .active}
|
||||
all_nodes['node2'] = Node{id: 'node2', status: .active}
|
||||
all_nodes['node3'] = Node{id: 'node3', status: .active}
|
||||
all_nodes['node4'] = Node{id: 'node4', status: .buffer}
|
||||
|
||||
// Set current node status
|
||||
all_nodes[node_id].status = status
|
||||
|
||||
servers := ['127.0.0.1:6379', '127.0.0.1:6380', '127.0.0.1:6381', '127.0.0.1:6382']
|
||||
mut conns := []redis.Connection{}
|
||||
for s in servers {
|
||||
mut c := redis.connect(redis.Options{ server: s }) or {
|
||||
panic('could not connect to redis $s: $err')
|
||||
}
|
||||
conns << c
|
||||
}
|
||||
|
||||
mut election := Election{
|
||||
clients: conns
|
||||
pubkeys: pubkeys
|
||||
self: Node{
|
||||
id: node_id
|
||||
term: 0
|
||||
leader: false
|
||||
status: status
|
||||
}
|
||||
keys: Keys{ priv: priv, pub: pub }
|
||||
all_nodes: all_nodes
|
||||
buffer_nodes: ['node4'] // Initially node4 is buffer
|
||||
}
|
||||
|
||||
println('[$node_id] started as $status_str, connected to 4 redis servers.')
|
||||
|
||||
// Start health monitoring in background
|
||||
go election.health_monitor_loop()
|
||||
|
||||
// Start main heartbeat loop
|
||||
election.heartbeat_loop()
|
||||
}
|
||||
@@ -45,46 +45,6 @@ pub mut:
|
||||
}
|
||||
|
||||
|
||||
@[heap]
|
||||
pub struct Comment {
|
||||
pub mut:
|
||||
id u32
|
||||
comment string
|
||||
parent u32 //id of parent comment if any, 0 means none
|
||||
updated_at i64
|
||||
author u32 //links to user
|
||||
}
|
||||
|
||||
|
||||
pub fn (self Comment) dump() ![]u8{
|
||||
// Create a new encoder
|
||||
mut e := encoder.new()
|
||||
e.add_u8(1)
|
||||
e.add_u32(self.id)
|
||||
e.add_string(self.comment)
|
||||
e.add_u32(self.parent)
|
||||
e.add_i64(self.updated_at)
|
||||
e.add_u32(self.author)
|
||||
return e.data
|
||||
}
|
||||
|
||||
|
||||
pub fn comment_load(self []u8) !Comment{
|
||||
// Create a new encoder
|
||||
mut e := decoder.new()
|
||||
version:=e.get_u8(1)
|
||||
if version != 1 {
|
||||
panic("wrong version in comment load")
|
||||
}
|
||||
self.id = e.get_u32()
|
||||
self.comment = e.get_string()
|
||||
self.parent = e.get_u32()
|
||||
self.updated_at = e.get_i64()
|
||||
self.author = e.get_u32()
|
||||
return e.data
|
||||
}
|
||||
|
||||
|
||||
/////////////////
|
||||
|
||||
@[params]
|
||||
@@ -98,12 +58,6 @@ pub mut:
|
||||
comments []CommentArg
|
||||
}
|
||||
|
||||
pub struct CommentArg {
|
||||
pub mut:
|
||||
comment string
|
||||
parent u32 //id of parent comment if any, 0 means none
|
||||
author u32 //links to user
|
||||
}
|
||||
|
||||
pub fn tags2id(tags []string) !u32 {
|
||||
mut myid:=0
|
||||
@@ -122,21 +76,6 @@ pub fn tags2id(tags []string) !u32 {
|
||||
return myid
|
||||
}
|
||||
|
||||
pub fn comment2id(args CommentArg) !u32{
|
||||
myid := redis.incr("db:comments:id")!
|
||||
mut o:=Comment {
|
||||
id:
|
||||
comment: args.comment
|
||||
parent:args.parent
|
||||
updated_at: ourtime.now().unix()
|
||||
author: args.author
|
||||
}
|
||||
data:=o.dump()!
|
||||
redis.hset("db:comments:data", myid, data)!
|
||||
return myid
|
||||
}
|
||||
|
||||
|
||||
pub fn [T] new(args BaseArgs) Base {
|
||||
|
||||
mut redis := redisclient.core_get()!
|
||||
|
||||
@@ -104,10 +104,10 @@ pub fn calendar_event_new(args CalendarEventArgs) CalendarEvent {
|
||||
}
|
||||
|
||||
pub fn (mut e CalendarEvent) dump() []u8 {
|
||||
//TODO: implement based on lib/data/encoder/readme.md
|
||||
//TODO: implement based on lib/data/encoder/readme.md and comment.v as example
|
||||
}
|
||||
|
||||
pub fn calendar_event_load(data []u8) CalendarEvent {
|
||||
//TODO: implement based on lib/data/encoder/readme.md
|
||||
//TODO: implement based on lib/data/encoder/readme.md and comment.v as example
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
module heromodels
|
||||
|
||||
import crypto.md5
|
||||
import freeflowuniverse.herolib.core.redisclient
|
||||
import json
|
||||
|
||||
import freeflowuniverse.herolib.core.redisclient
|
||||
import freeflowuniverse.herolib.data.encoder
|
||||
import freeflowuniverse.herolib.data.ourtime
|
||||
|
||||
|
||||
@[heap]
|
||||
@@ -30,19 +31,20 @@ pub fn (self Comment) dump() ![]u8{
|
||||
}
|
||||
|
||||
|
||||
pub fn comment_load(self []u8) !Comment{
|
||||
// Create a new encoder
|
||||
mut e := decoder.new()
|
||||
version:=e.get_u8(1)
|
||||
pub fn comment_load(data []u8) !Comment{
|
||||
// Create a new decoder
|
||||
mut e := encoder.decoder_new(data)
|
||||
version := e.get_u8()
|
||||
if version != 1 {
|
||||
panic("wrong version in comment load")
|
||||
}
|
||||
self.id = e.get_u32()
|
||||
self.comment = e.get_string()
|
||||
self.parent = e.get_u32()
|
||||
self.updated_at = e.get_i64()
|
||||
self.author = e.get_u32()
|
||||
return e.data
|
||||
mut comment := Comment{}
|
||||
comment.id = e.get_u32()
|
||||
comment.comment = e.get_string()
|
||||
comment.parent = e.get_u32()
|
||||
comment.updated_at = e.get_i64()
|
||||
comment.author = e.get_u32()
|
||||
return comment
|
||||
}
|
||||
|
||||
|
||||
@@ -64,23 +66,23 @@ pub fn comment_new(args CommentArg) !Comment{
|
||||
return o
|
||||
}
|
||||
|
||||
pub fn comment_set(o Comment) !u32{
|
||||
pub fn comment_set(mut o Comment) !u32{
|
||||
mut redis := redisclient.core_get()!
|
||||
myid := redis.incr("db:comments:id")!
|
||||
i.id = myid
|
||||
data:=o.dump()!
|
||||
o.id = myid
|
||||
data := o.dump()!
|
||||
redis.hset("db:comments:data", myid, data)!
|
||||
return myid
|
||||
}
|
||||
|
||||
pub fn comment_exist(id u32) !bool{
|
||||
mut redis := redisclient.core_get()!
|
||||
return redis.hexist("db:comments",id)!
|
||||
return redis.hexist("db:comments:data",id)!
|
||||
}
|
||||
|
||||
pub fn comment_get(id u32) !Comment{
|
||||
mut redis := redisclient.core_get()!
|
||||
mut data:= redis.hget("db:comments",id)!
|
||||
mut data:= redis.hget("db:comments:data",id)!
|
||||
if data.len>0{
|
||||
return comment_load(data)!
|
||||
}else{
|
||||
|
||||
Reference in New Issue
Block a user