Search in sources :

Example 6 with Message

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

the class ClientContext method receiveMessage.

public Message receiveMessage(int sid, long timeout) {
    ClientContext context = this.getContext(sid);
    Message msg = null;
    if (context != null) {
        synchronized (context) {
            msg = context.getMessage(timeout);
        }
    } else {
        log.warn("Could not receive message for sid " + sid);
    }
    return msg;
}
Also used : Message(org.jboss.narayana.blacktie.jatmibroker.core.transport.Message)

Example 7 with Message

use of org.jboss.narayana.blacktie.jatmibroker.core.transport.Message 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)

Example 8 with Message

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

the class SocketReceiverImpl method run.

public void run() {
    try {
        DataInputStream ins = new DataInputStream(socket.getInputStream());
        int size;
        while ((size = ins.readInt()) != -1) {
            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]);
                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));
                synchronized (this) {
                    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);
                        }
                    }
                    data.add(message);
                    if (responseMonitor != null) {
                        responseMonitor.responseReceived(this.cd, false);
                    }
                    notify();
                }
            }
        }
    } catch (EOFException e) {
        log.info("receiver " + socket + " close");
        closed = true;
    } catch (SocketException e) {
    } catch (IOException e) {
        log.error("receiver " + socket + " run failed with " + e);
    }
}
Also used : SocketException(java.net.SocketException) Message(org.jboss.narayana.blacktie.jatmibroker.core.transport.Message) EOFException(java.io.EOFException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 9 with Message

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

the class ConnectionImplTest method test.

public void test() throws Exception {
    AtmiBrokerEnvXML xml = new AtmiBrokerEnvXML();
    Properties properties = xml.getProperties();
    TransportFactory factory = new TransportFactory(properties);
    Transport proxy = factory.createTransport();
    Sender serviceFactory = proxy.getSender("BAR", false);
    String aString = "Hello from Java Land";
    Receiver endpoint = proxy.createReceiver(1, null, null);
    serviceFactory.send(endpoint.getReplyTo(), (short) 0, 0, aString.getBytes(), aString.getBytes().length, 0, 0, 0, "X_OCTET", "");
    Message receive = endpoint.receive(0);
    assertNotNull(receive);
    String string = new String(receive.data).intern();
    String expectedResponse = "BAR SAYS HELLO";
    log.debug("Bar ServiceManager service_request response is " + string);
    log.debug("Bar ServiceManager service_request size of response is " + receive.len);
    assertEquals(string, expectedResponse);
    proxy.close();
    factory.close();
}
Also used : TransportFactory(org.jboss.narayana.blacktie.jatmibroker.core.transport.TransportFactory) Sender(org.jboss.narayana.blacktie.jatmibroker.core.transport.Sender) Message(org.jboss.narayana.blacktie.jatmibroker.core.transport.Message) Receiver(org.jboss.narayana.blacktie.jatmibroker.core.transport.Receiver) AtmiBrokerEnvXML(org.jboss.narayana.blacktie.jatmibroker.core.conf.AtmiBrokerEnvXML) Properties(java.util.Properties) Transport(org.jboss.narayana.blacktie.jatmibroker.core.transport.Transport)

Example 10 with Message

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

the class ConnectionImpl method receive.

/**
 * Retrieve a response.
 *
 * @param cd
 *            The connection descriptor
 * @param flags
 *            The flags to use
 * @return The response
 * @throws ConnectionException
 *             If the response cannot be retrieved.
 * @throws ConfigurationException
 */
private Response receive(int cd, int flags) throws ConnectionException, ConfigurationException {
    log.debug("receive: " + cd);
    Receiver endpoint = temporaryQueues.get(cd);
    if (endpoint == null) {
        throw new ConnectionException(ConnectionImpl.TPEBADDESC, "Session does not exist: " + cd);
    }
    Message message = endpoint.receive(flags);
    Buffer buffer = null;
    if (message.type != null && !message.type.equals("")) {
        CodecFactory factory = new CodecFactory(this);
        String coding_type = properties.getProperty("blacktie." + message.serviceName + ".coding_type");
        Codec codec = factory.getCodec(coding_type);
        buffer = codec.decode(message.type, message.subtype, message.data, message.len);
    // buffer = tpalloc(message.type, message.subtype, message.len);
    // buffer.deserialize(message.data);
    }
    if (message.rval == ConnectionImpl.TPFAIL) {
        if (message.rcode == ConnectionImpl.TPESVCERR) {
            throw new ResponseException(ConnectionImpl.TPESVCERR, "Got an error back from the remote service", -1, message.rcode, buffer);
        }
        throw new ResponseException(ConnectionImpl.TPESVCFAIL, "Got a fail back from the remote service", -1, message.rcode, buffer);
    } else {
        Response response = new Response(cd, message.rval, message.rcode, buffer, message.flags);
        log.debug("received returned a response? " + (response == null ? "false" : "true"));
        return response;
    }
}
Also used : Buffer(org.jboss.narayana.blacktie.jatmibroker.xatmi.Buffer) Response(org.jboss.narayana.blacktie.jatmibroker.xatmi.Response) Codec(org.jboss.narayana.blacktie.jatmibroker.core.transport.Codec) Message(org.jboss.narayana.blacktie.jatmibroker.core.transport.Message) ResponseException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException) Receiver(org.jboss.narayana.blacktie.jatmibroker.core.transport.Receiver) CodecFactory(org.jboss.narayana.blacktie.jatmibroker.codec.CodecFactory) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Aggregations

Message (org.jboss.narayana.blacktie.jatmibroker.core.transport.Message)11 ConnectionException (org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)5 IOException (java.io.IOException)4 Receiver (org.jboss.narayana.blacktie.jatmibroker.core.transport.Receiver)3 DataInputStream (java.io.DataInputStream)2 EOFException (java.io.EOFException)2 SocketException (java.net.SocketException)2 Sender (org.jboss.narayana.blacktie.jatmibroker.core.transport.Sender)2 ResponseException (org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException)2 DataOutputStream (java.io.DataOutputStream)1 Socket (java.net.Socket)1 Properties (java.util.Properties)1 CodecFactory (org.jboss.narayana.blacktie.jatmibroker.codec.CodecFactory)1 ResponseMonitor (org.jboss.narayana.blacktie.jatmibroker.core.ResponseMonitor)1 AtmiBrokerEnvXML (org.jboss.narayana.blacktie.jatmibroker.core.conf.AtmiBrokerEnvXML)1 Codec (org.jboss.narayana.blacktie.jatmibroker.core.transport.Codec)1 EventListener (org.jboss.narayana.blacktie.jatmibroker.core.transport.EventListener)1 Transport (org.jboss.narayana.blacktie.jatmibroker.core.transport.Transport)1 TransportFactory (org.jboss.narayana.blacktie.jatmibroker.core.transport.TransportFactory)1 TransactionException (org.jboss.narayana.blacktie.jatmibroker.core.tx.TransactionException)1