File: LocalPeerResolverService.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (214 lines) | stat: -rwxr-xr-x 7,136 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// 
// LocalPeerResolverService.cs
// 
// Author: 
//	Atsushi Enomoto  <atsushi@ximian.com>
// 
// Copyright (C) 2009 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if true
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.PeerResolvers;

namespace System.ServiceModel.PeerResolvers
{
	// This implementation of peer resolver should open up node
	// registration to some extent, say, valid within the same machine.
	//
	// A correct implementation should be using something like zeroconf
	// to register the local machine as a peer node.

	class LocalPeerResolverService : ICustomPeerResolverContract
	{
		public LocalPeerResolverService (TextWriter log)
		{
			this.log = log ?? TextWriter.Null;
		}

		TextWriter log;
		Dictionary<string,Mesh> mesh_map = new Dictionary<string,Mesh> ();

		// CustomPeerResolverService delegation

		// Open(), Close(), ControlShape, RefreshInterval, CleanupInterval

		public bool ControlShape { get; set; }
		public TimeSpan RefreshInterval { get; set; }
		public TimeSpan CleanupInterval { get; set; }

		// (internal) ICustomPeerResolverContract implementation

		public PeerServiceSettingsInfo GetCustomServiceSettings ()
		{
			log.WriteLine ("REQUEST: GetCustomServiceSettings");
			return new PeerServiceSettingsInfo () {
				ControlMeshShape = this.ControlShape,
				RefreshInterval = this.RefreshInterval, 
				CleanupInterval = this.CleanupInterval };
		}

		public void SetCustomServiceSettings (PeerServiceSettingsInfo info)
		{
			log.WriteLine ("REQUEST: SetCustomServiceSettings(ControlMeshShape:{0}, RefreshInterval:{1}, CleanupInterval:{2}", info.ControlMeshShape, info.RefreshInterval, info.CleanupInterval);
			ControlShape = info.ControlMeshShape;
			RefreshInterval = info.RefreshInterval;
			CleanupInterval = info.CleanupInterval;
		}

		// IPeerResolverContract implementation

		public ServiceSettingsResponseInfo GetServiceSettings ()
		{
			return new ServiceSettingsResponseInfo () { ControlMeshShape = this.ControlShape };
		}

		public RefreshResponseInfo Refresh (RefreshInfo refreshInfo)
		{
			var r = refreshInfo;
			log.WriteLine ("REQUEST: Refresh (Mesh: {0}, Registraion: {1})", r.MeshId, r.RegistrationId);
			var mesh = GetExistingMesh (r.MeshId);
			var node = mesh.FirstOrDefault (n => n.RegistrationId == r.RegistrationId);
			if (node == null)
				return new RefreshResponseInfo () { Result = RefreshResult.RegistrationNotFound };
			node.Refresh ();
			return new RefreshResponseInfo () { Result = RefreshResult.Success, RegistrationLifetime = RefreshInterval - (DateTime.UtcNow - node.LastRefreshTime) };
		}

		public RegisterResponseInfo Register (RegisterInfo registerInfo)
		{
			var r = registerInfo;
			log.WriteLine ("REQUEST: Register (Mesh: {0}, Client: {1}, NodeAddress: endpoint {2})", r.MeshId, r.ClientId, r.NodeAddress.EndpointAddress);
			Mesh mesh;
			if (!mesh_map.TryGetValue (r.MeshId, out mesh)) {
				mesh = new Mesh (r.MeshId);
				mesh_map.Add (r.MeshId, mesh);
			}
			var node = RegisterNode (mesh, r.ClientId, r.NodeAddress);
			return new RegisterResponseInfo () { RegistrationId = node.RegistrationId };
		}

		public ResolveResponseInfo Resolve (ResolveInfo resolveInfo)
		{
			var r = resolveInfo;
			log.WriteLine ("REQUEST: Resolve (Mesh: {0}, Client: {1}, MaxAddresses: {2})", r.MeshId, r.ClientId, r.MaxAddresses);
			Mesh mesh;
			var rr = new ResolveResponseInfo ();
			if (!mesh_map.TryGetValue (r.MeshId, out mesh))
				return rr;
			// FIXME: find out how to use the argument ClientId.
			// So far, it is used to filter out the registered node from the same ClientId.
			foreach (var node in mesh.TakeWhile (n => n.ClientId != r.ClientId)) {
				rr.Addresses.Add (node.Address);
				if (rr.Addresses.Count == r.MaxAddresses)
					break;
			}
			return rr;
		}

		public void Unregister (UnregisterInfo unregisterInfo)
		{
			var u = unregisterInfo;
			log.WriteLine ("REQUEST: Unregister (Mesh: {0}, Registration: {1})", u.MeshId, u.RegistrationId);
			Mesh mesh = GetExistingMesh (u.MeshId);
			lock (mesh) {
				var node = mesh.GetRegisteredNode (u.RegistrationId);
				mesh.Remove (node);
			}
		}

		public RegisterResponseInfo Update (UpdateInfo updateInfo)
		{
			var u = updateInfo;
			log.WriteLine ("REQUEST: Update (Mesh: {0}, Registration: {1}, NodeAddress:)", u.MeshId, u.RegistrationId, u.NodeAddress);
			var mesh = GetExistingMesh (u.MeshId);
			var node = mesh.GetRegisteredNode (u.RegistrationId);
			node.Update (u.NodeAddress);
			return new RegisterResponseInfo () { RegistrationId = node.RegistrationId };
		}

		Mesh GetExistingMesh (string meshId)
		{
			Mesh mesh;
			if (!mesh_map.TryGetValue (meshId, out mesh))
				throw new InvalidOperationException (String.Format ("Specified mesh {0} does not exist", meshId));
			return mesh;
		}

		Node RegisterNode (Mesh mesh, Guid clientId, PeerNodeAddress addr)
		{
			lock (mesh) {
				var node = new Node () { ClientId = clientId, Address = addr };
				mesh.Add (node);
				node.LastRefreshTime = DateTime.UtcNow;
				return node;
			}
		}
	}

	class Mesh : List<Node>
	{
		public Mesh (string id)
		{
			Id = id;
		}

		public string Id { get; private set; }

		public Node GetRegisteredNode (Guid registrationId)
		{
			var node = this.FirstOrDefault (n => n.RegistrationId == registrationId);
			if (node == null)
				throw new InvalidOperationException (String.Format ("Node with registration Id {0} does not exist in the specified mesh {0}", registrationId, Id));
			return node;
		}
	}

	class Node
	{
		public Node ()
		{
			RegistrationId = Guid.NewGuid ();
		}

		public Guid RegistrationId { get; private set; }
		public Guid ClientId { get; set; }
		public PeerNodeAddress Address { get; set; }
		public DateTime LastRefreshTime { get; set; }

		public void Refresh ()
		{
			LastRefreshTime = DateTime.UtcNow;
		}

		public void Update (PeerNodeAddress addr)
		{
			Address = addr;
			LastRefreshTime = DateTime.UtcNow;
		}
	}
}
#endif