use of org.jboss.narayana.blacktie.jatmibroker.core.transport.Codec in project narayana by jbosstm.
the class ConnectionImpl method tpacall.
/**
* Asynchronous call
*
* @param svc
* The name of the service to call
* @param toSend
* The inbound data
* @param flags
* The flags to use
* @return The connection descriptor
* @throws ConnectionException
* If the service cannot be contacted.
*/
public int tpacall(String svc, Buffer toSend, int flags) throws ConnectionException {
log.debug("tpacall");
int toCheck = flags & ~(TPNOTRAN | TPNOREPLY | TPNOBLOCK | TPNOTIME | TPSIGRSTRT);
if (toCheck != 0) {
log.trace("invalid flags remain: " + toCheck);
throw new ConnectionException(ConnectionImpl.TPEINVAL, "Invalid flags remain: " + toCheck);
}
svc = svc.substring(0, Math.min(ConnectionImpl.XATMI_SERVICE_NAME_LENGTH, svc.length()));
String qtype = (String) properties.get("blacktie." + svc + ".type");
log.debug(svc + " qtype is " + qtype + " and flags is " + flags);
if ("topic".equals(qtype) && (flags & TPNOREPLY) == 0) {
log.warn(svc + " type is " + qtype + " and MUST have TPNOREPLY set");
throw new ConnectionException(ConnectionImpl.TPEINVAL, svc + " type is " + qtype + " and MUST have TPNOREPLY set");
}
int correlationId = 0;
synchronized (this) {
correlationId = ++nextId;
log.trace("Allocated next sessionId: " + correlationId);
}
Transport transport = getTransport(svc);
Receiver endpoint = transport.createReceiver(correlationId, responseMonitor, null);
temporaryQueues.put(correlationId, endpoint);
log.trace("Added a queue for: " + correlationId);
// 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;
// Don't set ttl when tpacall and TPNOREPLY set
if (timeToLive != null && ((flags & ConnectionImpl.TPNOREPLY) != ConnectionImpl.TPNOREPLY)) {
ttl = Integer.parseInt(timeToLive) * 1000;
log.debug("Set ttl: " + ttl);
}
transport.getSender(svc, false).send(endpoint.getReplyTo(), (short) 0, 0, data, len, correlationId, flags, ttl, type, subtype);
if ((flags & ConnectionImpl.TPNOREPLY) == ConnectionImpl.TPNOREPLY) {
correlationId = 0;
}
log.debug("Returning cd: " + correlationId);
return correlationId;
}
use of org.jboss.narayana.blacktie.jatmibroker.core.transport.Codec 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.Codec 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;
}
}
Aggregations