Search in sources :

Example 6 with ResponsePacket

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()));
}
Also used : ResponsePacket(edu.umass.cs.gnscommon.packets.ResponsePacket) Test(org.junit.Test) DefaultGNSTest(edu.umass.cs.gnsserver.utils.DefaultGNSTest)

Example 7 with ResponsePacket

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()));
}
Also used : ResponsePacket(edu.umass.cs.gnscommon.packets.ResponsePacket) Test(org.junit.Test) DefaultGNSTest(edu.umass.cs.gnsserver.utils.DefaultGNSTest)

Example 8 with ResponsePacket

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() });
    }
}
Also used : ResponsePacket(edu.umass.cs.gnscommon.packets.ResponsePacket) InetSocketAddress(java.net.InetSocketAddress) ActiveReplicaError(edu.umass.cs.reconfiguration.reconfigurationpackets.ActiveReplicaError)

Example 9 with ResponsePacket

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. */
}
Also used : Callback(edu.umass.cs.gigapaxos.interfaces.Callback) ResponsePacket(edu.umass.cs.gnscommon.packets.ResponsePacket) Request(edu.umass.cs.gigapaxos.interfaces.Request) ReconfiguratorRequest(edu.umass.cs.reconfiguration.interfaces.ReconfiguratorRequest) ClientRequest(edu.umass.cs.gigapaxos.interfaces.ClientRequest) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) ExecutionException(java.util.concurrent.ExecutionException) InternalCommandPacket(edu.umass.cs.gnsserver.gnsapp.packet.InternalCommandPacket) CommandPacket(edu.umass.cs.gnscommon.packets.CommandPacket) TimeoutException(java.util.concurrent.TimeoutException)

Example 10 with ResponsePacket

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;
}
Also used : ResponsePacket(edu.umass.cs.gnscommon.packets.ResponsePacket)

Aggregations

ResponsePacket (edu.umass.cs.gnscommon.packets.ResponsePacket)12 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)4 JSONObject (org.json.JSONObject)4 Test (org.junit.Test)4 Request (edu.umass.cs.gigapaxos.interfaces.Request)3 ActiveReplicaError (edu.umass.cs.reconfiguration.reconfigurationpackets.ActiveReplicaError)3 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)2 CommandPacket (edu.umass.cs.gnscommon.packets.CommandPacket)2 Callback (edu.umass.cs.gigapaxos.interfaces.Callback)1 ClientRequest (edu.umass.cs.gigapaxos.interfaces.ClientRequest)1 InternalCommandPacket (edu.umass.cs.gnsserver.gnsapp.packet.InternalCommandPacket)1 ReconfiguratorRequest (edu.umass.cs.reconfiguration.interfaces.ReconfiguratorRequest)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 JSONException (org.json.JSONException)1