/*
*Copyright (c) jmin Organization. All rights reserved.
*/
package org.jmin.j2ee.kernel.net;
/**
* A listener run in service side and catch connection request event from
* client.
*
* @author Chris Liao
*/
publicinterface ServerListener extends NetEventListener {
/**
* When a connection requst arrives,this method will run.
*/
publicvoid onConnect(ConnectEvent event);
/**
* Method run after acceptance in server side.
*/
publicvoid onConnected(ConnectedEvent event);
/**
* Method run after remote host close connection.
*/
publicvoid onClosed(DisconnectEvent event);
}
/*
*Copyright (c) jmin Organization. All rights reserved.
*/
package org.jmin.j2ee.kernel.net.nio.test;
import org.jmin.j2ee.kernel.net.ConnectEvent;
import org.jmin.j2ee.kernel.net.ConnectedEvent;
import org.jmin.j2ee.kernel.net.DisconnectEvent;
import org.jmin.j2ee.kernel.net.ServerListener;
import org.jmin.j2ee.kernel.net.nio.NIOConnector;
/**
* A sample implementation of server listener interface and action by event.
* This listener act on server channel.
*
* @author Chris Liao
*/
publicclass NIOServerListener implements ServerListener {
/**
* Method to handle a event represents request from remote host
*
* @param event
*/
publicvoid onConnect(ConnectEvent event){
/**
* cast event source to a connector
*/
NIOConnector con = (NIOConnector)event.getSource();
/**
* print request
*/
System.out.println("Find a connection request from: " + con.getRemoteHost());
}
/**
* If the comming connection is accepted by server, a notification event will
* be sent out.
*
* @param event
*/
publicvoid onConnected(ConnectedEvent event){
/**
* cast event source to a connector
*/
NIOConnector con = (NIOConnector)event.getSource();
/**
* print request
*/
System.out.println("Accepted a connection request from: " + con.getRemoteHost());
}
/**
* If remote host close connection to server, a event will be sent and handled
* by this method.
*
* @param event
*/
publicvoid onClosed(DisconnectEvent event){
/**
* cast event source to a connector
*/
NIOConnector con = (NIOConnector)event.getSource();
/**
* print a hint message
*/
System.out.println("Connection to host[" + con.getRemoteHost() +"] is closed from server");
}
}
/*
*Copyright (c) jmin Organization. All rights reserved.
*/
package org.jmin.j2ee.kernel.net.nio.test;
import java.io.IOException;
import org.jmin.j2ee.kernel.net.nio.ServerCluster;
/**
* Test sample to build a socket server based on NIO and don't care more detail.
*
* @author chris
*/
publicclass NIOServer {
/**
* Entrance method to build a server.If get familiar with nio, it is easy to be
* catched.
*/
publicstaticvoid main(String args[]) throws IOException {
/**
* Cluster object works as a server collection manager,some servers can be open
* in it.Before useful operation,<method>open</method>must be invoked to create
* a cluster instance.
*/
ServerCluster cluster = ServerCluster.open();
/**
* Create a server listener
*/
NIOServerListener serverListener = new NIOServerListener();
/**
* Call<method>openServer</method>to create a socket server,two necessary
* arguments are applied here: server port and message lister class.
*
* We can assume that every server can handle matched messages from clients,
* this listener class is extended from super <class>org.jmin.j2ee.kernel.Listener</class>,
* of course,you can define sub implementation based on the class.
*/
cluster.openServer(9988,serverListener,NIOConnectionListener.class);
/**
* Print a successful creation message on console.
*/
System.out.println("A nio server run on port: "+ 9988);
/**
* Create a synchronized object to block the main thread and make the server kept running.
*/
Object obj = new Object();
try {
synchronized (obj) {
obj.wait();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*
*Copyright (c) jmin Organization. All rights reserved.
*/
package org.jmin.j2ee.kernel.net.nio.test;
import org.jmin.j2ee.kernel.net.NetConnectorListener;
import org.jmin.j2ee.kernel.net.DisconnectEvent;
import org.jmin.j2ee.kernel.net.ErrorEvent;
import org.jmin.j2ee.kernel.net.ReadEvent;
import org.jmin.j2ee.kernel.net.WriteEvent;
import org.jmin.j2ee.kernel.net.ConnectedEvent;
import org.jmin.j2ee.kernel.net.nio.NIOConnector;
/**
* A reference implementation of listener interface on net connector.
* This object can handle some events from net connector, for example:
* connection event, read event, write event and so on.
*
* Class instances work in client side and server side.
*
* @author Chris Liao
*/
publicclass NIOConnectionListener implements NetConnectorListener {
/**
* When connection to remote host is stable, a connection event will be published,
* and catched by this method.
*/
publicvoid onConnected(ConnectedEvent event){
/**
* cast event source to a connector
*/
NIOConnector con = (NIOConnector)event.getSource();
/**
* print a hint message
*/
System.out.println("Connection to host[" + con.getRemoteHost() +"] is stable");
}
/**
* When some data has been read from remote host,then a read event is sent out.
* This method is used to handle the event,some detailed logic can be added here.
*/
publicvoid onRead(ReadEvent event) {
/**
* get a byte array from event object
*/
byte[] data = event.getData();
/**
* cast event source to a connector
*/
NIOConnector con = (NIOConnector)event.getSource();
/**
* print message on console
*/
System.out.println("Read a meessage: " + new String(data));
/**
* send the data to remote, in fact the byte data will stored in temp pool
* util ready to be sent.
*/
con.write(data);
}
/**
* When a connector associated with the current listener send a message to remote,
* then publish a event as notification.
*/
publicvoid onWrite(WriteEvent event) {
byte[] data = event.getData();
System.out.println("Send a message: " + new String(data));
}
/**
* When remote host close connection, a event will be published.
*
* @param event
*/
publicvoid onClosed(DisconnectEvent event) {
/**
* cast event source to a connector
*/
NIOConnector con = (NIOConnector)event.getSource();
/**
* print a hint message
*/
System.out.println("Connection to host[" + con.getRemoteHost() +"] is closed");
}
/**
* When a error cause during communication,publish a error event
*
* @param event
*/
publicvoid onError(ErrorEvent event) {
/**
* get the cuase of error
*/
Throwable cause = event.getCause();
/**
* Print the cause of error
*/
System.out.println("Catch a error: " + cause.getMessage());
}
}
/*
*Copyright (c) jmin Organization. All rights reserved.
*/
package org.jmin.j2ee.kernel.net.nio.test;
import java.io.IOException;
import org.jmin.j2ee.kernel.net.nio.NIOConnector;
import org.jmin.j2ee.kernel.net.nio.NIOConnectorCluster;
/**
* Test sample to show that how to build a nio client.
*
* @author Chris Liao
*/
publicclass NIOClient {
/**
* Main method to run a nio client.
*
*/
publicstaticvoid main(String args[]) throws IOException {
/**
* Call static method from ConnectionCluster class to build its a instance,
* this action is similar with nio.
*/
NIOConnectorCluster cluster = NIOConnectorCluster.open();
/**
* Build a connection to remote host with threa parameters
*
* @arg remote host
* @arg remote port
* @arg message listener
*
* Message listener is extended from<class>org.jmin.j2ee.kernel.Listener</class>
* You can define yourself message listeners for it.
*/
NIOConnector con = cluster.connect("localhost", 9988,new NIOConnectionListener());
/**
* Send a message to the remote host.
*/
con.write("I am a Chinese".getBytes());
/**
* synchronized objec to block main thread.
*/
Object obj = new Object();
try {
synchronized (obj) {
obj.wait();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
经过上面四个步骤,我们就已经完成建立NIO通讯的全部过程,至此我们可以编译他们,并分别运行Server 与Client来看运行效果吧. 正确运行后将会在Console频繁看到 "Receive data: I am a Chinese"