Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MessageAgent.cs « Novell.Directory.Ldap « Novell.Directory.Ldap « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 55ec2994ad5fb631b4a2f1b36a6de2e5954318c7 (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/******************************************************************************
* The MIT License
* Copyright (c) 2003 Novell Inc.  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.
*******************************************************************************/
//
// Novell.Directory.Ldap.MessageAgent.cs
//
// Author:
//   Sunil Kumar (Sunilk@novell.com)
//
// (C) 2003 Novell, Inc (http://www.novell.com)
//

using System;
using Novell.Directory.Ldap.Utilclass;

namespace Novell.Directory.Ldap
{
	
	/* package */
	class MessageAgent
	{
		private void  InitBlock()
		{
			messages = new MessageVector(5, 5);
		}
		/// <summary> empty and return all messages owned by this agent
		/// 
		/// 
		/// </summary>
		virtual internal System.Object[] MessageArray
		{
			/* package */
			
			get
			{
				return messages.ObjectArray;
			}
			
		}
		/// <summary> Get a list of message ids controlled by this agent
		/// 
		/// </summary>
		/// <returns> an array of integers representing the message ids
		/// </returns>
		virtual internal int[] MessageIDs
		{
			/* package */
			
			get
			{
				int size = messages.Count;
				int[] ids = new int[size];
				Message info;
				
				for (int i = 0; i < size; i++)
				{
					info = (Message) messages[i];
					ids[i] = info.MessageID;
				}
				return ids;
			}
			
		}
		/// <summary> Get the maessage agent number for debugging
		/// 
		/// </summary>
		/// <returns> the agent number
		/// </returns>
		virtual internal System.String AgentName
		{
			/*packge*/
			
			get
			{
				return name;
			}
			
		}
		/// <summary> Get a count of all messages queued</summary>
		virtual internal int Count
		{
			/* package */
			
			get
			{
				int count = 0;
				System.Object[] msgs = messages.ToArray();
				for (int i = 0; i < msgs.Length; i++)
				{
					Message m = (Message) msgs[i];
					count += m.Count;
				}
				return count;
			}
			
		}
		private MessageVector messages;
		private int indexLastRead = 0;
		private static System.Object nameLock; // protect agentNum
		private static int agentNum = 0; // Debug, agent number
		private System.String name; // String name for debug
		
		/* package */
		internal MessageAgent()
		{
			InitBlock();
			// Get a unique agent id for debug
		}
		
		/// <summary> merges two message agents
		/// 
		/// </summary>
		/// <param name="fromAgent">the agent to be merged into this one
		/// </param>
		/* package */
		internal void  merge(MessageAgent fromAgent)
		{
			System.Object[] msgs = fromAgent.MessageArray;
			for (int i = 0; i < msgs.Length; i++)
			{
				messages.Add(msgs[i]);
				((Message) (msgs[i])).Agent = this;
			}
			lock (messages)
			{
				if (msgs.Length > 1)
				{
					System.Threading.Monitor.PulseAll(messages); // wake all threads waiting for messages
				}
				else if (msgs.Length == 1)
				{
					System.Threading.Monitor.Pulse(messages); // only wake one thread
				}
			}
			return ;
		}
		
		
		/// <summary> Wakes up any threads waiting for messages in the message agent
		/// 
		/// </summary>
		/* package */
		internal void  sleepersAwake(bool all)
		{
			lock (messages)
			{
				if (all)
					System.Threading.Monitor.PulseAll(messages);
				else
					System.Threading.Monitor.Pulse(messages);
			}
			return ;
		}
		
		/// <summary> Returns true if any responses are queued for any of the agent's messages
		/// 
		/// return false if no responses are queued, otherwise true
		/// </summary>
		/* package */
		internal bool isResponseReceived()
		{
			int size = messages.Count;
			int next = indexLastRead + 1;
			Message info;
			for (int i = 0; i < size; i++)
			{
				if (next == size)
				{
					next = 0;
				}
				info = (Message) messages[next];
				if (info.hasReplies())
				{
					return true;
				}
			}
			return false;
		}
		
		/// <summary> Returns true if any responses are queued for the specified msgId
		/// 
		/// return false if no responses are queued, otherwise true
		/// </summary>
		/* package */
		internal bool isResponseReceived(int msgId)
		{
			try
			{
				Message info = messages.findMessageById(msgId);
				return info.hasReplies();
			}
			catch (System.FieldAccessException ex)
			{
				return false;
			}
		}
		
		/// <summary> Abandon the request associated with MsgId
		/// 
		/// </summary>
		/// <param name="msgId">the message id to abandon
		/// 
		/// </param>
		/// <param name="cons">constraints associated with this request
		/// </param>
		/* package */
		internal void  Abandon(int msgId, LdapConstraints cons)
		//, boolean informUser)
		{
			Message info = null;
			try
			{
				// Send abandon request and remove from connection list
				info = messages.findMessageById(msgId);
				SupportClass.VectorRemoveElement(messages, info); // This message is now dead
				info.Abandon(cons, null);
				
				return ;
			}
			catch (System.FieldAccessException ex)
			{
			}
			return ;
		}
		
		/// <summary> Abandon all requests on this MessageAgent</summary>
		/* package */
		internal void  AbandonAll()
		{
			int size = messages.Count;
			Message info;
			
			for (int i = 0; i < size; i++)
			{
				info = (Message) messages[i];
				// Message complete and no more replies, remove from id list
				SupportClass.VectorRemoveElement(messages, info);
				info.Abandon(null, null);
			}
			return ;
		}
		
		/// <summary> Indicates whether a specific operation is complete
		/// 
		/// </summary>
		/// <returns> true if a specific operation is complete
		/// </returns>
		/* package */
		internal bool isComplete(int msgid)
		{
			try
			{
				Message info = messages.findMessageById(msgid);
				if (!info.Complete)
				{
					return false;
				}
			}
			catch (System.FieldAccessException ex)
			{
				; // return true, if no message, it must be complete
			}
			return true;
		}
		
		/// <summary> Returns the Message object for a given messageID
		/// 
		/// </summary>
		/// <param name="msgid">the message ID.
		/// </param>
		/* package */
		internal Message getMessage(int msgid)
		{
			return messages.findMessageById(msgid);
		}
		
		/// <summary> Send a request to the server.  A Message class is created
		/// for the specified request which causes the message to be sent.
		/// The request is added to the list of messages being managed by
		/// this agent.
		/// 
		/// </summary>
		/// <param name="conn">the connection that identifies the server.
		/// 
		/// </param>
		/// <param name="msg">the LdapMessage to send
		/// 
		/// </param>
		/// <param name="timeOut">the interval to wait for the message to complete or
		/// <code>null</code> if infinite.
		/// </param>
		/// <param name="queue">the LdapMessageQueue associated with this request.
		/// </param>
		/* package */
		internal void  sendMessage(Connection conn, LdapMessage msg, int timeOut, LdapMessageQueue queue, BindProperties bindProps)
		{
			// creating a messageInfo causes the message to be sent
			// and a timer to be started if needed.
			Message message = new Message(msg, timeOut, conn, this, queue, bindProps);
			messages.Add(message);
			message.sendMessage(); // Now send message to server
			return ;
		}
		
		/// <summary> Returns a response queued, or waits if none queued
		/// 
		/// </summary>
		/* package */
//		internal System.Object getLdapMessage(System.Int32 msgId)
		internal System.Object getLdapMessage(System.Int32 msgId)
		{
			return (getLdapMessage(new Integer32(msgId)));
		}

		internal System.Object getLdapMessage(Integer32 msgId)
		{
			System.Object rfcMsg;
			// If no messages for this agent, just return null
			if (messages.Count == 0)
			{
				return null;
			}
			if ( msgId != null)
			{
				// Request messages for a specific ID
				try
				{
					// Get message for this ID
//					Message info = messages.findMessageById(msgId);
					Message info = messages.findMessageById(msgId.intValue);
					rfcMsg = info.waitForReply(); // blocks for a response
					if (!info.acceptsReplies() && !info.hasReplies())
					{
						// Message complete and no more replies, remove from id list
						SupportClass.VectorRemoveElement(messages, info);
						info.Abandon(null, null); // Get rid of resources
					}
					else
					{
					}
					return rfcMsg;
				}
				catch (System.FieldAccessException ex)
				{
					// no such message id
					return null;
				}
			}
			else
			{
				// A msgId was NOT specified, any message will do
				lock (messages)
				{
					while (true)
					{
						int next = indexLastRead + 1;
						Message info;
						for (int i = 0; i < messages.Count; i++)
						{
							if (next >= messages.Count)
							{
								next = 0;
							}
							info = (Message) messages[next];
							indexLastRead = next++;
							rfcMsg = info.Reply;
							// Check this request is complete
							if (!info.acceptsReplies() && !info.hasReplies())
							{
								// Message complete & no more replies, remove from id list
								SupportClass.VectorRemoveElement(messages, info); // remove from list
								info.Abandon(null, null); // Get rid of resources
								// Start loop at next message that is now moved
								// to the current position in the Vector.
								i -= 1;
							}
							if (rfcMsg != null)
							{
								// We got a reply
								return rfcMsg;
							}
							else
							{
								// We found no reply here
							}
						} // end for loop */
						// Messages can be removed in this loop, we we must
						// check if any messages left for this agent
						if (messages.Count == 0)
						{
							return null;
						}
						
						// No data, wait for something to come in.
						try
						{
							System.Threading.Monitor.Wait(messages);
						}
						catch (System.Threading.ThreadInterruptedException ex)
						{
						}
					} /* end while */
				} /* end synchronized */
			}
		}
		
		/// <summary> Debug code to print messages in message vector</summary>
		private void  debugDisplayMessages()
		{
			return ;
		}
		static MessageAgent()
		{
			nameLock = new System.Object();
		}
	}
}