1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright (c) 2003-2012 by AG-Software 											 *
3  * All Rights Reserved.																 *
4  * Contact information for AG-Software is available at http://www.ag-software.de	 *
5  *																					 *
6  * Licence:																			 *
7  * The agsXMPP SDK is released under a dual licence									 *
8  * agsXMPP can be used under either of two licences									 *
9  * 																					 *
10  * A commercial licence which is probably the most appropriate for commercial 		 *
11  * corporate use and closed source projects. 										 *
12  *																					 *
13  * The GNU Public License (GPL) is probably most appropriate for inclusion in		 *
14  * other open source projects.														 *
15  *																					 *
16  * See README.html for details.														 *
17  *																					 *
18  * For general enquiries visit our website at:										 *
19  * http://www.ag-software.de														 *
20  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21 
22 using System;
23 using System.Collections;
24 
25 using agsXMPP.protocol;
26 using agsXMPP.protocol.client;
27 using agsXMPP.Collections;
28 
29 namespace agsXMPP
30 {
MessageCB(object sender, Message msg, object data)31 	public delegate void MessageCB(object sender, Message msg, object data);
32 
33 	public class MessageGrabber : PacketGrabber
34 	{
35 		/// <summary>
36 		///
37 		/// </summary>
38 		/// <param name="conn"></param>
MessageGrabber(XmppClientConnection conn)39 		public MessageGrabber(XmppClientConnection conn)
40 		{
41 			m_connection		= conn;
42 			conn.OnMessage += new MessageHandler(m_connection_OnMessage);
43 		}
44 
Add(Jid jid, MessageCB cb, object cbArg)45 		public void Add(Jid jid, MessageCB cb, object cbArg)
46 		{
47             lock (m_grabbing)
48             {
49                 if (m_grabbing.ContainsKey(jid.ToString()))
50                     return;
51             }
52 
53 			TrackerData td = new TrackerData();
54 			td.cb		= cb;
55 			td.data		= cbArg;
56 			td.comparer = new BareJidComparer();
57 
58             lock (m_grabbing)
59             {
60                 m_grabbing.Add(jid.ToString(), td);
61             }
62 		}
63 
Add(Jid jid, IComparer comparer, MessageCB cb, object cbArg)64 		public void Add(Jid jid, IComparer comparer, MessageCB cb, object cbArg)
65 		{
66             lock (m_grabbing)
67             {
68                 if (m_grabbing.ContainsKey(jid.ToString()))
69                     return;
70             }
71 
72 			TrackerData td = new TrackerData();
73 			td.cb		= cb;
74 			td.data		= cbArg;
75 			td.comparer = comparer;
76 
77             lock (m_grabbing)
78             {
79                 m_grabbing.Add(jid.ToString(), td);
80             }
81 		}
82 
83 		/// <summary>
84 		/// Pending request can be removed.
85 		/// This is useful when a ressource for the callback is destroyed and
86 		/// we are not interested anymore at the result.
87 		/// </summary>
88 		/// <param name="id">ID of the Iq we are not interested anymore</param>
Remove(Jid jid)89 		public void Remove(Jid jid)
90 		{
91             lock (m_grabbing)
92             {
93                 if (m_grabbing.ContainsKey(jid.ToString()))
94                     m_grabbing.Remove(jid.ToString());
95             }
96 		}
97 
98 		private class TrackerData
99 		{
100 			public MessageCB	cb;
101 			public object		data;
102 			// by default the Bare Jid is compared
103 			public IComparer	comparer;
104 		}
105 
106 		/// <summary>
107 		/// A Message is received. Now check if its from a Jid we are looking for and
108 		/// raise the event in this case.
109 		/// </summary>
110 		/// <param name="sender"></param>
111 		/// <param name="msg"></param>
m_connection_OnMessage(object sender, Message msg)112 		private void m_connection_OnMessage(object sender, Message msg)
113 		{
114 			if (msg == null)
115 				return;
116 
117             lock (m_grabbing)
118             {
119 				IDictionaryEnumerator myEnum = m_grabbing.GetEnumerator();
120 
121 				while(myEnum.MoveNext())
122 				{
123 					TrackerData t = myEnum.Value as TrackerData;
124 					if (t.comparer.Compare(new Jid((string)myEnum.Key), msg.From) == 0)
125 					{
126 						// Execute the callback
127 						t.cb(this, msg, t.data);
128 					}
129 				}
130             }
131 		}
132 	}
133 }