Beispiel #1
0
    async Task <bool> DoFindCloudlet()
    {
        bool                ok    = false;
        MatchingEngine      dme   = mexSample.dme;
        VerifyLocationReply reply = null;

        try
        {
            var deviceSourcedLocation = await LocationService.RetrieveLocation();

            if (deviceSourcedLocation == null)
            {
                Debug.Log("VerifyLocation must have a device sourced location to send to DME.");
                return(false);
            }
            Debug.Log("Device sourced location: Lat: " + deviceSourcedLocation.latitude + "  Long: " + deviceSourcedLocation.longitude);

            var verifyLocationRequest = dme.CreateVerifyLocationRequest(
                mexSample.carrierName, // TODO: carrierName is the current carrier string, and must be provided by the app.
                deviceSourcedLocation);

            reply = await dme.VerifyLocation(mexSample.host, mexSample.port, verifyLocationRequest);

            statusContainer.Post("VerifyLocation Reply mobile tower status: " + reply.tower_status.ToString());
            if (reply.tower_status.Equals(FindCloudletReply.FindStatus.FIND_FOUND.ToString()))
            {
                ok = true;
            }
        }
        catch (System.Net.WebException we)
        {
            Console.WriteLine(we.StackTrace);
            statusContainer.Post(we.Source + ", WebException: " + we.Message);
            statusContainer.Post(we.StackTrace);
        }
        finally
        {
            if (reply != null)
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(VerifyLocationReply));
                MemoryStream ms = new MemoryStream();
                serializer.WriteObject(ms, reply);
                string jsonStr = Util.StreamToString(ms);

                statusContainer.Post("VeryfyLocation Reply: " + jsonStr);
            }
        }
        return(ok);
    }
Beispiel #2
0
    async Task <bool> DoRegisterClient()
    {
        bool                ok    = false;
        MatchingEngine      dme   = mexSample.dme;
        RegisterClientReply reply = null;

        try
        {
            var registerClientRequest = dme.CreateRegisterClientRequest(
                mexSample.carrierName,
                mexSample.orgName,
                mexSample.appName,
                mexSample.appVers,
                mexSample.authToken);

            reply = await dme.RegisterClient(mexSample.host, mexSample.port, registerClientRequest);

            // the dme object stores the session tokens, so the only thing to do here is
            // inspect parts of the JSON registration status and retry:
            if (reply.status != ReplyStatus.RS_SUCCESS)
            {
                statusContainer.Post("RegisterClient did not succeed!");
            }
            else
            {
                ok = true;
            }
        }
        catch (System.Net.WebException we)
        {
            Console.WriteLine(we.StackTrace);
            statusContainer.Post(we.Source + ", WebException: " + we.Message);
            statusContainer.Post(we.StackTrace);
        }
        finally
        {
            if (reply != null)
            {
                statusContainer.Post(
                    "RegisterClient Button results:" +
                    " Status: " + reply.status +
                    " SessionCookie: " + reply.session_cookie +
                    " TokenServerURI: " + reply.token_server_uri
                    );
            }
        }
        return(ok);
    }
Beispiel #3
0
    public static async Task <LocationInfo> UpdateLocation()
    {
        statusContainer.Post("Location Services updateLocation()");
        // First, check if user has location service enabled
        if (!Input.location.isEnabledByUser)
        {
            statusContainer.Post("Location Services Disabled");
            // Per documentation, on iOS, CoreLocation asks the user for permission.
#if UNITY_IOS
            statusContainer.Post("CoreLocation.");
#else
            statusContainer.Post("Location Services Disabled, cannot get location.");
            return(Input.location.lastData);
#endif
        }

        // Start service before querying location
        Input.location.Start();

        // Wait until service initializes
        int maxWait = 10;
        int start   = maxWait;
        while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
        {
            await Task.Delay(TimeSpan.FromSeconds(1));

            maxWait--;
        }

        statusContainer.Post("Location Services waited " + (start - maxWait));
        // Service didn't initialize in time.
        if (maxWait < 1)
        {
            print("Timed out");
            throw new LocationTimeoutException("Location Services not returning results!");
        }

        // Connection has failed
        if (Input.location.status == LocationServiceStatus.Failed)
        {
            print("Unable to determine device location");
            throw new LocationException("Location Services can't find location!");
        }
        else
        {
            // Access granted and location value could be retrieved
            statusContainer.Post("Location Services= lat: " + Input.location.lastData.latitude + ", long: " + Input.location.lastData.longitude);
            print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
        }

        // Stop service if there is no need to query location updates continuously
        Input.location.Stop();

        LocationInfo info = Input.location.lastData;


        return(info);
    }
Beispiel #4
0
    public async void RunSampleFlow()
    {
        try
        {
            carrierName = getCurrentCarrierName();

            Console.WriteLine("RestSample!");
            statusContainer.Post("RestSample!");

            dme  = new MatchingEngine();
            port = 38001; // MatchingEngine.defaultDmeRestPort;
            statusContainer.Post("RestSample Port:" + port);

            // Start location and await:
            var location = await LocationService.RetrieveLocation();

            statusContainer.Post("RestSample Location Task started.");

            var registerClientRequest = dme.CreateRegisterClientRequest(carrierName, orgName, appName, appVers, developerAuthToken);

            // Await synchronously.

            statusContainer.Post("RegisterClient.");
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(RegisterClientRequest));
            MemoryStream ms = new MemoryStream();
            // have object?
            if (registerClientRequest == null)
            {
                statusContainer.Post("Weird, RegisterClient create is null");
            }
            statusContainer.Post("RegisterClient: app_name" + registerClientRequest.app_name);

            serializer.WriteObject(ms, registerClientRequest);

            string jsonStr = Util.StreamToString(ms);
            statusContainer.Post(" --> RegisterClient as string: " + jsonStr);
            Debug.Log(" --> RegisterClient as string: " + jsonStr);

            statusContainer.Post(" RegisterClient to host: " + host + ", port: " + port);

            var registerClientReply = await dme.RegisterClient(host, port, registerClientRequest);

            Console.WriteLine("Reply: Session Cookie: " + registerClientReply.session_cookie);

            statusContainer.Post("RegisterClient token_server_uri: " + registerClientReply.token_server_uri);

            // Do Verify and FindCloudlet in parallel tasks:

            var verifyLocationRequest = dme.CreateVerifyLocationRequest(carrierName, location);
            var findCloudletRequest   = dme.CreateFindCloudletRequest(carrierName, location, orgName, appName, appVers);
            var getLocationRequest    = dme.CreateGetLocationRequest(carrierName);


            // Async:
            var findCloudletTask = dme.FindCloudlet(host, port, findCloudletRequest);
            //var verfiyLocationTask = me.VerifyLocation(host, port, verifyLocationRequest);


            var getLocationTask = dme.GetLocation(host, port, getLocationRequest);

            // Awaits:
            var findCloudletReply = await findCloudletTask;
            Console.WriteLine("FindCloudlet Reply: " + findCloudletReply.status);
            statusContainer.Post("FindCloudlet Status: " + findCloudletReply.status);

            // The following requires a valid SIM card from a MobiledgeX enabled carrier
            // to validate the device location. It will attempt to contact the carrier,
            // and will timeout if not within the carrier network.
            //var verifyLocationReply = await verfiyLocationTask;
            //Console.WriteLine("VerifyLocation Reply: " + verifyLocationReply.gps_location_status);
            //statusContainer.Post("VerifyLocation Status: " + verifyLocationReply.gps_location_status);

            var getLocationReply = await getLocationTask;
            var carrierLocation  = getLocationReply.network_location;
            Console.WriteLine("GetLocationReply: longitude: " + carrierLocation.longitude + ", latitude: " + carrierLocation.latitude);
            statusContainer.Post("GetLocationReply: longitude: " + carrierLocation.longitude + ", latitude: " + carrierLocation.latitude);
        }
        catch (InvalidTokenServerTokenException itste)
        {
            Console.WriteLine(itste.StackTrace);
            statusContainer.Post("Token Exception: " + itste.ToString());
            statusContainer.Post(itste.StackTrace);
        }
        catch (System.Net.WebException we)
        {
            Console.WriteLine(we.StackTrace);
            statusContainer.Post(we.Source + "WebException: " + we.Message);
            statusContainer.Post(we.StackTrace);
        }
    }
Beispiel #5
0
 public void Start()
 {
     statusContainer = GameObject.Find("/UICanvas/SampleOutput").GetComponent <StatusContainer>();
     statusContainer.Post("Location Services Start()");
 }
Beispiel #6
0
    async Task <bool> DoFindCloudlet()
    {
        bool              ok    = false;
        MatchingEngine    dme   = mexSample.dme;
        FindCloudletReply reply = null;

        try
        {
            var deviceSourcedLocation = await LocationService.RetrieveLocation();

            if (deviceSourcedLocation == null)
            {
                Debug.Log("FindCloudlet must have a device sourced location to send.");
                return(false);
            }
            Debug.Log("Device sourced location: Lat: " + deviceSourcedLocation.latitude + "  Long: " + deviceSourcedLocation.longitude);

            var findCloudletRequest = dme.CreateFindCloudletRequest(
                mexSample.carrierName,
                mexSample.devName,
                mexSample.appName,
                mexSample.appVers,
                deviceSourcedLocation);

            if (findCloudletRequest == null)
            {
                Debug.Log("Failed to create request.");
                ok = false;
                return(ok);
            }

            reply = await dme.FindCloudlet(mexSample.host, mexSample.port, findCloudletRequest);

            statusContainer.Post("FindCloudlet Reply status: " + reply.status);
        }
        catch (System.Net.WebException we)
        {
            Console.WriteLine(we.StackTrace);
            statusContainer.Post(we.Source + ", WebException: " + we.Message);
            statusContainer.Post(we.StackTrace);
        }
        finally
        {
            if (reply != null)
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(FindCloudletReply));
                MemoryStream ms = new MemoryStream();
                serializer.WriteObject(ms, reply);
                string jsonStr = Util.StreamToString(ms);

                statusContainer.Post("GPS Cloudlet location: " + jsonStr + ", fqdn: " + reply.fqdn);

                // The list of registered edge cloudlets that the app can use:
                if (reply.ports.Length == 0)
                {
                    statusContainer.Post("There are no app ports for this app's edge cloudlets.");
                }
                else
                {
                    statusContainer.Post("Cloudlet app ports:");
                    foreach (var appPort in reply.ports)
                    {
                        statusContainer.Post(
                            "Protocol: " + appPort.proto +
                            ", internal_port: " + appPort.internal_port +
                            ", public_port: " + appPort.public_port +
                            ", path_prefix: " + appPort.path_prefix +
                            ", fqdn_prefix: " + appPort.fqdn_prefix
                            );
                    }
                }
            }
        }
        return(ok);
    }