V3 How do we connect two nodes? #177
Replies: 2 comments 1 reply
-
Just posting what I had, to test that ping-pong, what ping package main
import (
"fmt"
"log"
"time"
"ergo.services/ergo"
"ergo.services/ergo/act"
"ergo.services/ergo/gen"
)
type Ping struct {
act.Actor
}
func (p *Ping) HandleCall(from gen.PID, ref gen.Ref, request any) (any, error) {
switch request.(type) {
case string:
log.Println("Ping received pong, sending ping")
return "ping", nil
}
return nil, fmt.Errorf("unexpected request: %#v", request)
}
func (p *Ping) HandleMessage(from gen.PID, message any) error {
switch message.(type) {
case string:
log.Println("Ping received pong, sending ping")
_, err := p.Call(from, "ping")
return err
}
return fmt.Errorf("unexpected message: %#v", message)
}
func NewPing() gen.ProcessBehavior {
return &Ping{}
}
func main() {
node, err := ergo.StartNode("ping@localhost", gen.NodeOptions{
Network: gen.NetworkOptions{
Cookie: "cookie",
InsecureSkipVerify: true,
},
})
if err != nil {
log.Fatalf("Failed to start ping: %v", err)
}
defer node.Stop()
node.Spawn(NewPing, gen.ProcessOptions{})
go func() {
time.Sleep(1 * time.Second)
if err = node.Send("pong@localhost", "ping"); err != nil {
fmt.Printf("Unable to send message to node '%s': %s\n", "pong@localhost", err)
return
}
}()
node.Wait()
}
pong package main
import (
"fmt"
"log"
"ergo.services/ergo"
"ergo.services/ergo/act"
"ergo.services/ergo/gen"
)
type Pong struct {
act.Actor
}
func (p *Pong) HandleCall(from gen.PID, ref gen.Ref, request any) (any, error) {
switch request.(type) {
case string:
log.Println("Pong received ping, sending pong")
return "pong", nil
}
return nil, fmt.Errorf("unexpected request: %#v", request)
}
func (p *Pong) HandleMessage(from gen.PID, message any) error {
switch message.(type) {
case string:
log.Println("Pong received ping, sending pong")
_, err := p.Call(from, "pong")
return err
}
return fmt.Errorf("unexpected message: %#v", message)
}
func NewPong() gen.ProcessBehavior {
return &Pong{}
}
func main() {
node, err := ergo.StartNode("pong@localhost", gen.NodeOptions{
Network: gen.NetworkOptions{
Cookie: "cookie",
InsecureSkipVerify: true,
},
})
if err != nil {
log.Fatalf("Failed to start pong: %v", err)
}
defer node.Stop()
node.Spawn(NewPong, gen.ProcessOptions{})
node.Wait()
}
|
Beta Was this translation helpful? Give feedback.
-
Things that need to be fixed in your example:
Beside that...
Please, take a look at "call" example https://github.com/ergo-services/examples/tree/v300. It does exactly what you want: |
Beta Was this translation helpful? Give feedback.
-
I've been looking at the v3 codebase and played with the ergo_examples repo on the v3 branch, there are lot of examples on how to setup one node with all the tooling
ergo
is providing (udp, tcp, http, web server, etc), so far so good!I know you're going to release the documentation soon but in the same time I would like to continue playing with it!
I saw there is a "registrar" that is supposed to help to connect nodes, I'm a little bit confused, I tried but i end up with
err
being filled for various reasons, I'm pretty sure i'm missing one little thing!Do you have a simple code snippet of a simple ping-pong between two different runtimes? 🙏
Beta Was this translation helpful? Give feedback.
All reactions