use of edu.umass.cs.contextservice.client.ContextServiceClient in project GNS by MobilityFirst.
the class ContextServiceExample method main.
/**
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String csHost = args[0];
int csPort = Integer.parseInt(args[1]);
client = new GNSClientCommands();
System.out.println("[Client connected to GNS]\n");
try {
/**
* Create an account GUID if one doesn't already exists. The true
* flag makes it verbosely print out what it is doing. The password
* is for future use and is needed mainly if the keypair is
* generated on the server in order to retrieve the private key.
* lookupOrCreateAccountGuid "cheats" by bypassing email-based or
* other verification mechanisms using a shared secret between the
* server and the client.
*
*/
System.out.println("// account GUID creation\n" + "GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS," + " \"password\", true)\n");
account_guid = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS, "password", true);
} catch (Exception | Error e) {
System.out.println("Exception during accountGuid creation: " + e);
e.printStackTrace();
System.exit(1);
}
System.out.println("csHost " + csHost + " csPort " + csPort);
csClient = new ContextServiceClient(csHost, csPort, false, PrivacySchemes.NO_PRIVACY);
String csSearchQuery = "latitude >= 32 AND latitude <= 33 AND " + "longitude >= -98 AND longitude <= -97";
System.out.println("\n Issuing a search query to context service " + csSearchQuery + "\n");
JSONArray resultArray = new JSONArray();
int replySize = csClient.sendSearchQuery(csSearchQuery, resultArray, QUERY_EXPIRY_TIME);
System.out.println("Search query reply size " + replySize + " GUIDs " + resultArray + "\n");
JSONObject userJSON = new JSONObject();
// user setting location inside the search query area.
userJSON.put("latitude", 32.5);
userJSON.put("longitude", -97.5);
System.out.println("A user with GUID " + account_guid.getGuid() + " entering the geofence specified in the search query by doing an update" + ", latitude=32.5 and longitude=-97.5, in GNS\n");
client.update(account_guid, userJSON);
// checking for trigger from context service that a new guid is added in an
// already existing group. This call blocks until there are non zero triggers.
JSONArray triggerArray = new JSONArray();
csClient.getQueryUpdateTriggers(triggerArray);
System.out.println("Context service sends a trigger showing that user with GUID " + account_guid.getGuid() + " enters the geofence, the geofence's group GUID is in the TO_BE_ADDED field\n");
for (int i = 0; i < triggerArray.length(); i++) {
System.out.println("Trigger JSON " + triggerArray.getJSONObject(i));
}
userJSON = new JSONObject();
// user setting location inside the search query area.
userJSON.put("latitude", 31);
userJSON.put("longitude", -97.5);
System.out.println("\n A user with guid" + account_guid.getGuid() + " going out of the geofence specified in the search query by doing an update, latitude=31 and longitude=-97.5, in GNS\n");
client.update(account_guid, userJSON);
// checking for trigger from context service that the guid is removed from an
// already existing group. This call blocks until there are non zero triggers.
triggerArray = new JSONArray();
csClient.getQueryUpdateTriggers(triggerArray);
System.out.println("Context service sends a trigger showing that user with GUID " + account_guid.getGuid() + " removed from the geofence, the geofence's group GUID is in TO_BE_REMOVED field ");
for (int i = 0; i < triggerArray.length(); i++) {
System.out.println("Trigger JSON " + triggerArray.getJSONObject(i));
}
System.out.println("\nBasic GNS CS test successful\n");
System.exit(0);
}
use of edu.umass.cs.contextservice.client.ContextServiceClient in project GNS by MobilityFirst.
the class ServerIntegrationTest method test_620_contextServiceTest.
/**
* Test to check context service triggers.
*/
// these two attributes right now are supported by CS
@Test
@Repeat(times = REPEAT)
public void test_620_contextServiceTest() {
// run it only when CS is enabled
// to check if context service is enabled.
boolean enableContextService = Config.getGlobalBoolean(GNSConfig.GNSC.ENABLE_CNS);
String csIPPort = Config.getGlobalString(GNSConfig.GNSC.CNS_NODE_ADDRESS);
if (enableContextService) {
try {
JSONObject attrValJSON = new JSONObject();
attrValJSON.put("geoLocationCurrentLat", 42.466);
attrValJSON.put("geoLocationCurrentLong", -72.58);
clientCommands.update(masterGuid, attrValJSON);
// just wait for 2 sec before sending search
Thread.sleep(1000);
String[] parsed = csIPPort.split(":");
String csIP = parsed[0];
int csPort = Integer.parseInt(parsed[1]);
ContextServiceClient csClient = new ContextServiceClient(csIP, csPort, false, PrivacySchemes.NO_PRIVACY);
// context service query format
String query = "geoLocationCurrentLat >= 40 " + "AND geoLocationCurrentLat <= 50 AND " + "geoLocationCurrentLong >= -80 AND " + "geoLocationCurrentLong <= -70";
JSONArray resultArray = new JSONArray();
// third argument is arbitrary expiry time, not used now
int resultSize = csClient.sendSearchQuery(query, resultArray, 300000);
Assert.assertThat(resultSize, Matchers.greaterThanOrEqualTo(1));
} catch (Exception e) {
failWithStackTrace("Exception during contextServiceTest: ", e);
}
}
}
use of edu.umass.cs.contextservice.client.ContextServiceClient in project GNS by MobilityFirst.
the class ContextServiceTest method test_620_contextServiceTest.
/**
* Test to check context service triggers.
*/
// these two attributes right now are supported by CS
@Test
@Repeat(times = REPEAT)
public void test_620_contextServiceTest() {
// run it only when CS is enabled
// to check if context service is enabled.
boolean enableContextService = Config.getGlobalBoolean(GNSConfig.GNSC.ENABLE_CNS);
String csIPPort = Config.getGlobalString(GNSConfig.GNSC.CNS_NODE_ADDRESS);
if (enableContextService) {
try {
JSONObject attrValJSON = new JSONObject();
attrValJSON.put("geoLocationCurrentLat", 42.466);
attrValJSON.put("geoLocationCurrentLong", -72.58);
clientCommands.update(masterGuid, attrValJSON);
// just wait for 2 sec before sending search
Thread.sleep(1000);
String[] parsed = csIPPort.split(":");
String csIP = parsed[0];
int csPort = Integer.parseInt(parsed[1]);
ContextServiceClient csClient = new ContextServiceClient(csIP, csPort, false, PrivacySchemes.NO_PRIVACY);
// context service query format
String query = "geoLocationCurrentLat >= 40 " + "AND geoLocationCurrentLat <= 50 AND " + "geoLocationCurrentLong >= -80 AND " + "geoLocationCurrentLong <= -70";
JSONArray resultArray = new JSONArray();
// third argument is arbitrary expiry time, not used now
int resultSize = csClient.sendSearchQuery(query, resultArray, 300000);
Assert.assertThat(resultSize, Matchers.greaterThanOrEqualTo(1));
} catch (Exception e) {
Utils.failWithStackTrace("Exception during contextServiceTest: ", e);
}
}
}
Aggregations