Search in sources :

Example 31 with GNSClientCommands

use of edu.umass.cs.gnsclient.client.GNSClientCommands 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 });
                clients[0].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 });
            clients[0].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 (GNSClientCommands client : clients) client.close();
    executor.shutdown();
    System.out.println(DelayProfiler.getStats());
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) 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)

Example 32 with GNSClientCommands

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

the class GNSQuickStart 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 a new client object
    GNSClientCommands client = new GNSClientCommands(null);
    System.out.println("Client connected to GNS");
    // Retrive the GUID using the account id
    String guid = client.lookupGuid(accountId);
    System.out.println("Retrieved GUID for " + accountId + ": " + guid);
    // Get the public key from the GNS
    PublicKey publicKey = client.publicKeyLookupFromGuid(guid);
    System.out.println("Retrieved public key: " + publicKey.toString());
    // Load the private key from a file
    PrivateKey privateKey = KeyPairUtils.getPrivateKeyFromPKCS8File(privateKeyFile);
    System.out.println("Retrieved private key: " + DatatypeConverter.printHexBinary(privateKey.getEncoded()));
    // Create a GuidEntry
    GuidEntry accountGuid = new GuidEntry(accountId, guid, publicKey, privateKey);
    System.out.println("Created GUID entry: " + accountGuid.toString());
    // Use the GuidEntry create a new record in the GNS
    client.fieldCreateOneElementList(accountGuid, "homestate", "Florida");
    System.out.println("Added location -> Florida record to the GNS for GUID " + accountGuid.getGuid());
    // Retrieve that record from the GNS
    String result = client.fieldReadArrayFirstElement(accountGuid.getGuid(), "homestate", accountGuid);
    System.out.println("Result of read location: " + result);
    // Update the value of the field
    client.fieldReplace(accountGuid, "homestate", "Massachusetts");
    System.out.println("Changed location -> Massachusetts in the GNS for GUID " + accountGuid.getGuid());
    // Retrieve that record from the GNS again
    result = client.fieldReadArrayFirstElement(accountGuid.getGuid(), "homestate", accountGuid);
    System.out.println("Result of read location: " + result);
    System.exit(0);
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 33 with GNSClientCommands

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

the class GNSClientCapacityTest method setupClientsAndGuids.

private static void setupClientsAndGuids() throws Exception {
    clients = new GNSClientCommands[numClients];
    executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(numClients);
    for (int i = 0; i < numClients; i++) clients[i] = new GNSClientCommands();
    String gnsInstance = clients[0].getGNSProvider();
    accountGuidEntries = new GuidEntry[numAccountGuids];
    int numPreExisting = 0;
    for (int i = 0; i < numAccountGuids; i++) {
        log.log(Level.FINE, "Creating account GUID {0}", new Object[] { accountGUIDPrefix + i });
        try {
            accountGuidEntries[i] = GuidUtils.lookupOrCreateAccountGuid(clients[0], accountGUIDPrefix + i, PASSWORD);
            log.log(Level.FINE, "Created account {0}", new Object[] { accountGuidEntries[i] });
            assert (accountGuidEntries[i].getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, accountGUIDPrefix + i).getGuid()));
        } catch (DuplicateNameException e) {
            numPreExisting++;
            accountGuidEntries[i] = KeyPairUtils.getGuidEntry(gnsInstance, accountGUIDPrefix + i);
            log.log(Level.INFO, "Found that account {0} already exists", new Object[] { accountGuidEntries[i] });
        }
    // any other exceptions should be thrown up
    }
    System.out.println("Created (" + (numAccountGuids - numPreExisting) + ") or found pre-existing (" + numPreExisting + ") a total of " + numAccountGuids + " account GUIDs: " + Arrays.asList(accountGuidEntries));
    if (accountGuidsOnly) {
        for (int i = 0; i < accountGuidEntries.length; i++) guidEntries[i] = accountGuidEntries[i];
        return;
    }
    guidEntries = new GuidEntry[numGuids];
    Set<String> subGuids = new HashSet<String>();
    for (int i = 0, j = 0; i < numGuids; i++) {
        subGuids.add(Config.getGlobalString(TC.TEST_GUID_PREFIX) + i);
        if (subGuids.size() == numGuidsPerAccount || i == numGuids - 1) {
            // because batch creation seems buggy
            if (subGuids.size() == 1) {
                String subGuid = subGuids.iterator().next();
                try {
                    GuidEntry created = GuidUtils.lookupOrCreateGuid(clients[0], accountGuidEntries[i / numGuidsPerAccount], subGuid);
                    assert (created.getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, subGuid).getGuid()));
                } catch (DuplicateNameException de) {
                // ignore, will retrieve it locally below
                }
            // any other exceptions should be thrown up
            } else
                try {
                    // batch create
                    clients[0].guidBatchCreate(accountGuidEntries[i / numGuidsPerAccount], subGuids);
                } catch (Exception e) {
                    for (String subGuid : subGuids) {
                        try {
                            clients[0].guidCreate(accountGuidEntries[i / numGuidsPerAccount], subGuid);
                        } catch (DuplicateNameException de) {
                        // ignore, will retrieve it locally below
                        }
                    // any other exception should be throw up
                    }
                }
            for (String subGuid : subGuids) {
                guidEntries[j++] = KeyPairUtils.getGuidEntry(gnsInstance, subGuid);
            }
            log.log(Level.FINE, "Created sub-guid(s) {0}", new Object[] { subGuids });
            subGuids.clear();
        }
    }
    for (GuidEntry guidEntry : accountGuidEntries) assert (guidEntry != null);
    for (GuidEntry guidEntry : guidEntries) assert (guidEntry != null);
    System.out.println("Created or found " + guidEntries.length + " pre-existing sub-guids " + Arrays.asList(guidEntries));
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) 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) HashSet(java.util.HashSet)

Example 34 with GNSClientCommands

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

the class SetupChainCode method main.

/**
	 * @param args
	 * @throws Exception
	 */
public static void main(String[] args) throws Exception {
    int depth = 1;
    if (System.getProperty("depth") != null) {
        depth = Integer.parseInt(System.getProperty("depth"));
    }
    String codeFile = "scripts/activeCode/depth.js";
    if (System.getProperty("codeFile") != null) {
        codeFile = System.getProperty("codeFile");
    }
    boolean isRead = true;
    if (System.getProperty("isRead") != null) {
        isRead = Boolean.parseBoolean(System.getProperty("isRead"));
    }
    int numChains = 1;
    if (System.getProperty("numChains") != null) {
        numChains = Integer.parseInt(System.getProperty("numChains"));
    }
    String code = new String(Files.readAllBytes(Paths.get(codeFile)));
    client = new GNSClientCommands();
    for (int j = 0; j < numChains; j++) {
        entries = new GuidEntry[depth];
        for (int i = 0; i < depth; i++) {
            entries[i] = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_GUID_PREFIX + (j * 1000 + i), PASSWORD);
        }
        String[] nextGuid = new String[depth];
        for (int i = 0; i < depth - 1; i++) {
            nextGuid[i] = entries[i + 1].getGuid();
        }
        nextGuid[depth - 1] = successResult;
        for (int i = 0; i < depth; i++) {
            if (i == 0)
                client.activeCodeClear(entries[i].getGuid(), ActiveCode.WRITE_ACTION, entries[i]);
            client.activeCodeClear(entries[i].getGuid(), ActiveCode.READ_ACTION, entries[i]);
        }
        for (int i = 0; i < depth; i++) {
            client.fieldUpdate(entries[i], targetGuidField, nextGuid[i]);
        }
        Thread.sleep(1000);
        for (int i = 0; i < depth; i++) {
            if (!isRead && i == 0)
                client.activeCodeSet(entries[i].getGuid(), ActiveCode.WRITE_ACTION, code, entries[i]);
            else
                client.activeCodeSet(entries[i].getGuid(), ActiveCode.READ_ACTION, code, entries[i]);
        }
        Thread.sleep(1000);
        for (int i = 0; i < depth; i++) {
            String response = client.fieldRead(entries[i], targetGuidField);
            //assert(response.equals(successResult));
            System.out.println("Response is " + response + ", succeeds for " + entries[i].getEntityName() + "(" + entries[i].getGuid() + ")");
            Thread.sleep(1000);
        }
        System.out.println("Depth query code chain has been successfully set up!");
        // save all guids
        for (int i = 0; i < depth; i++) {
            ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(new File("guid" + (j * 1000 + i))));
            entries[i].writeObject(output);
            output.flush();
            output.close();
        }
    }
    System.exit(0);
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) FileOutputStream(java.io.FileOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File)

Example 35 with GNSClientCommands

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

the class GuidEntry method main.

// Test code
/**
 * @param args
 * @throws IOException
 * @throws Exception
 */
public static void main(String[] args) throws IOException, Exception {
    String name = "testGuid@gigapaxos.net";
    String password = "123";
    String file_name = "guid";
    GNSClientCommands client = new GNSClientCommands();
    GuidEntry guidEntry = client.accountGuidCreate(name, password);
    FileOutputStream fos = new FileOutputStream(file_name);
    ObjectOutputStream os = new ObjectOutputStream(fos);
    guidEntry.writeObject(os);
    // Important to flush
    os.flush();
    FileInputStream fis = new FileInputStream(file_name);
    ObjectInputStream ois = new ObjectInputStream(fis);
    GuidEntry newEntry = new GuidEntry(ois);
    System.out.println(newEntry);
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) FileOutputStream(java.io.FileOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

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