Search in sources :

Example 1 with ConnectionException

use of org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException in project narayana by jbosstm.

the class TPConversationService method tpservice.

public Response tpservice(TPSVCINFO svcinfo) throws ConnectionException, ConfigurationException {
    log.info("testTPConversation_service");
    boolean fail = false;
    if (TestTPConversation.strcmp((X_OCTET) svcinfo.getBuffer(), "conversate") != 0) {
        if (svcinfo.getBuffer() != null) {
            log.error("Got invalid data %s" + new String(((X_OCTET) svcinfo.getBuffer()).getByteArray()));
        } else {
            log.error("GOT A NULL");
        }
        fail = true;
    } else {
        long revent = 0;
        log.info("Chatting");
        for (int i = 0; i < TestTPConversation.interationCount; i++) {
            byte[] bytes = ("hi" + i).getBytes();
            X_OCTET sendbuf = (X_OCTET) svcinfo.getConnection().tpalloc("X_OCTET", null);
            sendbuf.setByteArray(bytes);
            // btlogger((char*) "testTPConversation_service:%s:",
            // sendbuf);
            int result = svcinfo.getSession().tpsend(sendbuf, Connection.TPRECVONLY);
            if (result != -1) {
                try {
                    svcinfo.getSession().tprecv(0);
                    fail = true;
                    break;
                } catch (ResponseException e) {
                    if (e.getEvent() == Connection.TPEV_SENDONLY) {
                        Buffer rcvbuf = e.getReceived();
                        if (TestTPConversation.strcmp("yo" + i, rcvbuf) != 0) {
                            fail = true;
                            break;
                        }
                    } else {
                        fail = true;
                        break;
                    }
                } catch (ConnectionException e) {
                    fail = true;
                    break;
                }
            } else {
                fail = true;
                break;
            }
        }
        log.info("Chatted");
    }
    if (fail) {
        return new Response((short) Connection.TPESVCFAIL, 0, null, 0);
    } else {
        byte[] bytes = ("hi" + TestTPConversation.interationCount).getBytes();
        X_OCTET sendbuf = (X_OCTET) svcinfo.getConnection().tpalloc("X_OCTET", null);
        sendbuf.setByteArray(bytes);
        return new Response(Connection.TPSUCCESS, 0, sendbuf, 0);
    }
}
Also used : Buffer(org.jboss.narayana.blacktie.jatmibroker.xatmi.Buffer) Response(org.jboss.narayana.blacktie.jatmibroker.xatmi.Response) X_OCTET(org.jboss.narayana.blacktie.jatmibroker.xatmi.X_OCTET) ResponseException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 2 with ConnectionException

use of org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException in project narayana by jbosstm.

the class SessionImpl method tpsend.

/**
 * Send a buffer to a remote server in a conversation
 *
 * @param toSend
 *            The outbound data
 * @param flags
 *            The flags to use
 * @throws ConnectionException
 *             If the message cannot be sent.
 */
public int tpsend(Buffer toSend, int flags) throws ConnectionException {
    log.debug("tpsend invoked: " + cd);
    if (closed) {
        throw new ConnectionException(ConnectionImpl.TPEPROTO, "Session already closed");
    }
    int toReturn = -1;
    int toCheck = flags & ~(ConnectionImpl.TPRECVONLY | ConnectionImpl.TPNOBLOCK | ConnectionImpl.TPNOTIME | ConnectionImpl.TPSIGRSTRT);
    if (toCheck != 0) {
        log.trace("invalid flags remain: " + toCheck);
        throw new ConnectionException(ConnectionImpl.TPEINVAL, "Invalid flags remain: " + toCheck);
    }
    if (this.lastEvent > -1) {
        throw new ResponseException(ConnectionImpl.TPEEVENT, "Event existed on descriptor: " + lastEvent, lastEvent, lastRCode, null);
    } else if (!canSend) {
        throw new ConnectionException(ConnectionImpl.TPEPROTO, "Session can't send");
    }
    // Can only send in certain circumstances
    if (sender != null) {
        log.debug("Sender not null, sending");
        String type = null;
        String subtype = null;
        byte[] data = null;
        int len = 0;
        if (toSend != null) {
            data = ((BufferImpl) toSend).serialize();
            type = toSend.getType();
            subtype = toSend.getSubtype();
            len = toSend.getLen();
        }
        sender.send(receiver.getReplyTo(), (short) 0, 0, data, len, cd, flags, 0, type, subtype);
        // Sort out session state
        if ((flags & ConnectionImpl.TPRECVONLY) == ConnectionImpl.TPRECVONLY) {
            canSend = false;
            canRecv = true;
        }
        toReturn = 0;
    } else {
        throw new ConnectionException(ConnectionImpl.TPEPROTO, "Session in receive mode");
    }
    return toReturn;
}
Also used : ResponseException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 3 with ConnectionException

use of org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException in project narayana by jbosstm.

the class SessionImpl method tprecv.

/**
 * Received the next response in a conversation
 *
 * @param flags
 *            The flags to use
 * @return The next response
 * @throws ConnectionException
 *             If the message cannot be received or the flags are incorrect
 * @throws ConfigurationException
 */
public Buffer tprecv(int flags) throws ConnectionException, ConfigurationException {
    log.debug("Receiving: " + cd);
    if (closed) {
        throw new ConnectionException(ConnectionImpl.TPEPROTO, "Session already closed");
    }
    int toCheck = flags & ~(ConnectionImpl.TPNOCHANGE | ConnectionImpl.TPNOBLOCK | ConnectionImpl.TPNOTIME | ConnectionImpl.TPSIGRSTRT);
    if (toCheck != 0) {
        log.trace("invalid flags remain: " + toCheck);
        throw new ConnectionException(ConnectionImpl.TPEINVAL, "Invalid flags remain: " + toCheck);
    }
    if (!canRecv) {
        throw new ConnectionException(ConnectionImpl.TPEPROTO, "Session can't receive");
    }
    Message m = receiver.receive(flags);
    // Prepare the outbound channel
    if (m.replyTo == null || (sender != null && !m.replyTo.equals(sender.getSendTo()))) {
        log.trace("Send to location has altered");
        sender.close();
        sender = null;
    }
    if (sender == null && m.replyTo != null && !m.replyTo.equals("")) {
        log.trace("Will require a new sender");
        if (((String) m.replyTo).contains("IOR:")) {
            sender = transport.createSender(m.replyTo);
        } else {
            sender = transport.createSender(receiver);
        }
    } else {
        log.debug("Not setting the sender");
    }
    BufferImpl received = null;
    if (m.type != null && !m.type.equals("")) {
        received = (BufferImpl) connection.tpalloc(m.type, m.subtype);
        received.deserialize(m.data);
    }
    log.debug("Prepared and ready to launch");
    // Sort out session state
    if ((m.flags & ConnectionImpl.TPRECVONLY) == ConnectionImpl.TPRECVONLY) {
        canSend = true;
        canRecv = false;
    }
    // Check the condition of the response
    if ((m.flags & ConnectionImpl.TPRECVONLY) == ConnectionImpl.TPRECVONLY) {
        throw new ResponseException(ConnectionImpl.TPEEVENT, "Reporting send only event", ConnectionImpl.TPEV_SENDONLY, m.rcode, received);
    } else if (m.rval == EventListener.DISCON_CODE) {
        close();
        throw new ResponseException(ConnectionImpl.TPEEVENT, "Received a disconnect event", ConnectionImpl.TPEV_DISCONIMM, m.rcode, received);
    } else if (m.rval == ConnectionImpl.TPSUCCESS || m.rval == ConnectionImpl.TPFAIL) {
        log.debug("Completed session is being closed: " + cd);
        close();
        if (m.rval == ConnectionImpl.TPSUCCESS) {
            throw new ResponseException(ConnectionImpl.TPEEVENT, "Service completed successfully event", ConnectionImpl.TPEV_SVCSUCC, 0, received);
        } else if (m.rcode == ConnectionImpl.TPESVCERR) {
            throw new ResponseException(ConnectionImpl.TPEEVENT, "Service received an error", ConnectionImpl.TPEV_SVCERR, m.rcode, received);
        } else {
            throw new ResponseException(ConnectionImpl.TPEEVENT, "Service received a fail", ConnectionImpl.TPEV_SVCFAIL, m.rcode, received);
        }
    }
    return received;
}
Also used : Message(org.jboss.narayana.blacktie.jatmibroker.core.transport.Message) ResponseException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 4 with ConnectionException

use of org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException in project narayana by jbosstm.

the class BlackTieServer method tpadvertise.

/**
 * Advertise a blacktie service with the specified name
 *
 * @param serviceName The name of the service
 * @throws ConnectionException If the service cannot be advertised
 */
public void tpadvertise(String serviceName, String serviceClassName) throws ConnectionException {
    int min = Math.min(Connection.XATMI_SERVICE_NAME_LENGTH, serviceName.length());
    serviceName = serviceName.substring(0, min);
    log.debug("Advertising: " + serviceName);
    ServiceData serviceData = this.serviceData.get(serviceName);
    if (serviceData == null) {
        try {
            ServiceData data = new ServiceData(transportFactory, properties, serviceName, serviceClassName);
            this.serviceData.put(serviceName, data);
            log.info("Advertised: " + serviceName);
        } catch (ConnectionException e) {
            throw e;
        } catch (Throwable t) {
            throw new ConnectionException(Connection.TPESYSTEM, "Could not create service factory for: " + serviceName, t);
        }
    } else if (!serviceData.getServiceClassName().equals(serviceClassName)) {
        throw new ConnectionException(Connection.TPEMATCH, "Service already registered");
    } else {
        log.trace("This is a duplicate advertise");
    }
}
Also used : ServiceData(org.jboss.narayana.blacktie.jatmibroker.core.server.ServiceData) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 5 with ConnectionException

use of org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException in project narayana by jbosstm.

the class TTLService method tpservice.

public Response tpservice(TPSVCINFO svcinfo) throws ConnectionException, ConfigurationException {
    log.info("TTLService");
    X_OCTET dptr = (X_OCTET) svcinfo.getBuffer();
    String data = new String(dptr.getByteArray());
    log.info("test_ttl_service get data: " + data);
    int len = 60;
    X_OCTET toReturn = (X_OCTET) svcinfo.getConnection().tpalloc("X_OCTET", null);
    log.info("Data was: " + data);
    if (data.contains("counter")) {
        String counter = String.valueOf(n);
        toReturn.setByteArray(counter.getBytes());
        len = counter.length();
    } else {
        try {
            int timeout = 60;
            log.info("TTLService sleep for " + timeout + " seconds");
            Thread.sleep(timeout * 1000);
            log.info("TTLService slept for " + timeout + " seconds");
            toReturn.setByteArray("test_ttl_service".getBytes());
        } catch (Exception e) {
            log.error("sleep failed with " + e);
        }
        n++;
    }
    return new Response(Connection.TPSUCCESS, 22, toReturn, 0);
}
Also used : Response(org.jboss.narayana.blacktie.jatmibroker.xatmi.Response) X_OCTET(org.jboss.narayana.blacktie.jatmibroker.xatmi.X_OCTET) ConfigurationException(org.jboss.narayana.blacktie.jatmibroker.core.conf.ConfigurationException) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Aggregations

ConnectionException (org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)41 Response (org.jboss.narayana.blacktie.jatmibroker.xatmi.Response)12 ConfigurationException (org.jboss.narayana.blacktie.jatmibroker.core.conf.ConfigurationException)11 IOException (java.io.IOException)9 X_OCTET (org.jboss.narayana.blacktie.jatmibroker.xatmi.X_OCTET)9 ResponseException (org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException)6 Message (org.jboss.narayana.blacktie.jatmibroker.core.transport.Message)5 Receiver (org.jboss.narayana.blacktie.jatmibroker.core.transport.Receiver)5 Buffer (org.jboss.narayana.blacktie.jatmibroker.xatmi.Buffer)5 CodecFactory (org.jboss.narayana.blacktie.jatmibroker.codec.CodecFactory)3 Codec (org.jboss.narayana.blacktie.jatmibroker.core.transport.Codec)3 Transport (org.jboss.narayana.blacktie.jatmibroker.core.transport.Transport)3 TransactionException (org.jboss.narayana.blacktie.jatmibroker.core.tx.TransactionException)3 SocketException (java.net.SocketException)2 HashMap (java.util.HashMap)2 StringTokenizer (java.util.StringTokenizer)2 ServiceData (org.jboss.narayana.blacktie.jatmibroker.core.server.ServiceData)2 Sender (org.jboss.narayana.blacktie.jatmibroker.core.transport.Sender)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1