-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroker.go
190 lines (159 loc) · 4.25 KB
/
broker.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
. "broker-pablitto/control_packets"
"bufio"
"fmt"
"io"
"log"
"net"
"os"
"strconv"
"time"
)
const (
base10 = 10
bits32 = 32
)
func keep_alive(timeout chan bool, duration int) {
seconds := time.Duration(duration)
for {
time.After(seconds)
timeout <- true
}
}
func handler(connection net.Conn, s *Subscriptions) {
defer connection.Close()
var (
r = bufio.NewReader(connection)
)
duration := Keep_alive_duration
isAlive := true
timeout := make(chan bool)
go keep_alive(timeout, duration)
for {
select {
case <-timeout:
if isAlive == false {
return
}
isAlive = false
break
default:
control_packet, err := Analyze_packet(r)
switch err {
case io.EOF:
case nil:
switch control_packet.(type) {
case *ConnectPacket:
log.Println("CONNECT: Received")
connect_packet := control_packet.(*ConnectPacket)
log.Println(connect_packet)
session_present := false
if connect_packet.Clean_session == false {
session_present = true
} else {
session_present = false
}
return_code := byte(Connection_accepted)
fh := FixedHeader{Message_type: Connack, Dup: false, Retain: false}
connack_packet := ConnackPacket{fh, session_present, return_code}
connack_packet.Boxing(connection)
isAlive = true
break
case *PublishPacket:
log.Println("PUBLISH: Received")
publish_packet := control_packet.(*PublishPacket)
log.Println(publish_packet)
if !s.Find_topic(publish_packet.Topic_name) {
s.Add_topic(publish_packet.Topic_name)
s.SendToSubscriber(publish_packet.Topic_name, publish_packet)
} else {
//lo standard dice che nel caso non esiste lo aggiunge
s.SendToSubscriber(publish_packet.Topic_name, publish_packet)
}
if publish_packet.Qos == 0x01 {
fh := FixedHeader{Message_type: Puback, Dup: false, Retain: false}
puback_packet := PubackPacket{fh, publish_packet.Message_id}
puback_packet.Boxing(connection)
}
isAlive = true
break
case *SubscribePacket:
log.Println("SUBSCRIBE: Received")
subscribe_packet := control_packet.(*SubscribePacket)
var return_code []byte
for _, topic_name := range subscribe_packet.Topics {
if s.IsValidTopic(topic_name) == true {
s.Add_subscription(topic_name, connection)
return_code = append(return_code, 0x00)
}
}
//rember subscribe can be multiple
fh := FixedHeader{Message_type: Suback, Dup: false, Retain: false}
suback_packet := SubackPacket{fh, subscribe_packet.Message_id, return_code}
suback_packet.Boxing(connection)
isAlive = true
break
case *UnsubscribePacket:
log.Println("UNSUBSCRIBE: Received")
unsubscribe_packet := control_packet.(*UnsubscribePacket)
for _, topic_name := range unsubscribe_packet.Topics {
if s.IsValidTopic(topic_name) == true {
s.Remove_subscription(topic_name, connection)
}
}
isAlive = true
break
case *DisconnectPacket:
log.Println("DISCONNECT: Received")
return
case *PingreqPacket:
fh := FixedHeader{Message_type: Pingresp, Dup: false, Retain: false}
fmt.Println(control_packet.(*PingreqPacket))
pingresp_packet := PingrespPacket{fh}
pingresp_packet.Boxing(connection)
isAlive = true
break
default:
log.Println("Sorry, packet is not yet supported")
isAlive = true
break
}
default:
log.Print("Receive data failed:%s", err)
isAlive = false
break
}
}
}
}
func SocketServer(port int) {
s := &Subscriptions{nil}
listen, err := net.Listen("tcp4", ":"+strconv.Itoa(port))
if err != nil {
log.Fatalf("Socket listen port %d failed,%s", port, err)
os.Exit(1)
}
defer listen.Close()
log.Printf("Begin listen port: %d", port)
for {
connection, err := listen.Accept()
strRemoteAddr := connection.RemoteAddr().String()
log.Println("Address client: ", strRemoteAddr)
if err != nil {
log.Println("Error accept")
}
go handler(connection, s)
}
}
func main() {
if len(os.Args) > 1 {
port64, err := strconv.ParseInt(os.Args[1], base10, bits32)
if err == nil {
port := int(port64)
SocketServer(port)
}
} else {
log.Println("try again with ./boker port_number")
}
}