Search in sources :

Example 1 with ResponseMonitor

use of org.jboss.narayana.blacktie.jatmibroker.core.ResponseMonitor in project narayana by jbosstm.

the class ClientContext method run.

public void run() {
    try {
        DataInputStream ins = new DataInputStream(socket.getInputStream());
        int size;
        while ((size = ins.readInt()) != -1) {
            log.debug("size is " + size);
            Message message = new Message();
            byte[] buf = new byte[size];
            int readn = 0;
            int remain = size;
            while (remain > 0) {
                int n;
                n = ins.read(buf, readn, remain);
                if (n != -1) {
                    remain -= n;
                    readn += n;
                } else {
                    log.error("expect " + size + " but read " + readn);
                    break;
                }
            }
            if (remain == 0) {
                log.debug("receive from " + socket + " and size is " + size + " buffer is " + buf);
                String[] s = new String(buf).split("\n");
                log.debug("sid is " + s[0]);
                sid = Integer.parseInt(s[0]);
                log.debug("cd is " + s[1]);
                message.cd = Integer.parseInt(s[1]);
                log.debug("rcode is " + s[2]);
                message.rcode = Integer.parseInt(s[2]);
                log.debug("len is " + s[3]);
                message.len = Integer.parseInt(s[3]);
                log.debug("flags is " + s[4]);
                message.flags = Integer.parseInt(s[4]);
                log.debug("rval is " + s[5]);
                message.rval = Short.parseShort(s[5]);
                log.debug("replyto is " + s[6]);
                message.replyTo = s[6].equals("(null)") ? null : s[6];
                log.debug("type is " + s[7]);
                message.type = s[7].equals("(null)") ? null : s[7];
                log.debug("subtype is " + s[8]);
                message.subtype = s[8].equals("(null)") ? null : s[8];
                message.data = new byte[message.len];
                System.arraycopy(buf, size - message.len, message.data, 0, message.len);
                log.debug("data is " + new String(message.data));
                ClientContext context = server.getContext(sid);
                if (context != null) {
                    synchronized (context) {
                        EventListener eventListener = context.getEventListener();
                        if (eventListener != null) {
                            log.debug("Event listener will be called back");
                            if (message.rval == EventListener.DISCON_CODE) {
                                eventListener.setLastEvent(Connection.TPEV_DISCONIMM, message.rcode);
                            } else if (message.rcode == Connection.TPESVCERR) {
                                eventListener.setLastEvent(Connection.TPEV_SVCERR, message.rcode);
                            } else if (message.rval == Connection.TPFAIL) {
                                eventListener.setLastEvent(Connection.TPEV_SVCFAIL, message.rcode);
                            }
                        }
                        context.getData().add(message);
                        log.debug("add message to context " + context.getSid());
                        context.setSocket(socket);
                        ResponseMonitor responseMonitor = context.getResponseMonitor();
                        if (responseMonitor != null) {
                            responseMonitor.responseReceived(sid, false);
                        }
                        log.debug("notifying");
                        context.notify();
                        log.debug("notified");
                    }
                }
            }
        }
        socket.shutdownInput();
        isClose = true;
    } catch (EOFException e) {
        log.debug("client " + socket + " close");
        isClose = true;
    } catch (SocketException e) {
        isClose = true;
    } catch (IOException e) {
        log.error("client " + socket + " run failed with " + e);
    }
}
Also used : SocketException(java.net.SocketException) Message(org.jboss.narayana.blacktie.jatmibroker.core.transport.Message) EOFException(java.io.EOFException) EventListener(org.jboss.narayana.blacktie.jatmibroker.core.transport.EventListener) ResponseMonitor(org.jboss.narayana.blacktie.jatmibroker.core.ResponseMonitor) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Aggregations

DataInputStream (java.io.DataInputStream)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 SocketException (java.net.SocketException)1 ResponseMonitor (org.jboss.narayana.blacktie.jatmibroker.core.ResponseMonitor)1 EventListener (org.jboss.narayana.blacktie.jatmibroker.core.transport.EventListener)1 Message (org.jboss.narayana.blacktie.jatmibroker.core.transport.Message)1