4 // Copyright (C) 2004 Novell, Inc.
7 // Fredrik Hedberg (fredrik.hedberg@avafan.com)
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.
34 using System
.Threading
;
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
;
47 using Mono
.P2p
.mDnsResponder
;
48 using Mono
.P2p
.mDnsResponderApi
;
54 namespace Beagle
.Daemon
56 public class NetworkService
58 private static Logger log
= Logger
.Get ("Network");
60 QueryDriver queryDriver
;
61 ArrayList authenticators
;
67 string localAddress
= "EVO";
68 static string serviceName
= "beagle._tcp.local";
70 public NetworkService (QueryDriver queryDriver
, int localPort
)
72 this.queryDriver
= queryDriver
;
75 this.localPort
= localPort
;
77 authenticators
= new ArrayList();
84 log
.Debug ("Network service starting");
86 ScanForAuthenticators ();
89 server
= new TcpListener (localPort
);
91 } catch (Exception e
) {
92 log
.Error("Network service could not be started: {0}",e
);
96 serverThread
= new Thread (new ThreadStart (Listen
));
97 serverThread
.Start ();
99 RegisterWithRendezvous ();
101 log
.Debug ("Network service started");
104 // Shutdown everything
108 log
.Debug ("Network service stopping");
110 serverThread
.Suspend ();
112 log
.Debug ("Network service stopped");
115 // Service handling loop
120 TcpClient client
= server
.AcceptTcpClient ();
121 ServerNetworkHandler handler
= new ServerNetworkHandler (client
, queryDriver
, this);
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?
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");
149 log
.Debug ("Done registering rendezvous service");
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
)))
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
),
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
) {
199 result
.AddRange (authenticator
.Authenticate (hit
, query
));
201 // Do nothing, hit not authenticated
208 public interface IHitAuthenticator
210 ICollection
Authenticate (Hit hit
, QueryBody query
);