aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--demo/main.go44
-rw-r--r--demo/main_.go7
-rw-r--r--salticidae.go17
-rw-r--r--test_msgnet/main.go40
-rw-r--r--test_msgnet/main_.go7
5 files changed, 48 insertions, 67 deletions
diff --git a/demo/main.go b/demo/main.go
deleted file mode 100644
index 8dcbe98..0000000
--- a/demo/main.go
+++ /dev/null
@@ -1,44 +0,0 @@
-package main
-
-// void onTerm_cgo(int sig);
-import "C"
-
-import "salticidae-go"
-import "unsafe"
-
-var ec salticidae.EventContext
-
-//export onTerm
-func onTerm(_ int) {
- ec.Stop()
-}
-
-func run(ec salticidae.EventContext, my_addr string, other_addr string) salticidae.MsgNetwork {
- netconfig := salticidae.NewMsgNetworkConfig()
- net := salticidae.NewMsgNetwork(ec, netconfig)
- listen_addr := salticidae.NewAddrFromIPPortString(my_addr)
- connect_addr := salticidae.NewAddrFromIPPortString(other_addr)
-
- net.Start()
- net.Listen(listen_addr)
- net.Connect(connect_addr)
- return net
-}
-
-func main() {
- ec = salticidae.NewEventContext()
- 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)
-
- alice := "127.0.0.1:10000"
- bob := "127.0.0.1:10001"
- alice_net := run(ec, alice, bob)
- bob_net := run(ec, bob, alice)
- ec.Dispatch()
- ev_int.Free()
- ev_term.Free()
- alice_net.Free()
- bob_net.Free()
-}
diff --git a/demo/main_.go b/demo/main_.go
deleted file mode 100644
index 2f53d93..0000000
--- a/demo/main_.go
+++ /dev/null
@@ -1,7 +0,0 @@
-package main
-
-// void onTerm_cgo(int sig) {
-// void onTerm(int);
-// onTerm(sig);
-// }
-import "C"
diff --git a/salticidae.go b/salticidae.go
index ad80b88..01056a1 100644
--- a/salticidae.go
+++ b/salticidae.go
@@ -8,6 +8,10 @@ package salticidae
// #include "salticidae/netaddr.h"
// #include "salticidae/network.h"
// #include "salticidae/event.h"
+//
+// void salticidae_injected_msg_callback(const msg_t *msg, msgnetwork_conn_t *conn) {
+//
+// }
import "C"
import "unsafe"
@@ -54,6 +58,7 @@ type MsgNetworkInner = *C.struct_msgnetwork_t
type MsgNetwork struct {
inner MsgNetworkInner
+ msgHandlers map[Opcode] func(Msg, MsgNetworkConn)
}
type MsgNetworkConn = *C.struct_msgnetwork_conn_t
@@ -106,6 +111,17 @@ func (self MsgNetworkInner) Connect(addr NetAddr) {
C.msgnetwork_connect(self, addr)
}
+type MsgNetworkMsgCallback = C.msgnetwork_msg_callback_t
+type MsgNetworkConnCallback = C.msgnetwork_conn_callback_t
+
+func (self MsgNetwork) RegHandler(opcode Opcode, callback MsgNetworkMsgCallback) {
+ C.msgnetwork_reg_handler(self.inner, C._opcode_t(opcode), callback)
+}
+
+func (self MsgNetwork) RegConnHandler(callback MsgNetworkConnCallback) {
+ C.msgnetwork_reg_conn_handler(self.inner, callback)
+}
+
type SigEvent = *C.sigev_t
type SigEventCallback = C.sigev_callback_t
var SIGTERM = C.SIGTERM
@@ -117,3 +133,4 @@ func NewSigEvent(ec EventContext, cb SigEventCallback) SigEvent {
func (self SigEvent) Add(sig int) { C.sigev_add(self, C.int(sig)) }
func (self SigEvent) Free() { C.sigev_free(self) }
+
diff --git a/test_msgnet/main.go b/test_msgnet/main.go
index b9a5048..ce6d0d0 100644
--- a/test_msgnet/main.go
+++ b/test_msgnet/main.go
@@ -1,6 +1,13 @@
package main
+// void onReceiveAck(const msg_t *, const msgnetwork_conn_t *);
-// void onTerm_cgo(int sig);
+
+// #cgo CFLAGS: -I${SRCDIR}/../salticidae/include/
+// #include "salticidae/network.h"
+// void onTerm(int sig);
+// void onReceiveHello(msg_t *, msgnetwork_conn_t *);
+// void onReceiveAck(msg_t *, msgnetwork_conn_t *);
+// void connHandler(msgnetwork_conn_t *, bool);
import "C"
import (
@@ -17,7 +24,7 @@ const (
)
//export onTerm
-func onTerm(_ int) {
+func onTerm(_ C.int) {
ec.Stop()
}
@@ -56,7 +63,10 @@ type MyNet struct {
var alice, bob MyNet
-func onReceiveHello(_msg salticidae.Msg, conn salticidae.MsgNetworkConn) {
+//export onReceiveHello
+func onReceiveHello(__msg *C.struct_msg_t, _conn *C.struct_msgnetwork_conn_t) {
+ _msg := salticidae.Msg(__msg)
+ conn := salticidae.MsgNetworkConn(_conn)
net := conn.GetNet()
name := bob.name
if net == alice.net.GetInner() {
@@ -69,7 +79,9 @@ func onReceiveHello(_msg salticidae.Msg, conn salticidae.MsgNetworkConn) {
ack.Free()
}
-func onReceiveAck(msg salticidae.Msg, conn salticidae.MsgNetworkConn) {
+//export onReceiveAck
+func onReceiveAck(_ *C.struct_msg_t, _conn *C.struct_msgnetwork_conn_t) {
+ conn := salticidae.MsgNetworkConn(_conn)
net := conn.GetNet()
name := bob.name
if net == alice.net.GetInner() {
@@ -78,7 +90,9 @@ func onReceiveAck(msg salticidae.Msg, conn salticidae.MsgNetworkConn) {
fmt.Printf("[%s] the peer knows\n", name)
}
-func connHandler(conn salticidae.MsgNetworkConn, connected bool) {
+//export connHandler
+func connHandler(_conn *C.struct_msgnetwork_conn_t, connected C.bool) {
+ conn := salticidae.MsgNetworkConn(_conn)
net := conn.GetNet()
n := &bob
if net == alice.net.GetInner() {
@@ -115,8 +129,16 @@ func main() {
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 = genMyNet(ec, "Alice")
+ bob = genMyNet(ec, "Bob")
+
+ alice.net.RegHandler(MSG_OPCODE_HELLO, salticidae.MsgNetworkMsgCallback(unsafe.Pointer(C.onReceiveHello)))
+ alice.net.RegHandler(MSG_OPCODE_ACK, salticidae.MsgNetworkMsgCallback(unsafe.Pointer(C.onReceiveAck)))
+ alice.net.RegConnHandler(salticidae.MsgNetworkConnCallback(unsafe.Pointer(C.connHandler)))
+
+ bob.net.RegHandler(MSG_OPCODE_HELLO, salticidae.MsgNetworkMsgCallback(unsafe.Pointer(C.onReceiveHello)))
+ bob.net.RegHandler(MSG_OPCODE_ACK, salticidae.MsgNetworkMsgCallback(unsafe.Pointer(C.onReceiveAck)))
+ bob.net.RegConnHandler(salticidae.MsgNetworkConnCallback(unsafe.Pointer(C.connHandler)))
alice.net.Start()
bob.net.Start()
@@ -130,9 +152,9 @@ func main() {
alice_addr.Free()
bob_addr.Free()
- ev_int := salticidae.NewSigEvent(ec, salticidae.SigEventCallback(unsafe.Pointer(C.onTerm_cgo)))
+ ev_int := salticidae.NewSigEvent(ec, salticidae.SigEventCallback(unsafe.Pointer(C.onTerm)))
ev_int.Add(salticidae.SIGINT)
- ev_term := salticidae.NewSigEvent(ec, salticidae.SigEventCallback(unsafe.Pointer(C.onTerm_cgo)))
+ ev_term := salticidae.NewSigEvent(ec, salticidae.SigEventCallback(unsafe.Pointer(C.onTerm)))
ev_term.Add(salticidae.SIGTERM)
ec.Dispatch()
diff --git a/test_msgnet/main_.go b/test_msgnet/main_.go
deleted file mode 100644
index 2f53d93..0000000
--- a/test_msgnet/main_.go
+++ /dev/null
@@ -1,7 +0,0 @@
-package main
-
-// void onTerm_cgo(int sig) {
-// void onTerm(int);
-// onTerm(sig);
-// }
-import "C"