Search in sources :

Example 1 with ContextServiceClient

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);
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ContextServiceClient(edu.umass.cs.contextservice.client.ContextServiceClient)

Example 2 with ContextServiceClient

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);
        }
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) RandomString(edu.umass.cs.gnscommon.utils.RandomString) ContextServiceClient(edu.umass.cs.contextservice.client.ContextServiceClient) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) DefaultGNSTest(edu.umass.cs.gnsserver.utils.DefaultGNSTest) Test(org.junit.Test) Repeat(edu.umass.cs.utils.Repeat)

Example 3 with ContextServiceClient

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);
        }
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) RandomString(edu.umass.cs.gnscommon.utils.RandomString) ContextServiceClient(edu.umass.cs.contextservice.client.ContextServiceClient) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) Test(org.junit.Test) DefaultGNSTest(edu.umass.cs.gnsserver.utils.DefaultGNSTest) Repeat(edu.umass.cs.utils.Repeat)

Aggregations

ContextServiceClient (edu.umass.cs.contextservice.client.ContextServiceClient)3 JSONArray (org.json.JSONArray)3 JSONObject (org.json.JSONObject)3 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)2 RandomString (edu.umass.cs.gnscommon.utils.RandomString)2 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)2 Repeat (edu.umass.cs.utils.Repeat)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 GNSClientCommands (edu.umass.cs.gnsclient.client.GNSClientCommands)1 EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)1 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException)1 FileNotFoundException (java.io.FileNotFoundException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 JSONException (org.json.JSONException)1