コード例 #1
0
        public static async Task <LocationFromIPAddress> GetLocationFromIP()
        {
            HttpClient httpClient = new HttpClient();

            try
            {
                HttpResponseMessage response = await httpClient.GetAsync("https://freegeoip.app/json/").ConfigureAwait(false);

                string responseBodyStr         = response.Content.ReadAsStringAsync().Result;
                LocationFromIPAddress location = Messaging <LocationFromIPAddress> .Deserialize(responseBodyStr);

                return(location);
            }
            catch (Exception)
            {
                return(new LocationFromIPAddress()
                {
                    latitude = 37.3382f,
                    longitude = 121.8863f
                });
            }
        }
コード例 #2
0
        /// <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);
        }