Search in sources :

Example 16 with MatchingEngine

use of com.mobiledgex.matchingengine.MatchingEngine in project edge-cloud-sampleapps by mobiledgex.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /**
     * MatchingEngine APIs require special user approved permissions to READ_PHONE_STATE and
     * one of the following:
     * ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION.
     *
     * The example RequestPermissions utility creates a UI dialog, if needed.
     *
     * You can do this anywhere, MainApplication.onActivityResumed(), or a subset of permissions
     * onResume() on each Activity.
     *
     * Permissions must exist prior to API usage to avoid SecurityExceptions.
     */
    mRpUtil = new RequestPermissions();
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    mLocationRequest = new LocationRequest();
    // Restore app's matching engine location preference, defaulting to false:
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    PreferenceManager.setDefaultValues(this, R.xml.location_preferences, false);
    boolean matchingEngineLocationAllowed = prefs.getBoolean(getResources().getString(R.string.preference_matching_engine_location_verification), false);
    // ! [matchingengine_allow_location_usage_gdpr]
    MatchingEngine.setMatchingEngineLocationAllowed(matchingEngineLocationAllowed);
    // ! [matchingengine_allow_location_usage_gdpr]
    // Watch allowed preference:
    prefs.registerOnSharedPreferenceChangeListener(this);
    // Client side FusedLocation updates.
    mDoLocationUpdates = true;
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    automationCloudlet1.setLatitude(50.110922);
    automationCloudlet1.setLongitude(8.682127);
    automationCloudlet2.setLatitude(53.5511);
    automationCloudlet2.setLongitude(9.9937);
    // ! [basic_location_handler_example]
    mLocationCallback = new LocationCallback() {

        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                return;
            }
            String clientLocText = "";
            mLastLocationResult = locationResult;
            // Post into edgeEvents updater:
            me.getEdgeEventsConnectionFuture().thenApply(connection -> {
                if (connection != null) {
                    connection.postLocationUpdate(locationResult.getLastLocation());
                }
                return null;
            });
            for (Location location : locationResult.getLocations()) {
                // Update UI with client location data
                clientLocText += "[" + location.toString() + "]";
            }
            TextView tv = findViewById(R.id.client_location_content);
            tv.setText(clientLocText);
        }
    };
    // ! [basic_location_handler_example]
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            doEnhancedLocationVerification();
        }
    });
    Toolbar myToolbar = findViewById(R.id.toolbar);
    setSupportActionBar(myToolbar);
    // Open dialog for MobiledgeX MatchingEngine if this is the first time the app is created:
    String firstTimeUsePrefKey = getResources().getString(R.string.preference_first_time_use);
    boolean firstTimeUse = prefs.getBoolean(firstTimeUsePrefKey, true);
    if (firstTimeUse) {
        Intent intent = new Intent(this, FirstTimeUseActivity.class);
        startActivity(intent);
    }
    netTest = new NetTest();
}
Also used : FindCloudletEvent(com.mobiledgex.matchingengine.edgeeventsconfig.FindCloudletEvent) Bundle(android.os.Bundle) PackageManager(android.content.pm.PackageManager) NonNull(androidx.annotation.NonNull) LocationRequest(com.google.android.gms.location.LocationRequest) MatchingEngine(com.mobiledgex.matchingengine.MatchingEngine) Response(com.squareup.okhttp.Response) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) Future(java.util.concurrent.Future) EdgeEventsConnection(com.mobiledgex.matchingengine.EdgeEventsConnection) Locale(java.util.Locale) Looper(android.os.Looper) FloatingActionButton(com.google.android.material.floatingactionbutton.FloatingActionButton) View(android.view.View) NetTest(com.mobiledgex.matchingengine.performancemetrics.NetTest) Request(com.squareup.okhttp.Request) PreferenceManager(android.preference.PreferenceManager) CarrierConfigManager(android.telephony.CarrierConfigManager) Log(android.util.Log) LocationResult(com.google.android.gms.location.LocationResult) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) Executors(java.util.concurrent.Executors) List(java.util.List) TextView(android.widget.TextView) AppClient(distributed_match_engine.AppClient) Toolbar(androidx.appcompat.widget.Toolbar) Location(android.location.Location) MelMessaging(com.mobiledgex.mel.MelMessaging) LocationServices(com.google.android.gms.location.LocationServices) EdgeEventsConfig(com.mobiledgex.matchingengine.edgeeventsconfig.EdgeEventsConfig) Intent(android.content.Intent) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Task(com.google.android.gms.tasks.Task) MenuItem(android.view.MenuItem) Site(com.mobiledgex.matchingengine.performancemetrics.Site) Menu(android.view.Menu) OkHttpClient(com.squareup.okhttp.OkHttpClient) Subscribe(com.google.common.eventbus.Subscribe) SubscriptionInfo(android.telephony.SubscriptionInfo) ExecutorService(java.util.concurrent.ExecutorService) DmeDnsException(com.mobiledgex.matchingengine.DmeDnsException) LocationCallback(com.google.android.gms.location.LocationCallback) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) StatusRuntimeException(io.grpc.StatusRuntimeException) SharedPreferences(android.content.SharedPreferences) FusedLocationProviderClient(com.google.android.gms.location.FusedLocationProviderClient) NetworkRequestTimeoutException(com.mobiledgex.matchingengine.NetworkRequestTimeoutException) RequestPermissions(com.mobiledgex.matchingengine.util.RequestPermissions) Appcommon(distributed_match_engine.Appcommon) Resources(android.content.res.Resources) LocationRequest(com.google.android.gms.location.LocationRequest) SharedPreferences(android.content.SharedPreferences) NetTest(com.mobiledgex.matchingengine.performancemetrics.NetTest) LocationCallback(com.google.android.gms.location.LocationCallback) Intent(android.content.Intent) View(android.view.View) TextView(android.widget.TextView) LocationResult(com.google.android.gms.location.LocationResult) RequestPermissions(com.mobiledgex.matchingengine.util.RequestPermissions) FloatingActionButton(com.google.android.material.floatingactionbutton.FloatingActionButton) TextView(android.widget.TextView) Toolbar(androidx.appcompat.widget.Toolbar) Location(android.location.Location)

Example 17 with MatchingEngine

use of com.mobiledgex.matchingengine.MatchingEngine in project edge-cloud-sampleapps by mobiledgex.

the class MatchingEngineUnitTest method testGetQosPositionKpi.

@Test
public void testGetQosPositionKpi() {
    Context ctx = getInstrumentation().getTargetContext();
    MatchingEngine me = new MatchingEngine(ctx);
    me.setMatchingEngineLocationAllowed(true);
    me.setAllowSwitchIfNoSubscriberInfo(true);
    registerClient(me, ctx);
    LocOuterClass.Loc loc = LocOuterClass.Loc.newBuilder().setLongitude(latitude).setLatitude(longitude).build();
    List<AppClient.QosPosition> qosPositions = generateQosPositionList(loc, 45, 200, 1);
    try {
        AppClient.QosPositionRequest request = me.createDefaultQosPositionRequest(qosPositions, lteCategory, bandSelection).build();
        ChannelIterator<AppClient.QosPositionKpiReply> responseIterator = me.getQosPositionKpi(request, GRPC_TIMEOUT_MS);
        assertTrue("No QosPositionKpi reply", responseIterator.hasNext());
        while (responseIterator.hasNext()) {
            AppClient.QosPositionKpiReply reply = responseIterator.next();
            AppClient.ReplyStatus status = reply.getStatus();
            assertEquals("QosPositionKpi reply status is " + status, AppClient.ReplyStatus.RS_SUCCESS, status);
        }
    } catch (DmeDnsException dde) {
        assertTrue("ExecutionException getting app inst list. " + dde.getMessage(), false);
    } catch (ExecutionException ee) {
        assertTrue("ExecutionException getting app inst list. " + ee.getMessage(), false);
    } catch (InterruptedException ie) {
        assertTrue("InterruptedException getting app inst list. " + ie.getMessage(), false);
    }
}
Also used : Context(android.content.Context) LocOuterClass(distributed_match_engine.LocOuterClass) MatchingEngine(com.mobiledgex.matchingengine.MatchingEngine) AppClient(distributed_match_engine.AppClient) ExecutionException(java.util.concurrent.ExecutionException) DmeDnsException(com.mobiledgex.matchingengine.DmeDnsException) Test(org.junit.Test)

Example 18 with MatchingEngine

use of com.mobiledgex.matchingengine.MatchingEngine in project edge-cloud-sampleapps by mobiledgex.

the class MatchingEngineUnitTest method testVerifyLocation.

@Test
public void testVerifyLocation() {
    Context ctx = getInstrumentation().getTargetContext();
    MatchingEngine me = new MatchingEngine(ctx);
    me.setMatchingEngineLocationAllowed(true);
    me.setAllowSwitchIfNoSubscriberInfo(true);
    registerClient(me, ctx);
    Location location = new Location("MobiledgeX_Loc_Sim");
    location.setLatitude(latitude);
    location.setLongitude(longitude);
    try {
        AppClient.VerifyLocationRequest request = me.createDefaultVerifyLocationRequest(ctx, location).build();
        AppClient.VerifyLocationReply reply = me.verifyLocation(request, GRPC_TIMEOUT_MS);
        assertTrue("Unable to get VerifyLocation", reply != null);
        assertEquals("VerifyLocation Tower status is " + reply.getTowerStatus(), AppClient.VerifyLocationReply.TowerStatus.CONNECTED_TO_SPECIFIED_TOWER, reply.getTowerStatus());
        assertEquals("VerifyLocation GPS Location status is " + reply.getGpsLocationStatus(), AppClient.VerifyLocationReply.GPSLocationStatus.LOC_VERIFIED, reply.getGpsLocationStatus());
    } catch (DmeDnsException dde) {
        assertTrue("DmeDnsException verifying location. " + dde.getMessage(), false);
    } catch (ExecutionException ee) {
        assertTrue("ExecutionException verifying location. " + ee.getMessage(), false);
    } catch (InterruptedException ie) {
        assertTrue("InterruptedException verifying location. " + ie.getMessage(), false);
    } catch (IOException ioe) {
        assertTrue("IOException verifying location. " + ioe.getMessage(), false);
    }
}
Also used : Context(android.content.Context) AppClient(distributed_match_engine.AppClient) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) MatchingEngine(com.mobiledgex.matchingengine.MatchingEngine) Location(android.location.Location) DmeDnsException(com.mobiledgex.matchingengine.DmeDnsException) Test(org.junit.Test)

Example 19 with MatchingEngine

use of com.mobiledgex.matchingengine.MatchingEngine in project edge-cloud-sampleapps by mobiledgex.

the class EngineCallTest method verifyMockedLocationTest_NorthPole.

/**
 * Mocked Location test. Note that responsibility of verifying location is in the MatchingEngine
 * on the server side, not client side.
 */
@Test
public void verifyMockedLocationTest_NorthPole() {
    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    enableMockLocation(context, true);
    Location mockLoc = MockUtils.createLocation("verifyMockedLocationTest_NorthPole", 90d, 1d);
    MatchingEngine me = new MatchingEngine(context);
    me.setUseWifiOnly(useWifiOnly);
    me.setMatchingEngineLocationAllowed(true);
    me.setAllowSwitchIfNoSubscriberInfo(true);
    AppClient.VerifyLocationReply verifyLocationReply = null;
    try {
        Location location = getTestLocation(47.6062, 122.3321);
        String carrierName = me.retrieveNetworkCarrierName(context);
        registerClient(me);
        AppClient.VerifyLocationRequest verifyLocationRequest = me.createDefaultVerifyLocationRequest(context, location).setCarrierName(carrierName).build();
        if (useHostOverride) {
            verifyLocationReply = me.verifyLocation(verifyLocationRequest, hostOverride, portOverride, GRPC_TIMEOUT_MS);
        } else {
            verifyLocationReply = me.verifyLocation(verifyLocationRequest, GRPC_TIMEOUT_MS);
        }
        assert (verifyLocationReply != null);
    } catch (DmeDnsException dde) {
        Log.e(TAG, Log.getStackTraceString(dde));
        assertFalse("verifyMockedLocationTest_NorthPole: DmeDnsException", true);
    } catch (IOException ioe) {
        Log.e(TAG, Log.getStackTraceString(ioe));
        assertFalse("verifyMockedLocationTest_NorthPole: IOException!", true);
    } catch (ExecutionException ee) {
        Log.e(TAG, Log.getStackTraceString(ee));
        assertFalse("verifyMockedLocationTest_NorthPole: ExecutionException!", true);
    } catch (InterruptedException ie) {
        Log.e(TAG, Log.getStackTraceString(ie));
        assertFalse("verifyMockedLocationTest_NorthPole: InterruptedException!", true);
    }
    // Temporary.
    assertEquals(0, verifyLocationReply.getVer());
    assertEquals(AppClient.VerifyLocationReply.TowerStatus.TOWER_UNKNOWN, verifyLocationReply.getTowerStatus());
    // Based on test data.
    assertEquals(AppClient.VerifyLocationReply.GPSLocationStatus.LOC_ROAMING_COUNTRY_MATCH, verifyLocationReply.getGpsLocationStatus());
}
Also used : Context(android.content.Context) AppClient(distributed_match_engine.AppClient) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) MatchingEngine(com.mobiledgex.matchingengine.MatchingEngine) Location(android.location.Location) DmeDnsException(com.mobiledgex.matchingengine.DmeDnsException) Test(org.junit.Test)

Example 20 with MatchingEngine

use of com.mobiledgex.matchingengine.MatchingEngine in project edge-cloud-sampleapps by mobiledgex.

the class EngineCallTest method testRegisterAndFindCloudlet_001.

@Test
public void testRegisterAndFindCloudlet_001() {
    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    MatchingEngine me = new MatchingEngine(context);
    me.setUseWifiOnly(useWifiOnly);
    me.setMatchingEngineLocationAllowed(true);
    me.setAllowSwitchIfNoSubscriberInfo(true);
    AppConnectionManager appConnectionManager = me.getAppConnectionManager();
    AppClient.RegisterClientReply registerClientReply = null;
    String carrierName = "TDG";
    String organizationName = "MobiledgeX";
    String appName = "HttpEcho";
    String appVersion = "20191204";
    Socket socket = null;
    try {
        Location location = getTestLocation(47.6062, 122.3321);
        Future<AppClient.FindCloudletReply> findCloudletReplyFuture = me.registerAndFindCloudlet(context, hostOverride, portOverride, organizationName, appName, appVersion, location, "", 0, null, null, // FIXME: These parameters should be overloaded or optional.
        null);
        // Just wait:
        AppClient.FindCloudletReply findCloudletReply = findCloudletReplyFuture.get();
        HashMap<Integer, AppPort> appTcpPortMap = appConnectionManager.getTCPMap(findCloudletReply);
        AppPort appPort = appTcpPortMap.get(3001);
        // There should be at least one for a connection to be made.
        assertTrue(appPort != null);
        Future<Socket> socketFuture = me.getAppConnectionManager().getTcpSocket(findCloudletReply, appPort, appPort.getPublicPort(), (int) GRPC_TIMEOUT_MS);
        socket = socketFuture.get();
        assertTrue("FindCloudletReply failed!", findCloudletReply != null);
    } catch (ExecutionException ee) {
        Log.i(TAG, Log.getStackTraceString(ee));
        assertFalse("testRegisterAndFindCloudlet_001: ExecutionException! " + ee.getLocalizedMessage(), true);
    } catch (StatusRuntimeException sre) {
        Log.i(TAG, Log.getStackTraceString(sre));
        assertFalse("testRegisterAndFindCloudlet_001: StatusRuntimeException!", true);
    } catch (InterruptedException ie) {
        Log.i(TAG, Log.getStackTraceString(ie));
        assertFalse("testRegisterAndFindCloudlet_001: InterruptedException!", true);
    } finally {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        enableMockLocation(context, false);
    }
}
Also used : Context(android.content.Context) AppPort(distributed_match_engine.Appcommon.AppPort) IOException(java.io.IOException) MatchingEngine(com.mobiledgex.matchingengine.MatchingEngine) StatusRuntimeException(io.grpc.StatusRuntimeException) AppClient(distributed_match_engine.AppClient) ExecutionException(java.util.concurrent.ExecutionException) Socket(java.net.Socket) AppConnectionManager(com.mobiledgex.matchingengine.AppConnectionManager) Location(android.location.Location) Test(org.junit.Test)

Aggregations

MatchingEngine (com.mobiledgex.matchingengine.MatchingEngine)34 AppClient (distributed_match_engine.AppClient)30 Context (android.content.Context)29 DmeDnsException (com.mobiledgex.matchingengine.DmeDnsException)29 ExecutionException (java.util.concurrent.ExecutionException)29 Test (org.junit.Test)29 Location (android.location.Location)26 StatusRuntimeException (io.grpc.StatusRuntimeException)19 PackageManager (android.content.pm.PackageManager)16 IOException (java.io.IOException)8 AppConnectionManager (com.mobiledgex.matchingengine.AppConnectionManager)5 AppPort (distributed_match_engine.Appcommon.AppPort)4 OkHttpClient (com.squareup.okhttp.OkHttpClient)3 Request (com.squareup.okhttp.Request)3 Response (com.squareup.okhttp.Response)3 Appcommon (distributed_match_engine.Appcommon)3 Socket (java.net.Socket)3 DecodeException (com.auth0.android.jwt.DecodeException)2 MediaType (com.squareup.okhttp.MediaType)2 RequestBody (com.squareup.okhttp.RequestBody)2