Skip to content

Remove debug and fix notification message #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions example/sample_mprpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
type Resolver map[string]reflect.Value

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

Expand All @@ -22,12 +21,10 @@ func (self Resolver) Functions() []string {
for el := range self {
functions = append(functions, el)
}
fmt.Println(functions)
return functions
}

func echo(test string) (string, fmt.Stringer) {
fmt.Println(test)
return "Hello, " + test, nil
}

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

func add(a, b uint) (uint, fmt.Stringer) {
fmt.Println("calling add on ", a, " and ", b)
return a + b, nil
}

var proxyname string = "m4-proxy"

func serialportListener(serport *os.File) {
for {
data := make([]byte, 1024)
n, err := serport.Read(data)

if err != nil {
fmt.Println(err)
continue
}

fmt.Println("got data on serial port")

data = data[:n]
fmt.Println(data)

conn, err := net.Dial("tcp", "m4-proxy:5001")
conn, err := net.Dial("tcp", proxyname+":5001")
client := rpc.NewSession(conn, true)
xerr := client.Send("tty", data)
if xerr != nil {
fmt.Println(xerr)
continue
}
}
Expand All @@ -69,23 +62,42 @@ func serialportListener(serport *os.File) {
var serport *os.File

func tty(test []reflect.Value) fmt.Stringer {
fmt.Println("tty called: ", test)
var temp []byte
for _, elem := range test {
temp = append(temp, byte(elem.Int()))
}
//fmt.Println(temp)
serport.Write(temp)
return nil
}

func led(status uint) fmt.Stringer {
conn, _ := net.Dial("tcp", proxyname+":5001")
client := rpc.NewSession(conn, true)
xerr := client.Send("led", status)
if xerr != nil {
fmt.Println(xerr)
}
return nil
}

func main() {

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

// if Proxy is started outside a container, m4-proxy will be localhost
// ping m4-proxy, if failed then m4-proxy is localhost
// if ping success, then m4-proxy is m4-proxy
if err := exec.Command("ping", "-c", "1", proxyname).Run(); err != nil {
fmt.Println("Proxy is localhost")
proxyname = "localhost"
}

// serialportListener listens to the serial port and forwards the data to the proxy

go serialportListener(serport)

res := Resolver{"echo": reflect.ValueOf(echo), "add": reflect.ValueOf(add), "tty": reflect.ValueOf(tty), "whoami": reflect.ValueOf(whoami)}
res := Resolver{"echo": reflect.ValueOf(echo), "add": reflect.ValueOf(add), "tty": reflect.ValueOf(tty), "whoami": reflect.ValueOf(whoami), "led": reflect.ValueOf(led)}

serv := rpc.NewServer(res, true, nil, 5002)
l, _ := net.Listen("tcp", ":5002")
Expand Down
78 changes: 8 additions & 70 deletions proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,63 +24,30 @@ const (
type Resolver map[string]reflect.Value

func handleConnection(c net.Conn, chardev *os.File, resp chan []byte) {

fmt.Printf("Serving %s\n", c.RemoteAddr().String())

data, _, err := msgpack.Unpack(c)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(data)
var buf bytes.Buffer

msgpack.Pack(&buf, data.Interface())

/*
msgId := _req[1]
msgName := _req[2]
msgArgs := _req[3]

rawdata := make([]byte, 5)
rawdata[0] = byte(msgType.Int())
rawdata[1] = byte(msgId.Int())
rawdata[2] = byte(msgId.Int())
rawdata[3] = byte(msgId.Int())
rawdata[4] = byte(msgId.Int())
rawdata = append(rawdata, msgName.Bytes()...)

something := msgArgs.Addr().Bytes()

fmt.Println(something)
rawdata = append(rawdata, something...)

fmt.Println(data)
fmt.Println(rawdata)
*/

fmt.Println(buf)

chardev.Write(buf.Bytes())

msgType := buf.Bytes()[1]

if msgType == REQUEST {
// wait to be unlocked by the other reading goroutine
// TODO: add timeout handling
fmt.Println("wait for response")
select {
case response := <-resp:
//chardev.Read(response)
fmt.Println("return response to client")
c.Write(response)
case <-time.After(1 * time.Second):
c.Write(nil)
}
}
fmt.Println("done")

if msgType == NOTIFICATION {
// fire and forget
}
Expand All @@ -91,9 +58,6 @@ func handleConnection(c net.Conn, chardev *os.File, resp chan []byte) {
func chardevListener(chardev *os.File, resp chan []byte) {

for {

fmt.Println("charDevListener")

data := make([]byte, 1024)
response := make([]byte, 1024)

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

if err != nil {
fmt.Println(err)
continue
}
fmt.Println("chardev.Read returned")

if n <= 0 {
continue
}

fmt.Println("got data from chardev")
fmt.Println(data)

start := 0
for {

fmt.Println("unpacker loop")

copy_data := data[start:]

message, n, err := msgpack.UnpackReflected(bytes.NewReader(copy_data))

start += n

fmt.Printf("%d bytes consumed\n", n)
fmt.Printf("%v\n", message)
fmt.Println(message)
if err != nil {
fmt.Printf("Err: %v\n", err)
}

if err == io.EOF {
break
}
Expand All @@ -142,12 +86,8 @@ func chardevListener(chardev *os.File, resp chan []byte) {
}

msgType := _req[0].Int()
fmt.Printf("MsgType: %d\n", msgType)

fmt.Println("before response")

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

fmt.Println("before serving")
method := string(msgFunction.Bytes())
port := functionToPort(method)

fmt.Println("Serving function ", method, " to port ", port)

// REQUEST or NOTIFICATION
conn, err := net.Dial("tcp", port)
if err != nil {
continue
}
_, err = conn.Write(copy_data[:n])
if err != nil {
fmt.Println(err)
continue
}
conn.Write(copy_data[:n])

if msgType == REQUEST {
fmt.Println("ask for a response")

var to_send []byte
i := 0
for {
Expand All @@ -191,12 +129,10 @@ func chardevListener(chardev *os.File, resp chan []byte) {
break
}
}
fmt.Println("sending ", to_send[:i])
chardev.Write(to_send[:i])
}

if msgType == NOTIFICATION {
fmt.Println("got a notification")
// fire and forget
}

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

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

Expand All @@ -215,7 +150,6 @@ func (self Resolver) Functions() []string {
for el := range self {
functions = append(functions, el)
}
fmt.Println(functions)
return functions
}

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

chardev, err := os.OpenFile("/dev/x8h7_ui", os.O_RDWR, 0)
if (err != nil) {
fmt.Println(err)
return
}

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

Expand Down
Loading