Skip to content

Commit 29d8acf

Browse files
authored
Merge pull request #2 from arduino/refactor
Remove debug and fix notification message
2 parents b9af564 + 1bdf98d commit 29d8acf

File tree

3 files changed

+72
-105
lines changed

3 files changed

+72
-105
lines changed

example/sample_mprpc.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
type Resolver map[string]reflect.Value
1414

1515
func (self Resolver) Resolve(name string, arguments []reflect.Value) (reflect.Value, error) {
16-
//fmt.Println("resolving ", name)
1716
return self[name], nil
1817
}
1918

@@ -22,12 +21,10 @@ func (self Resolver) Functions() []string {
2221
for el := range self {
2322
functions = append(functions, el)
2423
}
25-
fmt.Println(functions)
2624
return functions
2725
}
2826

2927
func echo(test string) (string, fmt.Stringer) {
30-
fmt.Println(test)
3128
return "Hello, " + test, nil
3229
}
3330

@@ -37,30 +34,26 @@ func whoami() (string, fmt.Stringer) {
3734
}
3835

3936
func add(a, b uint) (uint, fmt.Stringer) {
40-
fmt.Println("calling add on ", a, " and ", b)
4137
return a + b, nil
4238
}
4339

40+
var proxyname string = "m4-proxy"
41+
4442
func serialportListener(serport *os.File) {
4543
for {
4644
data := make([]byte, 1024)
4745
n, err := serport.Read(data)
4846

4947
if err != nil {
50-
fmt.Println(err)
5148
continue
5249
}
5350

54-
fmt.Println("got data on serial port")
55-
5651
data = data[:n]
57-
fmt.Println(data)
5852

59-
conn, err := net.Dial("tcp", "m4-proxy:5001")
53+
conn, err := net.Dial("tcp", proxyname+":5001")
6054
client := rpc.NewSession(conn, true)
6155
xerr := client.Send("tty", data)
6256
if xerr != nil {
63-
fmt.Println(xerr)
6457
continue
6558
}
6659
}
@@ -69,23 +62,42 @@ func serialportListener(serport *os.File) {
6962
var serport *os.File
7063

7164
func tty(test []reflect.Value) fmt.Stringer {
72-
fmt.Println("tty called: ", test)
7365
var temp []byte
7466
for _, elem := range test {
7567
temp = append(temp, byte(elem.Int()))
7668
}
77-
//fmt.Println(temp)
7869
serport.Write(temp)
7970
return nil
8071
}
8172

73+
func led(status uint) fmt.Stringer {
74+
conn, _ := net.Dial("tcp", proxyname+":5001")
75+
client := rpc.NewSession(conn, true)
76+
xerr := client.Send("led", status)
77+
if xerr != nil {
78+
fmt.Println(xerr)
79+
}
80+
return nil
81+
}
82+
8283
func main() {
8384

85+
exec.Command("stty", "-F", "/dev/ttyGS0", "raw")
8486
serport, _ = os.OpenFile("/dev/ttyGS0", os.O_RDWR, 0)
8587

88+
// if Proxy is started outside a container, m4-proxy will be localhost
89+
// ping m4-proxy, if failed then m4-proxy is localhost
90+
// if ping success, then m4-proxy is m4-proxy
91+
if err := exec.Command("ping", "-c", "1", proxyname).Run(); err != nil {
92+
fmt.Println("Proxy is localhost")
93+
proxyname = "localhost"
94+
}
95+
96+
// serialportListener listens to the serial port and forwards the data to the proxy
97+
8698
go serialportListener(serport)
8799

88-
res := Resolver{"echo": reflect.ValueOf(echo), "add": reflect.ValueOf(add), "tty": reflect.ValueOf(tty), "whoami": reflect.ValueOf(whoami)}
100+
res := Resolver{"echo": reflect.ValueOf(echo), "add": reflect.ValueOf(add), "tty": reflect.ValueOf(tty), "whoami": reflect.ValueOf(whoami), "led": reflect.ValueOf(led)}
89101

90102
serv := rpc.NewServer(res, true, nil, 5002)
91103
l, _ := net.Listen("tcp", ":5002")

proxy/main.go

Lines changed: 8 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -24,63 +24,30 @@ const (
2424
type Resolver map[string]reflect.Value
2525

2626
func handleConnection(c net.Conn, chardev *os.File, resp chan []byte) {
27-
28-
fmt.Printf("Serving %s\n", c.RemoteAddr().String())
29-
3027
data, _, err := msgpack.Unpack(c)
3128
if err != nil {
3229
fmt.Println(err)
3330
return
3431
}
3532

36-
fmt.Println(data)
3733
var buf bytes.Buffer
3834

3935
msgpack.Pack(&buf, data.Interface())
40-
41-
/*
42-
msgId := _req[1]
43-
msgName := _req[2]
44-
msgArgs := _req[3]
45-
46-
rawdata := make([]byte, 5)
47-
rawdata[0] = byte(msgType.Int())
48-
rawdata[1] = byte(msgId.Int())
49-
rawdata[2] = byte(msgId.Int())
50-
rawdata[3] = byte(msgId.Int())
51-
rawdata[4] = byte(msgId.Int())
52-
rawdata = append(rawdata, msgName.Bytes()...)
53-
54-
something := msgArgs.Addr().Bytes()
55-
56-
fmt.Println(something)
57-
rawdata = append(rawdata, something...)
58-
59-
fmt.Println(data)
60-
fmt.Println(rawdata)
61-
*/
62-
63-
fmt.Println(buf)
64-
6536
chardev.Write(buf.Bytes())
6637

6738
msgType := buf.Bytes()[1]
6839

6940
if msgType == REQUEST {
7041
// wait to be unlocked by the other reading goroutine
7142
// TODO: add timeout handling
72-
fmt.Println("wait for response")
7343
select {
7444
case response := <-resp:
7545
//chardev.Read(response)
76-
fmt.Println("return response to client")
7746
c.Write(response)
7847
case <-time.After(1 * time.Second):
7948
c.Write(nil)
8049
}
8150
}
82-
fmt.Println("done")
83-
8451
if msgType == NOTIFICATION {
8552
// fire and forget
8653
}
@@ -91,9 +58,6 @@ func handleConnection(c net.Conn, chardev *os.File, resp chan []byte) {
9158
func chardevListener(chardev *os.File, resp chan []byte) {
9259

9360
for {
94-
95-
fmt.Println("charDevListener")
96-
9761
data := make([]byte, 1024)
9862
response := make([]byte, 1024)
9963

@@ -102,36 +66,16 @@ func chardevListener(chardev *os.File, resp chan []byte) {
10266
data = data[:n]
10367

10468
if err != nil {
105-
fmt.Println(err)
10669
continue
10770
}
108-
fmt.Println("chardev.Read returned")
109-
11071
if n <= 0 {
11172
continue
11273
}
113-
114-
fmt.Println("got data from chardev")
115-
fmt.Println(data)
116-
11774
start := 0
11875
for {
119-
120-
fmt.Println("unpacker loop")
121-
12276
copy_data := data[start:]
123-
12477
message, n, err := msgpack.UnpackReflected(bytes.NewReader(copy_data))
125-
12678
start += n
127-
128-
fmt.Printf("%d bytes consumed\n", n)
129-
fmt.Printf("%v\n", message)
130-
fmt.Println(message)
131-
if err != nil {
132-
fmt.Printf("Err: %v\n", err)
133-
}
134-
13579
if err == io.EOF {
13680
break
13781
}
@@ -142,12 +86,8 @@ func chardevListener(chardev *os.File, resp chan []byte) {
14286
}
14387

14488
msgType := _req[0].Int()
145-
fmt.Printf("MsgType: %d\n", msgType)
146-
147-
fmt.Println("before response")
14889

14990
if msgType == RESPONSE {
150-
fmt.Println("got response and continue")
15191
// unlock thread waiting on handleConnection
15292
resp <- copy_data[:n]
15393
continue
@@ -163,23 +103,21 @@ func chardevListener(chardev *os.File, resp chan []byte) {
163103
msgFunction = _req[1]
164104
}
165105

166-
fmt.Println("before serving")
167106
method := string(msgFunction.Bytes())
168107
port := functionToPort(method)
169108

170-
fmt.Println("Serving function ", method, " to port ", port)
171-
172109
// REQUEST or NOTIFICATION
173110
conn, err := net.Dial("tcp", port)
111+
if err != nil {
112+
continue
113+
}
114+
_, err = conn.Write(copy_data[:n])
174115
if err != nil {
175116
fmt.Println(err)
176117
continue
177118
}
178-
conn.Write(copy_data[:n])
179119

180120
if msgType == REQUEST {
181-
fmt.Println("ask for a response")
182-
183121
var to_send []byte
184122
i := 0
185123
for {
@@ -191,12 +129,10 @@ func chardevListener(chardev *os.File, resp chan []byte) {
191129
break
192130
}
193131
}
194-
fmt.Println("sending ", to_send[:i])
195132
chardev.Write(to_send[:i])
196133
}
197134

198135
if msgType == NOTIFICATION {
199-
fmt.Println("got a notification")
200136
// fire and forget
201137
}
202138

@@ -206,7 +142,6 @@ func chardevListener(chardev *os.File, resp chan []byte) {
206142
}
207143

208144
func (self Resolver) Resolve(name string, arguments []reflect.Value) (reflect.Value, error) {
209-
fmt.Println("resolving ", name)
210145
return self[name], nil
211146
}
212147

@@ -215,7 +150,6 @@ func (self Resolver) Functions() []string {
215150
for el := range self {
216151
functions = append(functions, el)
217152
}
218-
fmt.Println(functions)
219153
return functions
220154
}
221155

@@ -238,6 +172,10 @@ func main() {
238172
functions = make(map[string]int)
239173

240174
chardev, err := os.OpenFile("/dev/x8h7_ui", os.O_RDWR, 0)
175+
if (err != nil) {
176+
fmt.Println(err)
177+
return
178+
}
241179

242180
chardev_reader_chan := make(chan []byte, 1024)
243181

0 commit comments

Comments
 (0)