Beispiel #1
0
 private IAResponse IAResponseFromRestResponse(IRestResponse response)
 {
     IAResponse result = new IAResponse();
     result.StatusCode = (int)response.StatusCode;
     result.ContentType = response.ContentType;
     result.Body = response.RawBytes;
     foreach (Parameter p in response.Headers)
     {
         result.Metadata.Add(p.Name, "" + p.Value);
     }
     return result;
 }
Beispiel #2
0
        private IAResponse IAResponseFromRestResponse(IRestResponse response)
        {
            IAResponse result = new IAResponse();

            result.StatusCode  = (int)response.StatusCode;
            result.ContentType = response.ContentType;
            result.Body        = response.RawBytes;
            foreach (Parameter p in response.Headers)
            {
                result.Metadata.Add(p.Name, "" + p.Value);
            }
            return(result);
        }
Beispiel #3
0
        public override bool Equals(Object obj)
        {
            if (this == obj)
            {
                return(true);
            }

            if (obj == null || (obj as IAResponse) == null)
            {
                return(false);
            }

            IAResponse response = (IAResponse)obj;

            return((this.StatusCode == response.StatusCode) &&
                   (this.Body == response.Body) &&
                   (this.Metadata != null && this.Metadata.Equals(response.Metadata)));
        }
Beispiel #4
0
        /// <summary>
        /// Handle a request with updated location for a device.
        /// </summary>
        /// <param name="request">IntAirAct Request</param>
        /// <param name="response">IntAirAct Response</param>
        public void UpdateDeviceLocation(IARequest request, IAResponse response)
        {
            IntermediatePoint intermediatePoint = request.BodyAs<IntermediatePoint>();
            Point result = intermediatePoint.ToPoint();

            String name = request.Parameters["identifier"];
            Device localDevice = locator.Devices.Find(d => d.Identifier.Equals(name));

            if (localDevice != null)
            {
                localDevice.Location = result;
                response.StatusCode = 201; // created
            }
            else
            {
                response.StatusCode = 404; // not found
            }
        }
Beispiel #5
0
        private static void GetResponse(IAsyncResult asyncResult)
        {
            IAResponse   res   = null;
            Exception    e     = null;
            RequestState state = null;

            try
            {
                state = (RequestState)asyncResult.AsyncState;
                HttpWebResponse response = (HttpWebResponse)state.WebRequest.EndGetResponse(asyncResult);
                res = IAResponseFromHttpWebResponse(response);
            }
            catch (Exception exception)
            {
                e = new Exception(exception.Message);
            }

            if (state != null && state.Action != null)
            {
                state.Action(res, e);
            }
        }
Beispiel #6
0
        private static IAResponse IAResponseFromHttpWebResponse(HttpWebResponse webResponse)
        {
            IAResponse response = new IAResponse();

            response.StatusCode  = (int)webResponse.StatusCode;
            response.ContentType = webResponse.ContentType;

            for (int i = 0; i < webResponse.Headers.Count; i++)
            {
                response.Metadata.Add(webResponse.Headers.Keys[i], webResponse.Headers[i]);
            }

            Stream stream = webResponse.GetResponseStream();

            byte[] b;
            using (MemoryStream ms = new MemoryStream())
            {
                stream.CopyTo(ms);
                b = ms.ToArray();
            }
            response.Body = b;

            return(response);
        }
Beispiel #7
0
        void GetOffsetAngle(IARequest request, IAResponse response)
        {
            // Find the device
            String deviceIdentifier = request.Parameters["identifier"];
            Device requestingDevice = locator.Devices.Find(d => d.Identifier == deviceIdentifier);

            // Device Does Not Exist
            if (requestingDevice == null)
            {
                response.StatusCode = 404; // not found
                return;
            }

            if (requestingDevice.Location.HasValue && locator.Trackers[0].Location.HasValue)
            {
                Point requestingDeviceLocation = requestingDevice.Location.Value;
                Point offsetLocation = locator.Trackers[0].Location.Value;

                double angle = Util.AngleBetweenPoints(requestingDeviceLocation, offsetLocation);
                response.SetBodyWith(angle);

                //response.SetBodyWith(90);
            }
            else
            {
                // Device doesn't have location
                response.StatusCode = 400;
            }
        }
        public NancyRouteModule(NancyServerAdapter adapter)
        {
            foreach (KeyValuePair<IARoute, Action<IARequest, IAResponse>> kvp in adapter.Routes)
            {
                IARoute route = kvp.Key;
                Action<IARequest, IAResponse> action = kvp.Value;
                RouteBuilder rb = new RouteBuilder(route.Action, this);
                rb[route.Resource] = nancyDynamicDictionary =>
                {
                    Dictionary<string, string> parameters = new Dictionary<string, string>();

                    // get parameters out of path
                    foreach (string key in nancyDynamicDictionary)
                    {

                        DynamicDictionaryValue value = nancyDynamicDictionary[key];
                        string urldecoded = HttpUtility.UrlDecode(value.ToString());
                        parameters.Add(key, urldecoded);
                    }

                    // get parameters out of query string
                    foreach (string key in Request.Query)
                    {
                        DynamicDictionaryValue value = Request.Query[key];
                        parameters.Add(key, "" + value.Value);
                    }
                    string contentType = Request.Headers.ContentType;

                    IADevice origin = null;
                    if (Request.Headers.Keys.Contains("X-IA-Origin"))
                    {
                        IAIntAirAct intAirAct = TinyIoC.TinyIoCContainer.Current.Resolve<IAIntAirAct>();
                        if (intAirAct != null)
                        {
                            origin = intAirAct.DeviceWithName(Request.Headers["X-IA-Origin"].First());
                        }
                    }

                    Dictionary<string, string> metadata = new Dictionary<string, string>();
                    foreach (KeyValuePair<string, IEnumerable<string>> header in Request.Headers)
                    {
                        var value = header.Value.First();
                        metadata[header.Key] = value;
                    }

                    IARequest iaRequest = new IARequest(route, metadata, parameters, origin, Request.BodyAsByte(), contentType);
                    IAResponse iaResponse = new IAResponse();
                    action(iaRequest, iaResponse);
                    Response response = new Response();
                    response.StatusCode = (HttpStatusCode)iaResponse.StatusCode;
                    response.Contents = stream =>
                    {
                        var writer = new BinaryWriter(stream);
                        writer.Write(iaResponse.Body);
                        writer.Flush();
                    };
                    response.Headers = iaResponse.Metadata;

                    response.ContentType = iaResponse.ContentType;

                    return response;
                };
            }
        }
Beispiel #9
0
        /// <summary>
        /// Return All Devices known to the Locator 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        void GetDevices(IARequest request, IAResponse response)
        {
            List<IntermediateDevice> intermediateDevices = PairableDevice.GetCompleteIntermediateDevicesList(locator.Devices);

            if (intermediateDevices.Count == 0)
            {
                response.StatusCode = 404;
            }
            else
            {
                response.SetBodyWith(intermediateDevices);
            }
        }
Beispiel #10
0
        public NancyRouteModule(NancyServerAdapter adapter)
        {
            foreach (KeyValuePair <IARoute, Action <IARequest, IAResponse> > kvp in adapter.Routes)
            {
                IARoute route = kvp.Key;
                Action <IARequest, IAResponse> action = kvp.Value;
                RouteBuilder rb = new RouteBuilder(route.Action, this);
                rb[route.Resource] = nancyDynamicDictionary =>
                {
                    Dictionary <string, string> parameters = new Dictionary <string, string>();

                    // get parameters out of path
                    foreach (string key in nancyDynamicDictionary)
                    {
                        DynamicDictionaryValue value = nancyDynamicDictionary[key];
                        string urldecoded            = HttpUtility.UrlDecode(value.ToString());
                        parameters.Add(key, urldecoded);
                    }

                    // get parameters out of query string
                    foreach (string key in Request.Query)
                    {
                        DynamicDictionaryValue value = Request.Query[key];
                        parameters.Add(key, "" + value.Value);
                    }
                    string contentType = Request.Headers.ContentType;

                    IADevice origin = null;
                    if (Request.Headers.Keys.Contains("X-IA-Origin"))
                    {
                        IAIntAirAct intAirAct = TinyIoC.TinyIoCContainer.Current.Resolve <IAIntAirAct>();
                        if (intAirAct != null)
                        {
                            origin = intAirAct.DeviceWithName(Request.Headers["X-IA-Origin"].First());
                        }
                    }

                    Dictionary <string, string> metadata = new Dictionary <string, string>();
                    foreach (KeyValuePair <string, IEnumerable <string> > header in Request.Headers)
                    {
                        var value = header.Value.First();
                        metadata[header.Key] = value;
                    }

                    IARequest  iaRequest  = new IARequest(route, metadata, parameters, origin, Request.BodyAsByte(), contentType);
                    IAResponse iaResponse = new IAResponse();
                    action(iaRequest, iaResponse);
                    Response response = new Response();
                    response.StatusCode = (HttpStatusCode)iaResponse.StatusCode;
                    response.Contents   = stream =>
                    {
                        var writer = new BinaryWriter(stream);
                        writer.Write(iaResponse.Body);
                        writer.Flush();
                    };
                    response.Headers = iaResponse.Metadata;

                    response.ContentType = iaResponse.ContentType;

                    return(response);
                };
            }
        }
Beispiel #11
0
        /// <summary>
        /// Handle a request for information about a device
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        void GetDevice(IARequest request, IAResponse response)
        {
            String deviceIdentifier = request.Parameters["identifier"];

            // Find the associated device in the Current Devices
            Device device = locator.Devices.Find(d => d.Identifier.Equals(deviceIdentifier));
            if (device == null)
            {
                response.StatusCode = 404;
                return;
            }

            // Get the intermediateDevice for serialization
            IntermediateDevice intermediateDevice = PairableDevice.GetCompleteIntermediateDevice(device);
            if (intermediateDevice == null)
            {
                //TODO: Should this status code be different, to reflect that the device exists but couldn't be returned due to incompleteness?
                response.StatusCode = 404;
                return;
            }

            // Respond with the device
            response.SetBodyWith(intermediateDevice);
        }
Beispiel #12
0
        void GetDevicesInRange(IARequest request, IAResponse response)
        {
            String deviceIdentifier = request.Parameters["identifier"];
            double range = Double.Parse(request.Parameters["range"]);

            // Find the associated device in the Current Devices
            Device device = locator.Devices.Find(d => d.Identifier.Equals(deviceIdentifier));
            if (device == null)
            {
                response.StatusCode = 404;
                return;
            }

            List<Device> devicesInView = locator.GetDevicesWithinRange(device, range);
            List<IntermediateDevice> intermediateDevices = PairableDevice.GetCompleteIntermediateDevicesList(devicesInView);
            if (intermediateDevices.Count == 0)
            {
                response.StatusCode = 404;
                return;
            }

            // Respond with the device
            response.SetBodyWith(intermediateDevices);
        }
Beispiel #13
0
 void UpdateDevicePairingState(IARequest request, IAResponse response)
 {
     Console.WriteLine(request.Parameters["identifier"]);
     pairingRecognizer.DevicePairAttempt(request.Parameters["identifier"]);
 }
Beispiel #14
0
        /// <summary>
        /// Handle a request with updated information for a device.
        /// Presently, only used to update device location
        /// </summary>
        /// <param name="request">IntAirAct Request</param>
        /// <param name="response">IntAirAct Response</param>
        void UpdateDeviceOrientation(IARequest request, IAResponse response)
        {
            string result = request.BodyAsString();
            //TODO: Handle parse failure gracefully
            float newOrientation = float.Parse(result);

            String name = request.Parameters["identifier"];
            Device localDevice = locator.Devices.Find(d => d.Identifier.Equals(name));

            if (localDevice != null)
            {
                localDevice.Orientation = newOrientation;
                response.StatusCode = 201; // created
            }
            else
            {
                response.StatusCode = 404; // not found
            }
        }
Beispiel #15
0
        void GetNearestDeviceInView(IARequest request, IAResponse response)
        {
            String deviceIdentifier = request.Parameters["identifier"];

            // Find the associated device in the Current Devices
            Device observer = locator.Devices.Find(d => d.Identifier.Equals(deviceIdentifier));
            if (observer == null)
            {
                //TODO: Should we use distinct status codes for distinct failure types here?
                response.StatusCode = 404;
                return;
            }

            // Find the nearest device that we observe
            Device nearestDevice = locator.GetNearestDeviceInView(observer);
            if (nearestDevice == null)
            {
                response.StatusCode = 404;
                return;
            }

            // Prepare the device for serialization
            IntermediateDevice intermediateDevice = PairableDevice.GetCompleteIntermediateDevice(nearestDevice);
            if (intermediateDevice == null)
            {
                response.StatusCode = 404;
                return;
            }

            response.SetBodyWith(intermediateDevice);
        }
Beispiel #16
0
        void GetNearestDeviceInRange(IARequest request, IAResponse response)
        {
            String deviceIdentifier = request.Parameters["identifier"];
            double range = Double.Parse(request.Parameters["range"]);

            // Find the associated device in the Current Devices
            Device observer = locator.Devices.Find(d => d.Identifier.Equals(deviceIdentifier));
            if (observer == null)
            {
                response.StatusCode = 404;
                return;
            }

            Device nearestDevice = locator.GetNearestDeviceWithinRange(observer, range);
            if (nearestDevice == null)
            {
                response.StatusCode = 404;
                return;
            }

            IntermediateDevice intermediateDevice = PairableDevice.GetCompleteIntermediateDevice(nearestDevice);
            if (intermediateDevice == null)
            {
                response.StatusCode = 404;
                return;
            }

            // Respond with the device
            response.SetBodyWith(intermediateDevice);
        }
Beispiel #17
0
        void GetDevicesInView(IARequest request, IAResponse response)
        {
            String deviceIdentifier = request.Parameters["identifier"];

            // Find the associated device in the Current Devices
            Device observer = locator.Devices.Find(d => d.Identifier.Equals(deviceIdentifier));
            if (observer == null)
            {
                response.StatusCode = 404;
                return;
            }

            // Get the devices in view, and convert them for serialization
            List<Device> devicesInView = locator.GetDevicesInView(observer);
            List<IntermediateDevice> intDevices = PairableDevice.GetCompleteIntermediateDevicesList(devicesInView);
            if (intDevices.Count == 0)
            {
                response.StatusCode = 404;
                return;
            }

            response.SetBodyWith(intDevices);
        }