tags:

views:

219

answers:

1

Relevant Code -- Note Instructions is merely a class with several methods which will operate on the data. A new thread is created operate on the data read.

READ THREAD:

while(true) {
     System.out.println(".");
     if(selector.select(500) == 0)
          continue;

     System.out.println("processing read");

     for(SelectionKey sk : selector.keys()) {
          Instructions ins = myHashTable.get(sk);
          if(ins == null) { 
               myHashTable.put(sk, new Instructions(sk));
               ins = myHashTable.get(sk);
          }
          ins.readChannel();
     }
}

READCHANNEL

public void readChannel() {
     BufferedReader reader = new BufferedReader(Channels.newReader((ReadableByteChannel) this.myKey.channel(), "UTF-8"));
     Worker w = new Worker(this, reader.readLine());
     (new Thread(w)).start();
}

The new thread then calls more Instructions methods.

When the ins function finishes it might write to a Writer:

Writer out = Channels.newWriter((WritableByteChannel) key.channel(), "UTF-8");
out.write(output);
out.flush();

I can confirm that my client (a flash movie), then receives and acts on the output.

Finally, w exits.

After the receipt of the first message from the client, and successful processing, however, no more messages are taken care of by the READ THREAD loop. I believe the key is registered with the selector and ready to read. I have checked by looping on all the keys to see if they are readable with isReadable & isRegistered on the channel and the result is true in all cases to date. When a second message is sent from the client, however, the only response I see in the read thread is that the '.' character is printed out not every half second, but continuously faster. I believe, then, that the data is there in the channel, but for some reason the Selector isn't selecting any key.

Can anyone help me?

A: 

Shouldn't

for(SelectionKey sk : selector.keys())

be

for(SelectionKey sk : selector.selectedKeys())

Since you would like to process only those events that have occurred in the current select operation ?

Since you say that the select(500) returns before 5 seconds, my guess is that you have registered a channel with the selector for the WRITE operation. A channel is ready for write most of the times. Hence it is necessary to set the interest ops to WRITE only when data is available for writing.

Vijay