Thank you for your response.
Yes, you are right (in the echo client, the "received message" is logged before
the "sent message").
I have pasted the code which iam using.
Please suggest.
Client:
=======
public class clientSocket {
private static final String HOSTNAME = "localhost";
private static final int PORT = 4444;
private static final long CONNECT_TIMEOUT = 30*1000L; // 30 seconds
// Set this to false to use object serialization instead of custom codec.
private static final boolean USE_CUSTOM_CODEC = true;
public static void main(String[] args) throws Throwable {
NioSocketConnector connector = new NioSocketConnector();
// Configure the service.
connector.setConnectTimeoutMillis(CONNECT_TIMEOUT);
connector.getSessionConfig().setTcpNoDelay(true);
if (USE_CUSTOM_CODEC) {
TextLineCodecFactory TLCF = new
TextLineCodecFactory(Charset.forName("US-ASCII"), "\r\n-", "\r\n-");
TLCF.setDecoderMaxLineLength(Integer.MAX_VALUE);
TLCF.setEncoderMaxLineLength(Integer.MAX_VALUE);
connector.getFilterChain().addLast("codec", new
ProtocolCodecFilter(TLCF));
}
else {
connector.getFilterChain().addLast("codec", new
ProtocolCodecFilter(
new ObjectSerializationCodecFactory()));
}
connector.getFilterChain().addLast("logger", new LoggingFilter());
connector.setHandler(new ClientSessionHandler());
IoSession session;
for (;;) {
try {
ConnectFuture future = connector.connect(new
InetSocketAddress(HOSTNAME, PORT));
future.awaitUninterruptibly();
session = future.getSession();
break;
} catch (RuntimeIoException e) {
System.err.println("Failed to connect.");
e.printStackTrace();
Thread.sleep(5000);
}
}
for (int i=0; i<1000; i++) {
AddMessage m = new AddMessage();
session.write(m.toString());
}
Thread.sleep(100000000);
// wait until the summation is done
session.getCloseFuture().awaitUninterruptibly();
connector.dispose();
}
}
// does nothing as of now.
// will add more logics to this later
public class ClientSessionHandler extends IoHandlerAdapter {
private final static Logger LOGGER =
LoggerFactory.getLogger(ClientSessionHandler.class.toString());
private boolean finished;
int values[];
public ClientSessionHandler() {
}
public boolean isFinished() {
return finished;
}
@Override
public void sessionOpened(IoSession session) {
}
@Override
public void messageReceived(IoSession session, Object message) {
}
@Override
public void exceptionCaught(IoSession session, Throwable cause) {
session.close(true);
}
}
// just a simple messaging class
public class AddMessage extends AbstractMessage {
private static final long serialVersionUID = -940833727168119141L;
private static int value = 0;
private String str;
private final static Logger LOGGER =
LoggerFactory.getLogger(AddMessage.class.toString());
public AddMessage() {
setSequence(value);
str = getSequence() + ":Test Message Header" + "\r\n";
str += "This is the body of message:";
value++;
}
public String toString() {
return str;
}
}
public abstract class AbstractMessage implements Serializable {
private static final long serialVersionUID = -7659971084983487382L;
private int sequence;
public int getSequence() {
return sequence;
}
public void setSequence(int sequence) {
this.sequence = sequence;
}
}
Echo server:
===========
// this is not using MINA. just a simple echo server in Java
package Server;
import java.net.*;
import java.io.*;
public class Sconn implements Runnable{
private Socket connection;
private String TimeStamp;
private int ID;
public static void main(String[] args) {
int port = 4444;
int count = 0;
try{
ServerSocket socket1 = new ServerSocket(port);
System.out.println("MultipleSocketServer Initialized");
while (true) {
Socket connection = socket1.accept();
Runnable runnable = new Sconn(connection, ++count);
Thread thread = new Thread(runnable);
thread.start();
}
}
catch (Exception e) {
System.out.println("Exception caught in main:" + e.toString());
}
}
public Sconn(Socket s, int i) {
this.connection = s;
this.ID = i;
}
public int getID() {
return ID;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
BufferedInputStream is = new
BufferedInputStream(connection.getInputStream());
InputStreamReader isr = new InputStreamReader(is);
int character;
BufferedOutputStream os = new
BufferedOutputStream(connection.getOutputStream());
OutputStreamWriter osw = new OutputStreamWriter(os,
"US-ASCII");
boolean canClose = false;
while (!canClose) {
StringBuffer process = new StringBuffer();
while((character = isr.read()) != -1) {
process.append((char)character);
if
(process.toString().endsWith("\r\n-")) {
break;
}
}
if (process.toString().equals("Bye\r\n-")) {
canClose = true;
}
System.out.println(process);
//need to wait 10 seconds to pretend that we're
processing something
try {
//Thread.sleep(100);
}
catch (Exception e){
System.out.println("exception caught
during sleep:" + e.toString());
}
TimeStamp = new java.util.Date().toString();
String returnCode =
"SocketServer repsonded at "+ TimeStamp
+ " " + process;
osw.write(returnCode);
osw.flush();
}
}
catch (Exception e) {
System.out.println(e);
}
finally {
try {
connection.close();
}
catch (IOException e){
System.out.println("Exception during close:" +
e.toString());
}
}
}
}
Sathyanarayan Vaidyanathan
RBS Global Banking & Markets
-----Original Message-----
From: Emmanuel Lecharny [mailto:[email protected]]
Sent: 24 April 2010 13:37
To: [email protected]
Subject: Re: MINA logs shows the received message before the sent message
On 4/24/10 6:46 AM, [email protected] wrote:
> Guys,
> Firstly, my thanks to all fo you guys to comeup with a good project like MINA.
>
> Iam a new to this (and new to java as well), but coming from C++ background,
> i could see that most of the networking aspects are tackled in MINA.
>
> I have a query to you guys:
> I wrote a simple MINA client that connects an echo server.
> One weird thing i notice is, (for some messages) the MINA logs shows the
> received message log prior to the sent message log.
> Could you please suggest on how to avoid this ? Meaning, i would like to see
> the messages logged in actual sequence.
>
I guess that what you want to show is that the body of the received message is
logged *after* the 'message sent' message.
can ypu provide the code of your application, so that we can check what's ging
on ?
Thanks !
--
Regards,
Cordialement,
Emmanuel Lécharny
www.nextury.com
***********************************************************************************
The Royal Bank of Scotland plc. Registered in Scotland No 90312.
Registered Office: 36 St Andrew Square, Edinburgh EH2 2YB.
Authorised and regulated by the Financial Services Authority. The
Royal Bank of Scotland N.V. is authorised and regulated by the
De Nederlandsche Bank and has its seat at Amsterdam, the
Netherlands, and is registered in the Commercial Register under
number 33002587. Registered Office: Gustav Mahlerlaan 10,
Amsterdam, The Netherlands. The Royal Bank of Scotland N.V. and
The Royal Bank of Scotland plc are authorised to act as agent for each
other in certain jurisdictions.
This e-mail message is confidential and for use by the addressee only.
If the message is received by anyone other than the addressee, please
return the message to the sender by replying to it and then delete the
message from your computer. Internet e-mails are not necessarily
secure. The Royal Bank of Scotland plc and The Royal Bank of Scotland
N.V. including its affiliates ("RBS group") does not accept responsibility
for changes made to this message after it was sent.
Whilst all reasonable care has been taken to avoid the transmission of
viruses, it is the responsibility of the recipient to ensure that the onward
transmission, opening or use of this message and any attachments will
not adversely affect its systems or data. No responsibility is accepted
by the RBS group in this regard and the recipient should carry out such
virus and other checks as it considers appropriate.
Visit our website at www.rbs.com
***********************************************************************************