users@grizzly.java.net

Newbie not getting it...

From: sasonline <sstacha_at_gmail.com>
Date: Mon, 10 Aug 2009 11:31:33 -0700 (PDT)

I am trying to figure out grizzly. I thought I would start by a simple
example where I have a server that gets a String from any client and sends
it back out to all connected clients since there was already a couple code
example snippits to draw from; then create a client that sends a string to
the server and listens for strings sent from the server. To keep it simple
I just use command line. Here is the code (using grizzly 2.0 library)

Server:

import com.sun.grizzly.TransportFactory;
import com.sun.grizzly.filterchain.TransportFilter;
import com.sun.grizzly.nio.transport.TCPNIOTransport;
import java.io.IOException;

/**
 *
 * @author sstacha
 */
public class Main
{

    public static final String DEFAULT_HOST = "localhost";
    public static final int DEFAULT_PORT = 4447;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException
    {
                String host;
                try
                {
                        host = args[0];
                        if (host == null || host.length() == 0)
                                host = DEFAULT_HOST;
                }
                catch (Exception ex) {host = DEFAULT_HOST;}
                int port;
                try {port = Integer.parseInt(args[1]);}
                catch (Exception ex) {port = DEFAULT_PORT;}

        TCPNIOTransport transport =
TransportFactory.getInstance().createTCPTransport();
        transport.getFilterChain().add(new TransportFilter());
        transport.getFilterChain().add(new MyEchoFilter());
        try
        {
            transport.bind(host, port);
            transport.start();
            System.out.println("[" + host + "] listening on port [" + port +
"]");
            System.out.println("Press <Return> to stop the server...");
            System.in.read();
        }
        finally
        {
            System.out.println("Stopping chat server...");
            transport.stop();
            TransportFactory.getInstance().close();
            System.out.println("Server stopped...");
        }
    }

}

My code for MyEchoFilter (don't think this is right):

import com.sun.grizzly.Buffer;
import com.sun.grizzly.Connection;
import com.sun.grizzly.TransformationResult;
import com.sun.grizzly.TransformationResult.Status;
import com.sun.grizzly.Transformer;
import com.sun.grizzly.filterchain.FilterAdapter;
import com.sun.grizzly.filterchain.FilterChain;
import com.sun.grizzly.filterchain.FilterChainContext;
import com.sun.grizzly.filterchain.NextAction;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 *
 * @author sstacha
 */
public class MyEchoFilter extends FilterAdapter{

    @Override
    public void exceptionOccurred(FilterChainContext ctx, Throwable error) {
        System.out.println("EXCEPTION: " + error);
        super.exceptionOccurred(ctx, error);
    }

    @Override
    public NextAction postAccept(FilterChainContext ctx, NextAction
nextAction) throws IOException {
        System.out.println("ACCEPTED");
        return super.postAccept(ctx, nextAction);
    }

    @Override
    public NextAction postClose(FilterChainContext ctx, NextAction
nextAction) throws IOException {
        System.out.println("CLOSED");
        return super.postClose(ctx, nextAction);
    }

    @Override
    public NextAction postConnect(FilterChainContext ctx, NextAction
nextAction) throws IOException {
        System.out.println("CONNECTED");
        return super.postConnect(ctx, nextAction);
    }

    @Override
    public NextAction postRead(FilterChainContext ctx, NextAction
nextAction) throws IOException {
        System.out.println("READ");
        // Echo the read message
        // incoming message
        final Object message = ctx.getMessage();
        // peer address
        final Object address = ctx.getAddress();
        // connection, where response will be sent
        final Connection connection = ctx.getConnection();
        // FilterChain
        final FilterChain filterChain = ctx.getFilterChain();

        // FilterChain encoder, which will pass throw all FilterChain
Filters,
        // so each of them will be able to transform the message, before it
        // will be sent on a wire
        final Transformer encoder = filterChain.getCodec().getEncoder();

        // transform the message. Each CodecFilter in FilterChain is able
        // to take part in message transformation
        TransformationResult<Buffer> result = encoder.transform(connection,
message, null);
        if (result.getStatus() == Status.COMPLETED) {
            Buffer echoMessage = result.getMessage();
            System.out.println("Echo '" +
echoMessage.contentAsString(Charset.defaultCharset()) + "' address: " +
address);
            connection.write(address, echoMessage);
        } else {
            throw new IllegalStateException("Can not transform the
message");
        }

        encoder.release(connection);

        return super.postRead(ctx, nextAction);
    }

    @Override
    public NextAction postWrite(FilterChainContext ctx, NextAction
nextAction) throws IOException {
        System.out.println("WRITE");
        return super.postWrite(ctx, nextAction);
    }

}

Code for client:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutionException;

/**
 *
 * @author stevestacha
 */
public class Main
{
        public static final String HOST = "localhost";
        public static final int PORT = 4447;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException,
InterruptedException, ExecutionException
    {
                GrizzlyChatClientHandler chatClient = new GrizzlyChatClientHandler();
                chatClient.connect(HOST, PORT);
                BufferedReader inBuffer = new BufferedReader(new
InputStreamReader(System.in));
                System.out.println("Type 'quit' to quit the client");
                System.out.println("Type anything else to send the message to the
server");
                String clientInput = inBuffer.readLine();

                while (!clientInput.equalsIgnoreCase("quit"))
                {
                        chatClient.sendMessage(clientInput);
                        clientInput = inBuffer.readLine();
                }
    }

}

Code for LogFilter:

import com.sun.grizzly.filterchain.FilterAdapter;
import com.sun.grizzly.filterchain.FilterChainContext;
import com.sun.grizzly.filterchain.NextAction;
import java.io.IOException;

/**
 *
 * @author sstacha
 */
public class LogFilter extends FilterAdapter
{

    @Override
    public void exceptionOccurred(FilterChainContext ctx, Throwable error) {
        System.out.println("EXCEPTION: " + error);
        super.exceptionOccurred(ctx, error);
    }

    @Override
    public NextAction postAccept(FilterChainContext ctx, NextAction
nextAction) throws IOException {
        System.out.println("ACCEPTED");
        return super.postAccept(ctx, nextAction);
    }

    @Override
    public NextAction postClose(FilterChainContext ctx, NextAction
nextAction) throws IOException {
        System.out.println("CLOSED");
        return super.postClose(ctx, nextAction);
    }

    @Override
    public NextAction postConnect(FilterChainContext ctx, NextAction
nextAction) throws IOException {
        System.out.println("CONNECTED");
        return super.postConnect(ctx, nextAction);
    }

    @Override
    public NextAction postRead(FilterChainContext ctx, NextAction
nextAction) throws IOException {
        System.out.println("READ");
        return super.postRead(ctx, nextAction);
    }

    @Override
    public NextAction postWrite(FilterChainContext ctx, NextAction
nextAction) throws IOException {
        System.out.println("WRITE");
        return super.postWrite(ctx, nextAction);
    }

}

Output / Results:
I see the message
Accepted connection from java.nio.channels.SocketChannel[connected
local=/127.0.0.1:4447 remote=/127.0.0.1:51111]
on the server and CONNECTED on the client when the client starts up.
However, when I type a message in the client all I get is READ over and over
again in a loop. I see nothing on the server indicating it got the message
not even the READ that should be displayed first. I feel like I am missing
something important. Can anyone give guidance?

Thanks,

Steve

-- 
View this message in context: http://www.nabble.com/Newbie-not-getting-it...-tp24904966p24904966.html
Sent from the Grizzly - Users mailing list archive at Nabble.com.