/
sirservalot.go
159 lines (148 loc) · 2.74 KB
/
sirservalot.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
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"net"
)
import "os"
import "syscall"
// #include <termios.h>
// #include <unistd.h>
import "C"
/**
* Open the serial port setting the baud etc.
*/
func openSerial() (io.ReadWriteCloser, error) {
file, err :=
os.OpenFile(
"/dev/ttyUSB0",
syscall.O_RDWR|syscall.O_NOCTTY,
0600)
if err != nil {
return nil, err
}
fd := C.int(file.Fd())
if C.isatty(fd) == 0 {
err := errors.New("File is not a serial port")
return nil, err
}
var termios C.struct_termios
_, err = C.tcgetattr(fd, &termios)
if err != nil {
return nil, err
}
var baud C.speed_t
baud = C.B115200
_, err = C.cfsetispeed(&termios, baud)
if err != nil {
return nil, err
}
_, err = C.cfsetospeed(&termios, baud)
if err != nil {
return nil, err
}
return file, nil
}
/**
* Main serial handling function
*/
func serialReader(port io.ReadWriteCloser, serin chan string) {
scanner := bufio.NewScanner(port)
for {
if scanner.Scan() {
s := fmt.Sprintln(scanner.Text())
fmt.Print(s)
serin <- s
}
if err := scanner.Err(); err != nil {
fmt.Println(err)
break
}
}
port.Close()
}
/**
* Main TCP handling function
*/
func handleConnection(conn net.Conn, port io.ReadWriteCloser, chout chan string) {
// the read from net
go func(c net.Conn, port io.ReadWriteCloser) {
for {
_, err := io.Copy(port, conn)
if err != nil {
fmt.Println(err)
break
}
}
conn.Close()
}(conn, port)
// the write to net
go func(c net.Conn, ch chan string) {
for {
_, err := io.WriteString(conn, <-ch)
if err != nil {
fmt.Println(err)
break
}
}
conn.Close()
}(conn, chout)
}
/**
* Due to vagaries of chan we need to add to each client queue
*
*/
func fanOut(serin chan string, clients []chan string, reset chan struct{}) {
var str string
Loop:
for {
select {
case str = <-serin:
for i := range clients {
clients[i] <- str
}
case <-reset:
break Loop
}
}
}
/**
* Main() - Program entry function
*/
func main() {
serin := make(chan string, 0) // serial read channel
done := make(chan struct{}, 0) // finish signal channel
clients := make([]chan string, 0)
go fanOut(serin, clients, done)
// open the serial port
port, err := openSerial()
if err != nil {
log.Fatal(err)
}
go serialReader(port, serin)
// listen on 1812 for connections
ln, err := net.Listen("tcp", ":1812")
if err != nil {
log.Fatal(err)
}
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
log.Fatal(err)
}
// maximum 10 clients
if len(clients) >= 10 {
conn.Close()
continue
}
chout := make(chan string, 0)
clients = append(clients, chout)
done <- struct{}{}
go fanOut(serin, clients, done)
go handleConnection(conn, port, chout)
}
}