/
main.go
213 lines (200 loc) · 5.97 KB
/
main.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*------------------------------------------------------------------------------
-- DATE: February, 2016
--
-- Source File: main.go
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Marc Vouve
--
-- PROGRAMMER: Marc Vouve
--
--
-- INTERFACE:
-- func main()
-- func manageConnections(srvInfo serverInfo, osSignals chan os.Signal)
-- func newServerInfo() serverInfo
-- func getAddr() syscall.SockaddrInet4
-- func (s serverInfo) Close()
--
-- NOTES: This is the main file for the EPoll Scalable server
------------------------------------------------------------------------------*/
package main
import (
"container/list"
"fmt"
"log"
"net"
"os"
"os/signal"
"strconv"
"syscall"
"time"
)
type connectionInfo struct {
FileDescriptor int // connections file descriptor
HostName string // the remote host name
AmmountOfData int // the ammount of data transfered to/from the host
NumberOfRequests int // the total requests sent to the server from this client
ConnectionsAtClose int // the total number of connections being sustained when the connection was closed.
}
type serverInfo struct {
serverConnection chan int // channel used to inform main loop of new connections
connectInfo chan connectionInfo // channel to connection info of closing connections to main loop
listener int // listening socket
}
const newConnectionConst = 1
const finishedConnectionConst = -1
const epollQueueLen = 1000000
const backlog = 10000
const numThreads = 30
/*-----------------------------------------------------------------------------
-- FUNCTION: main
--
-- DATE: February 6, 2016
--
-- REVISIONS: February 11, 2016 Modified for EPoll
--
--
-- DESIGNER: Marc Vouve
--
-- PROGRAMMER: Marc Vouve
--
-- INTERFACE: func main()
--
-- RETURNS: void
--
-- NOTES: The main entry point for the scalable server.
------------------------------------------------------------------------------*/
func main() {
if len(os.Args) < 2 { // validate args
fmt.Println("Missing args:", os.Args[0], " [PORT]")
return
}
srvInfo := newServerInfo()
defer srvInfo.Close()
for i := 0; i < numThreads; i++ {
go listen(srvInfo) // spawn listen routines
}
osSignals := make(chan os.Signal)
signal.Notify(osSignals, os.Interrupt, os.Kill)
manageConnections(srvInfo, osSignals)
}
/*-----------------------------------------------------------------------------
-- FUNCTION: manageConnections
--
-- DATE: February 7, 2016
--
-- REVISIONS: February 11, 2016 Modified for EPoll
--
-- DESIGNER: Marc Vouve
--
-- PROGRAMMER: Marc Vouve
--
-- INTERFACE: func manageConnections(srvInfo serverInfo, osSignals chan os.Signal)
-- srvInfo: information about the server (IPC and listening port)
-- osSignals: listens for signals from the OS that should stop the server from running
--
-- RETURNS: void
--
-- NOTES: This server will loop until inturupted by an OS Signal on soSignals.
------------------------------------------------------------------------------*/
func manageConnections(srvInfo serverInfo, osSignals chan os.Signal) {
currentConnections := 0
connectionsMade := list.New()
for {
select {
case <-srvInfo.serverConnection:
currentConnections++
case serverHost := <-srvInfo.connectInfo:
serverHost.ConnectionsAtClose = currentConnections
connectionsMade.PushBack(serverHost)
currentConnections--
case <-osSignals:
generateReport(time.Now().String(), connectionsMade)
fmt.Println("Total connections made:", connectionsMade.Len())
os.Exit(1)
}
}
}
/*-----------------------------------------------------------------------------
-- FUNCTION: manageConnections
--
-- DATE: February 7, 2016
--
-- REVISIONS: February 11, 2016 Modified for EPoll
--
--
-- DESIGNER: Marc Vouve
--
-- PROGRAMMER: Marc Vouve
--
-- INTERFACE: func manageConnections(srvInfo serverInfo, osSignals chan os.Signal)
-- srvInfo: information about the server (IPC and listening port)
-- osSignals: listens for signals from the OS that should stop the server from running
--
-- RETURNS: void
--
-- NOTES: This server will loop until inturupted by an OS Signal on soSignals.
------------------------------------------------------------------------------*/
func newServerInfo() serverInfo {
srvInfo := serverInfo{serverConnection: make(chan int, 100), connectInfo: make(chan connectionInfo, 100)}
fd, err := syscall.Socket(syscall.AF_INET, syscall.O_NONBLOCK|syscall.SOCK_STREAM, 0)
if err != nil {
log.Println(err)
}
syscall.SetNonblock(fd, true)
// TODO: make port vairable
addr := getAddr()
syscall.Bind(fd, &addr)
syscall.Listen(fd, backlog)
srvInfo.listener = fd
return srvInfo
}
/*-----------------------------------------------------------------------------
-- FUNCTION: getAddr
--
-- DATE: February 11, 2016
--
--
-- DESIGNER: Marc Vouve
--
-- PROGRAMMER: Marc Vouve
--
-- INTERFACE: getAddr() syscall.SockaddrInet4
--
-- RETURNS: syscall.SockaddrInet4 a sockaddr of the port specified by the user.
--
-- NOTES: this funciton parses a port out of user input
------------------------------------------------------------------------------*/
func getAddr() syscall.SockaddrInet4 {
port, err := strconv.Atoi(os.Args[1])
if err != nil {
log.Fatal("Port must be declaired as [PORT]")
}
addr := syscall.SockaddrInet4{Port: port}
copy(addr.Addr[:], net.ParseIP("0.0.0.0").To4())
return addr
}
/*-----------------------------------------------------------------------------
-- FUNCTION: close
--
-- DATE: February 7, 2016
--
-- REVISIONS: February 11, 2016 Modified for EPoll
--
--
-- DESIGNER: Marc Vouve
--
-- PROGRAMMER: Marc Vouve
--
-- INTERFACE: func (s serverInfo) Close()
--
-- RETURNS: void
--
-- NOTES: This function closes everything that must be closed in serverInfo
-- upon program termination. currently just the listener
------------------------------------------------------------------------------*/
func (s serverInfo) Close() {
syscall.Close(s.listener)
}