started adding OrderedCollection
This commit is contained in:
		| @ -253,6 +253,88 @@ class(#byte) ByteArray(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) | ||||
|  | ||||
| @ -1,14 +1,6 @@ | ||||
| ###include 'Moo.moo'. | ||||
| #include 'Socket.moo'. | ||||
|  | ||||
| class HttpReq(Object) | ||||
| { | ||||
| } | ||||
|  | ||||
| class HttpReqBuilder(Object) | ||||
| { | ||||
| } | ||||
|  | ||||
| class HttpConnReg(Object) | ||||
| { | ||||
| 	var connections. | ||||
| @ -66,8 +58,9 @@ class HttpConnReg(Object) | ||||
| 	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.	 | ||||
| 		## the following loop won't evaluate the given block 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) | ||||
| @ -82,18 +75,15 @@ class HttpConnReg(Object) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| class HttpBuffer(Object) | ||||
| { | ||||
| } | ||||
|  | ||||
| class HttpSocket(SyncSocket) | ||||
| { | ||||
| 	var(#get) server := nil. | ||||
| 	var(#get) rid := -1. | ||||
|  | ||||
| 	method onSocketDataIn | ||||
| 	{ | ||||
| 		'CLIENT got DATA' dump. | ||||
| 		###self readBytes: buf. | ||||
| 		self close. | ||||
| 	} | ||||
|  | ||||
| 	method close | ||||
| 	{ | ||||
| ('Http Connection closing.......... handle ' & self.handle asString) dump. | ||||
| @ -111,12 +101,30 @@ class HttpSocket(SyncSocket) | ||||
| 		self.rid := rid. | ||||
| 	} | ||||
|  | ||||
| 	method getLine | ||||
| 	{ | ||||
| 		 | ||||
| 	} | ||||
|  | ||||
| 	method readRequest | ||||
| 	{ | ||||
| 		 | ||||
| 	} | ||||
|  | ||||
| 	method _run_service | ||||
| 	{ | ||||
| 		| buf | | ||||
|  | ||||
| 		self timeout: 10. | ||||
| 		(*while (true) | ||||
| 		{ | ||||
| 			req := self readRequest. | ||||
|  | ||||
| 		}. *) | ||||
|  | ||||
| 		buf := ByteArray new: 128. | ||||
| 'IM RUNNING SERVICE...............' dump. | ||||
| 		self timeout: 10. | ||||
| 		 | ||||
| 		self readBytes: buf. | ||||
| 		buf dump. | ||||
| 		self readBytes: buf. | ||||
| @ -143,18 +151,19 @@ class HttpListener(AsyncServerSocket) | ||||
| 		super initialize. | ||||
| 	} | ||||
|  | ||||
| 	##method close | ||||
| 	##{ | ||||
| 	##	super close. | ||||
| 	##} | ||||
| 	method close | ||||
| 	{ | ||||
| 		if (self.server notNil) { self.server removeListener: self }. | ||||
| 		^super close. | ||||
| 	} | ||||
|  | ||||
| 	method onSocketAccepted: clisck from: cliaddr | ||||
| 	{ | ||||
| 		| rid | | ||||
|  | ||||
| 		'CLIENT accepted ..............' dump. | ||||
| 'CLIENT accepted ..............' dump. | ||||
| clisck dump. | ||||
| 		cliaddr dump. | ||||
| cliaddr dump. | ||||
|  | ||||
| 		if (self.server notNil) | ||||
| 		{ | ||||
| @ -167,10 +176,10 @@ clisck dump. | ||||
| 				} | ||||
| 			] | ||||
| 			on: Exception do: [:ex | | ||||
| 				clisck close. | ||||
| 				Exception signal: ('unable to handle a new connection - ' & ex messageText). | ||||
| 			]. | ||||
| 		}. | ||||
|  | ||||
| 	} | ||||
|  | ||||
| 	method acceptedSocketClass | ||||
| @ -206,60 +215,33 @@ class HttpServer(Object) | ||||
| 		listener server: self rid: rid. | ||||
| 	} | ||||
|  | ||||
| 	method __remove_listener: listener | ||||
| 	method removeListener: listener | ||||
| 	{ | ||||
| 		| rid | | ||||
| 		rid = listener rid. | ||||
| 		rid := listener rid. | ||||
| 		if (rid notNil) | ||||
| 		{ | ||||
| ('REALLY REMOVE LISTENER ' & rid asString) dump. | ||||
| 			self.listeners remove: (listener rid). | ||||
| 			listener server: nil rid: nil. | ||||
| 		}. | ||||
| 	} | ||||
|  | ||||
| 	method __start_new_listener: addr | ||||
| 	method __add_new_listener: addr | ||||
| 	{ | ||||
| 		| listener | | ||||
| 		listener := HttpListener family: (addr family) type: Socket.Type.STREAM. | ||||
| 		[ | ||||
| 			if ((self __add_listener: listener) notError) | ||||
| 			{ | ||||
| 				listener bind: addr. | ||||
| 				listener listen: 128. | ||||
| 			} | ||||
| 			self __add_listener: listener. | ||||
| 			listener bind: addr. | ||||
| 			listener listen: 128. | ||||
| 		] on: Exception do: [:ex | | ||||
| 			self __remove_listener: listener. | ||||
| 			listener close. | ||||
| 			## ex pass. | ||||
| 			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 | ||||
| 	{ | ||||
| 		| rid | | ||||
| @ -274,114 +256,23 @@ class HttpServer(Object) | ||||
| 		rid := conn rid. | ||||
| 		if (rid notNil) | ||||
| 		{ | ||||
| ('REMOVE CONNECTION ' & rid asString) dump. | ||||
| 			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 | ||||
| 	{ | ||||
| 		| listener sem ss | | ||||
|  | ||||
| 		| 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 | | ||||
| 				listener := HttpListener2 family: (addr family) type: Socket.Type.STREAM. | ||||
| 				if ((self addListener: listener) notError) | ||||
| 				{ | ||||
| 					listener bind: addr. | ||||
| 					listener _listen: 128. | ||||
| 				} | ||||
| 			]. | ||||
| 			laddr do: [:addr | self __add_new_listener: addr ]. | ||||
| 		} | ||||
| 		else | ||||
| 		{ | ||||
| 			listener := HttpListener2 family: (laddr family) type: Socket.Type.STREAM. | ||||
| 			if ((self addListener: listener) notError) | ||||
| 			{ | ||||
| 				listener bind: laddr. | ||||
| 				listener _listen: 128. | ||||
| 			} | ||||
| 			self __add_new_listener: laddr. | ||||
| 		}. | ||||
| 	} | ||||
|  | ||||
| @ -395,48 +286,6 @@ class HttpServer2(Object) | ||||
| 			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) | ||||
| @ -624,7 +473,7 @@ httpd connect: addr. | ||||
| 		sg := SemaphoreGroup new. | ||||
|  | ||||
| 		[ | ||||
| 			httpd := HttpServer2 new. | ||||
| 			httpd := HttpServer new. | ||||
| 			[ | ||||
| 				httpd start: %( | ||||
| 					SocketAddress fromString: '[::]:7777', | ||||
|  | ||||
| @ -555,7 +555,7 @@ class AsyncSocket(Socket) | ||||
| 		pos := offset. | ||||
| 		rem := length. | ||||
|  | ||||
| 		while (rem > 0) ## TODO: loop to write as much as possible | ||||
| 		while (rem > 0) | ||||
| 		{ | ||||
| 			n := self _writeBytes: bytes offset: pos length: rem. | ||||
| 			if (n <= -1)  { break }. | ||||
| @ -582,7 +582,6 @@ class AsyncSocket(Socket) | ||||
| 		^self writeBytes: bytes offset: 0 length: (bytes size) | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	##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) | ||||
| { | ||||
| @ -762,7 +748,6 @@ class ListenerSocket(Socket) | ||||
| 				sg addSemaphore: self.inreadysem. | ||||
| 				self.inreadysem signalOnInput: self.handle. | ||||
|  | ||||
|  | ||||
| 				self onSocketAccepted: clisck from: cliaddr. | ||||
| 			}. | ||||
| 		]. | ||||
|  | ||||
		Reference in New Issue
	
	Block a user