use of org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException 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);
}
}
use of org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException 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;
}
use of org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException 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;
}
use of org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException in project narayana by jbosstm.
the class TestRollbackOnly method test_tprecv_TPEV_SVCFAIL.
public void test_tprecv_TPEV_SVCFAIL() throws ConnectionException, ConfigurationException {
log.info("test_tprecv_TPEV_SVCFAIL");
server.tpadvertiseTestRollbackOnlyTprecvTPEVSVCFAILService();
assertTrue(TX.tx_open() == TX.TX_OK);
assertTrue(TX.tx_begin() == TX.TX_OK);
Session cd = connection.tpconnect(RunServer.getServiceNameTestRollbackOnly2(), sendbuf, Connection.TPRECVONLY);
try {
cd.tprecv(0);
fail("Expected e.getEvent() == Connection.TPEV_SVCFAIL");
} catch (ResponseException e) {
assertTrue(e.getEvent() == Connection.TPEV_SVCFAIL);
assertTrue(e.getTperrno() == Connection.TPEEVENT);
Buffer rcvbuf = e.getReceived();
assertTrue(TestTPConversation.strcmp(rcvbuf, "test_tprecv_TPEV_SVCFAIL_service") == 0);
} catch (ConnectionException e) {
fail("Expected e.getEvent() == Connection.TPEV_SVCFAIL");
}
TXINFO txinfo = new TXINFO();
int inTx = TX.tx_info(txinfo);
log.info("inTx=" + inTx);
assertTrue(txinfo.transaction_state == TX.TX_ROLLBACK_ONLY);
assertTrue(TX.tx_commit() == TX.TX_ROLLBACK);
}
use of org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException 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;
}
Aggregations