switch the HttpServer class to maintain listeners in HttpConnReg instead of LinkedList

This commit is contained in:
hyunghwan.chung 2018-05-13 18:16:04 +00:00
parent 1c68849fa0
commit ea2347bb82

View File

@ -54,12 +54,30 @@ class HttpConnReg(Object)
self.connections at: index put: -1. self.connections at: index put: -1.
self.last_free_slot := index. self.last_free_slot := index.
} }
method do: block
{
| index size conn |
## the following loop won't fire for an element added after resizing of self.connections.
## at present, there is no self.connections resizing impelemented. so no worry on this.
size := self.connections size.
index := 0.
while (index < size)
{
conn := self.connections at: index.
if ((conn isKindOf: Integer) not)
{
block value: (self.connections at: index).
}.
index := index + 1.
}.
}
} }
class HttpSocket(Socket) class HttpSocket(Socket)
{ {
var(#get) server := nil. var(#get) server := nil.
var(#get) cid := -1. var(#get) rid := -1.
method onSocketDataIn method onSocketDataIn
{ {
@ -72,22 +90,23 @@ class HttpSocket(Socket)
('Http Connection closing.......... handle ' & self.handle asString) dump. ('Http Connection closing.......... handle ' & self.handle asString) dump.
if (self.server notNil) if (self.server notNil)
{ {
('Http Connection ' & self.cid asString & ' removing from server ..........') dump. ('Http Connection ' & self.rid asString & ' removing from server ..........') dump.
self.server removeConnection: self. self.server removeConnection: self.
}. }.
^super close. ^super close.
} }
method server: server cid: cid method server: server rid: rid
{ {
self.server := server. self.server := server.
self.cid := cid. self.rid := rid.
} }
} }
class HttpListener(ServerSocket) class HttpListener(ServerSocket)
{ {
var(#get,#set) server := nil. var(#get) server := nil.
var(#get) rid := -1.
method initialize method initialize
{ {
@ -96,7 +115,7 @@ class HttpListener(ServerSocket)
method onSocketAccepted: clisck from: cliaddr method onSocketAccepted: clisck from: cliaddr
{ {
| cid | | rid |
'CLIENT accepted ..............' dump. 'CLIENT accepted ..............' dump.
clisck dump. clisck dump.
@ -106,6 +125,7 @@ clisck dump.
{ {
server addConnection: clisck. server addConnection: clisck.
}. }.
} }
method acceptedSocketClass method acceptedSocketClass
@ -113,6 +133,12 @@ clisck dump.
##^if (self currentAddress port == 80) { HttpSocket } else { HttpSocket }. ##^if (self currentAddress port == 80) { HttpSocket } else { HttpSocket }.
^HttpSocket. ^HttpSocket.
} }
method server: server rid: rid
{
self.server := server.
self.rid := rid.
}
} }
class HttpServer(Object) class HttpServer(Object)
@ -123,7 +149,7 @@ class HttpServer(Object)
method initialize method initialize
{ {
super initialize. super initialize.
self.listeners := LinkedList new. self.listeners := HttpConnReg new.
self.connreg := HttpConnReg new. self.connreg := HttpConnReg new.
} }
@ -135,56 +161,75 @@ class HttpServer(Object)
{ {
laddr do: [:addr | laddr do: [:addr |
listener := HttpListener family: (addr family) type: Socket.Type.STREAM. listener := HttpListener family: (addr family) type: Socket.Type.STREAM.
self.listeners addLast: listener. if ((self addListener: listener) notError)
listener server: self. {
listener bind: addr. listener bind: addr.
listener listen: 128.
}
]. ].
} }
else else
{ {
listener := HttpListener family: (laddr family) type: Socket.Type.STREAM. listener := HttpListener family: (laddr family) type: Socket.Type.STREAM.
self.listeners addLast: listener. if ((self addListener: listener) notError)
listener server: self. {
listener bind: laddr. listener bind: laddr.
listener listen: 128.
}
}. }.
self.listeners do: [:l_listener |
l_listener listen: 128.
].
} }
method close method close
{ {
self.listeners do: [:listener | self.listeners do: [:listener |
listener server: nil.
listener close. listener close.
]. ].
while (self.listeners size > 0) self.connreg do: [:conn |
{ conn close.
self.listeners removeLastLink. ].
}.
} }
method addConnection: conn method addConnection: conn
{ {
| cid | | rid |
cid := self.connreg add: conn. rid := self.connreg add: conn.
if (cid isError) if (rid isError)
{ {
'ERROR - CANNOT REGISTER NEW CONNECTION >>>>>>>>>> ' dump. 'ERROR - CANNOT REGISTER NEW CONNECTION >>>>>>>>>> ' dump.
conn close. conn close.
^self. ^rid.
}. }.
('ADD NEW CONNECTION ' & cid asString) dump. ('ADD NEW CONNECTION ' & rid asString) dump.
conn server: self cid: cid. conn server: self rid: rid.
} }
method removeConnection: conn method removeConnection: conn
{ {
self.connreg remove: (conn cid). self.connreg remove: (conn rid).
conn server: nil cid: -1. conn server: nil rid: -1.
}
method addListener: listener
{
| rid |
rid := self.listeners add: listener.
if (rid isError)
{
'ERROR - CANNOT REGISTER NEW LISTENER >>>>>>>>>> ' dump.
listener close.
^rid.
}.
('ADD NEW LISTENER ' & rid asString) dump.
listener server: self rid: rid.
}
method removeListener: listener
{
self.listeners remove: (listener rid).
listener server: nil rid: -1.
} }
} }