use of com.mobiledgex.matchingengine.performancemetrics.Site 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