|
For the client part “MinaClient” and “MinaClientHandler” class has been used. In the “MinaClient” class the “IoConnector” interface is used to communicate with the server and that fires the event to the handler. Like the server part, The same “LoggingFilter” and “ProtocolCodecFilter” has been used. An interface named “ConnectFuture” is used to windup the asynchronous connection requests.The code for the “MinaClient” class is given below,
package com.sample.timeserver; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.charset.Charset; import org.apache.mina.core.future.ConnectFuture; import org.apache.mina.core.service.IoConnector; import org.apache.mina.core.session.IoSession; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.textline.TextLineCodecFactory; import org.apache.mina.filter.logging.LoggingFilter; import org.apache.mina.transport.socket.nio.NioSocketConnector; /** * @author giftsam */ public class MinaClient { private static final int PORT = 1234; public static void main(String[] args) throws IOException, InterruptedException { IoConnector connector = new NioSocketConnector(); connector.getSessionConfig().setReadBufferSize(2048); connector.getFilterChain().addLast("logger", new LoggingFilter()); connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))); connector.setHandler(new MinaClientHandler("Hello Server..")); ConnectFuture future = connector.connect(new InetSocketAddress("172.108.0.12", PORT)); future.awaitUninterruptibly(); if (!future.isConnected()) { return; } IoSession session = future.getSession(); session.getConfig().setUseReadOperation(true); session.getCloseFuture().awaitUninterruptibly(); System.out.println("After Writing"); connector.dispose(); } }For the handler, Like the server part the same methods “sessionOpened”, “messageReceived” and “exceptionCaught” has been used. The code for the “MinaClientHandler” class is given below,
package com.sample.timeserver; import org.apache.mina.core.service.IoHandlerAdapter; import org.apache.mina.core.session.IoSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author giftsam */ public class MinaClientHandler extends IoHandlerAdapter { private final Logger logger = (Logger) LoggerFactory.getLogger(getClass()); private final String values; private boolean finished; public MinaClientHandler(String values) { this.values = values; } public boolean isFinished() { return finished; } @Override public void sessionOpened(IoSession session) { session.write(values); } @Override public void messageReceived(IoSession session, Object message) { logger.info("Message received in the client.."); logger.info("Message is: " + message.toString()); } @Override public void exceptionCaught(IoSession session, Throwable cause) { session.close(); } }
Now its time to test the preceding codes, First the code “MinaServer” should be executed and then execute the “MinaClient”, the outcome of the codes will looks like the below,
MinaClient – Output
Dec 8, 2010 8:31:49 PM org.apache.mina.filter.logging.LoggingFilter log INFO: CREATED Dec 8, 2010 8:31:49 PM org.apache.mina.filter.logging.LoggingFilter log INFO: OPENED Dec 8, 2010 8:31:49 PM org.apache.mina.filter.logging.LoggingFilter log INFO: SENT: HeapBuffer[pos=0 lim=0 cap=0: empty] Dec 8, 2010 8:32:00 PM org.apache.mina.filter.logging.LoggingFilter log INFO: CLOSED Client completed..
No comments:
Post a Comment