started adding OrderedCollection

This commit is contained in:
hyunghwan.chung 2018-05-22 16:22:32 +00:00
parent c014789fa9
commit 29a5f0716d
3 changed files with 131 additions and 215 deletions

View File

@ -253,6 +253,88 @@ class(#byte) ByteArray(Array)
## TODO: is it ok for ByteArray to inherit from Array? ## TODO: is it ok for ByteArray to inherit from Array?
} }
## -------------------------------------------------------------------------------
class SequenceableCollection(Collection)
{
}
class OrderedCollection(SequenceableCollection)
{
var contents.
var firstIndex.
var lastIndex.
method(#class) new
{
^self new: 16.
}
method(#class) new: size
{
^super _basicNew initialize: size.
}
method initialize: size
{
self.contents := Array new: size.
self.firstIndex := size // 2 max: 1.
self.lastIndex := self.firstIndex - 1.
}
method size
{
^self.lastIndex - self.firstIndex + 1.
}
method at: index
{
| i |
i := index + self.firstIndex - 1.
if (i >= self.firstIndex and: [i <= self.lastIndex]) { ^self.contents at: index }.
Exception signal: ('index ' & index asString & ' out of range').
}
method at: index put: obj
{
| i |
i := index + self.firstIndex - 1.
if (i >= self.firstIndex and: [i <= self.lastIndex]) { ^self.contents at: index put: obj }.
Exception signal: ('index ' & index asString & ' out of range').
}
method addFirst: obj
{
}
method addLast: obj
{
}
method add: obj beforeIndex: index
{
}
method add: obj afterIndex: index
{
}
method removeFirst
{
}
method removeLast
{
}
method remove: obj ifAbsent: error_block
{
}
method growBy: size
{
}
}
## ------------------------------------------------------------------------------- ## -------------------------------------------------------------------------------
class Set(Collection) class Set(Collection)

View File

@ -1,14 +1,6 @@
###include 'Moo.moo'. ###include 'Moo.moo'.
#include 'Socket.moo'. #include 'Socket.moo'.
class HttpReq(Object)
{
}
class HttpReqBuilder(Object)
{
}
class HttpConnReg(Object) class HttpConnReg(Object)
{ {
var connections. var connections.
@ -66,8 +58,9 @@ class HttpConnReg(Object)
method do: block method do: block
{ {
| index size conn | | index size conn |
## the following loop won't fire for an element added after resizing of self.connections. ## the following loop won't evaluate the given block for an element added after
## at present, there is no self.connections resizing impelemented. so no worry on this. ## resizing of self.connections at present, there is no self.connections resizing
## impelemented. so no worry on this.
size := self.connections size. size := self.connections size.
index := 0. index := 0.
while (index < size) while (index < size)
@ -82,18 +75,15 @@ class HttpConnReg(Object)
} }
} }
class HttpBuffer(Object)
{
}
class HttpSocket(SyncSocket) class HttpSocket(SyncSocket)
{ {
var(#get) server := nil. var(#get) server := nil.
var(#get) rid := -1. var(#get) rid := -1.
method onSocketDataIn
{
'CLIENT got DATA' dump.
###self readBytes: buf.
self close.
}
method close method close
{ {
('Http Connection closing.......... handle ' & self.handle asString) dump. ('Http Connection closing.......... handle ' & self.handle asString) dump.
@ -111,12 +101,30 @@ class HttpSocket(SyncSocket)
self.rid := rid. self.rid := rid.
} }
method getLine
{
}
method readRequest
{
}
method _run_service method _run_service
{ {
| buf | | buf |
self timeout: 10.
(*while (true)
{
req := self readRequest.
}. *)
buf := ByteArray new: 128. buf := ByteArray new: 128.
'IM RUNNING SERVICE...............' dump. 'IM RUNNING SERVICE...............' dump.
self timeout: 10.
self readBytes: buf. self readBytes: buf.
buf dump. buf dump.
self readBytes: buf. self readBytes: buf.
@ -143,10 +151,11 @@ class HttpListener(AsyncServerSocket)
super initialize. super initialize.
} }
##method close method close
##{ {
## super close. if (self.server notNil) { self.server removeListener: self }.
##} ^super close.
}
method onSocketAccepted: clisck from: cliaddr method onSocketAccepted: clisck from: cliaddr
{ {
@ -167,10 +176,10 @@ clisck dump.
} }
] ]
on: Exception do: [:ex | on: Exception do: [:ex |
clisck close.
Exception signal: ('unable to handle a new connection - ' & ex messageText). Exception signal: ('unable to handle a new connection - ' & ex messageText).
]. ].
}. }.
} }
method acceptedSocketClass method acceptedSocketClass
@ -206,60 +215,33 @@ class HttpServer(Object)
listener server: self rid: rid. listener server: self rid: rid.
} }
method __remove_listener: listener method removeListener: listener
{ {
| rid | | rid |
rid = listener rid. rid := listener rid.
if (rid notNil) if (rid notNil)
{ {
('REALLY REMOVE LISTENER ' & rid asString) dump.
self.listeners remove: (listener rid). self.listeners remove: (listener rid).
listener server: nil rid: nil. listener server: nil rid: nil.
}. }.
} }
method __start_new_listener: addr method __add_new_listener: addr
{ {
| listener | | listener |
listener := HttpListener family: (addr family) type: Socket.Type.STREAM. listener := HttpListener family: (addr family) type: Socket.Type.STREAM.
[ [
if ((self __add_listener: listener) notError) self __add_listener: listener.
{
listener bind: addr. listener bind: addr.
listener listen: 128. listener listen: 128.
}
] on: Exception do: [:ex | ] on: Exception do: [:ex |
self __remove_listener: listener.
listener close. listener close.
## ex pass. ## ex pass.
Exception signal: ('unable to add new listener - ' & ex messageText). Exception signal: ('unable to add new listener - ' & ex messageText).
]. ].
} }
method start: laddr
{
| listener |
if (laddr class == Array)
##if (laddr respondsTo: #do:) ## can i check if the message receives a block and the block accepts 1 argument?
{
laddr do: [:addr | self __start_new_listener: addr ].
}
else
{
self __start_new_listener: laddr.
}.
}
method close
{
self.listeners do: [:listener |
listener close.
].
self.connreg do: [:conn |
conn close.
].
}
method addConnection: conn method addConnection: conn
{ {
| rid | | rid |
@ -274,114 +256,23 @@ class HttpServer(Object)
rid := conn rid. rid := conn rid.
if (rid notNil) if (rid notNil)
{ {
('REMOVE CONNECTION ' & rid asString) dump.
self.connreg remove: (conn rid). self.connreg remove: (conn rid).
conn server: nil rid: -1. conn server: nil rid: nil.
}. }.
} }
}
class HttpListener2(Socket)
{
var(#get) server := nil.
var(#get) rid := -1.
var sem.
method initialize
{
super initialize.
self.sem := Semaphore new.
self.sem signalAction: [:sem |
| cliaddr clisck cliact fd |
cliaddr := SocketAddress new.
fd := self _accept: cliaddr.
##if (fd >= 0)
if (fd notNil)
{
clisck := (self acceptedSocketClass) __with: fd.
clisck beWatched.
self onSocketAccepted: clisck from: cliaddr.
}.
].
}
(*
method onSocketAccepted: clisck from: cliaddr
{
| rid |
'CLIENT accepted ..............' dump.
clisck dump.
cliaddr dump.
if (self.server notNil)
{
server addConnection: clisck.
if (clisck isKindOf: SyncSocket)
{
'SERVICE READLLY STARTING' dump.
[clisck runService] fork.
}
}.
}
method acceptedSocketClass
{
##^if (self currentAddress port == 80) { HttpSocket } else { HttpSocket }.
^HttpSocket.
}
method server: server rid: rid
{
self.server := server.
self.rid := rid.
}
*)
}
class HttpServer2(Object)
{
var listeners.
var connreg.
var sg.
method initialize
{
super initialize.
self.sg := SemaphoreGroup new.
self.listeners := HttpConnReg new.
self.connreg := HttpConnReg new.
}
method start: laddr method start: laddr
{ {
| listener sem ss | | listener |
if (laddr class == Array) if (laddr class == Array)
##if (laddr respondsTo: #do:) ## can i check if the message receives a block and the block accepts 1 argument?
{ {
laddr do: [:addr | laddr do: [:addr | self __add_new_listener: addr ].
listener := HttpListener2 family: (addr family) type: Socket.Type.STREAM.
if ((self addListener: listener) notError)
{
listener bind: addr.
listener _listen: 128.
}
].
} }
else else
{ {
listener := HttpListener2 family: (laddr family) type: Socket.Type.STREAM. self __add_new_listener: laddr.
if ((self addListener: listener) notError)
{
listener bind: laddr.
listener _listen: 128.
}
}. }.
} }
@ -395,48 +286,6 @@ class HttpServer2(Object)
conn close. conn close.
]. ].
} }
method addConnection: conn
{
| rid |
rid := self.connreg add: conn.
if (rid isError)
{
'ERROR - CANNOT REGISTER NEW CONNECTION >>>>>>>>>> ' dump.
conn close.
^rid.
}.
('ADD NEW CONNECTION ' & rid asString) dump.
conn server: self rid: rid.
}
method removeConnection: conn
{
self.connreg remove: (conn rid).
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.
}
} }
class MyObject(Object) class MyObject(Object)
@ -624,7 +473,7 @@ httpd connect: addr.
sg := SemaphoreGroup new. sg := SemaphoreGroup new.
[ [
httpd := HttpServer2 new. httpd := HttpServer new.
[ [
httpd start: %( httpd start: %(
SocketAddress fromString: '[::]:7777', SocketAddress fromString: '[::]:7777',

View File

@ -555,7 +555,7 @@ class AsyncSocket(Socket)
pos := offset. pos := offset.
rem := length. rem := length.
while (rem > 0) ## TODO: loop to write as much as possible while (rem > 0)
{ {
n := self _writeBytes: bytes offset: pos length: rem. n := self _writeBytes: bytes offset: pos length: rem.
if (n <= -1) { break }. if (n <= -1) { break }.
@ -582,7 +582,6 @@ class AsyncSocket(Socket)
^self writeBytes: bytes offset: 0 length: (bytes size) ^self writeBytes: bytes offset: 0 length: (bytes size)
} }
##method onSocketClosed ##method onSocketClosed
##{ ##{
##} ##}
@ -726,19 +725,6 @@ class AsyncServerSocket(AsyncSocket)
} }
} }
class Mux(SemaphoreGroup)
{
method addSocket: sck
{
self addSemaphore: (sck inreadysem).
}
method removeSocket: sck
{
}
}
(* (*
class ListenerSocket(Socket) class ListenerSocket(Socket)
{ {
@ -762,7 +748,6 @@ class ListenerSocket(Socket)
sg addSemaphore: self.inreadysem. sg addSemaphore: self.inreadysem.
self.inreadysem signalOnInput: self.handle. self.inreadysem signalOnInput: self.handle.
self onSocketAccepted: clisck from: cliaddr. self onSocketAccepted: clisck from: cliaddr.
}. }.
]. ].