Thanks to some help of some colleague (Marcelo Marmol)Marcelo's Blog, I took my first experience with Apache Mina.
In shorts words this library helps with the transmission of packets, to develop server or client tools.
My first step it's to create a connection between a Tomcat Server and a Java Service (all the info I have). But the code is like this.
*****************CODE**************
import java.net.InetSocketAddress;
import org.apache.mina.core.RuntimeIoException;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.future.ReadFuture;
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.transport.socket.nio.NioSocketConnector;
import com.ats.example.ClientHandler;
public class ClientTcpIp {
public ClientTcpIp(){
}
//TODO ver diferente mensajes
public String SendMessage(String sMensaje){
String sRespuestaTcp;
IoSession session;
NioSocketConnector connector;
ClientHandler chEventoIO;
//WE CREATE THE CONNECTION (CLIENT SIDE, WITH 3 SECONDS OF TIMEOUT)
connector = new NioSocketConnector();
connector.setConnectTimeoutMillis(30 * 1000L);
//THE HANDLER TO DO SOMETHING WITH THE EVENTS
chEventoIO = new ClientHandler();
chEventoIO.setMensaje("Hi my server, how are you?");
connector.setHandler(chEventoIO);
//THE CODING TO CONNECT WITH THE SERVER, IN THIS CASE STRING
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory()));
sRespuestaTcp=null;
//TRY TO CONNECT
for (;;) {
try {
//SETTING THE IP AND PORT (EXAMPLE TO LOCALHOST AND PORT 555)
ConnectFuture future = connector.connect(new InetSocketAddress("127.0.0.1",5555));
//BECAUSE THIS IS SYNC, NO ASYNC WE MUST USE THIS
session.getConfig().setUseReadOperation(true);
ReadFuture read =session.read();
//AND WE WAIT THE MESSAGE FROM SERVER OR FIVE SECONDS
if (read.await(5000)){
//WE GET THE MESSAGE FROM THE SERVER
sRespuestaTcp=(String)read.getMessage();
// WE LEAVE THE FOR
break;
}
//IF FIVE SECONDS PASS WE CONNECT AGAIN
} catch (RuntimeIoException e) {
//System.err.println("Failed to connect.");
e.printStackTrace();
try {
// WE WAIT 5 TO TRY AGAIN
Thread.sleep(5000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//IF EVERYTHING IS OK, WE RETURN THE STRING
return sRespuestaTcp;
}
}
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.logging.LoggingFilter;
public class ClientHandler extends IoHandlerAdapter {
private String sMensaje;
public ClientHandler(){
setMensaje("");
}
@Override
public void sessionOpened(IoSession session) throws Exception {
if (getMensaje()==""){
session.close(true).awaitUninterruptibly();
throw new Exception("YOU MUST DEFINE A MESSAGE");
}
session.write(getMensaje());
}
@Override
public void sessionClosed(IoSession session) {
}
@Override
public void messageReceived(IoSession session, Object message) {
}
@Override
public void messageSent(IoSession session, Object message) {
}
@Override
public void exceptionCaught(IoSession session, Throwable cause) {
session.close(true);
}
public void setMensaje(String sMensaje) {
this.sMensaje = sMensaje;
}
public String getMensaje() {
return sMensaje;
}
}
************END CODE *****************
Basically, we connect to the server in very simple and few lines with the all mighty and open source mina. This is a not working example, that why is very commented it's only to show how it works, if you want one, leave a comment.
BG
Tuesday, November 24, 2009
Dancing Packets with Apache Mina
Labels:
apache mina,
mina,
packets java,
packets mina,
tcp java,
tcpip java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment