use of com.mobiledgex.matchingengine.DmeDnsException in project edge-cloud-sampleapps by mobiledgex.
the class FindCloudletTest method findCloudletTest.
@Test
public void findCloudletTest() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
AppClient.FindCloudletReply findCloudletReply1 = null;
AppClient.FindCloudletReply findCloudletReply2 = null;
MatchingEngine me = new MatchingEngine(context);
me.setUseWifiOnly(useWifiOnly);
me.setMatchingEngineLocationAllowed(true);
me.setAllowSwitchIfNoSubscriberInfo(true);
try {
Location location = getTestLocation(47.6062, 122.3321);
String carrierName = me.retrieveNetworkCarrierName(context);
registerClient(me);
// Set orgName and location, then override the rest for testing:
AppClient.FindCloudletRequest findCloudletRequest = me.createDefaultFindCloudletRequest(context, location).setCarrierName(findCloudletCarrierOverride).build();
if (useHostOverride) {
findCloudletReply1 = me.findCloudlet(findCloudletRequest, hostOverride, portOverride, GRPC_TIMEOUT_MS);
} else {
findCloudletReply1 = me.findCloudlet(findCloudletRequest, GRPC_TIMEOUT_MS);
}
// Second try:
me.setThreadedPerformanceTest(true);
if (useHostOverride) {
findCloudletReply2 = me.findCloudlet(findCloudletRequest, hostOverride, portOverride, GRPC_TIMEOUT_MS);
} else {
findCloudletReply2 = me.findCloudlet(findCloudletRequest, GRPC_TIMEOUT_MS);
}
} catch (DmeDnsException dde) {
Log.e(TAG, Log.getStackTraceString(dde));
assertFalse("FindCloudlet: DmeDnsException", true);
} catch (ExecutionException ee) {
Log.e(TAG, Log.getStackTraceString(ee));
assertFalse("FindCloudlet: ExecutionException!", true);
} catch (StatusRuntimeException sre) {
Log.e(TAG, sre.getMessage());
Log.e(TAG, Log.getStackTraceString(sre));
assertFalse("FindCloudlet: StatusRunTimeException!", true);
} catch (InterruptedException ie) {
Log.e(TAG, Log.getStackTraceString(ie));
assertFalse("FindCloudlet: InterruptedException!", true);
} catch (PackageManager.NameNotFoundException nnfe) {
Log.e(TAG, Log.getStackTraceString(nnfe));
assertFalse("FindCloudlet: PackageManager.NameNotFoundException!", true);
}
assertNotNull("FindCloudletReply1 is null!", findCloudletReply1);
assertNotNull("FindCloudletReply2 is null!", findCloudletReply2);
// Might also fail, since the network is not under test control:
assertEquals("App's expected test cloudlet FQDN doesn't match.", "sdkdemo-app-cluster.dusseldorf-main.tdg.mobiledgex.net", findCloudletReply1.getFqdn());
}
use of com.mobiledgex.matchingengine.DmeDnsException in project edge-cloud-sampleapps by mobiledgex.
the class RegisterClientTest method registerClientFutureTest.
@Test
public void registerClientFutureTest() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
MatchingEngine me = new MatchingEngine(context);
me.setUseWifiOnly(useWifiOnly);
me.setMatchingEngineLocationAllowed(true);
me.setAllowSwitchIfNoSubscriberInfo(true);
Future<AppClient.RegisterClientReply> registerReplyFuture;
AppClient.RegisterClientReply reply = null;
try {
Location location = getTestLocation(47.6062, 122.3321);
AppClient.RegisterClientRequest request = me.createDefaultRegisterClientRequest(context, organizationName).setAppName(applicationName).setAppVers(appVersion).setCellId(getCellId(context, me)).setUniqueIdType("applicationInstallId").setUniqueId(me.getUniqueId(context)).build();
if (useHostOverride) {
registerReplyFuture = me.registerClientFuture(request, hostOverride, portOverride, GRPC_TIMEOUT_MS);
} else {
registerReplyFuture = me.registerClientFuture(request, GRPC_TIMEOUT_MS);
}
reply = registerReplyFuture.get();
assert (reply != null);
} catch (PackageManager.NameNotFoundException nnfe) {
Log.e(TAG, Log.getStackTraceString(nnfe));
assertFalse("ExecutionException registering using PackageManager.", true);
} catch (DmeDnsException dde) {
Log.e(TAG, Log.getStackTraceString(dde));
assertFalse("ExecutionException registering client.", true);
} catch (ExecutionException ee) {
Log.e(TAG, Log.getStackTraceString(ee));
assertFalse("registerClientFutureTest: ExecutionException!", true);
} catch (InterruptedException ie) {
Log.e(TAG, Log.getStackTraceString(ie));
assertFalse("registerClientFutureTest: InterruptedException!", true);
}
// TODO: Validate JWT
Log.i(TAG, "registerClientFutureTest() response: " + reply.toString());
assertEquals(0, reply.getVer());
assertEquals(AppClient.ReplyStatus.RS_SUCCESS, reply.getStatus());
}
use of com.mobiledgex.matchingengine.DmeDnsException in project edge-cloud-sampleapps by mobiledgex.
the class RegisterClientTest method registerClientBadAppName.
@Test
public void registerClientBadAppName() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
MatchingEngine me = new MatchingEngine(context);
me.setUseWifiOnly(useWifiOnly);
me.setMatchingEngineLocationAllowed(true);
me.setAllowSwitchIfNoSubscriberInfo(true);
AppClient.RegisterClientReply reply = null;
try {
AppClient.RegisterClientRequest request = me.createDefaultRegisterClientRequest(context, organizationName).setAppName("Leon's Bogus App").setAppVers(appVersion).setCellId(getCellId(context, me)).build();
if (useHostOverride) {
reply = me.registerClient(request, hostOverride, portOverride, GRPC_TIMEOUT_MS);
} else {
reply = me.registerClient(request, me.generateDmeHostAddress(), me.getPort(), GRPC_TIMEOUT_MS);
}
} catch (PackageManager.NameNotFoundException nnfe) {
Log.e(TAG, Log.getStackTraceString(nnfe));
assertFalse("ExecutionException registering using PackageManager.", true);
} catch (DmeDnsException dde) {
Log.e(TAG, Log.getStackTraceString(dde));
assertFalse("registerClientTest: DmeDnsException!", true);
} catch (ExecutionException ee) {
Log.e(TAG, Log.getStackTraceString(ee));
assertFalse("registerClientTest: ExecutionException!", true);
} catch (StatusRuntimeException sre) {
Log.e(TAG, Log.getStackTraceString(sre));
// This is expected when appName is wrong.
assertEquals("NOT_FOUND: app not found", sre.getLocalizedMessage());
} catch (InterruptedException ie) {
Log.e(TAG, Log.getStackTraceString(ie));
assertFalse("registerClientTest: InterruptedException!", true);
}
}
use of com.mobiledgex.matchingengine.DmeDnsException in project edge-cloud-sampleapps by mobiledgex.
the class RegisterClientTest method registerClientTest.
@Test
public void registerClientTest() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
MatchingEngine me = new MatchingEngine(context);
me.setUseWifiOnly(useWifiOnly);
me.setMatchingEngineLocationAllowed(true);
me.setAllowSwitchIfNoSubscriberInfo(true);
AppClient.RegisterClientReply reply = null;
String appName = applicationName;
try {
Location location = getTestLocation(47.6062, 122.3321);
AppClient.RegisterClientRequest request = me.createDefaultRegisterClientRequest(context, organizationName).setAppName(applicationName).setAppVers(appVersion).setCellId(getCellId(context, me)).build();
if (useHostOverride) {
reply = me.registerClient(request, hostOverride, portOverride, GRPC_TIMEOUT_MS);
} else {
reply = me.registerClient(request, me.generateDmeHostAddress(), me.getPort(), GRPC_TIMEOUT_MS);
}
JWT jwt = null;
try {
jwt = new JWT(reply.getSessionCookie());
} catch (DecodeException e) {
Log.e(TAG, Log.getStackTraceString(e));
assertFalse("registerClientTest: DecodeException!", true);
}
// Validate JWT
// 10 seconds leeway
boolean isExpired = jwt.isExpired(10);
assertTrue(!isExpired);
Log.i(TAG, "Claims count: " + jwt.getClaims().keySet().size());
for (String key : jwt.getClaims().keySet()) {
Claim claim = jwt.getClaims().get(key);
Log.i(TAG, "key: " + key + " Claim: " + claim.asString());
}
Claim c = jwt.getClaim("key");
JsonObject claimJson = c.asObject(JsonObject.class);
String orgName = claimJson.get("orgname").getAsString();
assertEquals("orgname doesn't match!", "MobiledgeX", orgName);
Log.i(TAG, "registerReply.getSessionCookie()=" + reply.getSessionCookie());
assertTrue(reply != null);
assertTrue(reply.getStatus() == AppClient.ReplyStatus.RS_SUCCESS);
assertTrue(!reply.getUniqueId().isEmpty());
assertTrue(reply.getSessionCookie().length() > 0);
} catch (PackageManager.NameNotFoundException nnfe) {
Log.e(TAG, Log.getStackTraceString(nnfe));
assertFalse("ExecutionException registering using PackageManager.", true);
} catch (DmeDnsException dde) {
Log.e(TAG, Log.getStackTraceString(dde));
assertFalse("registerClientTest: DmeDnsException!", true);
} catch (ExecutionException ee) {
Log.e(TAG, Log.getStackTraceString(ee));
assertFalse("registerClientTest: ExecutionException!", true);
} catch (StatusRuntimeException sre) {
Log.e(TAG, Log.getStackTraceString(sre));
assertFalse("registerClientTest: StatusRuntimeException!", true);
} catch (InterruptedException ie) {
Log.e(TAG, Log.getStackTraceString(ie));
assertFalse("registerClientTest: InterruptedException!", true);
}
Log.i(TAG, "registerClientTest reply: " + reply.toString());
assertEquals(0, reply.getVer());
assertEquals(AppClient.ReplyStatus.RS_SUCCESS, reply.getStatus());
}
use of com.mobiledgex.matchingengine.DmeDnsException in project edge-cloud-sampleapps by mobiledgex.
the class MainActivity method runFlow.
private void runFlow(Task<Location> locationTask, AppCompatActivity ctx) {
Location location = locationTask.getResult();
if (location == null) {
Log.e(TAG, "Mising location. Cannot update.");
return;
}
// Location found. Create a request:
try {
someText = "";
// Switch entire process over to cellular for application use.
// mMatchingEngine.getNetworkManager().switchToCellularInternetNetworkBlocking();
// String adId = mMatchingEngine.GetHashedAdvertisingID(ctx);
// If no carrierName, or active Subscription networks, the app should use the public cloud instead.
List<SubscriptionInfo> subList = me.getActiveSubscriptionInfoList();
if (subList != null && subList.size() > 0) {
for (SubscriptionInfo info : subList) {
CharSequence carrierName = info.getCarrierName();
if (carrierName != null && carrierName.equals("Android")) {
someText += "Emulator Active Subscription Network: " + info.toString() + "\n";
} else {
someText += "Active Subscription network: " + info.toString() + "\n";
}
}
// mMatchingEngine.setNetworkSwitchingEnabled(true);
} else {
// This example will continue to execute anyway, as Demo DME may still be reachable to discover nearby edge cloudlets.
someText += "No active cellular networks: app should use public cloud instead of the edgecloudlet at this time.\n";
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
boolean locationVerificationAllowed = prefs.getBoolean(getResources().getString(R.string.preference_matching_engine_location_verification), false);
// String carrierName = mMatchingEngine.getCarrierName(ctx); // Regular use case
// Override carrierName
String carrierName = "";
if (carrierName == null) {
someText += "No carrier Info!\n";
}
// It's possible the Generated DME DNS host doesn't exist yet for your SIM.
String dmeHostAddress;
try {
dmeHostAddress = me.generateDmeHostAddress();
someText += "(e)SIM card based DME address: " + dmeHostAddress + "\n";
} catch (DmeDnsException dde) {
someText += dde.getMessage();
// Here, being unable to register to the Edge infrastructure, app should
// fall back to public cloud server. Edge is not available.
// For Demo app, we use the wifi dme server to continue to MobiledgeX.
dmeHostAddress = MatchingEngine.wifiOnlyDmeHost;
}
dmeHostAddress = "eu-qa." + MatchingEngine.baseDmeHost;
me.setUseWifiOnly(true);
me.setSSLEnabled(true);
// dmeHostAddress = mMatchingEngine.generateDmeHostAddress(); // normal usage.
// Keep same port.
int port = me.getPort();
// Always supplied by application, and in the MobiledgeX web admin console.
String orgName = "automation_dev_org";
// For illustration, the matching engine can be used to programmatically get the name of your application details
// so it can go to the correct appInst version. That AppInst on the server side must match the application
// version or else it won't be found and cannot be used.
// AppName must be added to the MobiledgeX web admin console.
String appName = "automation-sdk-porttest";
// override the version of that known registered app.
String appVers = "1.0";
// Use createDefaultRegisterClientRequest() to get a Builder class to fill in optional parameters
// like AuthToken or Tag key value pairs.
AppClient.RegisterClientRequest registerClientRequest = me.createDefaultRegisterClientRequest(ctx, orgName).setAppName(appName).setAppVers(appVers).build();
Log.i(TAG, "registerclient request is " + registerClientRequest);
// This exercises a threadpool that can have a dependent call depth larger than 1
AppClient.RegisterClientReply registerClientReply;
Future<AppClient.RegisterClientReply> registerClientReplyFuture = me.registerClientFuture(registerClientRequest, dmeHostAddress, port, 10000);
registerClientReply = registerClientReplyFuture.get();
Log.i(TAG, "RegisterReply status is " + registerClientReply.getStatus());
if (registerClientReply.getStatus() != AppClient.ReplyStatus.RS_SUCCESS) {
someText += "Registration Failed. Error: " + registerClientReply.getStatus();
return;
}
// Find the closest cloudlet for your application to use. (Blocking call, or use findCloudletFuture)
// There is also createDefaultFindClouldletRequest() to get a Builder class to fill in optional parameters.
AppClient.FindCloudletRequest findCloudletRequest = // location requires location services active with permissions.)
me.createDefaultFindCloudletRequest(ctx, automationCloudlet2).setCarrierName("").build();
AppClient.FindCloudletReply closestCloudlet = me.findCloudlet(findCloudletRequest, dmeHostAddress, port, 10000);
Log.i(TAG, "closest Cloudlet is " + closestCloudlet);
mLastFindCloudlet = closestCloudlet;
if (closestCloudlet.getStatus() != AppClient.FindCloudletReply.FindStatus.FIND_FOUND) {
someText += "Cloudlet not found!";
return;
}
// This is a legal point to keep posting edgeEvents updates, as the EdgeEventBus
// should now be initalized, unless disabled.
registerClientReplyFuture = me.registerClientFuture(registerClientRequest, dmeHostAddress, port, 10000);
registerClientReply = registerClientReplyFuture.get();
Log.i(TAG, "Register status: " + registerClientReply.getStatus());
AppClient.VerifyLocationRequest verifyRequest = me.createDefaultVerifyLocationRequest(ctx, location).build();
Log.i(TAG, "verifyRequest is " + verifyRequest);
if (verifyRequest != null) {
// Location Verification (Blocking, or use verifyLocationFuture):
AppClient.VerifyLocationReply verifiedLocation = me.verifyLocation(verifyRequest, dmeHostAddress, port, 10000);
Log.i(TAG, "VerifyLocationReply is " + verifiedLocation);
someText += "[Location Verified: Tower: " + verifiedLocation.getTowerStatus() + ", GPS LocationStatus: " + verifiedLocation.getGpsLocationStatus() + ", Location Accuracy: " + verifiedLocation.getGpsLocationAccuracyKm() + " ]\n";
List<distributed_match_engine.Appcommon.AppPort> ports = closestCloudlet.getPortsList();
String portListStr = "";
boolean first = true;
String appPortFormat = "{Protocol: %d, Container Port: %d, External Port: %d, Path Prefix: '%s'}";
for (Appcommon.AppPort aPort : ports) {
if (!first) {
portListStr += ", ";
}
portListStr += String.format(Locale.getDefault(), appPortFormat, aPort.getProto().getNumber(), aPort.getInternalPort(), aPort.getPublicPort(), aPort.getEndPort());
String host = aPort.getFqdnPrefix() + closestCloudlet.getFqdn();
int knownPort = 2015;
int serverPort = aPort.getPublicPort() == 0 ? knownPort : aPort.getPublicPort();
// mMatchingEngine.getAppConnectionManager().getHttpClient(10000).get();
OkHttpClient client = new OkHttpClient();
// Our example server might not like random connections to non-existing /test.
String api = serverPort == knownPort ? "/test" : "";
Response response = null;
try {
Request request = new Request.Builder().url("http://" + host + ":" + serverPort + api).build();
response = client.newCall(request).execute();
someText += "[Test Server response: " + response.toString() + "]\n";
} catch (IOException | IllegalStateException e) {
someText += "[Error connecting to host: " + host + ", port: " + serverPort + ", api: " + api + ", Reason: " + e.getMessage() + "]\n";
} finally {
if (response != null) {
response.body().close();
}
}
// Test from a particular network path. Here, the active one is Cellular since we switched the whole process over earlier.
Site site = new Site(me.getNetworkManager().getActiveNetwork(), NetTest.TestType.CONNECT, 5, host, serverPort);
netTest.addSite(site);
}
someText += "[Cloudlet App Ports: [" + portListStr + "]\n";
String appInstListText = "";
AppClient.AppInstListRequest appInstListRequest = me.createDefaultAppInstListRequest(ctx, location).build();
AppClient.AppInstListReply appInstListReply = me.getAppInstList(appInstListRequest, dmeHostAddress, port, 10000);
for (AppClient.CloudletLocation cloudletLocation : appInstListReply.getCloudletsList()) {
String location_carrierName = cloudletLocation.getCarrierName();
String location_cloudletName = cloudletLocation.getCloudletName();
double location_distance = cloudletLocation.getDistance();
appInstListText += "[CloudletLocation: CarrierName: " + location_carrierName;
appInstListText += ", CloudletName: " + location_cloudletName;
appInstListText += ", Distance: " + location_distance;
appInstListText += " , AppInstances: [";
for (AppClient.Appinstance appinstance : cloudletLocation.getAppinstancesList()) {
appInstListText += "Name: " + appinstance.getAppName() + ", Version: " + appinstance.getAppVers() + ", FQDN: " + appinstance.getFqdn() + ", Ports: " + appinstance.getPortsList().toString();
}
appInstListText += "]]\n";
}
if (!appInstListText.isEmpty()) {
someText += appInstListText;
}
} else {
someText = "Cannot create request object.\n";
if (!locationVerificationAllowed) {
someText += " Reason: Enhanced location is disabled.\n";
}
}
someText += "[Is WiFi Enabled: " + me.isWiFiEnabled(ctx) + "]\n";
if (android.os.Build.VERSION.SDK_INT >= 28) {
someText += "[Is Roaming Data Enabled: " + me.isRoamingData() + "]\n";
} else {
someText += "[Roaming Data status unknown.]\n";
}
CarrierConfigManager carrierConfigManager = ctx.getSystemService(CarrierConfigManager.class);
someText += "[Enabling WiFi Calling could disable Cellular Data if on a Roaming Network!\nWiFi Calling Support Status: " + me.isWiFiCallingSupported(carrierConfigManager) + "]\n";
// Background thread. Post update to the UI thread:
updateText(ctx, someText);
// Set network back to last default one, if desired:
me.getNetworkManager().resetNetworkToDefault();
} catch (/*DmeDnsException |*/
ExecutionException | StatusRuntimeException e) {
Log.e(TAG, e.getMessage());
Log.e(TAG, Log.getStackTraceString(e));
if (e.getCause() instanceof NetworkRequestTimeoutException) {
String causeMessage = e.getCause().getMessage();
someText = "Network connection failed: " + causeMessage;
Log.e(TAG, someText);
// Handle network error with failover logic. MobiledgeX MatchingEngine requests over cellular is needed to talk to the DME.
updateText(ctx, someText);
}
} catch (InterruptedException | IllegalArgumentException | Resources.NotFoundException | PackageManager.NameNotFoundException e) {
// Import MEL messaging.
MelMessaging.getCookie("MobiledgeX SDK Demo");
someText += "Exception failure: " + e.getMessage();
updateText(ctx, someText);
Log.e(TAG, Log.getStackTraceString(e));
} catch (Exception e) {
someText += "Exception failure: " + e.getMessage() + ": ";
updateText(ctx, someText);
e.printStackTrace();
} finally {
updateText(ctx, someText);
}
}
Aggregations