Search in sources :

Example 26 with GuidEntry

use of edu.umass.cs.gnsclient.client.util.GuidEntry 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 27 with GuidEntry

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

the class CapacityTestForLatencyClient method sequential_latency_test.

/**
	 * @throws InterruptedException
	 * @throws IOException 
	 * @throws EncryptionException 
	 */
public static void sequential_latency_test() throws InterruptedException, IOException, EncryptionException {
    guidIndex = Integer.parseInt(System.getProperty("guidIndex"));
    numGuids = Integer.parseInt(System.getProperty("numGuids"));
    entries = new GuidEntry[numGuids];
    for (int i = 0; i < numGuids; i++) {
        ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File("guid" + (i * 1000 + guidIndex))));
        entries[i] = new GuidEntry(input);
        input.close();
    }
    System.out.println("Start running experiment for " + (withSignature ? "signed" : "unsigned") + " " + (isRead ? "read" : "write"));
    executor.execute(new GNSClientTask(clients[0], entries, ((Integer) RATE).doubleValue(), TOTAL));
    while (getRcvd() < TOTAL) {
        System.out.println("Client received " + received + " responses, " + (TOTAL - received) + " left.");
        Thread.sleep(1000);
    }
    System.out.println("Received all responses!");
    executor.shutdown();
}
Also used : File(java.io.File) FileInputStream(java.io.FileInputStream) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) ObjectInputStream(java.io.ObjectInputStream)

Example 28 with GuidEntry

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

the class CapacityTestForLatencyClient method setup.

/**
	 * @throws Exception
	 */
public static void setup() throws Exception {
    numClients = 10;
    if (System.getProperty("numClients") != null) {
        numClients = Integer.parseInt(System.getProperty("numClients"));
    }
    System.out.println("There are " + numClients + " clients.");
    someField = "someField";
    if (System.getProperty("field") != null) {
        someField = System.getProperty("field");
    }
    withSignature = false;
    if (System.getProperty("withSigniture") != null) {
        withSignature = Boolean.parseBoolean(System.getProperty("withSigniture"));
    }
    RATE = 10;
    if (System.getProperty("rate") != null) {
        RATE = Integer.parseInt(System.getProperty("rate"));
    }
    TOTAL = RATE * DURATION / 1000;
    EXTRA_WAIT_TIME = 0;
    if (System.getProperty("extraTime") != null) {
        EXTRA_WAIT_TIME = 1000 * Integer.parseInt(System.getProperty("extraTime"));
    }
    isRead = true;
    if (System.getProperty("isRead") != null) {
        isRead = Boolean.parseBoolean(System.getProperty("isRead"));
    }
    if (System.getProperty("numThread") != null) {
        NUM_THREAD = Integer.parseInt(System.getProperty("numThread"));
    }
    if (System.getProperty("sequential") != null) {
        sequential = Boolean.parseBoolean(System.getProperty("sequential"));
    }
    String keyFile = "guid";
    if (System.getProperty("keyFile") != null) {
        keyFile = System.getProperty("keyFile");
    }
    ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File(keyFile)));
    entry = new GuidEntry(input);
    assert (entry != null);
    executor = Executors.newFixedThreadPool(NUM_THREAD);
    clients = new GNSClientCommands[numClients];
    for (int i = 0; i < numClients; i++) {
        clients[i] = new GNSClientCommands();
    }
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) File(java.io.File) FileInputStream(java.io.FileInputStream) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) ObjectInputStream(java.io.ObjectInputStream)

Example 29 with GuidEntry

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

the class CapacityTestForThruputClient method setup.

/**
	 * @throws Exception
	 */
public static void setup() throws Exception {
    numClients = 10;
    if (System.getProperty("numClients") != null) {
        numClients = Integer.parseInt(System.getProperty("numClients"));
    }
    someField = "someField";
    if (System.getProperty("field") != null) {
        someField = System.getProperty("field");
    }
    withMalicious = false;
    if (System.getProperty("withMalicious") != null) {
        withMalicious = Boolean.parseBoolean(System.getProperty("withMalicious"));
    }
    withSignature = false;
    if (System.getProperty("withSigniture") != null) {
        withSignature = Boolean.parseBoolean(System.getProperty("withSigniture"));
    }
    isRead = true;
    if (System.getProperty("isRead") != null) {
        isRead = Boolean.parseBoolean(System.getProperty("isRead"));
    }
    if (System.getProperty("numThread") != null) {
        NUM_THREAD = Integer.parseInt(System.getProperty("numThread"));
    }
    String keyFile = "guid";
    if (System.getProperty("keyFile") != null) {
        keyFile = System.getProperty("keyFile");
    }
    ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File(keyFile)));
    entry = new GuidEntry(input);
    input.close();
    assert (entry != null);
    String malKeyFile = "mal_guid";
    if (System.getProperty("malKeyFile") != null) {
        malKeyFile = System.getProperty("malKeyFile");
    }
    if (new File(malKeyFile).exists()) {
        input = new ObjectInputStream(new FileInputStream(new File(malKeyFile)));
        malEntry = new GuidEntry(input);
    }
    fraction = 0.0;
    if (System.getProperty("fraction") != null) {
        fraction = Double.parseDouble(System.getProperty("fraction"));
    }
    ratio = 0.0;
    if (System.getProperty("ratio") != null) {
        ratio = Double.parseDouble(System.getProperty("ratio"));
    }
    executor = Executors.newFixedThreadPool(NUM_THREAD);
    clients = new GNSClientCommands[numClients];
    for (int i = 0; i < numClients; i++) {
        clients[i] = new GNSClientCommands();
    }
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) File(java.io.File) FileInputStream(java.io.FileInputStream) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) ObjectInputStream(java.io.ObjectInputStream)

Example 30 with GuidEntry

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

the class GNSClientCapacityTest method setupClientsAndGuids.

private static void setupClientsAndGuids() throws Exception {
    clients = new GNSClient[numClients];
    executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(50 * numClients);
    for (int i = 0; i < numClients; i++) clients[i] = new GNSClientCommands();
    @SuppressWarnings("deprecation") String gnsInstance = GNSClient.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
    }
    accessor = GuidUtils.lookupOrCreateAccountGuid(clients[0], accountGUIDPrefix + "_accessor", PASSWORD);
    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(accountGUIDPrefix + 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].execute(GNSCommand.batchCreateGUIDs(accountGuidEntries[i / numGuidsPerAccount], subGuids));
                } catch (Exception e) {
                    for (String subGuid : subGuids) {
                        try {
                            clients[0].execute(GNSCommand.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)

Aggregations

GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)134 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)87 IOException (java.io.IOException)85 Test (org.junit.Test)69 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)61 RandomString (edu.umass.cs.gnscommon.utils.RandomString)54 BasicGuidEntry (edu.umass.cs.gnsclient.client.util.BasicGuidEntry)46 JSONException (org.json.JSONException)45 JSONArray (org.json.JSONArray)36 GNSClientCommands (edu.umass.cs.gnsclient.client.GNSClientCommands)28 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)18 EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)15 DuplicateNameException (edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException)14 StringTokenizer (java.util.StringTokenizer)13 JSONObject (org.json.JSONObject)12 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException)11 FileNotFoundException (java.io.FileNotFoundException)11 HashSet (java.util.HashSet)10 FileInputStream (java.io.FileInputStream)8 Repeat (edu.umass.cs.utils.Repeat)7