Search in sources :

Example 66 with GNSClientCommands

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

the class FieldSet method parse.

@Override
public void parse(String commandText) throws Exception {
    GNSClientCommands gnsClient = module.getGnsClient();
    try {
        StringTokenizer st = new StringTokenizer(commandText.trim());
        String guid;
        switch(st.countTokens()) {
            case 3:
                guid = module.getCurrentGuid().getGuid();
                break;
            case 4:
                guid = st.nextToken();
                if (!StringUtil.isValidGuidString(guid)) {
                    // We probably have an alias, lookup the GUID
                    guid = gnsClient.lookupGuid(guid);
                }
                break;
            default:
                wrongArguments();
                return;
        }
        String indexStr = st.nextToken();
        int index;
        try {
            index = Integer.valueOf(indexStr);
        } catch (Exception e) {
            index = -1;
        }
        if (index < 0) {
            console.printString("Invalid index value.\n");
            return;
        }
        String field = st.nextToken();
        String value = st.nextToken();
        gnsClient.fieldSetElement(guid, field, value, index, module.getCurrentGuid());
        console.printString("Value '" + value + "' written at index " + index + " of field " + field + " for GUID " + guid);
        console.printNewline();
    } catch (IOException | ClientException e) {
        console.printString("Failed to access GNS ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) IOException(java.io.IOException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException)

Example 67 with GNSClientCommands

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

the class FieldWriteList method parse.

@Override
public void parse(String commandText) throws Exception {
    GNSClientCommands gnsClient = module.getGnsClient();
    try {
        StringTokenizer st = new StringTokenizer(commandText.trim());
        String guid;
        switch(st.countTokens()) {
            case 2:
                guid = module.getCurrentGuid().getGuid();
                break;
            case 3:
                guid = st.nextToken();
                if (!StringUtil.isValidGuidString(guid)) {
                    // We probably have an alias, lookup the GUID
                    guid = gnsClient.lookupGuid(guid);
                }
                break;
            default:
                wrongArguments();
                return;
        }
        String field = st.nextToken();
        String value = st.nextToken();
        gnsClient.fieldReplaceOrCreateList(guid, field, new JSONArray().put(value), module.getCurrentGuid());
        console.printString("Value '" + value + "' written to field " + field + " for GUID " + guid);
        console.printNewline();
    } catch (IOException | ClientException e) {
        console.printString("Failed to access GNS ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) JSONArray(org.json.JSONArray) IOException(java.io.IOException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException)

Example 68 with GNSClientCommands

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

the class AdminBadAuthTest 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.");
    }
    badClient = new BadClient(ReconfigurationConfig.getReconfiguratorAddresses());
    badClient.setForceCoordinatedReads(true);
    connected = badClient instanceof GNSClient;
    if (connected) {
        System.out.println("BadClient created and connected to server.");
    }
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) GNSClient(edu.umass.cs.gnsclient.client.GNSClient) BeforeClass(org.junit.BeforeClass)

Example 69 with GNSClientCommands

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

the class SubGuidDeletesTest method cleanup.

/**
   * @throws Exception
   */
@After
public void cleanup() throws Exception {
    // sleep doesn't seem to help
    Thread.sleep(2000);
    assert (clients != null && clients[0] != null);
    System.out.println("About to delete " + guidEntries.length + " sub-guids: " + Arrays.asList(guidEntries));
    for (GuidEntry guidEntry : guidEntries) {
        try {
            LOGGER.log(Level.INFO, "{0} about to delete sub-guid {1}", new Object[] { this, guidEntry });
            clients[0].guidRemove(guidEntry);
            LOGGER.log(Level.INFO, "{0} deleted sub-guid {1}", new Object[] { this, guidEntry });
        } catch (ClientException | IOException e) {
            LOGGER.log(Level.WARNING, "{0} failed to delete sub-guid {1}", new Object[] { this, guidEntry });
            e.printStackTrace();
        // continue with rest
        }
    }
    System.out.println("About to delete " + accountGuidEntries.length + " account guids: " + Arrays.asList(accountGuidEntries));
    for (GuidEntry accGuidEntry : accountGuidEntries) {
        try {
            clients[0].accountGuidRemove(accGuidEntry);
            LOGGER.log(Level.FINE, "{0} deleted account guid {1}", new Object[] { this, accGuidEntry });
        } catch (ClientException | IOException e) {
            LOGGER.log(Level.WARNING, "{0} failed to delete account guid {1}", new Object[] { this, accGuidEntry });
            e.printStackTrace();
        // continue with rest
        }
    }
    for (GNSClientCommands clientCommands : clients) {
        clientCommands.close();
    }
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) After(org.junit.After)

Example 70 with GNSClientCommands

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

the class SubGuidDeletesTest method setupClientsAndGuids.

private void setupClientsAndGuids() throws Exception {
    clients = new GNSClientCommands[numClients];
    for (int i = 0; i < numClients; i++) {
        clients[i] = new GNSClientCommands();
    }
    String gnsInstance = GNSClientCommands.getGNSProvider();
    accountGuidEntries = new GuidEntry[numAccountGuids];
    for (int i = 0; i < numAccountGuids; i++) {
        LOGGER.log(Level.FINE, "{0} creating account GUID {1}", new Object[] { this, ACCOUNT_GUID_PREFIX + i });
        try {
            accountGuidEntries[i] = clients[0].accountGuidCreate(ACCOUNT_GUID_PREFIX + i, PASSWORD);
            LOGGER.log(Level.FINE, "{0} created account ", new Object[] { this, accountGuidEntries[i] });
            assert (accountGuidEntries[i].getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, ACCOUNT_GUID_PREFIX + i).getGuid()));
        } catch (DuplicateNameException e) {
            accountGuidEntries[i] = KeyPairUtils.getGuidEntry(gnsInstance, ACCOUNT_GUID_PREFIX + i);
            LOGGER.log(Level.INFO, "{0} found that account {1} already exists", new Object[] { this, accountGuidEntries[i] });
        }
    // any other exception should be throw up
    }
    System.out.println("Created or found pre-existing " + numAccountGuids + " account GUIDs: " + Arrays.asList(accountGuidEntries));
    guidEntries = new GuidEntry[numGuids];
    if (accountGuidsOnly) {
        System.arraycopy(accountGuidEntries, 0, guidEntries, 0, accountGuidEntries.length);
        return;
    }
    Set<String> subGuids = new HashSet<>();
    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 = clients[0].guidCreate(accountGuidEntries[i / numGuidsPerAccount], subGuid);
                    assert (created.getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, subGuid).getGuid()));
                } catch (DuplicateNameException de) {
                // ignore, will retrieve it locally below
                }
            // any other exception should be throw up
            } else {
                try {
                    // batch create
                    clients[0].guidBatchCreate(accountGuidEntries[i / numGuidsPerAccount], subGuids);
                } catch (ClientException | IOException 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);
            }
            LOGGER.log(Level.FINE, "{0} created sub-guid(s) {1}", new Object[] { this, 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) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) HashSet(java.util.HashSet)

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