Index: trunk/mwsearch/Daemon/Daemon.cs |
— | — | @@ -47,34 +47,22 @@ |
48 | 48 | |
49 | 49 | private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); |
50 | 50 | |
51 | | - private static Queue openConnections; |
52 | | - |
53 | 51 | public static void Main(string[] args) { |
54 | 52 | Console.WriteLine( |
55 | 53 | "MediaWiki Lucene search indexer - runtime search daemon.\n" + |
56 | | - "Version 20050103, copyright 2004 Kate Turner.\n" |
| 54 | + "Version 20050517, copyright 2004 Kate Turner.\n" |
57 | 55 | ); |
58 | | - /* |
59 | 56 | int i = 0; |
60 | | - while (i < args.length - 1) { |
61 | | - if (args[i].equals("-port")) |
62 | | - port = Integer.valueOf(args[++i]).intValue(); |
63 | | - else if (args[i].equals("-configfile")) |
64 | | - Configuration.setConfigFile(args[++i]); |
65 | | - else if (args[i].equals("-mwversion")) { |
66 | | - String vers = args[++i]; |
67 | | - if (vers.equals("old")) |
68 | | - mw_version = MW_OLD; |
69 | | - else if (vers.equals("new")) |
70 | | - mw_version = MW_NEW; |
71 | | - else { |
72 | | - System.err.println("Unknown MediaWiki version " + vers); |
73 | | - return; |
74 | | - } |
75 | | - } else break; |
| 57 | + while (i < args.Length - 1) { |
| 58 | + if (args[i].Equals("--port")) { |
| 59 | + port = Int32.Parse(args[++i]); |
| 60 | + } else if (args[i].Equals("--configfile")) { |
| 61 | + Configuration.SetConfigFile(args[++i]); |
| 62 | + } else { |
| 63 | + break; |
| 64 | + } |
76 | 65 | ++i; |
77 | 66 | } |
78 | | - */ |
79 | 67 | config = Configuration.Open(); |
80 | 68 | |
81 | 69 | indexPath = config.GetString("mwsearch", "indexpath"); |
— | — | @@ -88,26 +76,13 @@ |
89 | 77 | } |
90 | 78 | sock.Start(); |
91 | 79 | |
| 80 | + /* |
92 | 81 | log.Debug("Blah blah debug"); |
93 | 82 | log.Info("Blah blah info"); |
94 | 83 | log.Error("Blah blah error"); |
95 | 84 | log.Fatal("Blah blah fatal"); |
| 85 | + */ |
96 | 86 | |
97 | | - Queue xxx = new Queue(); |
98 | | - openConnections = Queue.Synchronized(xxx); |
99 | | - |
100 | | - // start some worker threads |
101 | | - int maxThreads = 25; |
102 | | - IList threads = new ArrayList(); |
103 | | - for (int i = 0; i < maxThreads; i++) { |
104 | | - log.Debug("Starting worker thread #" + i); |
105 | | - Worker worker = new Worker(i); |
106 | | - ThreadStart start = new ThreadStart(worker.Run); |
107 | | - Thread thread = new Thread(start); |
108 | | - threads.Add(thread); |
109 | | - thread.Start(); |
110 | | - } |
111 | | - |
112 | 87 | // go! |
113 | 88 | for (;;) { |
114 | 89 | TcpClient client; |
— | — | @@ -118,21 +93,10 @@ |
119 | 94 | log.Error("accept() error: " + e.Message); |
120 | 95 | continue; |
121 | 96 | } |
122 | | - lock (openConnections) { |
123 | | - log.Debug("Queueing connection..."); |
124 | | - openConnections.Enqueue(client); |
125 | | - Monitor.Pulse(openConnections); |
126 | | - } |
| 97 | + Worker worker = new Worker(client.GetStream()); |
| 98 | + ThreadPool.QueueUserWorkItem(worker.Run); |
127 | 99 | } |
128 | 100 | |
129 | 101 | } |
130 | | - |
131 | | - public static TcpClient NextClient() { |
132 | | - lock (openConnections) { |
133 | | - Monitor.Wait(openConnections); |
134 | | - TcpClient client = (TcpClient)openConnections.Dequeue(); |
135 | | - return client; |
136 | | - } |
137 | | - } |
138 | 102 | } |
139 | 103 | } |
Index: trunk/mwsearch/Daemon/Worker.cs |
— | — | @@ -52,8 +52,6 @@ |
53 | 53 | private static readonly Encoding utf8 = new UTF8Encoding(); |
54 | 54 | |
55 | 55 | |
56 | | - /** A socket for our client. */ |
57 | | - TcpClient client; |
58 | 56 | /** The search term after urlencoding */ |
59 | 57 | string searchterm; |
60 | 58 | /** Client input stream */ |
— | — | @@ -72,55 +70,46 @@ |
73 | 71 | bool headersSent; |
74 | 72 | |
75 | 73 | // lucene special chars: + - && || ! ( ) { } [ ] ^ " ~ * ? : \. |
| 74 | + /* |
76 | 75 | static string[] specialChars = { |
77 | 76 | "\\+", "-", "&&", "\\|\\|", "!", "\\(", "\\)", "\\{", "\\}", "\\[", "\\]", |
78 | 77 | "\\^", "\"", "~", "\\*", "\\?", ":", "\\\\" |
79 | 78 | //"\\(", "\\)" |
80 | 79 | }; |
| 80 | + */ |
81 | 81 | |
82 | | - public Worker(TcpClient newclient) { |
83 | | - this.client = newclient; |
| 82 | + public Worker(Stream stream) { |
| 83 | + istrm = new StreamReader(stream); |
| 84 | + ostrm = new StreamWriter(stream); |
84 | 85 | } |
85 | | - public Worker(int i) { |
86 | | - num = i; |
| 86 | + |
| 87 | + public void Run(object par) { |
| 88 | + Run(); |
87 | 89 | } |
88 | | - int num; |
89 | 90 | |
90 | 91 | public void Run() { |
91 | | - Console.WriteLine("wtf" + num); |
92 | | - log.Info("starting handler #" + num); |
93 | 92 | //using (log4net.NDC.Push(client.Client.RemoteEndPoint)) { |
94 | | - for (;;) { |
95 | | - client = Daemon.NextClient(); |
96 | | - using (log4net.NDC.Push("thread" + num)) { |
97 | | - log.Debug("Thread " + num + " accepted a client"); |
98 | | - headersSent = false; |
| 93 | + headersSent = false; |
| 94 | + try { |
| 95 | + Handle(); |
| 96 | + log.Debug("request handled."); |
| 97 | + } catch (IOException e) { |
| 98 | + log.Error("net error: " + e.Message); |
| 99 | + } catch (Exception e) { |
| 100 | + log.Error(e); |
| 101 | + } finally { |
| 102 | + if (!headersSent) { |
99 | 103 | try { |
100 | | - Handle(); |
101 | | - log.Debug("request handled."); |
102 | | - } catch (IOException e) { |
103 | | - log.Error("net error: " + e.Message); |
104 | | - } catch (Exception e) { |
105 | | - log.Error(e); |
106 | | - } finally { |
107 | | - if (!headersSent) { |
108 | | - try { |
109 | | - SendHeaders(500, "Internal server error"); |
110 | | - } catch (IOException e) { } |
111 | | - } |
112 | | - // Make sure the client is closed out. |
113 | | - try { ostrm.Close(); } catch { } |
114 | | - try { istrm.Close(); } catch { } |
115 | | - try { client.Close(); } catch { } |
116 | | - } |
| 104 | + SendHeaders(500, "Internal server error"); |
| 105 | + } catch (IOException e) { } |
117 | 106 | } |
| 107 | + // Make sure the client is closed out. |
| 108 | + try { ostrm.Close(); } catch { } |
| 109 | + try { istrm.Close(); } catch { } |
118 | 110 | } |
119 | 111 | } |
120 | 112 | |
121 | 113 | private void Handle() { |
122 | | - istrm = new StreamReader(client.GetStream()); |
123 | | - ostrm = new StreamWriter(client.GetStream()); |
124 | | - |
125 | 114 | /* Simple HTTP protocol; accepts GET requests only. |
126 | 115 | * URL path format is /operation/database/searchterm |
127 | 116 | * The path should be URL-encoded UTF-8 (standard IRI). |
Index: trunk/mwsearch/Search.mds |
— | — | @@ -17,7 +17,7 @@ |
18 | 18 | <Entry build="False" name="Tests" /> |
19 | 19 | </Configuration> |
20 | 20 | </Configurations> |
21 | | - <StartMode startupentry="Benchmark" single="True"> |
| 21 | + <StartMode startupentry="Daemon" single="True"> |
22 | 22 | <Execute type="None" entry="Search" /> |
23 | 23 | <Execute type="None" entry="Updater" /> |
24 | 24 | <Execute type="Execute" entry="Daemon" /> |