Example #1
0
    public async Task <FindCloudletReply> FindCloudlet()
    {
        // Location is ephemeral, so retrieve a new location from the platform. May return 0,0 which is
        // technically valid, though less likely real, as of writing.
        Loc loc = await GetLocationFromDevice();

        // If MEX is reachable on your SIM card:
        string aCarrierName = GetCarrierName();
        string eCarrierName;

        if (me.useOnlyWifi) // There's no host (PC, UnityEditor, etc.)...
        {
            eCarrierName = carrierName;
        }
        else
        {
            if (aCarrierName == "" || aCarrierName == null)
            {
                Debug.Log("Missing CarrierName for FindCloudlet.");
                return(null);
            }
            eCarrierName = aCarrierName;
        }

        FindCloudletRequest req = me.CreateFindCloudletRequest(eCarrierName, loc);

        FindCloudletReply reply = await me.FindCloudlet(req);

        return(reply);
    }
        /// <summary>
        /// Wrapper for FindCloudlet. Will find the "nearest" cloudlet hosting the application backend
        /// To use Performance mode. Call UseFindCloudletPerformanceMode(true)
        /// </summary>
        /// <returns>FindCloudletReply Task</returns>
        public async Task <bool> FindCloudlet(string dmeHost = null, uint dmePort = 0)
        {
            latestFindCloudletReply = null;
            if (!latestRegisterStatus)
            {
                throw new FindCloudletException("Last RegisterClient was unsuccessful. Call RegisterClient again before FindCloudlet");
            }

            try
            {
                if (fallbackLocation.Longitude == 0 && fallbackLocation.Latitude == 0)
                {
                    LocationFromIPAddress locationFromIP = await GetLocationFromIP();

                    fallbackLocation = new Location(locationFromIP.longitude, locationFromIP.latitude);
                }
                await UpdateLocationAndCarrierInfo();
            }
            catch (CarrierInfoException cie)
            {
                Debug.LogError("MobiledgeX: FindCloudlet CarrierInfoException: " + cie.Message);
                throw new FindCloudletException(cie.Message);
            }

            FindCloudletReply reply = null;

            try
            {
                if (location == null)
                {
                    throw new FindCloudletException("Location must not be null!");
                }
                Logger.Log("FindCloudlet Location: " + location.longitude + ", lat: " + location.latitude);
                FindCloudletRequest req = matchingEngine.CreateFindCloudletRequest(location, "");
                if (dmeHost != null && dmePort != 0)
                {
                    Logger.Log("Doing FindCloudlet with DME: " + dmeHost + ", p: " + dmePort + " with req: " + req);
                    reply = await matchingEngine.FindCloudlet(dmeHost, dmePort, req, mode);
                }
                else
                {
                    if (!useSelectedRegionInProduction)
                    {
#if UNITY_EDITOR
                        Logger.Log("Doing FindCloudlet with DME: " + region + ", p: " + MatchingEngine.defaultDmeRestPort + " with req: " + req);
                        Logger.LogWarning("Region Selection will work only in UnityEditor not on Mobile Devices");
                        reply = await matchingEngine.FindCloudlet(region, MatchingEngine.defaultDmeRestPort, req);
#else
                        Logger.Log("Doing FindCloudlet, with req: " + req);
                        reply = await matchingEngine.FindCloudlet(req, mode);
#endif
                    }
                    else
                    {
                        Logger.Log("Doing FindCloudlet with DME: " + region + ", p: " + MatchingEngine.defaultDmeRestPort + " with req: " + req);
                        reply = await matchingEngine.FindCloudlet(region, MatchingEngine.defaultDmeRestPort, req, mode);
                    }
                }
            }
            catch (HttpException httpe)
            {
                throw new FindCloudletException("FindCloudlet Exception: " + httpe.Message + ", HTTP StatusCode: " + httpe.HttpStatusCode + ", API ErrorCode: " + httpe.ErrorCode + "\nStack: " + httpe.StackTrace);
            }
            catch (Exception e)
            {
                throw new FindCloudletException("FindCloudletException Type: " + e.GetType() + ", Message: " + e.Message + ", InnerException : " + e.InnerException + "\nStack: " + e.StackTrace);
            }
            finally
            {
                if (reply == null)
                {
                    throw new FindCloudletException("FindCloudletReply returned null. Make Sure you created App Instances for your Application and they are deployed in the correct region.");
                }
                if (reply.status != FindCloudletReply.FindStatus.FIND_FOUND)
                {
                    throw new FindCloudletException("Unable to findCloudlet. Status is " + reply.status);
                }
            }

            Logger.Log("FindCloudlet with DME result: " + reply.status);
            latestFindCloudletReply = reply;
            latestAppPortList       = reply.ports;
            return(reply.status == FindCloudletReply.FindStatus.FIND_FOUND);
        }