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));
}
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.");
}
}
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);
}
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 >>>>>>>>>>");
}
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());
}
Aggregations