Search in sources :

Example 1 with GNSClientCommands

use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.

the class ClientAsynchExample method main.

/**
   *
   * @param args
   * @throws IOException
   * @throws InvalidKeySpecException
   * @throws NoSuchAlgorithmException
   * @throws ClientException
   * @throws InvalidKeyException
   * @throws SignatureException
   * @throws Exception
   */
public static void main(String[] args) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, ClientException, InvalidKeyException, SignatureException, Exception {
    // Create the client
    GNSClientCommands client = new GNSClientCommands(null);
    GuidEntry accountGuidEntry = null;
    try {
        // Create a guid (which is also an account guid)
        accountGuidEntry = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS, "password", true);
    } catch (Exception e) {
        System.out.println("Exception during accountGuid creation: " + e);
        e.printStackTrace();
        System.exit(1);
    }
    System.out.println("Client connected to GNS");
    JSONObject command;
    if (args.length > 0 && args[0].equals("-write")) {
        JSONObject json = new JSONObject("{\"occupation\":\"busboy\"," + "\"friends\":[\"Joe\",\"Sam\",\"Billy\"]," + "\"gibberish\":{\"meiny\":\"bloop\",\"einy\":\"floop\"}," + "\"location\":\"work\",\"name\":\"frank\"}");
        command = createAndSignCommand(CommandType.ReplaceUserJSON, accountGuidEntry, GNSProtocol.GUID.toString(), accountGuidEntry.getGuid(), GNSProtocol.USER_JSON.toString(), json.toString(), GNSProtocol.WRITER.toString(), accountGuidEntry.getGuid());
    } else {
        command = createAndSignCommand(CommandType.Read, accountGuidEntry, GNSProtocol.GUID.toString(), accountGuidEntry.getGuid(), GNSProtocol.FIELD.toString(), "occupation", GNSProtocol.READER.toString(), accountGuidEntry.getGuid());
    }
    // Create the command packet with a bogus id
    // arun: can not change request ID
    CommandPacket commandPacket = new CommandPacket((long) (Math.random() * Long.MAX_VALUE), command);
    // Keep track of what we've sent for the other thread to look at.
    Set<Long> pendingIds = Collections.newSetFromMap(new ConcurrentHashMap<Long, Boolean>());
    // Create and run another thread to pick up the responses
    Runnable companion = new Runnable() {

        @Override
        public void run() {
            lookForResponses(client, pendingIds);
        }
    };
    //Does this on Android as of 9/16:
    //ERROR: ClientAsynchExample.java:114: Lambda coming from jar file need their interfaces 
    //on the classpath to be compiled, unknown interfaces are java.lang.Runnable
    //    Runnable companion = () -> {
    //      lookForResponses(client, pendingIds);
    //    };
    new Thread(companion).start();
    while (true) {
        //long id = client.generateNextRequestID();
        // Important to set the new request id each time
        //commandPacket.setClientRequestId(id);
        // Record what we're sending
        pendingIds.add(commandPacket.getRequestID());
        // arun: disabled
        if (true) {
            throw new RuntimeException("disabled");
        }
        // Actually send out the packet
        //client.sendCommandPacketAsynch(commandPacket);
        // if you generate them too fast you'll clog things up 
        ThreadUtils.sleep(100);
    }
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) JSONObject(org.json.JSONObject) CommandPacket(edu.umass.cs.gnscommon.packets.CommandPacket) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 2 with GNSClientCommands

use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.

the class ContextServiceExample method main.

/**
   *
   * @param args
   * @throws Exception
   */
public static void main(String[] args) throws Exception {
    String csHost = args[0];
    int csPort = Integer.parseInt(args[1]);
    client = new GNSClientCommands();
    System.out.println("[Client connected to GNS]\n");
    try {
        /**
       * Create an account GUID if one doesn't already exists. The true
       * flag makes it verbosely print out what it is doing. The password
       * is for future use and is needed mainly if the keypair is
       * generated on the server in order to retrieve the private key.
       * lookupOrCreateAccountGuid "cheats" by bypassing email-based or
       * other verification mechanisms using a shared secret between the
       * server and the client.
       *
       */
        System.out.println("// account GUID creation\n" + "GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS," + " \"password\", true)\n");
        account_guid = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS, "password", true);
    } catch (Exception | Error e) {
        System.out.println("Exception during accountGuid creation: " + e);
        e.printStackTrace();
        System.exit(1);
    }
    System.out.println("csHost " + csHost + " csPort " + csPort);
    csClient = new ContextServiceClient(csHost, csPort, false, PrivacySchemes.NO_PRIVACY);
    String csSearchQuery = "latitude >= 32 AND latitude <= 33 AND " + "longitude >= -98 AND longitude <= -97";
    System.out.println("\n Issuing a search query to context service " + csSearchQuery + "\n");
    JSONArray resultArray = new JSONArray();
    int replySize = csClient.sendSearchQuery(csSearchQuery, resultArray, QUERY_EXPIRY_TIME);
    System.out.println("Search query reply size " + replySize + " GUIDs " + resultArray + "\n");
    JSONObject userJSON = new JSONObject();
    // user setting location inside the search query area.
    userJSON.put("latitude", 32.5);
    userJSON.put("longitude", -97.5);
    System.out.println("A user with GUID " + account_guid.getGuid() + " entering the geofence specified in the search query by doing an update" + ", latitude=32.5 and longitude=-97.5, in GNS\n");
    client.update(account_guid, userJSON);
    // checking for trigger from context service that a new guid is added in an
    // already existing group. This call blocks until there are non zero triggers.
    JSONArray triggerArray = new JSONArray();
    csClient.getQueryUpdateTriggers(triggerArray);
    System.out.println("Context service sends a trigger showing that user with GUID " + account_guid.getGuid() + " enters the geofence, the geofence's group GUID is in the TO_BE_ADDED field\n");
    for (int i = 0; i < triggerArray.length(); i++) {
        System.out.println("Trigger JSON " + triggerArray.getJSONObject(i));
    }
    userJSON = new JSONObject();
    // user setting location inside the search query area.
    userJSON.put("latitude", 31);
    userJSON.put("longitude", -97.5);
    System.out.println("\n A user with guid" + account_guid.getGuid() + " going out of the geofence specified in the search query by doing an update, latitude=31 and longitude=-97.5,  in GNS\n");
    client.update(account_guid, userJSON);
    // checking for trigger from context service that the guid is removed from an
    // already existing group. This call blocks until there are non zero triggers.
    triggerArray = new JSONArray();
    csClient.getQueryUpdateTriggers(triggerArray);
    System.out.println("Context service sends a trigger showing that user with GUID " + account_guid.getGuid() + " removed from the geofence, the geofence's group GUID is in TO_BE_REMOVED field ");
    for (int i = 0; i < triggerArray.length(); i++) {
        System.out.println("Trigger JSON " + triggerArray.getJSONObject(i));
    }
    System.out.println("\nBasic GNS CS test successful\n");
    System.exit(0);
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ContextServiceClient(edu.umass.cs.contextservice.client.ContextServiceClient)

Example 3 with GNSClientCommands

use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.

the class CreateMultiGuidClient method main.

/**
   * @param args
   * @throws Exception
   */
public static void main(String[] args) throws Exception {
    int node = Integer.parseInt(args[0]);
    BENIGN_CLIENT = Integer.parseInt(args[1]);
    NUM_CLIENT = Integer.parseInt(args[2]);
    boolean flag = Boolean.parseBoolean(args[3]);
    //Read in the code and serialize
    //		String code = new String(Files.readAllBytes(Paths.get(filename)));
    //		String code64 = Base64.encodeToString(code.getBytes("utf-8"), true);
    //		String mal_code = new String(Files.readAllBytes(Paths.get(mal_file)));		
    //		String mal_code64 = Base64.encodeToString(mal_code.getBytes("utf-8"), true);
    client = new GNSClientCommands();
    ExecutorService executor = Executors.newFixedThreadPool(numThread);
    for (int i = 0; i < NUM_CLIENT; i++) {
        String name = "test" + (node * 1000 + i) + ACCOUNT_ALIAS;
        GuidEntry guidAccount = null;
        try {
            guidAccount = lookupOrCreateAccountGuid(client, name, "password");
        } catch (VerificationException e) {
        // ignore verification exceptions
        } catch (Exception e) {
            System.out.println("Exception during accountGuid creation: " + e);
            System.exit(1);
        }
        System.out.println(name + ":" + guidAccount.getGuid());
        if (flag) {
            if (i < BENIGN_CLIENT) {
                executor.execute(new createGuidThread(client, guidAccount.getGuid(), guidAccount));
            } else {
                executor.execute(new createGuidThread(client, guidAccount.getGuid(), guidAccount));
            }
        }
    }
    while (createdGuid < NUM_CLIENT) {
        System.out.println(createdGuid + "/" + NUM_CLIENT + " guids have been created ...");
        Thread.sleep(1000);
    }
    System.out.println("Created all " + NUM_CLIENT + " guids.");
    System.exit(0);
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) ExecutorService(java.util.concurrent.ExecutorService) VerificationException(edu.umass.cs.gnscommon.exceptions.client.VerificationException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) VerificationException(edu.umass.cs.gnscommon.exceptions.client.VerificationException)

Example 4 with GNSClientCommands

use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.

the class SimpleClientExample method main.

/**
   *
   * @param args
   * @throws IOException
   * @throws InvalidKeySpecException
   * @throws NoSuchAlgorithmException
   * @throws ClientException
   * @throws InvalidKeyException
   * @throws SignatureException
   * @throws Exception
   */
public static void main(String[] args) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, ClientException, InvalidKeyException, SignatureException, Exception {
    // Create the client. Connects to a default reconfigurator as specified in gigapaxos.properties file.
    client = new GNSClientCommands();
    try {
        // Create an account guid if one doesn't already exists.
        // The true makes it verbosely print out what it is doing.
        // The password is for future use.
        // Note that lookupOrCreateAccountGuid "cheats" by bypassing the account verification
        // mechanisms.
        accountGuid = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS, PASSWORD, true);
    } catch (Exception e) {
        System.out.println("Exception during accountGuid creation: " + e);
        System.exit(1);
    }
    System.out.println("Client connected to GNS");
    // Retrive the GUID using the account id
    String guid = client.lookupGuid(ACCOUNT_ALIAS);
    System.out.println("Retrieved GUID for " + ACCOUNT_ALIAS + ": " + guid);
    // Get the public key from the GNS
    PublicKey publicKey = client.publicKeyLookupFromGuid(guid);
    System.out.println("Retrieved public key: " + publicKey.toString());
    // Use the GuidEntry create an new record in the GNS
    client.fieldUpdate(accountGuid, "homestate", "Florida");
    System.out.println("Added homestate -> Florida record to the GNS for GUID " + accountGuid.getGuid());
    // Retrive that record from the GNS
    String result = client.fieldRead(accountGuid.getGuid(), "homestate", accountGuid);
    System.out.println("Result of read location: " + result);
    System.exit(0);
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) PublicKey(java.security.PublicKey) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException)

Example 5 with GNSClientCommands

use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.

the class SequentialCreateFieldTimeout method setUpBeforeClass.

/**
   *
   * @throws Exception
   */
@BeforeClass
public static void setUpBeforeClass() throws Exception {
    client = new GNSClientCommands();
    // Make all the reads be coordinated
    client.setForceCoordinatedReads(true);
    // arun: connectivity check embedded in GNSClient constructor
    boolean connected = client instanceof GNSClient;
    if (connected) {
        System.out.println("Client created and connected to server.");
    }
    //
    int tries = 5;
    boolean accountCreated = false;
    do {
        try {
            System.out.println("Creating account guid: " + (tries - 1) + " attempt remaining.");
            masterGuid = GuidUtils.lookupOrCreateAccountGuid(client, accountAlias, PASSWORD, true);
            accountCreated = true;
        } catch (Exception e) {
            ThreadUtils.sleep((5 - tries) * 5000);
        }
    } while (!accountCreated && --tries > 0);
    if (accountCreated == false) {
        fail("Failure setting up account guid; aborting all tests.");
    }
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) GNSClient(edu.umass.cs.gnsclient.client.GNSClient) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Aggregations

GNSClientCommands (edu.umass.cs.gnsclient.client.GNSClientCommands)70 IOException (java.io.IOException)55 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)50 StringTokenizer (java.util.StringTokenizer)35 GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)28 JSONArray (org.json.JSONArray)13 JSONException (org.json.JSONException)11 DuplicateNameException (edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException)10 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)10 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)9 JSONObject (org.json.JSONObject)9 InvalidKeyException (java.security.InvalidKeyException)8 SignatureException (java.security.SignatureException)8 GNSClient (edu.umass.cs.gnsclient.client.GNSClient)6 PublicKey (java.security.PublicKey)6 HashSet (java.util.HashSet)6 BeforeClass (org.junit.BeforeClass)6 RandomString (edu.umass.cs.gnscommon.utils.RandomString)5 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5