Hi,
please check that "response" ByteBuffer is ready for the data to be
read there. Cause it's possible you call read, when response
ByteBuffer position=limit.
WBR,
Alexey.
On Nov 24, 2008, at 17:05 , Survivant 00 wrote:
> I suppose that if I want to obtain an async tcp client, I,ll need to
> create a Task that will poll the server for incoming messages, and
> when i receive one, I notifiy the client.. right ?
>
>
> I look into JavaMailAsyncFilter and you look doing that
>
> /**
> * The <code>ProcessorTask</code>, which is used to execture
> the HTTP
> * request.
> */
> private ProcessorTask processorTask;
>
>
> /**
> * The async wrapper around the <code>ProcessorTask</code>
> */
> private AsyncTask asyncProcessorTask;
>
>
> there is a actual implementation for AsyncTCP client that doesn't
> required external librairies ? Only plain Grizzly ?
>
>
>
>
> 2008/11/24 Survivant 00 <survivant00_at_gmail.com>
> I have a question. I,m using the demo client TCP found on the main
> page of grizzly.. (one of them :))
>
> I just moved some code into methods.
>
> My problem is I was excepted that the read block until a value >-1
> was returned. ( Even check into NonBlockingTCPIOClient and the
> function receive contains :
> public int receive(byte[] buf, int length) throws IOException {
> ByteBuffer byteBuffer = ByteBuffer.wrap(buf,0,length);
> long bytesRead = tcpConnectorHandler.read(byteBuffer,true);
>
> if (bytesRead == -1) {
> throw new EOFException("Unexpected client EOF!");
> }
>
> return (int) bytesRead;
> }
>
>
> ----------------------
>
>
> What I'm missing ?
>
>
>
> public class TCPClientTest {
>
> private String host = "localhost";
> private int port = 7803;
>
> private TCPConnectorHandler connector_handler;
> private Controller controller;
> private TCPSelectorHandler tcp_selector_handler;
>
> private ByteBuffer buf = ByteBufferFactory.allocateView(1000,
> false);
> private ByteBuffer response =
> ByteBufferFactory.allocateView(1000, false);
>
> public void init() {
> final CountDownLatch started = new CountDownLatch(1);
>
> controller = new Controller();
> tcp_selector_handler = new TCPSelectorHandler(true);
> controller.addSelectorHandler(tcp_selector_handler);
>
> controller.addStateListener(new
> ControllerStateListenerAdapter() {
>
> public void onException(Throwable e) {
> System.out.println("Grizzly controller exception:" +
> e.getMessage());
> }
>
> public void onReady() {
> System.out.println("Ready!");
> started.countDown();
> }
>
> });
>
> new Thread(controller).start();
> try {
> started.await();
> } catch (Exception e) {
> System.out.println("Timeout in wait" + e.getMessage());
> }
>
> connector_handler = (TCPConnectorHandler)
> controller.acquireConnectorHandler(Controller.Protocol.TCP);
> }
>
> public void connect(){
> try {
> connector_handler.connect(new InetSocketAddress(host,
> port), new CallbackHandler<Context>() {
> public void onConnect(IOEvent<Context> e) {
> SelectionKey k = e.attachment().getSelectionKey();
> System.out.println("Callbackhandler:
> OnConnect...");
> try {
> connector_handler.finishConnect(k);
> } catch (Exception ex) {
> System.out.println("exception in
> CallbackHandler:" + ex.getMessage());
> }
> e.attachment().getSelectorHandler().register(k,
> SelectionKey.OP_READ);
> }
>
> public void onRead(IOEvent<Context> e) {
> }
>
> public void onWrite(IOEvent<Context> e) {
> }
>
> });
>
> } catch (Exception e) {
> System.out.println("Exception in execute..." + e);
> e.printStackTrace(System.out);
> }
> }
>
> public void send(String quote) throws Exception {
> byte[] msg = quote.getBytes();
>
> buf = ByteBufferFactory.allocateView(msg.length, false);
>
> buf.put(msg);
> buf.flip();
> connector_handler.write(buf, true);
> buf.clear();
> long count = connector_handler.read(response, true);
>
> if(count>-1){
> byte[] b = new byte[(int)count];
> response.get(b);
>
> System.out.println(new String(b));
> buf.rewind();
> } else {
> System.out.println("count==-1");
> }
>
> }
>
> public void close(){
> try {
> if(connector_handler!=null){
> connector_handler.close();
> }
> if(controller!=null){
> controller.stop();
> }
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
>
> public static void main(String[] args) {
> TCPClientTest client = new TCPClientTest();
>
> try {
> client.init();
> client.connect();
>
> client.send("quote|aaa[eoq]");
> client.send("quote|bbb[eoq]");
> client.send("quote|ccc[eoq]");
>
> client.close();
> } catch(Exception e){
> e.printStackTrace();
> } finally {
> try {client.close();}catch(Exception e){}
> }
>
> }
>
> the output is :
>
> Ready!
> Callbackhandler: OnConnect...
> BID AAA = 1.123
> count==-1
> BID CCC = 2.00
>
>