Exemplo n.º 1
0
		public override ResponseMessage Execute (RequestMessage raw_request)
		{
			RemoteIndexerRequest remote_request = (RemoteIndexerRequest) raw_request;

			IndexHelperTool.ReportActivity ();

			// Find the appropriate driver for this request.
			LuceneIndexingDriver indexer;
			lock (indexer_table) {
				indexer = (LuceneIndexingDriver) indexer_table [remote_request.RemoteIndexName];

				if (indexer == null) {
					indexer = new LuceneIndexingDriver (remote_request.RemoteIndexName,
									    remote_request.RemoteIndexMinorVersion);
					indexer.DisableTextCache = IndexHelperTool.DisableTextCache;

					indexer_table [remote_request.RemoteIndexName] = indexer;

					indexer.FileFilterNotifier += delegate (Uri display_uri, Uri content_uri, Filter filter) {
						IndexHelperTool.ReportActivity ();
						IndexHelperTool.CurrentDisplayUri = display_uri;
						IndexHelperTool.CurrentContentUri = content_uri;
						IndexHelperTool.CurrentFilter = filter;
					};
				}
			}

			IndexerReceipt [] receipts = null;
			int item_count = 0;

			try {
				if (remote_request.Request != null) // If we just want the item count, this will be null
					receipts = indexer.Flush (remote_request.Request);
				item_count = indexer.GetItemCount ();
			} catch (Exception e) {
				// Send error response
				++Count;
				return new ErrorResponse (e);
			}

			// Construct a response containing the item count and
			// the receipts produced by the actual indexing.
			RemoteIndexerResponse response = new RemoteIndexerResponse ();
			response.ItemCount = item_count;
			response.Receipts = receipts;

			++Count;

			IndexHelperTool.ReportActivity ();

			return response;
		}
Exemplo n.º 2
0
		protected void SendRequest (RequestMessage request, Stream stream)
		{
			// The socket may be shut down at some point here.  It
			// is the caller's responsibility to handle the error
			// correctly.
#if ENABLE_XML_DUMP
			MemoryStream mem_stream = new MemoryStream ();
			XmlFu.SerializeUtf8 (req_serializer, mem_stream, new RequestWrapper (request));
			mem_stream.Seek (0, SeekOrigin.Begin);
			StreamReader r = new StreamReader (mem_stream);
			Logger.Log.Debug ("Sending request:\n{0}\n", r.ReadToEnd ());
			mem_stream.Seek (0, SeekOrigin.Begin);
			mem_stream.WriteTo (stream);
			mem_stream.Close ();
#else
			XmlFu.SerializeUtf8 (req_serializer, stream, new RequestWrapper (request));
#endif
			// Send end of message marker
			stream.WriteByte (0xff);
			stream.Flush ();
		}
Exemplo n.º 3
0
		protected override void SendRequest (RequestMessage request)
		{
			client = new UnixClient (this.socket_name);
			client.SendBufferSize = 4096;
			client.ReceiveBufferSize = 4096;
			NetworkStream stream = client.GetStream ();
			
			base.SendRequest (request, stream);
		}
Exemplo n.º 4
0
		public override ResponseMessage Send (RequestMessage request)
		{
			if (request.Keepalive)
				throw new Exception ("A blocking connection on a keepalive request is not allowed");

			Exception throw_me = null;

			try {
				SendRequest (request);
			} catch (IOException e) {
				throw_me = e;
			} catch (SocketException e) {
				throw_me = e;
			}

			if (throw_me != null)
				throw new ResponseMessageException (throw_me);

			NetworkStream stream = this.client.GetStream ();
			int bytes_read, end_index = -1;

			do {
				bytes_read = stream.Read (this.network_data, 0, 4096);

				//Logger.Log.Debug ("Read {0} bytes", bytes_read);

				if (bytes_read > 0) {
					// 0xff signifies end of message
					end_index = Array.IndexOf<byte> (this.network_data, (byte) 0xff);
					
					this.BufferStream.Write (this.network_data, 0,
								  end_index == -1 ? bytes_read : end_index);
				}
			} while (bytes_read > 0 && end_index == -1);

			// It's possible that the server side shut down the
			// connection before we had a chance to read any data.
			// If this is the case, throw a rather descriptive
			// exception.
			if (this.BufferStream.Length == 0) {
				this.BufferStream.Close ();
				throw new ResponseMessageException ("Socket was closed before any data could be read");
			}

			this.BufferStream.Seek (0, SeekOrigin.Begin);

#if ENABLE_XML_DUMP
			StreamReader dump_reader = new StreamReader (this.BufferStream);
			Logger.Log.Debug ("Received response:\n{0}\n", dump_reader.ReadToEnd ());
			this.BufferStream.Seek (0, SeekOrigin.Begin);
#endif
			
			ResponseMessage resp = null;

			try {
				ResponseWrapper wrapper = (ResponseWrapper)resp_serializer.Deserialize (this.BufferStream);
				resp = wrapper.Message;
			} catch (Exception e) {
				this.BufferStream.Seek (0, SeekOrigin.Begin);
				StreamReader r = new StreamReader (this.BufferStream);
				throw_me = new ResponseMessageException (e, "Exception while deserializing response", String.Format ("Message contents: '{0}'", r.ReadToEnd ()));
				this.BufferStream.Seek (0, SeekOrigin.Begin);
			}

			this.BufferStream.Close ();

			if (throw_me != null)
				throw throw_me;
			
			return resp;
		}
Exemplo n.º 5
0
		public override void SendAsyncBlocking (RequestMessage request)
		{
			Exception ex = null;

			try {
				SendRequest (request);
			} catch (IOException e) {
				ex = e;
			} catch (SocketException e) {
				ex = e;
			}

			if (ex != null) {
				ResponseMessage resp = new ErrorResponse (ex);				
				InvokeAsyncResponseEvent (resp);
				return;
			}
			
			NetworkStream stream = this.client.GetStream ();
			MemoryStream deserialize_stream = new MemoryStream ();

			// This buffer is annoyingly small on purpose, to avoid
			// having to deal with the case of multiple messages
			// in a single block.
			byte [] buffer = new byte [32];

			while (!this.IsClosed) {

				Array.Clear (buffer, 0, buffer.Length);

				int bytes_read;
				bytes_read = stream.Read (buffer, 0, buffer.Length);
				if (bytes_read == 0)
					break;

				int end_index;
				end_index = Array.IndexOf<byte> (buffer, (byte) 0xff);

				if (end_index == -1) {
					deserialize_stream.Write (buffer, 0, bytes_read);
				} else {
					deserialize_stream.Write (buffer, 0, end_index);
					deserialize_stream.Seek (0, SeekOrigin.Begin);

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

					ResponseMessage resp;
					try {
						ResponseWrapper wrapper;
						wrapper = (ResponseWrapper) resp_serializer.Deserialize (deserialize_stream);
						
						resp = wrapper.Message;
					} catch (Exception e) {
						resp = new ErrorResponse (e);
					}

					InvokeAsyncResponseEvent (resp);

					deserialize_stream.Close ();
					deserialize_stream = new MemoryStream ();
					if (bytes_read - end_index - 1 > 0)
						deserialize_stream.Write (buffer, end_index + 1, bytes_read - end_index - 1);
				}
			}
		}
Exemplo n.º 6
0
		public RequestWrapper (RequestMessage request)
		{
			this.Message = request;
		}
Exemplo n.º 7
0
		public abstract ResponseMessage Execute (RequestMessage req);
Exemplo n.º 8
0
		protected abstract void SendRequest (RequestMessage request);
Exemplo n.º 9
0
		public abstract void SendAsyncBlocking (RequestMessage request);
Exemplo n.º 10
0
		public abstract ResponseMessage Send (RequestMessage request);
Exemplo n.º 11
0
		public void SendAsync (RequestMessage request)
		{
			Exception exception = null;

			try {
				SendRequest (request);
			} catch (IOException e) {
				exception = e;
			} catch (SocketException e) {
				exception = e;
			}

			if (exception == null) {
				BeginRead ();
				return;
			}

			ResponseMessage response = new ErrorResponse (exception);
			
			if (AsyncResponse != null)
				AsyncResponse (response);
		}