use of edu.umass.cs.gnscommon.packets.ResponsePacket in project GNS by MobilityFirst.
the class AbstractGNSClient method desktopSendCommmandAndWait.
// arun: changed this to return CommandValueReturnPacket
private ResponsePacket desktopSendCommmandAndWait(JSONObject command) throws IOException {
Object myMonitor = new Object();
long id;
monitorMap.put(id = this.generateNextRequestID(), myMonitor);
CommandPacket commandPacket = desktopSendCommmandNoWait(command, id);
// now we wait until the correct packet comes back
try {
GNSClientConfig.getLogger().log(Level.FINE, "{0} waiting for query {1}", new Object[] { this, id + "" });
long monitorStartTime = System.currentTimeMillis();
if (!USE_GLOBAL_MONITOR) {
synchronized (myMonitor) {
while (monitorMap.containsKey(id) && (readTimeout == 0 || System.currentTimeMillis() - monitorStartTime < readTimeout)) {
myMonitor.wait(readTimeout);
}
}
} else {
synchronized (monitor) {
while (!resultMap.containsKey(id) && (readTimeout == 0 || System.currentTimeMillis() - monitorStartTime < readTimeout)) {
monitor.wait(readTimeout);
}
}
}
if (readTimeout != 0 && System.currentTimeMillis() - monitorStartTime >= readTimeout) {
return getTimeoutResponse(this, commandPacket);
}
GNSClientConfig.getLogger().log(Level.FINE, "Response received for query {0}", new Object[] { id + "" });
} catch (InterruptedException x) {
GNSClientConfig.getLogger().severe("Wait for return packet was interrupted " + x);
}
//CommandResult
Request result = resultMap.remove(id);
GNSClientConfig.getLogger().log(Level.FINE, "{0} received response {1} ", new Object[] { this, result.getSummary() });
return result instanceof ResponsePacket ? ((ResponsePacket) result) : new ResponsePacket(result.getServiceName(), id, ResponseCode.ACTIVE_REPLICA_EXCEPTION, ((ActiveReplicaError) result).getResponseMessage());
}
use of edu.umass.cs.gnscommon.packets.ResponsePacket in project GNS by MobilityFirst.
the class AbstractGNSClient method handleCommandValueReturnPacket.
/**
* arun: Handles both command return values and active replica error
* messages.
*
* @param response
* @param receivedTime
* @throws JSONException
*/
private void handleCommandValueReturnPacket(Request response) {
long methodStartTime = System.currentTimeMillis();
ResponsePacket packet = response instanceof ResponsePacket ? (ResponsePacket) response : null;
ActiveReplicaError error = response instanceof ActiveReplicaError ? (ActiveReplicaError) response : null;
assert (packet != null || error != null);
long id = packet != null ? packet.getClientRequestId() : error.getRequestID();
GNSClientConfig.getLogger().log(Level.FINE, "{0} received response {1}:{2} from {3}", new Object[] { this, id + "", response.getSummary(), packet != null ? //packet.getResponder()
"unknown" : error.getSender() });
// store the response away
if (packet != null) {
resultMap.put(id, packet);
} else {
resultMap.put(id, error);
}
// differentiates between synchronusly and asynchronusly sent
if (!pendingAsynchPackets.containsKey(id)) {
Object myMonitor = monitorMap.remove(id);
assert (myMonitor != null) : Util.suicide("No monitor entry found for request " + id);
if (!USE_GLOBAL_MONITOR && myMonitor != null) {
synchronized (myMonitor) {
myMonitor.notify();
}
}
/* for synchronous sends we notify waiting threads.
* arun: Needed now only for Android if at all.
*/
synchronized (monitor) {
monitor.notifyAll();
}
} else {
// Handle the asynchronus packets
// note that we have recieved the reponse
pendingAsynchPackets.remove(id);
}
DelayProfiler.updateDelay("handleCommandValueReturnPacket", methodStartTime);
}
use of edu.umass.cs.gnscommon.packets.ResponsePacket in project GNS by MobilityFirst.
the class GNSClient method sendSyncInternal.
/**
* Gets a {@link RespponsePacket} or {@link ActiveReplicaError} as a
* response.
*
* @param packet
* @param timeout
* @param retries
* @return
* @throws IOException
* @throws ClientException
*/
private ResponsePacket sendSyncInternal(CommandPacket packet, final long timeout, int retries) throws IOException, ClientException {
ResponsePacket response = null;
int count = 0;
do {
if (count > 0)
GNSClientConfig.getLogger().log(Level.INFO, "{0} attempting retransmission {1} upon timeout of {2}; {3}", new Object[] { this, count, packet.getSummary(), response == null ? "[null response]" : "" });
try {
response = defaultHandleResponse(this.sendSyncInternal(packet, timeout));
} catch (ClientException ce) {
// iOS client doesn't support empty body "if" statements
//if (ce.getCode() == ResponseCode.TIMEOUT)
// do nothing
// ;
}
} while ((count++ < this.numRetriesUponTimeout && (response == null || response.getErrorCode() == ResponseCode.TIMEOUT)));
return (response);
}
use of edu.umass.cs.gnscommon.packets.ResponsePacket in project GNS by MobilityFirst.
the class ByteificationComparison method test_16_CommandValueReturnPacket_toBytes_128B.
/**
*
* @throws UnsupportedEncodingException
* @throws JSONException
*/
@Test
public void test_16_CommandValueReturnPacket_toBytes_128B() throws UnsupportedEncodingException, JSONException {
ResponsePacket packet = new ResponsePacket(1, ResponseCode.NO_ERROR.getCodeValue(), new String(Util.getRandomAlphanumericBytes(64)), new String(Util.getRandomAlphanumericBytes(64)));
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 128B 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 CommandHandler method runCommand.
private static void runCommand(CommandPacket commandPacket, AbstractCommand command, ClientRequestHandlerInterface handler, boolean doNotReplyToClient, GNSApplicationInterface<String> app) {
JSONObject jsonFormattedCommand = PacketUtils.getCommand(commandPacket);
try {
// instrumentation
long receiptTime = System.currentTimeMillis();
// instrumentation
final Long executeCommandStart = System.currentTimeMillis();
// Other than this line, one below and some catches all of this
// method is instrumentation.
CommandResponse returnValue = executeCommand(command, commandPacket, handler);
assert (commandPacket.getRequestType() != null) : "request type is null";
assert (commandPacket.getCommandType() != null) : "command type is null";
assert (command != null) : "command is null";
// instrumentation
DelayProfiler.updateDelay("executeCommand", executeCommandStart);
if (System.currentTimeMillis() - executeCommandStart > LONG_DELAY_THRESHOLD) {
DelayProfiler.updateDelay(commandPacket.getRequestType() + "." + command.getCommandType(), executeCommandStart);
}
if (System.currentTimeMillis() - executeCommandStart > LONG_DELAY_THRESHOLD) {
ClientCommandProcessorConfig.getLogger().log(Level.FINE, "{0} command {1} took {2}ms of execution delay (delay logging threshold={2}ms)", new Object[] { handler.getApp(), command.getSummary(), (System.currentTimeMillis() - executeCommandStart), LONG_DELAY_THRESHOLD });
}
// the last arguments here in the call below are instrumentation
// that the client can use to determine LNS load
ResponsePacket returnPacket = new ResponsePacket(commandPacket.getRequestID(), commandPacket.getServiceName(), returnValue, 0, 0, System.currentTimeMillis() - receiptTime);
try {
assert (returnPacket.getErrorCode() != null);
ClientCommandProcessorConfig.getLogger().log(Level.FINE, "{0} handling command reply: {1}", new Object[] { handler.getApp(), returnPacket });
// Possibly send the return value back to the client
handleCommandReturnValuePacketForApp(commandPacket, returnPacket, doNotReplyToClient, app);
} catch (IOException e) {
ClientCommandProcessorConfig.getLogger().log(Level.SEVERE, "Problem replying to command: {0}", e);
}
} catch (JSONException e) {
ClientCommandProcessorConfig.getLogger().log(Level.SEVERE, "{0}: problem executing command: {1}", new Object[] { handler.getApp(), e });
e.printStackTrace();
}
// sending trigger to Context service here.
if (Config.getGlobalBoolean(GNSConfig.GNSC.ENABLE_CNS)) {
if (!doNotReplyToClient) {
if (command.getClass().getSuperclass() == AbstractUpdate.class) {
GNSConfig.getLogger().log(Level.FINE, "{0} sending trigger to context service for {1}:{2}", new Object[] { handler.getApp(), command, jsonFormattedCommand });
app.getContextServiceGNSClient().sendTiggerOnGnsCommand(jsonFormattedCommand, command, false);
}
}
}
}
Aggregations