Search in sources :

Example 1 with GNSClient

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

the class SelectRecordsExample 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
    client = new GNSClient();
    // Create an account guid
    client.execute(GNSCommand.createAccount(accountAlias));
    accountGuidEntry = GuidUtils.getGUIDKeys(accountAlias);
    // Next we create some fake user records as subguids
    createUserRecords();
    // Create the query string
    String query = buildLocationsAgePrefQuery(GNSProtocol.LOCATION_FIELD_NAME.toString(), AREA_EXTENT, "age", 30, 50, "preference", "Long");
    // Display the query
    System.out.println("QUERY:");
    System.out.println(new JSONObject("{" + query + "}").toString(4));
    // Create the command
    CommandPacket command = GNSCommand.selectRecords(accountGuidEntry, query, Arrays.asList(GNSProtocol.LOCATION_FIELD_NAME.toString(), "age", "preference"));
    // Execute the query command and print the results
    JSONArray jsonArray = client.execute(command).getResultJSONArray();
    System.out.println("RESULT:");
    System.out.println(jsonArray.toString(4));
}
Also used : GNSClient(edu.umass.cs.gnsclient.client.GNSClient) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) CommandPacket(edu.umass.cs.gnscommon.packets.CommandPacket)

Example 2 with GNSClient

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

the class AdminTestSuite method setupBeforeClass.

/**
   *
   * @throws IOException
   */
@BeforeClass
public static void setupBeforeClass() throws IOException {
    System.out.println("Starting client");
    clientCommands = new GNSClientCommands();
    // Make all the reads be coordinated
    clientCommands.setForceCoordinatedReads(true);
    // arun: connectivity check embedded in GNSClient constructor
    boolean connected = clientCommands 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.getGUIDKeys(globalAccountName);
            accountCreated = true;
        } catch (Exception e) {
            Utils.failWithStackTrace("Failure getting master guid");
            ThreadUtils.sleep((5 - tries) * 5000);
        }
    } while (!accountCreated && --tries > 0);
    if (accountCreated == false) {
        Utils.failWithStackTrace("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)

Example 3 with GNSClient

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

the class ActiveCodeHelloWorldExample method main.

/**
	 * @param args
	 * @throws IOException 
	 * @throws ClientException 
	 * @throws JSONException 
	 */
public static void main(String[] args) throws IOException, ClientException, JSONException {
    boolean update = true;
    if (System.getProperty("update") != null) {
        update = Boolean.parseBoolean(System.getProperty("update"));
    }
    boolean isRead = true;
    if (System.getProperty("isRead") != null) {
        isRead = Boolean.parseBoolean(System.getProperty("isRead"));
    }
    String name = "benign";
    if (System.getProperty("name") != null) {
        name = System.getProperty("name");
    }
    String codeFile = "scripts/activeCode/noop.js";
    if (System.getProperty("codeFile") != null) {
        codeFile = System.getProperty("codeFile");
    }
    if (args.length > 0) {
        codeFile = args[0];
    }
    // create a client
    final GNSClient client = new GNSClient();
    final String ACCOUNT_GUID_PREFIX = "GNS_ACCOUNT_";
    final String ACCOUNT_GUID = ACCOUNT_GUID_PREFIX + name;
    final String PASSWORD = "";
    edu.umass.cs.gnsclient.client.util.GuidEntry entry = null;
    // create an account	
    try {
        entry = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_GUID, PASSWORD);
        ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(new File("guid")));
        entry.writeObject(output);
        output.flush();
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
        // The guid is already created, try to read from a local file
        try {
            ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File("guid")));
            entry = new GuidEntry(input);
            input.close();
        } catch (IOException | EncryptionException ie) {
            ie.printStackTrace();
        }
    }
    String field = "someField";
    String value = "original value";
    String depth_field = "depthField";
    String depth_result = "Depth query succeeds";
    // set up a field
    client.execute(GNSCommand.fieldUpdate(entry, field, value));
    client.execute(GNSCommand.fieldUpdate(entry, depth_field, depth_result));
    // clear code for both read and write action
    client.execute(GNSCommand.activeCodeClear(entry.getGuid(), ActiveCode.READ_ACTION, entry));
    client.execute(GNSCommand.activeCodeClear(entry.getGuid(), ActiveCode.WRITE_ACTION, entry));
    // get the value of the field
    String response = client.execute(GNSCommand.fieldRead(entry, field)).getResultJSONObject().getString(field);
    System.out.println("Before the code is deployed, the value of field(" + field + ") is " + response);
    // read in the code as a string
    final String code = new String(Files.readAllBytes(Paths.get(codeFile)));
    // set up the code for on read operation
    if (isRead) {
        if (update) {
            client.execute(GNSCommand.activeCodeSet(entry.getGuid(), ActiveCode.READ_ACTION, code, entry));
        }
    } else {
        if (update) {
            client.execute(GNSCommand.activeCodeSet(entry.getGuid(), ActiveCode.WRITE_ACTION, code, entry));
        }
    }
    // get the value of the field again
    response = client.execute(GNSCommand.fieldRead(entry, field)).getResultJSONObject().getString(field);
    System.out.println("After the code is deployed, the value of field(" + field + ") is " + response);
    ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(new File("guid")));
    entry.writeObject(output);
    output.flush();
    output.close();
    System.exit(0);
}
Also used : GNSClient(edu.umass.cs.gnsclient.client.GNSClient) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) ObjectOutputStream(java.io.ObjectOutputStream) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) IOException(java.io.IOException) JSONException(org.json.JSONException) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) ObjectInputStream(java.io.ObjectInputStream)

Example 4 with GNSClient

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

the class TestActiveCodeRemoteQueryClient method setupClientsAndGuids.

/**
	 * @throws IOException
	 * @throws ClientException
	 */
@BeforeClass
public static void setupClientsAndGuids() throws IOException, ClientException {
    client = new GNSClient();
    entries = new GuidEntry[2];
    // initialize two GUID
    for (int i = 0; i < numGuid; i++) {
        try {
            entries[i] = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_GUID_PREFIX + i, PASSWORD);
            ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(new File("guid" + i)));
            entries[i].writeObject(output);
            output.flush();
            output.close();
        } catch (Exception e) {
            // the GUID has already been created, try to fetch it from a file
            try {
                ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File("guid" + i)));
                entries[i] = new GuidEntry(input);
                input.close();
            } catch (IOException | EncryptionException ie) {
                ie.printStackTrace();
            }
        }
    }
    // set the target guid to the second one and put it into the code
    targetGuid = entries[numGuid - 1].getGuid();
    noop_code = new String(Files.readAllBytes(Paths.get("scripts/activeCode/noop.js")));
    String codeFile = System.getProperty("activeReadCode");
    if (codeFile == null)
        codeFile = "scripts/activeCode/remoteReadQuery.js";
    String code = new String(Files.readAllBytes(Paths.get(codeFile)));
    read_code = code.replace("//substitute this line with the targetGuid", "var targetGuid=\"" + targetGuid + "\";");
    System.out.println("The read code is:\n" + read_code);
    codeFile = System.getProperty("activeWriteCode");
    if (codeFile == null)
        codeFile = "scripts/activeCode/remoteWriteQuery.js";
    code = new String(Files.readAllBytes(Paths.get(codeFile)));
    write_code = code.replace("//substitute this line with the targetGuid", "var targetGuid=\"" + targetGuid + "\";");
    System.out.println("The write code is:\n" + write_code);
    // initialize the fields for each guid
    client.execute(GNSCommand.fieldUpdate(entries[0], someField, someValue));
    client.execute(GNSCommand.fieldUpdate(entries[1], depthField, depthResult));
    System.out.println(">>>>>>>>>> Testing >>>>>>>>>>");
}
Also used : GNSClient(edu.umass.cs.gnsclient.client.GNSClient) FileOutputStream(java.io.FileOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) ObjectInputStream(java.io.ObjectInputStream) BeforeClass(org.junit.BeforeClass)

Example 5 with GNSClient

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

the class GNSClientCapacityTest method cleanup.

/**
	 * Removes all account and sub-guids created during the test.
	 * 
	 * @throws Exception
	 */
@AfterClass
public static void cleanup() throws Exception {
    Thread.sleep(2000);
    assert (clients != null && clients[0] != null);
    if (!accountGuidsOnly) {
        System.out.println("About to delete " + guidEntries.length + " sub-guids: " + Arrays.asList(guidEntries));
        for (GuidEntry guidEntry : guidEntries) {
            try {
                log.log(Level.FINE, "About to delete sub-guid {0}", new Object[] { guidEntry });
                if (guidEntry != null)
                    clients[0].execute(GNSCommand.guidRemove(guidEntry));
                log.log(Level.FINE, "Deleted sub-guid {0}", new Object[] { guidEntry });
            } catch (Exception e) {
                log.log(Level.WARNING, "Failed to delete sub-guid {0}", new Object[] { guidEntry });
                e.printStackTrace();
            // continue with rest
            }
        }
    }
    System.out.println("About to delete " + accountGuidEntries.length + " account guids: " + Arrays.asList(accountGuidEntries));
    for (GuidEntry accGuidEntry : accountGuidEntries) {
        try {
            log.log(Level.FINE, "About to delete account guid {0}", new Object[] { accGuidEntry });
            if (accGuidEntry != null)
                clients[0].execute(GNSCommand.accountGuidRemove(accGuidEntry));
            log.log(Level.FINE, "Deleted account guid {0}", new Object[] { accGuidEntry });
        } catch (Exception e) {
            log.log(Level.WARNING, "Failed to delete account guid {0}", new Object[] { accGuidEntry });
            e.printStackTrace();
        // continue with rest
        }
    }
    for (GNSClient client : clients) client.close();
    executor.shutdown();
    System.out.println(DelayProfiler.getStats());
}
Also used : GNSClient(edu.umass.cs.gnsclient.client.GNSClient) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) AfterClass(org.junit.AfterClass)

Aggregations

GNSClient (edu.umass.cs.gnsclient.client.GNSClient)13 IOException (java.io.IOException)11 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)7 GNSClientCommands (edu.umass.cs.gnsclient.client.GNSClientCommands)6 BeforeClass (org.junit.BeforeClass)6 File (java.io.File)4 JSONException (org.json.JSONException)4 GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)3 EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)3 RandomString (edu.umass.cs.gnscommon.utils.RandomString)3 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 Test (org.junit.Test)2 DuplicateNameException (edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException)1 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException)1