Minor fix, adding --port to beagled for networking.
[beagle.git] / beagled / NetworkService.cs
blob7133c6104a21c28396a0edb6a564a146a8638735
1 //
2 // NetworkService.cs
3 //
4 // Copyright (C) 2004 Novell, Inc.
5 //
6 // Authors:
7 // Fredrik Hedberg (fredrik.hedberg@avafan.com)
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining a
12 // copy of this software and associated documentation files (the "Software"),
13 // to deal in the Software without restriction, including without limitation
14 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 // and/or sell copies of the Software, and to permit persons to whom the
16 // Software is furnished to do so, subject to the following conditions:
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 // DEALINGS IN THE SOFTWARE.
30 using System;
31 using System.IO;
32 using System.Xml;
33 using System.Text;
34 using System.Threading;
36 using System.Net;
37 using System.Net.Sockets;
39 using System.Reflection;
40 using System.Collections;
42 using System.Runtime.Remoting;
43 using System.Runtime.Remoting.Channels;
44 using System.Runtime.Remoting.Channels.Tcp;
46 #if ENABLE_RENDEZVOUS
47 using Mono.P2p.mDnsResponder;
48 using Mono.P2p.mDnsResponderApi;
49 #endif
51 using Beagle;
52 using Beagle.Util;
54 namespace Beagle.Daemon
56 public class NetworkService
58 private static Logger log = Logger.Get ("Network");
60 QueryDriver queryDriver;
61 ArrayList authenticators;
63 TcpListener server;
64 Thread serverThread;
66 int localPort = 8505;
67 string localAddress = "EVO";
68 static string serviceName = "beagle._tcp.local";
70 public NetworkService (QueryDriver queryDriver, int localPort)
72 this.queryDriver = queryDriver;
74 if (localPort != 0)
75 this.localPort = localPort;
77 authenticators = new ArrayList();
80 // Start the show
82 public void Start ()
84 log.Debug ("Network service starting");
86 ScanForAuthenticators ();
88 try {
89 server = new TcpListener (localPort);
90 server.Start();
91 } catch (Exception e) {
92 log.Error("Network service could not be started: {0}",e);
93 return;
96 serverThread = new Thread (new ThreadStart (Listen));
97 serverThread.Start ();
99 RegisterWithRendezvous ();
101 log.Debug ("Network service started");
104 // Shutdown everything
106 public void Stop ()
108 log.Debug ("Network service stopping");
110 serverThread.Suspend ();
112 log.Debug ("Network service stopped");
115 // Service handling loop
117 void Listen ()
119 while (true) {
120 TcpClient client = server.AcceptTcpClient ();
121 ServerNetworkHandler handler = new ServerNetworkHandler (client, queryDriver, this);
122 handler.Start ();
126 // Register with rendezvous
128 void RegisterWithRendezvous ()
130 #if ENABLE_RENDEZVOUS
131 log.Debug ("Starting rendezvous service");
133 // Should we start the mDnsResponder service if it's not running?
135 try {
136 IRemoteFactory factory = (IRemoteFactory) Activator.GetObject (typeof (IRemoteFactory),
137 "tcp://localhost:8091/mDnsRemoteFactory.tcp");
139 IResourceRegistration rr = factory.GetRegistrationInstance ();
141 rr.RegisterServiceLocation (localAddress,serviceName, localPort, 0, 0);
143 catch (RemotingException e)
145 log.Debug ("No multicast service found, not advertising via rendezvous");
147 finally
149 log.Debug ("Done registering rendezvous service");
151 #endif
154 // Scan assembly for authenticators
156 void ScanForAuthenticators ()
158 log.Debug ("Scanning for network authenticators");
160 authenticators.Clear ();
162 foreach (Type type in Assembly.GetCallingAssembly ().GetTypes ()) {
164 if ( !TypeImplementsInterface (type, typeof (IHitAuthenticator)))
165 continue;
167 log.Debug ("Found network authenticator: {0}", type.ToString ());
169 IHitAuthenticator authenticator = (IHitAuthenticator) Activator.CreateInstance (type);
171 authenticators.Add (authenticator);
174 log.Debug ("Done scanning for network authenticators");
177 // From QueryDriver.cs
179 static bool ThisApiSoVeryIsBroken (Type m, object criteria)
181 return m == (Type) criteria;
184 static bool TypeImplementsInterface (Type t, Type iface)
186 Type[] impls = t.FindInterfaces (new TypeFilter (ThisApiSoVeryIsBroken),
187 iface);
188 return impls.Length > 0;
191 // Authenticate a hit
193 public ArrayList AuthenticateHit (Hit hit, QueryBody query)
195 ArrayList result = new ArrayList ();
197 foreach (IHitAuthenticator authenticator in authenticators) {
198 try {
199 result.AddRange (authenticator.Authenticate (hit, query));
200 } catch {
201 // Do nothing, hit not authenticated
204 return result;
208 public interface IHitAuthenticator
210 ICollection Authenticate (Hit hit, QueryBody query);