-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
198 lines (154 loc) · 4.97 KB
/
server.go
File metadata and controls
198 lines (154 loc) · 4.97 KB
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
191
192
193
194
195
196
197
198
package main
import (
"bufio"
"errors"
"fmt"
"io"
"net"
"os"
"sync"
"time"
connmanagement "example.com/stackserver/connectionmanagement"
eventhandler "example.com/stackserver/eventhandler"
stk "example.com/stackserver/stack"
)
const portNumber = 8080
var stack stk.Stack
var popMutex sync.Mutex = sync.Mutex{}
var pushMutex sync.Mutex = sync.Mutex{}
var eventHandler eventhandler.EventHandler
var connectionManager connmanagement.TcpConnectionManager = *connmanagement.NewTcpConnectionManager(100)
func main() {
fmt.Println("Starting server on port: ", portNumber)
server, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", portNumber))
if err != nil {
fmt.Printf("Error listening on port %d with error: %s", portNumber, err.Error())
os.Exit(1)
}
defer server.Close()
for {
connection, err := server.Accept()
if err != nil {
fmt.Println("Error accepting the connection request: ", err.Error())
continue
}
connectionWrapper, err := connectionManager.AddConnection(connection)
if err != nil {
// Too many connections, sending busy byte.
fmt.Println(err.Error())
connection.Write([]byte{0xFF})
connection.Close()
continue
}
go handleConnection(connectionWrapper)
}
}
func handleConnection(connectionWrapper *connmanagement.TcpConnection) {
header := make([]byte, 1)
connection := connectionWrapper.GetConnection()
bytesRead, err := connection.Read(header)
if err != nil || bytesRead <= 0 {
fmt.Println("Error retrieving header from connection", err.Error())
closeConnection(connectionWrapper)
return
}
payloadSize := header[0]
if isPush(payloadSize) {
fmt.Printf("Received push with connection ID: %s at: %s\n", connectionWrapper.GetId(), time.Now())
push(int(payloadSize), connectionWrapper)
} else {
fmt.Printf("Received pop with connection ID: %s at: %s\n", connectionWrapper.GetId(), time.Now())
pop(connectionWrapper)
}
closeConnection(connectionWrapper)
}
func isPush(payloadSize byte) bool {
return payloadSize&0x80 == 0
}
func push(payloadSize int, connectionWrapper *connmanagement.TcpConnection) {
payload, err := readPayload(payloadSize, connectionWrapper)
if err != nil {
fmt.Println(err.Error())
closeConnection(connectionWrapper)
return
}
go connmanagement.PollForClosedConnections(connectionWrapper, &connectionManager)
if stack.IsFull() {
eventWaitingChan := make(chan bool, 1)
eventHandler.WaitForPopEvent(eventWaitingChan, connectionWrapper)
<-eventWaitingChan
}
pushMutex.Lock()
defer pushMutex.Unlock()
if connectionWrapper.Closed() {
return
}
stack.PushToStack(payload)
writePayload([]byte{0}, connectionWrapper)
if err != nil {
fmt.Printf("Connection closed with error in push after byte write: %s\n", err)
closeConnection(connectionWrapper)
return
}
event := createNewEvent(eventhandler.Push, connectionWrapper)
eventHandler.HandleEvent(event)
}
func pop(connectionWrapper *connmanagement.TcpConnection) {
go connmanagement.PollForClosedConnections(connectionWrapper, &connectionManager)
if stack.IsEmpty() {
eventWaitingChan := make(chan bool, 1)
eventHandler.WaitForPushEvent(eventWaitingChan, connectionWrapper)
<-eventWaitingChan
}
popMutex.Lock()
defer popMutex.Unlock()
if connectionWrapper.Closed() {
return
}
bytesPopped := stack.PopFromStack()
error := writePayload(bytesPopped, connectionWrapper)
if error != nil {
fmt.Println(error.Error())
closeConnection(connectionWrapper)
return
}
event := createNewEvent(eventhandler.Pop, connectionWrapper)
eventHandler.HandleEvent(event)
}
func readPayload(payloadSize int, connectionWrapper *connmanagement.TcpConnection) ([]byte, error) {
connection := connectionWrapper.GetConnection()
payload := make([]byte, payloadSize)
reader := bufio.NewReader(connection)
bytesRead, err := io.ReadFull(reader, payload[:int(payloadSize)])
if err != nil || bytesRead < payloadSize {
message := fmt.Sprintf("Connection closed with error in push after payload read: %s\n", err)
return nil, errors.New(message)
}
return payload, nil
}
func writePayload(payload []byte, connectionWrapper *connmanagement.TcpConnection) error {
connection := connectionWrapper.GetConnection()
payloadLength := len(payload)
if len(payload) > 1 {
bytesWritten, err := connection.Write([]byte{byte(payloadLength)})
if bytesWritten <= 0 || err != nil {
message := fmt.Sprintf("There was an error writing the payload size: %s\n", err.Error())
return errors.New(message)
}
}
bytesWritten, err := connection.Write(payload)
if bytesWritten <= 0 || err != nil {
message := fmt.Sprintf("There was an error writing the payload: %s\n", err.Error())
return errors.New(message)
}
return nil
}
func createNewEvent(eventType eventhandler.EventType, connectionWrapper *connmanagement.TcpConnection) eventhandler.Event {
return eventhandler.Event{
EventType: eventType,
ConnectionWrapper: connectionWrapper,
}
}
func closeConnection(connection *connmanagement.TcpConnection) {
connectionManager.CloseConnection(connection)
}