So I've finished two big pieces of the "current projects" list. Now I have a full blown DNS client that lets me pick a dns server and request any kind of data from it. That means I can do reverse lookups, pointer lookups, ownership checks, and real time black hole list lookups from my own code. So, how will I add that to a back end server?
Well, the way I've implemented my "Generic" server, all I have to do is create a new class which extends my base class called "NctSessionHandler" and override a single method, called "acceptData". That method gets called by the base class when the first command is sent to the server from a connecting client. When the method finishes, it drops back to the server and if another base command comes in, the method is called again. I "own" the session once its passed though. If I'm finished with it, I can call the built in method to terminate the session, then when I drop control back to the server it will wait for a new connection. If I fail to call that, the sever will time the connection out if there are no more commands. I can control the time out time however, so I could keep the session open for as long as I want. If I want to do more complex things, I have methods I can call to send data back to the client, or to wait for a response from the client (with a time out that I set, and a terminating character that I determine). I can also find out the address and port the user is connecting with. In short, its a fully functional server, but I don't have to work about any of the threading, connections, or complex infrastructure.
The code below is all I've had to do to create a running server capable of handling up to around a hundred simple connections per SECOND. The code accepts data across the connection in the form of simple strings of text, and responds back with the same string, reversed.
First, I create the new session handler class from the base class ---
class NctAntiSpamSessionHandler extends NctSessionHandler { br> public void acceptData(String dataString) br> { br> String backLine = ""; br> for(int z = dataString.length() ; z > 0 ; z-- ) br> backLine = backLine + dataString.substring(z, z + 1); br> sendData(backLine + "\n"); br> } br> }
That's it. That's the whole class. Now, below is the code to instantiate and launch the server. Notice that I'm passing it a sample handler of my own. The handler I pass it must be a subclass of the base "NctSessionHandler" class, which also extends Thread and implements Cloneable. The sever will use the handler (which has not been started) and make cloned copies for each thread it needs to run. It will make sure the parameters are either set to default, or that I've set them myself, then start each listener thread.
int port = 33900 ;
int threads = 100 ;
int maxQueue = 5 ;
NctAntiSpamSessionHandler asHander = new NctAntiSpamSessionHandler();
NctGenericServer server = new NctGenericServer(port, threads, maxQueue, asHander);
server.start();
Ok, so if you've wondered why I'd spend days working on something that can't do much more than parot back what you put in -- really fast -- consider that I now have an easy, flexible infrastructure for all kinds of back end server needs.
Comment Entry |
Please wait while your document is saved.
http://www.nsftools.com/tips/JavaTips.htm#javaaddin
And also, I probably would have used the variable "assHandler" for
AntiSpamSessionHandler, but that's just me...
;-)
- Julian