From 4c7456063b467e7a8649434195ee446633d7e32a Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 12 Jun 2019 00:28:56 -0400 Subject: add README; clean up the examples --- README.rst | 14 ++++++++++ event.go | 4 +-- network.go | 2 +- test_msgnet/main.go | 70 ++++++++++++++++++++++--------------------------- test_p2p_stress/main.go | 19 +++++++------- 5 files changed, 58 insertions(+), 51 deletions(-) create mode 100644 README.rst diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..1e8af87 --- /dev/null +++ b/README.rst @@ -0,0 +1,14 @@ +salticidae-go +------------- + +Go bindings for Salticidae network library + +Build +----- + +:: + + go get github.com/Determinant/salticidae-go + cd $GOPATH/src/github.com/Determinant/salticidae-go/ + # make sure you already had salticidae dependecies installed (openssl and libuv) + make diff --git a/event.go b/event.go index aa1be2d..1c8a94e 100644 --- a/event.go +++ b/event.go @@ -44,8 +44,8 @@ type SigEventCallback = C.sigev_callback_t var SIGTERM = C.SIGTERM var SIGINT = C.SIGINT -func NewSigEvent(ec EventContext, cb SigEventCallback) SigEvent { - return C.sigev_new(ec, cb) +func NewSigEvent(ec EventContext, cb SigEventCallback, userdata rawptr_t) SigEvent { + return C.sigev_new(ec, cb, userdata) } func (self SigEvent) Add(sig int) { C.sigev_add(self, C.int(sig)) } diff --git a/network.go b/network.go index 10ab26c..b15c3ea 100644 --- a/network.go +++ b/network.go @@ -67,7 +67,7 @@ func (self MsgNetwork) Listen(addr NetAddr) { C.msgnetwork_listen(self, addr) } func (self MsgNetwork) Start() { C.msgnetwork_start(self) } func (self MsgNetwork) SendMsg(msg Msg, conn MsgNetworkConn) { C.msgnetwork_send_msg(self, msg, conn) } -func (self MsgNetwork) Connect(addr NetAddr) { C.msgnetwork_connect(self, addr) } +func (self MsgNetwork) Connect(addr NetAddr) MsgNetworkConn { return C.msgnetwork_connect(self, addr) } func (self MsgNetwork) Terminate(conn MsgNetworkConn) { C.msgnetwork_terminate(self, conn) } func (self MsgNetworkConn) Copy() MsgNetworkConn { return C.msgnetwork_conn_copy(self) } diff --git a/test_msgnet/main.go b/test_msgnet/main.go index 57ec533..6ed649b 100644 --- a/test_msgnet/main.go +++ b/test_msgnet/main.go @@ -2,7 +2,7 @@ package main // #cgo CFLAGS: -I${SRCDIR}/../salticidae/include/ // #include "salticidae/network.h" -// void onTerm(int sig); +// void onTerm(int sig, void *); // void onReceiveHello(msg_t *, msgnetwork_conn_t *, void *); // void onReceiveAck(msg_t *, msgnetwork_conn_t *, void *); // void connHandler(msgnetwork_conn_t *, bool, void *); @@ -15,22 +15,11 @@ import ( "github.com/Determinant/salticidae-go" ) -var ec salticidae.EventContext const ( MSG_OPCODE_HELLO salticidae.Opcode = iota MSG_OPCODE_ACK ) -//export onTerm -func onTerm(_ C.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) @@ -43,13 +32,13 @@ func msgHelloSerialize(name string, text string) salticidae.Msg { salticidae.NewByteArrayMovedFromDataStream(serialized)) } -func msgHelloUnserialize(msg salticidae.Msg) MsgHello { +func msgHelloUnserialize(msg salticidae.Msg) (name string, text string) { p := msg.ConsumePayload() length := binary.LittleEndian.Uint32(p.GetDataInPlace(4)) - name := string(p.GetDataInPlace(int(length))) - text := string(p.GetDataInPlace(p.Size())) + name = string(p.GetDataInPlace(int(length))) + text = string(p.GetDataInPlace(p.Size())) p.Free() - return MsgHello { name: name, text: text } + return } func msgAckSerialize() salticidae.Msg { @@ -61,19 +50,27 @@ type MyNet struct { name string } -var alice, bob MyNet +var ( + alice, bob MyNet + ec salticidae.EventContext +) + +//export onTerm +func onTerm(_ C.int, _ unsafe.Pointer) { + ec.Stop() +} //export onReceiveHello -func onReceiveHello(__msg *C.struct_msg_t, _conn *C.struct_msgnetwork_conn_t, _ unsafe.Pointer) { - _msg := salticidae.Msg(__msg) +func onReceiveHello(_msg *C.struct_msg_t, _conn *C.struct_msgnetwork_conn_t, _ unsafe.Pointer) { + msg := salticidae.Msg(_msg) conn := salticidae.MsgNetworkConn(_conn) net := conn.GetNet() - name := bob.name + myName := bob.name if net == alice.net { - name = alice.name + myName = alice.name } - msg := msgHelloUnserialize(_msg) - fmt.Printf("[%s] %s says %s\n", name, msg.name, msg.text) + name, text := msgHelloUnserialize(msg) + fmt.Printf("[%s] %s says %s\n", myName, name, text) ack := msgAckSerialize() net.SendMsg(ack, conn) ack.Free() @@ -110,7 +107,9 @@ func connHandler(_conn *C.struct_msgnetwork_conn_t, connected C.bool, _ unsafe.P } } else { fmt.Printf("[%s] Disconnected, retrying.\n", name) - net.Connect(conn.GetAddr()) + addr := conn.GetAddr() + net.Connect(addr).Free() + addr.Free() } } @@ -120,6 +119,9 @@ func genMyNet(ec salticidae.EventContext, name string) MyNet { net: salticidae.NewMsgNetwork(ec, netconfig), name: name, } + n.net.RegHandler(MSG_OPCODE_HELLO, salticidae.MsgNetworkMsgCallback(C.onReceiveHello), nil) + n.net.RegHandler(MSG_OPCODE_ACK, salticidae.MsgNetworkMsgCallback(C.onReceiveAck), nil) + n.net.RegConnHandler(salticidae.MsgNetworkConnCallback(C.connHandler), nil) netconfig.Free() return n } @@ -132,36 +134,28 @@ func main() { alice = genMyNet(ec, "Alice") bob = genMyNet(ec, "Bob") - alice.net.RegHandler(MSG_OPCODE_HELLO, salticidae.MsgNetworkMsgCallback(C.onReceiveHello), nil) - alice.net.RegHandler(MSG_OPCODE_ACK, salticidae.MsgNetworkMsgCallback(C.onReceiveAck), nil) - alice.net.RegConnHandler(salticidae.MsgNetworkConnCallback(C.connHandler), nil) - - bob.net.RegHandler(MSG_OPCODE_HELLO, salticidae.MsgNetworkMsgCallback(C.onReceiveHello), nil) - bob.net.RegHandler(MSG_OPCODE_ACK, salticidae.MsgNetworkMsgCallback(C.onReceiveAck), nil) - bob.net.RegConnHandler(salticidae.MsgNetworkConnCallback(C.connHandler), nil) - 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.net.Connect(bob_addr).Free() + bob.net.Connect(alice_addr).Free() alice_addr.Free() bob_addr.Free() - ev_int := salticidae.NewSigEvent(ec, salticidae.SigEventCallback(C.onTerm)) + ev_int := salticidae.NewSigEvent(ec, salticidae.SigEventCallback(C.onTerm), nil) ev_int.Add(salticidae.SIGINT) - ev_term := salticidae.NewSigEvent(ec, salticidae.SigEventCallback(C.onTerm)) + ev_term := salticidae.NewSigEvent(ec, salticidae.SigEventCallback(C.onTerm), nil) ev_term.Add(salticidae.SIGTERM) ec.Dispatch() - ev_int.Free() - ev_term.Free() alice.net.Free() bob.net.Free() + ev_int.Free() + ev_term.Free() ec.Free() } diff --git a/test_p2p_stress/main.go b/test_p2p_stress/main.go index c958a95..1051440 100644 --- a/test_p2p_stress/main.go +++ b/test_p2p_stress/main.go @@ -4,7 +4,7 @@ package main // #include // #include // #include "salticidae/network.h" -// void onTerm(int sig); +// void onTerm(int sig, void *); // void onReceiveRand(msg_t *, msgnetwork_conn_t *, void *); // void onReceiveAck(msg_t *, msgnetwork_conn_t *, void *); // void onStopLoop(threadcall_handle_t *, void *); @@ -32,7 +32,6 @@ import ( "strconv" ) -var ec salticidae.EventContext const ( MSG_OPCODE_RAND salticidae.Opcode = iota MSG_OPCODE_ACK @@ -140,8 +139,8 @@ func sendRand(size int, app *AppContext, conn salticidae.MsgNetworkConn) { var apps []AppContext var threads sync.WaitGroup - -var seg_buff_size = 4096 +var segBuffSize = 4096 +var ec salticidae.EventContext //export onTimeout func onTimeout(_ *C.timerev_t, userdata unsafe.Pointer) { @@ -188,7 +187,7 @@ func onReceiveAck(_msg *C.struct_msg_t, _conn *C.struct_msgnetwork_conn_t, userd } hash.Free() - if tc.state == seg_buff_size * 2 { + if tc.state == segBuffSize * 2 { sendRand(tc.state, app, conn) tc.state = -1 ctx := C.timeout_callback_context_new() @@ -206,7 +205,7 @@ func onReceiveAck(_msg *C.struct_msg_t, _conn *C.struct_msgnetwork_conn_t, userd tc.timer.Add(t) fmt.Printf("rand-bomboard phase, ending in %.2f secs\n", t) } else if tc.state == -1 { - sendRand(rand.Int() % (seg_buff_size * 10), app, conn) + sendRand(rand.Int() % (segBuffSize * 10), app, conn) } else { tc.state++ sendRand(tc.state, app, conn) @@ -237,7 +236,7 @@ func onStopLoop(_ *C.threadcall_handle_t, userdata unsafe.Pointer) { } //export onTerm -func onTerm(_ C.int) { +func onTerm(_ C.int, _ unsafe.Pointer) { for i, _ := range apps { a := &apps[i] a.tcall.AsyncCall( @@ -258,7 +257,7 @@ func main() { } netconfig := salticidae.NewPeerNetworkConfig() nc := netconfig.AsMsgNetworkConfig() - nc.SegBuffSize(4096) + nc.SegBuffSize(segBuffSize) nc.NWorker(2) netconfig.ConnTimeout(5) netconfig.PingPeriod(2) @@ -302,9 +301,9 @@ func main() { }() } - ev_int := salticidae.NewSigEvent(ec, salticidae.SigEventCallback(C.onTerm)) + ev_int := salticidae.NewSigEvent(ec, salticidae.SigEventCallback(C.onTerm), nil) ev_int.Add(salticidae.SIGINT) - ev_term := salticidae.NewSigEvent(ec, salticidae.SigEventCallback(C.onTerm)) + ev_term := salticidae.NewSigEvent(ec, salticidae.SigEventCallback(C.onTerm), nil) ev_term.Add(salticidae.SIGTERM) ec.Dispatch() -- cgit v1.2.3-70-g09d2