Access to mediated networking is achieved with just this code:
import org.jtrix.facets1.node.ITrampolineFacet; import org.jtrix.facets1.node.ISocketFactory; ... String tf_name = ITrampolineFacet.class.getName(); ITrampolineFacet tf = (ITrampolineFacet) node.bindFacet(tf_name); ISocketFactory sf = tf.getDefaultSocketFactory(); tf.setSocketFactory(sf);
That's it. Networking is initialised. After this the netlet's code can happily use the Java networking classes as usual.
The ITrampolineFacet accesses the trampolining mechanism for that netlet, allowing requests to be proxied (trampolined) to the right resources. The first two lines (after the import) simply bind to the netlet's trampoline system.
The second line then tells the facet to use its own socket factory for datagram and stream sockets.
One of the pleasing things about this approach is that the implementation behind the resulting trampoline facet is specific to that netlet. Another netlet running on the same node will get a different implementation when it also binds and sets. On the other hand, one netlet can still pass its received implementation to another netlet if it wishes.
When dealing with a server socket (not an ordinary socket) Java assumes its implementation does not change throughout the server socket's lifetime. When a server socket is created it waits for a connection; when a connection is made a new socket is created, and if the implementaion has changed in between then a SocketException is thrown, saying ``Socket Implementaion Differs''. This could be a problem if you change the socket factory. If you do need to change it then make sure your server sockets are closed before changing.
It is easy to imagine also a particular socket factory which manages use of two services simultaneously distinguishing them by their own port ranges, for example.
Nik Silver 2002-03-09