[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-smalltalk] Proxy learning exercise
From: |
Stephen |
Subject: |
[Help-smalltalk] Proxy learning exercise |
Date: |
Mon, 11 Aug 2008 13:02:08 +1200 |
User-agent: |
Thunderbird 2.0.0.16 (Macintosh/20080707) |
Hi All,
As a learning exercise I thought I would extend the Wiki Chatserver
(http://sblinn.jottit.com/GNU_Smalltalk_SimpleEcho_TCP_Server) to create
a simple forwarding proxy server.
I'm hoping someone can give me a hand...
Simply extending the example of the chat server could result in
something like the code below...
run
| source |
[
ss waitForConnection.
source := (ss accept).
[self handleSocket: source] fork
] repeat
!!
"Instance method to handle each connection"
!SimpleProxyServer methodsFor: 'handling'!
handleSocket: source
| msg dest |
dest := (TCP.Socket remote: amailserver port: 25).
[
"handle source data"
msg := (source nextHunk).
msg displayOn: dest.
dest flush.
"handle dest data"
msg := (dest nextHunk).
msg displayOn: source.
source flush.
] repeat
!!
-----------------------
But the code above wouldn't work since it blocks on input from source
and there may be data in the dest Stream. So I modified handleSocket to
look like this...
run
| source |
[
ss waitForConnection.
source := (ss accept).
[self handleSocket: source] fork
] repeat
!!
"Instance method to handle each connection"
!SimpleProxyServer methodsFor: 'handling'!
handleSocket: source
| msg dest |
dest := (TCP.Socket remote: amailserver port: 25).
[
"handle source data"
(source available) ifTrue: [
msg := (source nextHunk).
msg displayOn: dest.
dest flush ].
"handle dest data"
(dest available) ifTrue: [
msg := (dest nextHunk).
msg displayOn: source.
source flush ].
] repeat
!!
-----------------------
And this is where I'd like a hand. The code above works as a relay but
isn't suitable because:
1. It doesn't close the client (source) socket when the server (dest)
socket closes.
2. The loop is CPU intensive
I would like to know how to block until _either_ the "source" socket,
or, the "dest" socket has some data.
Also, if either of the source or dest sockets is closed then the thread
should terminate and close any sockets owned by the thread.
Thanks
Stephen
- [Help-smalltalk] Proxy learning exercise,
Stephen <=