-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmux-server.go
146 lines (123 loc) · 2.8 KB
/
mux-server.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
146
package main
import (
"fmt"
"github.com/tarm/serial"
"net"
"os"
)
const (
HOST = "localhost"
PORT = "23200"
TYPE = "tcp"
)
type Client struct {
conn net.Conn
ch chan []byte
}
func mux(from_serial chan []byte, to_serial chan []byte,
add_client chan Client, rm_client chan Client) {
clients := make(map[net.Conn]chan []byte)
for {
select {
case msg := <-from_serial:
i := 0
for _, ch := range clients {
ch <- msg
i += 1
}
case client := <-add_client:
fmt.Println("MUX: New client", client.conn.RemoteAddr())
clients[client.conn] = client.ch
case client := <-rm_client:
fmt.Println("MUX: Removing client", client.conn.RemoteAddr())
delete(clients, client.conn)
}
}
}
func mux2client(c Client) {
for msg := range c.ch {
_, err := c.conn.Write(msg)
if err != nil {
return
}
}
}
func client2mux(c Client, to_serial chan []byte) {
buf := make([]byte, 1024)
for {
n, err := c.conn.Read(buf)
if err != nil {
return
}
to_serial <- buf[:n]
}
}
func handle_client(conn net.Conn, to_serial chan []byte,
add_client chan Client, rm_client chan Client) {
client := Client{
conn: conn,
ch: make(chan []byte),
}
defer func() {
fmt.Println("Client: Connection closed", client.conn.RemoteAddr())
client.conn.Close()
rm_client <- client
}()
add_client <- client
go mux2client(client)
client2mux(client, to_serial)
}
func mux2serial(s *serial.Port, to_serial chan []byte) {
for msg := range to_serial {
_, err := s.Write(msg)
if err != nil {
fmt.Println("MUX> Error writing to serial port:", err.Error())
os.Exit(1)
}
}
}
func serial2mux(s *serial.Port, from_serial chan []byte) {
buf := make([]byte, 1024)
for {
n, err := s.Read(buf)
if err != nil {
fmt.Println("MUX> Error reading from serial port:", err.Error())
os.Exit(1)
}
from_serial <- buf[:n]
}
}
func handle_serial(from_serial chan []byte, to_serial chan []byte) {
c := &serial.Config{Name: "/dev/ttyACM0", Baud: 9600}
s, err := serial.OpenPort(c)
if err != nil {
fmt.Println("MUX> Error opening serial port:", err.Error())
os.Exit(1)
}
defer s.Close()
go mux2serial(s, to_serial)
serial2mux(s, from_serial)
}
func main() {
listener, err := net.Listen(TYPE, HOST+":"+PORT)
if err != nil {
fmt.Println("MUX> Error listening:", err.Error())
os.Exit(1)
}
defer listener.Close()
from_serial := make(chan []byte, 1024)
to_serial := make(chan []byte, 1024)
add_client := make(chan Client)
rm_client := make(chan Client)
go mux(from_serial, to_serial, add_client, rm_client)
go handle_serial(from_serial, to_serial)
fmt.Println("MUX> Listening on:", HOST+":"+PORT)
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("MUX> Error handling:", err.Error())
continue
}
go handle_client(conn, to_serial, add_client, rm_client)
}
}