Re: [Nbserver-devel] NBServer for "client" side ...
Status: Beta
Brought to you by:
szegedia
From: Attila S. <sze...@fr...> - 2002-02-26 16:09:32
|
Frankly, I dislike the design. The concept of factories is required for a server-side of a protocol since the server does not control when a connection is made - the client establishes the connection, thus the client determines its creation time. The server is notified of the fact through the "accept" event, and has to create a protocol handler at that time; thus the need for a protocol handler factory. On the other hand, the client side completely controls when is the connection established - it follows from the program logic. Therefore, client side can directly create protocol handlers and need not be burdened with complexity of dealing with factories and connect callbacks. In this spirit, client connecting code should not be piggybacked on Service. My proposal instead is to create a NioServer.registerClient method. The client code would then do something like: SocketChannel ch = socket.getChannel(); ProtocolHandler handler = new MyProtocolHandler(ch); server.registerClient(ch, handler); registerClientProtocolHandler would handle the details of calling connect(), finishConnect() and friends. Errors could be handler solely within NioServer code, however there would be need for the protocol handler to get a notificiation of a successful connect (i.e. to initialize state that should be only initialized in case of successful connect). Therefore I propose a new interface: public interface ClientProtocolHandler extends ProtocolHandler { public void afterConnect(); } and in the NioServer: public void registerClient(SocketAddress addr, SocketChannel channel, ClientProtocolHandler handler); The initial changes are already committed to CVS; while this does not resemble much your original idea, I've credited you as the author on relevant classes since I've borrowed many ideas (as well as the correct connect()/finishConnect() method call order). It's not in a new release yet. Take a look and tell me how do you like it. Note that in the current scenario the user has to create the socket channel, but it is OK - he might want to set different TCP options on the socket (linger, timeout, nodelay, etc.) and I don't want to take away that flexibility. Cheers, Attila. -- Attila Szegedi home: http://www.szegedi.org ----- Original Message ----- From: "Zombi" <zo...@ma...> To: <nbs...@li...> Sent: 2002. február 22. 1:30 Subject: [Nbserver-devel] NBServer for "client" side ... > > Hi ! > > I wrote some code, which helps to use nbserver protocol abstraction, > from the "client" side, when our program initiate the communication to > a remote address. > To use it,we just call : > service.doConnect( SocketAddress address ) > and (as i hope:-) the NioServer opens a SocketChannel to the address, > register selectionkey to the NioServer.connectingSelectors, which > delivers OP_CONNECT message back to Service.connected(SocketChannel) > which finally call the ProtocolHandlerFactory.createProtocolHandler() > method, and register the ProtocolHandler with the NioServer ... :-) > I hope it's works, but i can't test it now ... :-( at least, i'm sure, > i didn't brake any existing code :-) > I include the 2 modified source (NioServer.java, Service.java) the > new ConnectingSelectorLoop.java, and an output of a 'cvs-diff' > command ... > > good bye > Zsombor > |