use of org.jboss.narayana.blacktie.jatmibroker.core.transport.Receiver 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;
}
use of org.jboss.narayana.blacktie.jatmibroker.core.transport.Receiver 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;
}
}
use of org.jboss.narayana.blacktie.jatmibroker.core.transport.Receiver in project narayana by jbosstm.
the class TestConnection method test.
public void test() throws ConnectionException, IOException {
Receiver serviceDispatcher = serviceTransport.getReceiver("JAVA_Converse", false);
Sender clientSender = clientTransport.getSender("JAVA_Converse", false);
Receiver clientReceiver = clientTransport.createReceiver(1, null, null);
clientSender.send(clientReceiver.getReplyTo(), (short) 1, 1, "hi".getBytes(), 2, 0, 0, 0, "X_OCTET", null);
Message receive = serviceDispatcher.receive(0);
receive.ack();
assertTrue(receive.len == 2);
Sender serviceSender = serviceTransport.createSender(receive.replyTo);
Receiver serviceReceiver = serviceTransport.createReceiver(1, null, null);
log.info("Chatting");
for (int i = 0; i < 100; i++) {
String toSend = String.valueOf(i);
serviceSender.send(serviceReceiver.getReplyTo(), (short) 1, 1, toSend.getBytes(), toSend.length(), 0, 0, 0, "X_OCTET", null);
Message receive2 = clientReceiver.receive(0);
assertTrue(receive2.len == toSend.length());
String received = new String(receive2.data);
assertTrue(received + " " + toSend, received.equals(toSend));
}
log.info("Chatted");
}
Aggregations