Ejemplo n.º 1
0
 /// <summary>
 /// Adds a new web event to the server.
 /// </summary>
 /// <param name="webEvent">The web event and corresponding data that the server will handle.</param>
 /// <returns>A Boolean value that indicates whether the <see cref="WebEvent"/> could be added, <b>true</b> if added,
 /// <b>false</b>, if webEvent already exists. </returns>
 internal bool AddWebEvent(WebEvent webEvent)
 {
     if (!WebEvents.Contains(webEvent.GetWebEventId()))
     {
         WebEvents.Add(webEvent.GetWebEventId(), webEvent);
         return(true);
     }
     return(false);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a path on the server so that an http GET request can be processed.
        /// Specifies a path of the form: http://{IP}:{port}/{path}.
        /// </summary>
        /// <param name="path">The path used to identify a resource.</param>
        /// <returns>A <see cref="T:GTM.NetworkModule.WebEvent"/> object that is used to update data and respond to incoming requests.
        /// </returns>
        public static WebEvent SetupWebEvent(string path)
        {
            WebEvent webEvent = WebServerManager.GetInstance().GetWebEventById(path);

            if (webEvent == null)
            {
                webEvent = new WebEvent(path);
            }
            return(webEvent);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a path to a resource on the server so that a http GET requests can be processed.
        /// Specifies the path of the form: http://{IP}:{port}/{path}.
        /// </summary>
        /// <param name="path">The path used to identify a resource.</param>
        /// <param name="refreshAfter">Specifies the refresh interval in seconds.</param>
        /// <returns>A <see cref="WebEvent"/> object that is used to update data and respond to incoming requests.
        /// </returns>
        public static WebEvent SetupWebEvent(string path, uint refreshAfter)
        {
            WebEvent webEvent = WebServerManager.GetInstance().GetWebEventById(path);

            if (webEvent == null)
            {
                webEvent = new WebEvent(path);
            }
            webEvent.refreshAfter = refreshAfter;
            return(webEvent);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Stops and removes the given <see cref="WebEvent"/>.
        /// </summary>
        /// <param name="webEvent">The <see cref="WebEvent"/> to stop.</param>
        /// <returns>Returns <b>true</b> if the <see cref="WebEvent"/> exists and can be removed, <b>false</b> if <see cref="WebEvent"/>
        /// does not exist or cannot be stopped.</returns>
        public bool Stop(WebEvent webEvent)
        {
            if (WebEvents.Contains(webEvent.GetWebEventId()))
            {
                WebEvents.Remove(webEvent.GetWebEventId());
                return(true);
            }

            // Debug.Print("Service not found");
            return(false);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a path on the server so that an http GET request can be processed.
        /// Specifies a path of the form: http://{IP}:{port}/{path}.
        /// </summary>
        /// <param name="path">The path used to identify a resource.</param>
        /// <returns>A <see cref="T:GTM.NetworkModule.WebEvent"/> object that is used to update data and respond to incoming requests.
        /// </returns>
        public static WebEvent SetupWebEvent(string path)
        {
            WebEvent webEvent = WebServerManager.GetInstance().GetWebEventById(path);

            if (webEvent == null)
            {
                webEvent = new WebEvent(path);
            }
            return webEvent;
        }
Ejemplo n.º 6
0
 ///<summary>
 /// Stops and removes a single resource. If the resource is requested again, the server will return the default page. 
 ///</summary>
 ///<param name="webEvent">The <see cref="WebEvent"/> to be stopped</param>
 ///<returns>The result of the operation, <b>true</b> if the resource is found and removed, otherwise <b>false</b>.</returns>
 public static bool DisableWebEvent(WebEvent webEvent)
 {
     return WebServerManager.GetInstance().Stop(webEvent);
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates a path to a resource on the server so that a http GET requests can be processed.
        /// Specifies the path of the form: http://{IP}:{port}/{path}.
        /// </summary>
        /// <param name="path">The path used to identify a resource.</param>
        /// <param name="refreshAfter">Specifies the refresh interval in seconds.</param>
        /// <returns>A <see cref="WebEvent"/> object that is used to update data and respond to incoming requests.
        /// </returns>
        public static WebEvent SetupWebEvent(string path, uint refreshAfter)
        {
            WebEvent webEvent = WebServerManager.GetInstance().GetWebEventById(path);

            if (webEvent == null)
            {
                webEvent = new WebEvent(path);
            }
            webEvent.refreshAfter = refreshAfter;
            return webEvent;
        }
        /// <summary>
        /// Stops and removes the given <see cref="WebEvent"/>.
        /// </summary>
        /// <param name="webEvent">The <see cref="WebEvent"/> to stop.</param>
        /// <returns>Returns <b>true</b> if the <see cref="WebEvent"/> exists and can be removed, <b>false</b> if <see cref="WebEvent"/>
        /// does not exist or cannot be stopped.</returns>
        public bool Stop(WebEvent webEvent)
        {
            if (WebEvents.Contains(webEvent.GetWebEventId()))
            {
                WebEvents.Remove(webEvent.GetWebEventId());
                return true;
            }

            // Debug.Print("Service not found");
            return false;
        }
        /// <summary>
        /// Adds a new web event to the server.
        /// </summary>
        /// <param name="webEvent">The web event and corresponding data that the server will handle.</param>
        /// <returns>A Boolean value that indicates whether the <see cref="WebEvent"/> could be added, <b>true</b> if added, 
        /// <b>false</b>, if webEvent already exists. </returns>
        internal bool AddWebEvent(WebEvent webEvent)
        {

            if (!WebEvents.Contains(webEvent.GetWebEventId()))
            {
                WebEvents.Add(webEvent.GetWebEventId(), webEvent);
                return true;
            }
            return false;
        }
Ejemplo n.º 10
0
 ///<summary>
 /// Stops and removes a single resource. If the resource is requested again, the server will return the default page.
 ///</summary>
 ///<param name="webEvent">The <see cref="WebEvent"/> to be stopped</param>
 ///<returns>The result of the operation, <b>true</b> if the resource is found and removed, otherwise <b>false</b>.</returns>
 public static bool DisableWebEvent(WebEvent webEvent)
 {
     return(WebServerManager.GetInstance().Stop(webEvent));
 }
Ejemplo n.º 11
0
            /// <summary>
            /// Processes incoming requests
            /// </summary>
            protected void ProcessRequest()
            {
                // accept an incoming connection request and once we have one, spawn a new thread to accept more
                bool newThread = false;

                System.Net.Sockets.Socket clientSocket = null;
                Responder responder = null;

                while (IsRunning && !newThread)
                {
                    try
                    {
                        // process incoming request
                        clientSocket = LocalServer.Accept();
                        clientSocket.ReceiveTimeout = Timeout;
                        clientSocket.SendTimeout    = Timeout;

                        // parse message an create an object containing parsed data
                        responder = new Responder();
                        responder.ClientEndpoint = clientSocket.RemoteEndPoint.ToString();
                        responder.ClientSocket   = clientSocket;

                        Debug.Print("Accepted connection from " + responder.ClientEndpoint);

                        Thread t = new Thread(new ThreadStart(ProcessRequest));
                        t.Start();
                        newThread = true;
                    }
                    catch
                    {
                        if (clientSocket != null)
                        {
                            try
                            {
                                clientSocket.Close();
                            }
                            catch { }
                        }
                    }
                }

                // now process the request
                try
                {
                    bool finishedParsing = false;

                    TimeSpan parseStart = Gadgeteer.Timer.GetMachineTime();

                    while (!finishedParsing)
                    {
                        if (Gadgeteer.Timer.GetMachineTime() - parseStart > MaxProcessingTime)
                        {
                            return;
                        }
                        if (clientSocket == null)
                        {
                            return;
                        }
                        // receiving data, add to an array to process later
                        byte[] buffer = new byte[clientSocket.Available];
                        clientSocket.Receive(buffer);

                        finishedParsing = responder.parse(buffer);
                    }

                    // trigger event to get response
                    WebEvent webevent = null;

                    if (responder.Path != null && responder.Path.Length > 1)
                    {
                        webevent = serverManager.GetWebEventById(responder.Path);
                    }

                    if (webevent == null)
                    {
                        webevent = serverManager.DefaultEvent;
                    }

                    responder.webEvent = webevent;

                    webevent.OnWebEventReceived(responder.Path, responder.HttpMethod, responder);
                }
                catch
                {
                }
            }