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 {
PresenceCB(object sender, Presence pres, object data)31 	public delegate void PresenceCB(object sender, Presence pres, object data);
32 
33 	public class PresenceGrabber : PacketGrabber
34 	{
35         /// <summary>
36         /// Initializes a new instance of the <see cref="PresenceGrabber"/> class.
37         /// </summary>
38         /// <param name="conn">The conn.</param>
PresenceGrabber(XmppClientConnection conn)39 		public PresenceGrabber(XmppClientConnection conn)
40 		{
41 			m_connection		= conn;
42 			conn.OnPresence += new PresenceHandler(m_connection_OnPresence);
43 		}
44 
Add(Jid jid, PresenceCB cb, object cbArg)45 		public void Add(Jid jid, PresenceCB 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 
64         /// <summary>
65         /// Adds the specified jid.
66         /// </summary>
67         /// <param name="jid">The jid.</param>
68         /// <param name="comparer">The comparer.</param>
69         /// <param name="cb">The callback.</param>
70         /// <param name="cbArg">The callback Arguments.</param>
Add(Jid jid, IComparer comparer, PresenceCB cb, object cbArg)71 		public void Add(Jid jid, IComparer comparer, PresenceCB cb, object cbArg)
72 		{
73             lock (m_grabbing)
74             {
75                 if (m_grabbing.ContainsKey(jid.ToString()))
76                     return;
77             }
78 
79 			TrackerData td = new TrackerData();
80 			td.cb		= cb;
81 			td.data		= cbArg;
82 			td.comparer = comparer;
83 
84             lock (m_grabbing)
85             {
86                 m_grabbing.Add(jid.ToString(), td);
87             }
88 		}
89 
90 		/// <summary>
91 		/// Pending request can be removed.
92 		/// This is useful when a ressource for the callback is destroyed and
93 		/// we are not interested anymore at the result.
94 		/// </summary>
95 		/// <param name="id">ID of the Iq we are not interested anymore</param>
Remove(Jid jid)96 		public void Remove(Jid jid)
97 		{
98             lock (m_grabbing)
99             {
100                 if (m_grabbing.ContainsKey(jid.ToString()))
101                     m_grabbing.Remove(jid.ToString());
102             }
103 		}
104 
105 		private class TrackerData
106 		{
107 			public PresenceCB	cb;
108 			public object		data;
109 			// by default the Bare Jid is compared
110 			public IComparer	comparer;
111 
112 		}
113 
114 		/// <summary>
115 		/// A presence is received. Now check if its from a Jid we are looking for and
116 		/// raise the event in this case.
117 		/// </summary>
118 		/// <param name="sender"></param>
119 		/// <param name="pres"></param>
m_connection_OnPresence(object sender, Presence pres)120 		private void m_connection_OnPresence(object sender, Presence pres)
121 		{
122 			if (pres == null)
123 				return;
124 
125 			lock (m_grabbing)
126 			{
127 				IDictionaryEnumerator myEnum = m_grabbing.GetEnumerator();
128 
129 				while(myEnum.MoveNext())
130 				{
131 					TrackerData t = myEnum.Value as TrackerData;
132 					if (t.comparer.Compare(new Jid((string)myEnum.Key), pres.From) == 0)
133 					{
134 						// Execute the callback
135 						t.cb(this, pres, t.data);
136 					}
137 				}
138 			}
139 		}
140 	}
141 }
142