Пример #1
0
 public void EqualsActionIsNullTest()
 {
     IARoute route = new IARoute(null, "/example");
     IARoute other = new IARoute("GET", "/example2");
     Assert.IsFalse(route.Equals(other));
     Assert.AreNotEqual(route.GetHashCode(), other.GetHashCode());
 }
Пример #2
0
 public IARequest(IARoute route)
     : base()
 {
     this.Route = route;
     this.Metadata = new Dictionary<string,string>();
     this.Parameters = new Dictionary<string,string>();
 }
Пример #3
0
 public void EqualsDifferentResourceTest()
 {
     IARoute route = new IARoute("GET", "/example");
     IARoute other = new IARoute("GET", "/example2");
     Assert.IsFalse(route.Equals(other));
     Assert.AreNotEqual(route.GetHashCode(), other.GetHashCode());
 }
Пример #4
0
 private void OnServiceFound(SDService service, bool ownService)
 {
     if (ownService)
     {
         IADevice device = new IADevice(service.Name, service.Hostname, service.Port, this.SupportedRoutes);
         this.OwnDevice = device.AsOption();
         OnDeviceFound(device, true);
     }
     else
     {
         IADevice  device  = new IADevice(service.Name, service.Hostname, service.Port, null);
         IARequest request = new IARequest(IARoute.Get("/routes"));
         SendRequest(request, device, delegate(IAResponse response, Exception error)
         {
             if (error == null)
             {
                 if (response.StatusCode == 200)
                 {
                     List <IARoute> supportedRoutes = response.BodyAs <IARoute>();
                     IADevice deviceWithRoutes      = new IADevice(service.Name, service.Hostname, service.Port, new HashSet <IARoute>(supportedRoutes));
                     this.devices.Add(deviceWithRoutes);
                     OnDeviceFound(deviceWithRoutes, false);
                 }
                 else
                 {
                     Console.WriteLine(String.Format("An error ocurred trying to request routes from {0}", device));
                 }
             }
             else
             {
                 Console.WriteLine(String.Format("An error ocurred: {0}", error));
             }
         });
     }
 }
Пример #5
0
 public IARequest(IARoute route) : base()
 {
     this.Route      = route;
     this.Metadata   = new Dictionary <string, string>();
     this.Parameters = new Dictionary <string, string>();
     this.Origin     = new None <IADevice>();
 }
Пример #6
0
 public IARequest(IARoute route, Dictionary<String, String> metadata, Dictionary<String, String> parameters, IADevice origin, byte[] body, string contentType)
     : base(body, contentType)
 {
     this.Route = route;
     this.Metadata = metadata;
     this.Parameters = parameters;
     this.Origin = origin;
 }
Пример #7
0
 public IARequest(IARoute route, Dictionary <String, String> metadata, Dictionary <String, String> parameters, IADevice origin, byte[] body, string contentType)
     : base(body, contentType)
 {
     this.Route      = route;
     this.Metadata   = metadata;
     this.Parameters = parameters;
     this.Origin     = origin.AsOption();
 }
Пример #8
0
        public override bool Equals(Object obj)
        {
            if (this == obj)
            {
                return(true);
            }

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

            IARoute route = (IARoute)obj;

            return((this.Action != null && this.Action.Equals(route.Action)) &&
                   (this.Resource != null && this.Resource.Equals(route.Resource)));
        }
Пример #9
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);
                };
            }
        }
Пример #10
0
 public void IARouteConstructorTest()
 {
     string action = "GET";
     string resource = "/tests";
     string actualAction;
     string actualResource;
     IARoute target = new IARoute(action, resource);
     actualAction = target.Action;
     actualResource = target.Resource;
     Assert.AreEqual(action, actualAction);
     Assert.AreEqual(resource, actualResource);
 }
Пример #11
0
 public void Route(IARoute route, Action <IARequest, IAResponse> action)
 {
     SupportedRoutes.Add(route);
     server.Route(route, action);
 }
Пример #12
0
 public IEnumerable <IADevice> DevicesSupportingRoute(IARoute route)
 {
     return(this.Devices.FindAll(device => device.SupportedRoutes.Contains(route)));
 }
Пример #13
0
 public void Route(IARoute route, Action<IARequest, IAResponse> action)
 {
     Routes.Add(route, action);
     resolveCache().RebuildCache();
 }
Пример #14
0
 public void Route(IARoute route, Action<IARequest, IAResponse> action)
 {
     SupportedRoutes.Add(route);
     server.Route(route, action);
 }
Пример #15
0
 public IEnumerable<IADevice> DevicesSupportingRoute(IARoute route)
 {
     return this.Devices.FindAll(device => device.SupportedRoutes.Contains(route));
 }
Пример #16
0
 public void ToStringTest()
 {
     IARoute route = new IARoute("", "");
     Assert.IsNotNull(route.ToString());
 }
Пример #17
0
 public void EqualsSelfTest()
 {
     IARoute route = new IARoute("GET", "/example");
     IARoute other = route;
     Assert.IsTrue(route.Equals(other));
     Assert.AreEqual(route.GetHashCode(), other.GetHashCode());
 }
Пример #18
0
 public void EqualsNullTest()
 {
     IARoute route = new IARoute("GET", "/example");
     IARoute other = null;
     Assert.IsFalse(route.Equals(other));
 }
Пример #19
0
        private void CallIntAirActRoute(IARoute route, Dictionary<string, string> parameters, Delegate successGeneric, MSEErrorHandler failure)
        {
            //Setup Routes with Device in View
            IARequest request = new IARequest(route);

            //Add Parameters
            foreach (KeyValuePair<string, string> pair in parameters)
                request.Parameters[pair.Key] = pair.Value;

            //Get all devices that support this route
            IEnumerable devicesSupportingRoute = this.intAirAct.DevicesSupportingRoute(route);

            bool sentToServer = false;

            //Request Devices in View
            foreach (IADevice device in devicesSupportingRoute)
            {
                sentToServer = true;

                this.intAirAct.SendRequest(request, device, delegate(IAResponse response, Exception exception)
                {
                    if (exception != null)
                    {
                        failure(exception);
                    }

                    // QUESTION - Is this the same as saying if(successGeneric is MSEDeviceCollectionHandler)
                    if (successGeneric.GetType() == typeof(MSEDeviceCollectionHandler))
                    {
                        // Handle the response as if response was a collection
                        MSEDeviceCollectionHandler success = (MSEDeviceCollectionHandler)successGeneric;
                        List<MSEDevice> deviceList = Util.DeserializeIntoList(response.BodyAsString());
                        Util.CompleteDeviceListInformation(deviceList, this.intAirAct);

                        success(deviceList);
                        return;

                    }
                    else if(successGeneric.GetType() == typeof(MSESingleDeviceHandler))
                    {
                        // Handle the response as if response was a single device
                        MSESingleDeviceHandler success = (MSESingleDeviceHandler)successGeneric;
                        MSEDevice result = Util.DeserializeSingleDevice(response.BodyAsString());
                        Util.CompleteDeviceInformation(result, this.intAirAct);

                        success(result);
                        return;
                    }

                });

            }

            // If sentToServer is false, that means there was no devices capable of handling the route. This could mean that no devices can handle the route, or it just hasn't been found yet
            // I would normally use IEnumerable.Count, but that doesn't exist
            if (sentToServer == false)
            {
                failure(new Exception("MSE Error - There are no Devices discovered on the Network that can handle the " + route.Action + " " + route.Resource + " request. Is your Locator Server running and visible on the network?"));
                return;
            }
        }
Пример #20
0
 public void Route(IARoute route, Action <IARequest, IAResponse> action)
 {
     Routes.Add(route, action);
     resolveCache().RebuildCache();
 }