diff options
author | Determinant <[email protected]> | 2019-06-07 19:32:54 -0400 |
---|---|---|
committer | Determinant <[email protected]> | 2019-06-07 19:32:54 -0400 |
commit | 4a37b259717e545c090fb11b6a36f7e75118c2f9 (patch) | |
tree | 7bc20ad884e53c59f4a99bc2240159142cfb816f /test_msgnet | |
parent | 316b61a15ff334a27462715ed4c99cf244a25701 (diff) |
...
Diffstat (limited to 'test_msgnet')
-rw-r--r-- | test_msgnet/main.go | 145 | ||||
-rw-r--r-- | test_msgnet/main_.go | 7 |
2 files changed, 152 insertions, 0 deletions
diff --git a/test_msgnet/main.go b/test_msgnet/main.go new file mode 100644 index 0000000..b9a5048 --- /dev/null +++ b/test_msgnet/main.go @@ -0,0 +1,145 @@ +package main + +// void onTerm_cgo(int sig); +import "C" + +import ( + "unsafe" + "encoding/binary" + "fmt" + "salticidae-go" +) + +var ec salticidae.EventContext +const ( + MSG_OPCODE_HELLO salticidae.Opcode = iota + MSG_OPCODE_ACK +) + +//export onTerm +func onTerm(_ int) { + ec.Stop() +} + +type MsgHello struct { + name string + text string +} + +func msgHelloSerialize(name string, text string) salticidae.Msg { + serialized := salticidae.NewDataStream() + t := make([]byte, 4) + binary.LittleEndian.PutUint32(t, uint32(len(name))) + serialized.PutData(t) + serialized.PutData([]byte(name)) + serialized.PutData([]byte(text)) + return salticidae.NewMsg(MSG_OPCODE_HELLO, serialized.ToByteArray()) +} + +func msgHelloUnserialize(msg salticidae.Msg) MsgHello { + p := msg.GetPayload() + length := binary.LittleEndian.Uint32(p.GetData(4)) + name := string(p.GetData(int(length))) + text := string(p.GetData(p.Size())) + p.Free() + return MsgHello { name: name, text: text } +} + +func msgAckSerialize() salticidae.Msg { + return salticidae.NewMsg(MSG_OPCODE_ACK, salticidae.NewByteArray()) +} + +type MyNet struct { + net salticidae.MsgNetwork + name string +} + +var alice, bob MyNet + +func onReceiveHello(_msg salticidae.Msg, conn salticidae.MsgNetworkConn) { + net := conn.GetNet() + name := bob.name + if net == alice.net.GetInner() { + name = alice.name + } + msg := msgHelloUnserialize(_msg) + fmt.Printf("[%s] %s says %s\n", name, msg.name, msg.text) + ack := msgAckSerialize() + net.SendMsg(ack, conn) + ack.Free() +} + +func onReceiveAck(msg salticidae.Msg, conn salticidae.MsgNetworkConn) { + net := conn.GetNet() + name := bob.name + if net == alice.net.GetInner() { + name = alice.name + } + fmt.Printf("[%s] the peer knows\n", name) +} + +func connHandler(conn salticidae.MsgNetworkConn, connected bool) { + net := conn.GetNet() + n := &bob + if net == alice.net.GetInner() { + n = &alice + } + name := n.name + if connected { + if conn.GetMode() == salticidae.CONN_MODE_ACTIVE { + fmt.Printf("[%s] Connected, sending hello.", name) + hello := msgHelloSerialize(name, "Hello there!") + n.net.SendMsg(hello, conn) + hello.Free() + } else { + fmt.Printf("[%s] Accepted, waiting for greetings.\n", name) + } + } else { + fmt.Printf("[%s] Disconnected, retrying.\n", name) + net.Connect(conn.GetAddr()) + } +} + +func genMyNet(ec salticidae.EventContext, name string) MyNet { + netconfig := salticidae.NewMsgNetworkConfig() + n := MyNet { + net: salticidae.NewMsgNetwork(ec, netconfig), + name: name, + } + netconfig.Free() + return n +} + +func main() { + ec = salticidae.NewEventContext() + alice_addr := salticidae.NewAddrFromIPPortString("127.0.0.1:12345") + bob_addr := salticidae.NewAddrFromIPPortString("127.0.0.1:12346") + + alice := genMyNet(ec, "Alice") + bob := genMyNet(ec, "Bob") + + alice.net.Start() + bob.net.Start() + + alice.net.Listen(alice_addr) + bob.net.Listen(bob_addr) + + alice.net.Connect(bob_addr) + bob.net.Connect(alice_addr) + + alice_addr.Free() + bob_addr.Free() + + ev_int := salticidae.NewSigEvent(ec, salticidae.SigEventCallback(unsafe.Pointer(C.onTerm_cgo))) + ev_int.Add(salticidae.SIGINT) + ev_term := salticidae.NewSigEvent(ec, salticidae.SigEventCallback(unsafe.Pointer(C.onTerm_cgo))) + ev_term.Add(salticidae.SIGTERM) + + ec.Dispatch() + + ev_int.Free() + ev_term.Free() + alice.net.Free() + bob.net.Free() + ec.Free() +} diff --git a/test_msgnet/main_.go b/test_msgnet/main_.go new file mode 100644 index 0000000..2f53d93 --- /dev/null +++ b/test_msgnet/main_.go @@ -0,0 +1,7 @@ +package main + +// void onTerm_cgo(int sig) { +// void onTerm(int); +// onTerm(sig); +// } +import "C" |