This is what i 've done with my first version of SMTP driver, i wrote it in pure Tcl. Performance was terrible, the site was serving thousands of incoming SMTP connections and it was very slow. Second version was doing pretty same stuff but in C, i registered TCP callback and create new thread for each new connection. Not much a difference. Only after i started using AOLServer's thread pools and connection manager, performance issues disappear. So if new protocol does not serve thousands of connections, Tcl/TCP callback solution works just fine.
Dossy Shiobara wrote:
On 2004.08.17, Zoran Vasiljevic <[EMAIL PROTECTED]> wrote:
So, all of you interested in extending AS to handle non-http protocols, please go, have a look and give some feedback.
Just the other night, I had the need to implement a non-HTTP server in AOLserver in order to implement the TiVo "beacon" connection for the Home Media Option (MHO) feature of the Series2 TiVo. I thought about both Vlad's and Stephen's approaches, and decided I'd rather stay pure-Tcl instead. Here's what I came up with:
proc tivo.init {} { # ...some setting of nsv config vars here...
ns_thread begindetached tivo.beaconThread }
proc tivo.beaconThread {} { set address [nsv_get tivo address] set beaconport [nsv_get tivo beaconport] ns_log notice "TiVo beacon listening on $address:$beaconport"
set master [ns_socklisten $address $beaconport] while {![nsv_get tivo shutdown]} { foreach {rfd wfd} [ns_sockaccept $master] break ns_chan create $rfd $rfd ns_chan create $wfd $wfd ns_thread begindetached [list tivo.beacon $rfd $wfd] } close $master }
tivo.beacon {rfd wfd} { ns_chan get $rfd ns_chan get $wfd
# ... do actual beacon exchange ...
# connection must remain open, so we just poll until the remote # side disconnects. while {![nsv_get tivo shutdown]} { set sel [ns_sockselect -timeout 2 $rfd {} {}] if {[llength [lindex $sel 0]]} { read $rfd 1 if {[fblocked $rfd] || [eof $rfd]} { ns_log notice "TiVo: Beacon disconnected." break } } } close $rfd close $wfd }
The gist of this is that:
1. Can't use [ns_socklistencallback] since our connection handler keeps the connection open, and that would block the thread that executes the callback handler.
2. At server start-up, we create a thread to just accept connections which creates new threads to process those connections.
3. We use [ns_chan] to detach/attach Tcl channels to the socket fd's in order to pass them from the master thread to the slave threads.
So far, this has been working really well for me. No modification to the core involved, and it's implemented in very few LOC overall. Using the same pattern, I could easily implement an SMTP, IMAP, etc., server in pure Tcl. I'm not sure what the real performance difference there would be compared to the two proposals that require C code changes in the core: as long as the actual "functionality" is implemented in a Tcl callback that gets invoked from the C code, the performance difference is probably negligible.
On a side note: if you have a Series2 TiVo with Home Media Option, and want to serve content to it from AOLserver ... I've got a module that "works" (but is far from complete) that does it. :-)
-- Dossy
-- Dossy Shiobara mail: [EMAIL PROTECTED] Panoptic Computer Network web: http://www.panoptic.com/ "He realized the fastest way to change is to laugh at your own folly -- then you can let go and quickly move on." (p. 70)
-- AOLserver - http://www.aolserver.com/
To Remove yourself from this list, simply send an email to <[EMAIL PROTECTED]> with the body of "SIGNOFF AOLSERVER" in the email message. You can leave the Subject: field of your email blank.
-- Vlad Seryakov 703 488-2173 office [EMAIL PROTECTED] http://www.crystalballinc.com/vlad/
-- AOLserver - http://www.aolserver.com/
To Remove yourself from this list, simply send an email to <[EMAIL PROTECTED]> with the body of "SIGNOFF AOLSERVER" in the email message. You can leave the Subject: field of your email blank.
