-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCmesh.go
145 lines (122 loc) · 3.48 KB
/
SCmesh.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"sync"
_ "net/http/pprof"
log "github.com/Sirupsen/logrus"
"github.com/SlugCam/SCmesh/config"
"github.com/SlugCam/SCmesh/local/gateway"
"github.com/SlugCam/SCmesh/packet"
"github.com/SlugCam/SCmesh/pipeline"
"github.com/SlugCam/SCmesh/routing/dsr"
"github.com/SlugCam/SCmesh/simulation"
"github.com/tarm/serial"
)
func main() {
go func() {
log.Info(http.ListenAndServe(":6060", nil))
}()
// Parse command flags
localID := flag.Int("id", 0, "the id number for this node, sinks are 0")
baudRate := flag.Int("baud", 115200, "the baud rate for the serial connection")
debug := flag.Bool("debug", false, "print debug level log messages")
gwFlag := flag.Bool("gw", false, "run this server as a gateway")
messageServer := flag.String("ms", "localhost:7892", "address for the message server")
videoServer := flag.String("vs", "localhost:7893", "address for the video server")
serialDev := flag.String("serial", "/dev/ttyAMA0", "path of the serial device to use")
// TODO this is tacky
packetLog := flag.String("plog", "none", "path to log all packets to, or none for no logging")
cost := flag.Int("cost", 0, "the cost of the node")
flag.Parse()
packet.LocalID = uint32(*localID)
dsr.Cost = *cost
// Modify logging level
if *debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
// Setup serial
c := &serial.Config{Name: *serialDev, Baud: *baudRate}
serial, err := serial.OpenPort(c)
if err != nil {
log.Panic(err)
}
conf := config.DefaultConfig(uint32(*localID), serial)
// Setup packet logging if desired
if *packetLog == "none" {
log.Info("not logging incoming packets")
} else {
incomingPath := fmt.Sprintf("%s.in", *packetLog)
outgoingPath := fmt.Sprintf("%s.out", *packetLog)
// Outgoing packets
log.Infof("logging outgoing packets to: %s", outgoingPath)
fo, err := os.OpenFile(outgoingPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
log.Error("error opening packet log file: ", err)
}
// TODO is this correct?
defer fo.Close()
enco := json.NewEncoder(fo)
recordPacket := func(p packet.Packet) {
err := enco.Encode(p.Abbreviate())
if err != nil {
log.Error("error logging packet: ", err)
}
err = fo.Sync()
if err != nil {
log.Error("error logging packet: ", err)
}
}
packet.SendingLogCallback = &recordPacket
// Incoming packets
log.Infof("logging incoming packets to: %s", incomingPath)
incoming := simulation.InterceptIncoming(&conf)
f, err := os.OpenFile(incomingPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
log.Error("error opening packet log file: ", err)
}
// TODO is this correct?
defer f.Close()
enc := json.NewEncoder(f)
go func() {
for p := range incoming {
// log packet
err := enc.Encode(p.Abbreviate())
if err != nil {
log.Error("error logging packet: ", err)
}
err = f.Sync()
if err != nil {
log.Error("error logging packet: ", err)
}
}
}()
}
// Check if gateway
if *gwFlag {
gw := &gateway.Gateway{
MessageAddress: *messageServer,
VideoAddress: *videoServer,
}
conf.LocalProcessing = gw.LocalProcessing
}
pipeline.Start(conf)
// Block forever
var wg sync.WaitGroup
wg.Add(1)
wg.Wait()
}
func appendToFile(path string, data []byte) error {
f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(data)
return err
}