Beispiel #1
0
        static internal RequestMessageExecutor GetExecutor(RequestMessage req)
        {
            Type req_type = req.GetType();

            RequestMessageExecutor exec = null;

            RequestMessageHandler handler;

            handler = request_type_to_handler [req_type] as RequestMessageHandler;

            if (handler != null)
            {
                exec = new SimpleRequestMessageExecutor(handler);
            }
            else
            {
                Type t = request_type_to_executor_type [req_type] as Type;
                if (t != null)
                {
                    exec = (RequestMessageExecutor)Activator.CreateInstance(t);
                }
            }

            return(exec);
        }
Beispiel #2
0
        public void HandleConnection(Stream buffer_stream)
        {
            this.thread = Thread.CurrentThread;

            RequestMessage  req  = null;
            ResponseMessage resp = null;

            bool force_close_connection = false;

#if ENABLE_XML_DUMP
            StreamReader r = new StreamReader(buffer_stream);
            Logger.Log.Debug("Received request:\n{0}\n", r.ReadToEnd());
            buffer_stream.Seek(0, SeekOrigin.Begin);
#endif

            try {
                RequestWrapper wrapper = (RequestWrapper)req_serializer.Deserialize(buffer_stream);

                req = wrapper.Message;
            } catch (InvalidOperationException e) {
                // Undocumented: Xml Deserialization exceptions
                if (e.InnerException != null)
                {
                    resp = new ErrorResponse(e.InnerException);
                }
                else
                {
                    resp = new ErrorResponse(e);
                }
                force_close_connection = true;
            } catch (Exception e) {
                resp = new ErrorResponse(e);
                force_close_connection = true;
            }

            // If XmlSerializer can't deserialize the payload, we
            // may get a null payload and not an exception.  Or
            // maybe the client just didn't send one.
            if (req == null && resp == null)
            {
                resp = new ErrorResponse("Missing payload");
                force_close_connection = true;
            }

            // And if there are no errors, execute the command
            if (resp == null)
            {
                RequestMessageExecutor exec;
                exec = Server.GetExecutor(req);

                if (exec == null)
                {
                    resp = new ErrorResponse(String.Format("No handler available for {0}", req.GetType()));
                    force_close_connection = true;
                }
                else if (req.Keepalive)
                {
                    this.executor            = exec;
                    exec.AsyncResponseEvent += OnAsyncResponse;
                }

                if (exec != null)
                {
                    try {
                        resp = exec.Execute(req);
                    } catch (Exception e) {
                        Log.Warn(e, "Caught exception trying to execute {0}.  Sending error response", exec.GetType());
                        resp = new ErrorResponse(e);
                    }
                }
            }

            // It's okay if the response is null; this means
            // that keepalive is set and that we'll be sending
            // back responses asynchronously.  First, enforce
            // that the response is not null if keepalive isn't
            // set.
            if (resp == null && !req.Keepalive)
            {
                resp = new ErrorResponse("No response available, but keepalive is not set");
            }

            if (resp != null)
            {
                //Logger.Log.Debug ("Sending response of type {0}", resp.GetType ());
                if (!this.SendResponse(resp))
                {
                    force_close_connection = true;
                }
            }

cleanup:
            buffer_stream.Close();

            if (force_close_connection || !req.Keepalive)
            {
                Close();
            }
            else
            {
                SetupWatch();
            }

            // Release our reference to Thread.CurrentThread, which
            // is a static instance (although thread-local)
            this.thread = null;
        }
Beispiel #3
0
		public void HandleConnection (Stream buffer_stream)
		{
			this.thread = Thread.CurrentThread;

			RequestMessage req = null;
			ResponseMessage resp = null;

			bool force_close_connection = false;
			
#if ENABLE_XML_DUMP
			StreamReader r = new StreamReader (buffer_stream);
			Logger.Log.Debug ("Received request:\n{0}\n", r.ReadToEnd ());
			buffer_stream.Seek (0, SeekOrigin.Begin);
#endif

			try {
				RequestWrapper wrapper = (RequestWrapper) req_serializer.Deserialize (buffer_stream);
				
				req = wrapper.Message;
			} catch (InvalidOperationException e) {
				// Undocumented: Xml Deserialization exceptions
				if (e.InnerException != null)
					resp = new ErrorResponse (e.InnerException);
				else
					resp = new ErrorResponse (e);
				force_close_connection = true;
			} catch (Exception e) {
				resp = new ErrorResponse (e);
				force_close_connection = true;
			}

			// If XmlSerializer can't deserialize the payload, we
			// may get a null payload and not an exception.  Or
			// maybe the client just didn't send one.
			if (req == null && resp == null) {
				resp = new ErrorResponse ("Missing payload");
				force_close_connection = true;
			}

			// And if there are no errors, execute the command
			if (resp == null) {

				RequestMessageExecutor exec;
				exec = Server.GetExecutor (req);

				if (exec == null) {
					resp = new ErrorResponse (String.Format ("No handler available for {0}", req.GetType ()));
					force_close_connection = true;
				} else if (req.Keepalive) {
					this.executor = exec;
					exec.AsyncResponseEvent += OnAsyncResponse;
				}

				if (exec != null) {
					try {
						resp = exec.Execute (req);
					} catch (Exception e) {
						Log.Warn (e, "Caught exception trying to execute {0}.  Sending error response", exec.GetType ());
						resp = new ErrorResponse (e);
					}
				}
			}

			// It's okay if the response is null; this means
			// that keepalive is set and that we'll be sending
			// back responses asynchronously.  First, enforce
			// that the response is not null if keepalive isn't
			// set.
			if (resp == null && !req.Keepalive)
				resp = new ErrorResponse ("No response available, but keepalive is not set");

			if (resp != null) {
				//Logger.Log.Debug ("Sending response of type {0}", resp.GetType ());
				if (!this.SendResponse (resp))
					force_close_connection = true;
			}

		cleanup:
			buffer_stream.Close ();

			if (force_close_connection || !req.Keepalive)
				Close ();
			else
				SetupWatch ();

			// Release our reference to Thread.CurrentThread, which
			// is a static instance (although thread-local)
			this.thread = null;
		}
Beispiel #4
0
		static public void DoQuery (Query                                query,
					    QueryResult                          result,
					    RequestMessageExecutor.AsyncResponse send_response)
		{
			DehumanizeQuery (query);

			SearchTermResponse search_term_response;
			search_term_response = AssembleSearchTermResponse (query);
			send_response (search_term_response);

			QueryEachQueryable (query, result);
		}