use of edu.umass.cs.gnscommon.packets.ResponsePacket in project GNS by MobilityFirst.
the class ByteificationComparison method test_15_CommandValueReturnPacket_1024B_Strings.
/**
*
* @throws UnsupportedEncodingException
* @throws JSONException
*/
@Test
public void test_15_CommandValueReturnPacket_1024B_Strings() throws UnsupportedEncodingException, JSONException {
ResponsePacket packet = new ResponsePacket(1, ResponseCode.NO_ERROR.getCodeValue(), new String(Util.getRandomAlphanumericBytes(512)), new String(Util.getRandomAlphanumericBytes(512)));
long startTime = System.nanoTime();
for (int i = 0; i < TEST_RUNS; i++) {
byte[] bytes = packet.toBytes();
ResponsePacket.fromBytes(bytes);
}
long endTime = System.nanoTime();
double avg = (endTime - startTime) / (TEST_RUNS);
System.out.println("Average byteification time CommandValueReturnPacket 1024B was " + avg + " nanoseconds.");
byte[] bytes = packet.toBytes();
ResponsePacket outputPacket = ResponsePacket.fromBytes(bytes);
assert (packet.toJSONObject().toString().equals(outputPacket.toJSONObject().toString()));
}
use of edu.umass.cs.gnscommon.packets.ResponsePacket in project GNS by MobilityFirst.
the class ByteificationComparison method test_17_CommandValueReturnPacket_toBytes_1024B_Strings.
/**
*
* @throws UnsupportedEncodingException
* @throws JSONException
*/
@Test
public void test_17_CommandValueReturnPacket_toBytes_1024B_Strings() throws UnsupportedEncodingException, JSONException {
ResponsePacket packet = new ResponsePacket(1, ResponseCode.NO_ERROR.getCodeValue(), new String(Util.getRandomAlphanumericBytes(512)), new String(Util.getRandomAlphanumericBytes(512)));
long startTime = System.nanoTime();
for (int i = 0; i < TEST_RUNS; i++) {
packet.toBytes();
}
long endTime = System.nanoTime();
double avg = (endTime - startTime) / (TEST_RUNS);
System.out.println("Average byteification time CommandValueReturnPacket toBytes 1024B was " + avg + " nanoseconds.");
byte[] bytes = packet.toBytes();
ResponsePacket outputPacket = ResponsePacket.fromBytes(bytes);
assert (packet.toJSONObject().toString().equals(outputPacket.toJSONObject().toString()));
}
use of edu.umass.cs.gnscommon.packets.ResponsePacket in project GNS by MobilityFirst.
the class LNSPacketDemultiplexer method handleCommandReturnValuePacket.
/**
* Handles sending the results of a command packet back to the client. Passing
* json as well for legacy reasons and to avoid an unnecessary toJSON call.
*
* @throws JSONException
* @throws IOException
*/
private void handleCommandReturnValuePacket(Request response, JSONObject json) throws JSONException, IOException {
ResponsePacket returnPacket = response instanceof ResponsePacket ? (ResponsePacket) response : null;
ActiveReplicaError error = response instanceof ActiveReplicaError ? (ActiveReplicaError) response : null;
GNSConfig.getLogger().log(Level.INFO, "{0} received response {1}", new Object[] { this, returnPacket != null ? returnPacket : error.getSummary() });
assert (returnPacket != null || error != null);
long id = returnPacket != null ? returnPacket.getRequestID() : error.getRequestID();
String serviceName = returnPacket != null ? returnPacket.getServiceName() : error.getServiceName();
LNSRequestInfo sentInfo;
GNSConfig.getLogger().log(Level.INFO, "{0} matching {1} with {2}", new Object[] { this, id + "", handler.getRequestInfo(id) });
if ((sentInfo = handler.getRequestInfo(id)) != null) {
// doublecheck that it is for the same service name
if ((sentInfo.getServiceName().equals(serviceName) || // arun: except when service name is special name
(sentInfo.getServiceName().equals(Config.getGlobalString(RC.SPECIAL_NAME))))) {
// String serviceName = returnPacket.getServiceName();
GNSConfig.getLogger().log(Level.INFO, "{0} about to remove {1}", new Object[] { this, id + "" });
handler.removeRequestInfo(id);
// REQUEST
if (!CommandPacket.BOGUS_SERVICE_NAME.equals(serviceName) && sentInfo.getCommandType().isRead() && //&& sentInfo.getCommandName().equals(GNSCommandProtocol.READ)
returnPacket != null) {
handler.updateCacheEntry(serviceName, returnPacket.getReturnValue());
}
// send the response back
GNSConfig.getLogger().log(Level.FINE, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< LNS IS SENDING VALUE BACK TO {0}: {1}", new Object[] { sentInfo.getHost() + ":" + sentInfo.getPort(), returnPacket != null ? returnPacket.getSummary() : error.getSummary() });
handler.sendToClient(new InetSocketAddress(sentInfo.getHost(), sentInfo.getPort()), json != null ? json : returnPacket != null ? returnPacket.toJSONObject() : error.toJSONObject());
} else {
GNSConfig.getLogger().log(Level.SEVERE, "Command response packet mismatch: {0} vs. {1}", new Object[] { sentInfo.getServiceName(), returnPacket.getServiceName() });
}
} else {
GNSConfig.getLogger().log(Level.FINE, "Duplicate response for {0}: {1}", new Object[] { id, json != null ? json : returnPacket != null ? returnPacket.toJSONObject() : error.toJSONObject() });
}
}
use of edu.umass.cs.gnscommon.packets.ResponsePacket in project GNS by MobilityFirst.
the class GNSClient method sendSyncInternal.
/**
* All sync sends come here, which in turn calls
* {@link #sendAsync(CommandPacket, Callback)}{@code .get(timeout)}.
*
* @param packet
* @param timeout
* @return
* @throws IOException
* @throws ClientException
*/
private ResponsePacket sendSyncInternal(CommandPacket packet, final long timeout) throws IOException, ClientException {
ResponsePacket[] processed = new ResponsePacket[1];
Callback<Request, CommandPacket> future = new Callback<Request, CommandPacket>() {
@Override
public CommandPacket processResponse(Request response) {
GNSClientConfig.getLogger().log(Level.FINE, "{0} received response {1} for request {2}", new Object[] { GNSClient.this, packet.getSummary(), response.getSummary() });
processed[0] = defaultHandleResponse(response);
return packet;
}
};
try {
this.sendAsync(packet, future).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
throw new ClientException(e);
}
return processed[0];
/* We could also simply have used gigapaxos' sync send above, but using
* the async code above avoids the redundancy with sendAsync on checking
* for anycast/proxy/default. */
}
use of edu.umass.cs.gnscommon.packets.ResponsePacket in project GNS by MobilityFirst.
the class GNSClient method sendSync.
/**
* This method synchronously retrieves the response and checks for and
* throws exceptions if needed. This checkResponse behavior is unlike
* sendAsync that can not throw exceptions to the caller because the
* response is processed by a separate thread.
*
* @param packet
* @param timeout
* @param retries
* @return the request
* @throws IOException
* @throws ClientException
*/
private CommandPacket sendSync(CommandPacket packet, final long timeout, int retries) throws IOException, ClientException {
ResponsePacket response = this.sendSyncInternal(packet, timeout, retries);
CommandUtils.checkResponse(nullToTimeoutResponse(response, packet), PacketUtils.setResult(packet, response));
GNSClientConfig.getLogger().log(Level.FINE, "{0} received response {0} for request {1}", new Object[] { this, response.getSummary(), packet.getSummary() });
return packet;
}
Aggregations