aboutsummaryrefslogtreecommitdiff
path: root/demo/main.go
blob: 8dcbe985651f11886966720f433ec7207b2e1b53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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()
}