Search in sources :

Example 6 with Transport

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

the class ConnectionImpl method tpconnect.

/**
 * Handle the initiation of a conversation with the server.
 *
 * @param svc
 *            The name of the service
 * @param toSend
 *            The outbound buffer
 * @param flags
 *            The flags to use
 * @return The connection descriptor
 * @throws ConnectionException
 *             If the service cannot be contacted.
 */
public Session tpconnect(String svc, Buffer toSend, int flags) throws ConnectionException {
    log.debug("tpconnect: " + svc);
    svc = svc.substring(0, Math.min(ConnectionImpl.XATMI_SERVICE_NAME_LENGTH, svc.length()));
    // Initiate the session
    svc = svc.substring(0, Math.min(ConnectionImpl.XATMI_SERVICE_NAME_LENGTH, svc.length()));
    int correlationId = 0;
    synchronized (this) {
        correlationId = nextId++;
    }
    Transport transport = getTransport(svc);
    SessionImpl session = new SessionImpl(this, svc, transport, correlationId);
    Receiver receiver = session.getReceiver();
    // TODO HANDLE TRANSACTION
    String type = null;
    String subtype = null;
    int len = 0;
    byte[] data = null;
    if (toSend != null) {
        CodecFactory factory = new CodecFactory(this);
        String coding_type = properties.getProperty("blacktie." + svc + ".coding_type");
        Codec codec = factory.getCodec(coding_type);
        data = codec.encode((BufferImpl) toSend);
        // data = toSend.serialize();
        type = toSend.getType();
        subtype = toSend.getSubtype();
        len = toSend.getLen();
    }
    String timeToLive = properties.getProperty("TimeToLive");
    int ttl = 0;
    if (timeToLive != null) {
        ttl = Integer.parseInt(timeToLive) * 1000;
    }
    log.debug("tpconnect sending data");
    session.getSender().send(receiver.getReplyTo(), (short) 0, 0, data, len, correlationId, flags | TPCONV, ttl, type, subtype);
    byte[] response = null;
    try {
        log.debug("tpconnect receiving data");
        X_OCTET_Impl odata = (X_OCTET_Impl) session.tprecv(0);
        // TODO THIS SHOULD BE A BETTER ERROR AND CHECKED IF TPCONV AND NOT
        // CONV
        response = odata.getByteArray();
        log.debug("tpconnect received data");
    } catch (ResponseException e) {
        response = ((X_OCTET_Impl) e.getReceived()).getByteArray();
        log.debug("Caught an exception with data", e);
    } catch (ConnectionException e) {
        session.close();
        throw new ConnectionException(e.getTperrno(), "Could not connect");
    } catch (ConfigurationException e) {
        session.close();
        throw new ConnectionException(ConnectionImpl.TPEOS, "Configuration exception: " + e.getMessage(), e);
    }
    byte[] ack = new byte[4];
    byte[] bytes = "ACK".getBytes();
    System.arraycopy(bytes, 0, ack, 0, 3);
    boolean connected = response == null ? false : Arrays.equals(ack, response);
    if (!connected) {
        log.error("Could not connect");
        session.close();
        throw new ConnectionException(ConnectionImpl.TPESYSTEM, "Could not connect");
    }
    session.setCreatorState(flags);
    sessions.put(correlationId, session);
    log.trace("Added session: " + correlationId);
    // Return a handle to allow the connection to send/receive data on
    log.debug("Created session: " + correlationId);
    return session;
}
Also used : ResponseException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException) Receiver(org.jboss.narayana.blacktie.jatmibroker.core.transport.Receiver) Codec(org.jboss.narayana.blacktie.jatmibroker.core.transport.Codec) ConfigurationException(org.jboss.narayana.blacktie.jatmibroker.core.conf.ConfigurationException) CodecFactory(org.jboss.narayana.blacktie.jatmibroker.codec.CodecFactory) Transport(org.jboss.narayana.blacktie.jatmibroker.core.transport.Transport) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Aggregations

Transport (org.jboss.narayana.blacktie.jatmibroker.core.transport.Transport)6 Receiver (org.jboss.narayana.blacktie.jatmibroker.core.transport.Receiver)4 ConnectionException (org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)3 CodecFactory (org.jboss.narayana.blacktie.jatmibroker.codec.CodecFactory)2 Codec (org.jboss.narayana.blacktie.jatmibroker.core.transport.Codec)2 Properties (java.util.Properties)1 AtmiBrokerEnvXML (org.jboss.narayana.blacktie.jatmibroker.core.conf.AtmiBrokerEnvXML)1 ConfigurationException (org.jboss.narayana.blacktie.jatmibroker.core.conf.ConfigurationException)1 Message (org.jboss.narayana.blacktie.jatmibroker.core.transport.Message)1 Sender (org.jboss.narayana.blacktie.jatmibroker.core.transport.Sender)1 TransportFactory (org.jboss.narayana.blacktie.jatmibroker.core.transport.TransportFactory)1 ResponseException (org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException)1