-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from juliendelplanque/tcp-server
tcp-server
- Loading branch information
Showing
7 changed files
with
266 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
" | ||
I am a client for JSON-RPC 2.0 over TCP. | ||
Internally, I use a Socket. | ||
" | ||
Class { | ||
#name : #JRPCTCPClient, | ||
#superclass : #JRPCClient, | ||
#instVars : [ | ||
'address', | ||
'port' | ||
], | ||
#category : #'JRPC-Client' | ||
} | ||
|
||
{ #category : #defaults } | ||
JRPCTCPClient class >> defaultPort [ | ||
^ 4000 | ||
] | ||
|
||
{ #category : #accessing } | ||
JRPCTCPClient >> address [ | ||
^ address ifNil: [ address := NetNameResolver localHostAddress ] | ||
] | ||
|
||
{ #category : #accessing } | ||
JRPCTCPClient >> address: anObject [ | ||
address := anObject | ||
] | ||
|
||
{ #category : #accessing } | ||
JRPCTCPClient >> defaultPort [ | ||
^ self class defaultPort | ||
] | ||
|
||
{ #category : #accessing } | ||
JRPCTCPClient >> port [ | ||
^ port ifNil: [ port := self defaultPort ] | ||
] | ||
|
||
{ #category : #accessing } | ||
JRPCTCPClient >> port: anObject [ | ||
port := anObject | ||
] | ||
|
||
{ #category : #'private - sending' } | ||
JRPCTCPClient >> sendRequest: aJRPCRequestObject [ | ||
| socket result | | ||
socket := Socket newTCP. | ||
socket | ||
connectTo: self address | ||
port: self port. | ||
socket | ||
sendData: (self convertJRPCJsonableObjectToJSON: aJRPCRequestObject asJRPCJSON). | ||
result := socket receiveData. | ||
socket closeAndDestroy. | ||
^ self parseSupposedJRPCMessageObjectFromString: result | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
" | ||
I am a JSON-RPC 2.0 server running over the TCP protocol. | ||
Internally, I use a Socket. | ||
" | ||
Class { | ||
#name : #JRPCTCPServer, | ||
#superclass : #JRPCServer, | ||
#instVars : [ | ||
'port', | ||
'tcpServer', | ||
'serverLoop', | ||
'process' | ||
], | ||
#category : #'JRPC-Server' | ||
} | ||
|
||
{ #category : #defaults } | ||
JRPCTCPServer class >> defaultPort [ | ||
^ 4000 | ||
] | ||
|
||
{ #category : #defaults } | ||
JRPCTCPServer >> defaultPort [ | ||
^ self class defaultPort | ||
] | ||
|
||
{ #category : #accessing } | ||
JRPCTCPServer >> port [ | ||
^ port ifNil: [ port := self defaultPort ] | ||
] | ||
|
||
{ #category : #accessing } | ||
JRPCTCPServer >> port: anObject [ | ||
port := anObject | ||
] | ||
|
||
{ #category : #starting } | ||
JRPCTCPServer >> start [ | ||
tcpServer := Socket newTCP. | ||
tcpServer listenOn: self port backlogSize: 10. | ||
serverLoop := true. | ||
process := [ [ serverLoop ] | ||
whileTrue: [ (tcpServer waitForAcceptFor: 60) | ||
ifNotNil: [ :clientSocket | | ||
[ [ | ||
| data | | ||
data := clientSocket receiveData. | ||
clientSocket sendData: (self handleJSON: data contents) ] | ||
ensure: [ clientSocket closeAndDestroy ] ] | ||
forkAt: Processor lowIOPriority | ||
named: 'JRPC TCP connection' ] ] ] | ||
forkAt: Processor highIOPriority | ||
named: 'JRPC TCP server' | ||
] | ||
|
||
{ #category : #stopping } | ||
JRPCTCPServer >> stop [ | ||
serverLoop := false. | ||
tcpServer closeAndDestroy. | ||
|
||
"Ensure process finishes." | ||
process isTerminated | ||
ifTrue: [ ^ self ]. | ||
process terminate | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
Class { | ||
#name : #JRPCAbstractServerTest, | ||
#superclass : #TestCase, | ||
#instVars : [ | ||
'server' | ||
], | ||
#category : #'JRPC-Tests' | ||
} | ||
|
||
{ #category : #testing } | ||
JRPCAbstractServerTest class >> isAbstract [ | ||
^ self = JRPCAbstractServerTest | ||
] | ||
|
||
{ #category : #private } | ||
JRPCAbstractServerTest >> checkPortAvailability [ | ||
|
||
[ ( ZnNetworkingUtils serverSocketOn: self port ) close ] | ||
on: Error | ||
do: [ :error | self fail: ( 'Port <1p> is not available' expandMacrosWith: self port ) ] | ||
] | ||
|
||
{ #category : #'instance creation' } | ||
JRPCAbstractServerTest >> newJRPCClient [ | ||
^ self subclassResponsibility | ||
] | ||
|
||
{ #category : #private } | ||
JRPCAbstractServerTest >> port [ | ||
|
||
^ 7777 | ||
] | ||
|
||
{ #category : #running } | ||
JRPCAbstractServerTest >> tearDown [ | ||
|
||
server ifNotNil: [ server stop ]. | ||
server := nil. | ||
super tearDown | ||
] | ||
|
||
{ #category : #accessing } | ||
JRPCAbstractServerTest >> testNotification [ | ||
|
||
| notificationCount | | ||
|
||
notificationCount := 0. | ||
server addHandlerNamed: 'mail_sent' block: [ notificationCount := notificationCount + 1 ]. | ||
|
||
self newJRPCClient notifyMethod: 'mail_sent'. | ||
|
||
self assert: notificationCount equals: 1 | ||
] | ||
|
||
{ #category : #accessing } | ||
JRPCAbstractServerTest >> testNotificationWithInvalidMethod [ | ||
|
||
| notificationCount | | ||
|
||
notificationCount := 0. | ||
server addHandlerNamed: 'mail_sent' block: [ notificationCount := notificationCount + 1 ]. | ||
|
||
self newJRPCClient notifyMethod: 'invalid'. | ||
|
||
self assert: notificationCount equals: 0 | ||
] | ||
|
||
{ #category : #accessing } | ||
JRPCAbstractServerTest >> testRequestOnInvalidEndpoint [ | ||
|
||
| httpClient failed | | ||
|
||
server addHandlerNamed: 'sum' block: [ :a :b | a + b ]. | ||
failed := false. | ||
|
||
httpClient := JRPCClient http: ( 'http://localhost' asUrl port: self port ) / 'bad'. | ||
httpClient | ||
ifFail: [ :error | | ||
failed := true. | ||
'{}' | ||
]. | ||
self | ||
should: [ httpClient callMethod: 'sum' arguments: #(1 3) withId: 1 ] raise: JRPCIncorrectJSON; | ||
assert: failed | ||
] | ||
|
||
{ #category : #accessing } | ||
JRPCAbstractServerTest >> testRequestWithoutParameters [ | ||
|
||
server addHandlerNamed: 'zero' block: [ 0 ]. | ||
|
||
self assert: ( self newJRPCClient callMethod: 'zero' withId: 3 ) result equals: 0 | ||
] | ||
|
||
{ #category : #accessing } | ||
JRPCAbstractServerTest >> testValidRequest [ | ||
|
||
server addHandlerNamed: 'sum' block: [ :a :b | a + b ]. | ||
|
||
self assert: ( self newJRPCClient callMethod: 'sum' arguments: #(1 3) withId: 1 ) result equals: 4 | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Class { | ||
#name : #JRPCTCPServerTest, | ||
#superclass : #JRPCAbstractServerTest, | ||
#category : #'JRPC-Tests' | ||
} | ||
|
||
{ #category : #'instance creation' } | ||
JRPCTCPServerTest >> newJRPCClient [ | ||
^ JRPCClient tcpForAddress: NetNameResolver localHostAddress port: self port | ||
] | ||
|
||
{ #category : #running } | ||
JRPCTCPServerTest >> setUp [ | ||
|
||
super setUp. | ||
self checkPortAvailability. | ||
server := JRPCServer tcp. | ||
server | ||
port: self port. | ||
self assert: server port equals: self port. | ||
server start | ||
] |