/
socket-server.go
146 lines (123 loc) · 3.39 KB
/
socket-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 (
"bufio"
"log"
"net"
"net/http"
"time"
"sync/atomic"
"flag"
"fmt"
"code.google.com/p/go.net/websocket"
)
//transaction counter
var counter uint64 = 0
//to count transactions per second
var diffTransactions uint64 = 0
var counterPartial uint64 = 0
var lastTps uint64 = 0
var stackLastTps = new(Stack)
//webserver port
var port *int = flag.Int("p", 31415, "Port to listen.")
//struct to share information with websocket clients
type T struct {
Msg string
Tps int
}
func main() {
//parsing parameters
flag.Parse()
//websocket and http server
go func() {
//dispatch /json request to jsonServer handler
http.Handle("/json", websocket.Handler(jsonServer))
//dispath any other request to the fileserver handler
http.Handle("/", http.FileServer(http.Dir("./html")))
//listen websocket and http on port 31415
err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
if err != nil {
log.Fatalf("listen error on http port: %v", err.Error())
}
}()
//listen tcpip socket on port 5882
ln, err := net.Listen("tcp", ":5882")
if err != nil {
log.Fatalf("listen error on tcpip port: %v", err)
}
log.Printf("NyanCat-server Listening tpcip on port 5882")
log.Printf("NyanCat-server Listening websocket and http on http://localhost:PI")
log.Printf("NyanCat-server S2 U")
//treat each connection received with ln.Accept()
go func() {
for {
//try to accept a new socket connection
conn, err := ln.Accept()
if err != nil {
log.Fatalf("accept error: %v", err)
}
go HandleConnection(conn)
}
}()
//count the number of transactions per second every second! DUH!
go func(){
for {
//get the transaction counter
counterPartial = atomic.LoadUint64(&counter)
//how many transaction in the last second
lastTps = counterPartial - diffTransactions
stackLastTps.Push((int)(lastTps))
//print to stdout
log.Printf("NyanCat-server transaction-number:%v tps:%v\n", counterPartial, lastTps)
//store the last counter to know how much new transactions were proccessed in the last second
diffTransactions = counterPartial
//sleep 1 second
time.Sleep(time.Second * 1)
}
}()
//eternal wait
select {}
}
//handle a tcp connection receive from the main loop
func HandleConnection(conn net.Conn) {
//more one socket connected. Added to the counter
atomic.AddUint64(&counter, 1)
//initialize a bufio to read the socket stream
bufr := bufio.NewReader(conn)
for {
//wait to read the socket stream until \n
line, err := bufr.ReadString('\n')
if err != nil {
return
}
//sleep 2 second then echoe back the message
time.Sleep(time.Second * 2)
conn.Write([]byte(line))
//sleep more 1 second and close the socket
time.Sleep(time.Second * 1)
conn.Close()
}
}
//handle a websocket connection received from webserver
func jsonServer(ws *websocket.Conn) {
fmt.Printf("jsonServer %#v\n", ws.Config())
for {
var msg T
// Receive receives a text message serialized T as JSON.
err := websocket.JSON.Receive(ws, &msg)
if err != nil {
fmt.Println(err)
break
}
fmt.Printf("recv:%#v\n", msg)
// Send send a text message serialized T as JSON.
msg.Msg = "pong"
msg.Tps = stackLastTps.Pop().(int)
// Send to the webclients
err = websocket.JSON.Send(ws, msg)
if err != nil {
fmt.Println(err)
break
}
fmt.Printf("send:%#v\n", msg)
}
}