ObjXMPP  Check-in [a77ad914f2]

Overview
Comment:Make use of multicast delegates in XMPPRoster.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a77ad914f24e65903684dd5db32631f2c701b149d2f57ee927c38c7b93e9706e
User & Date: js on 2012-01-30 17:45:43
Other Links: manifest | tags
Context
2012-02-03
08:04
Update .hgignore. check-in: 62973cfa1b user: js tags: trunk
2012-01-30
22:39
Document XMPPConnectionDelegate check-in: b0ac3cc5eb user: florob@babelmonkeys.de tags: trunk
17:45
Make use of multicast delegates in XMPPRoster. check-in: a77ad914f2 user: js tags: trunk
13:35
Remove the roster delegate if it gets deallocated. check-in: 840e61d391 user: js tags: trunk
Changes

Modified src/XMPPConnection.m from [d30b689c50] to [187819bc21].

302
303
304
305
306
307
308
309

310
311
312
313
314
315
316
302
303
304
305
306
307
308

309
310
311
312
313
314
315
316







-
+







}

- (void)parseBuffer: (const char*)buffer
	 withLength: (size_t)length
{
	if (length < 1) {
		[delegates broadcastSelector: @selector(connectionWasClosed:)
			       forConnection: self];
				  withObject: self];
		return;
	}

	[parser parseBuffer: buffer
		 withLength: length];

	[oldParser release];
372
373
374
375
376
377
378
379

380
381
382
383
384
385
386
372
373
374
375
376
377
378

379
380
381
382
383
384
385
386







-
+







		    exceptionWithClass: isa
				reason: @"No matching identifier"];
}

- (void)sendStanza: (OFXMLElement*)element
{
	[delegates broadcastSelector: @selector(connection:didSendElement:)
		       forConnection: self
			  withObject: self
			  withObject: element];

	[sock writeString: [element XMLString]];
}

-	(void)sendIQ: (XMPPIQ*)iq
  withCallbackObject: (id)object
482
483
484
485
486
487
488
489

490
491
492
493
494
495
496
482
483
484
485
486
487
488

489
490
491
492
493
494
495
496







-
+







		return;

	[element setDefaultNamespace: XMPP_NS_CLIENT];
	[element setPrefix: @"stream"
	      forNamespace: XMPP_NS_STREAM];

	[delegates broadcastSelector: @selector(connection:didReceiveElement:)
		       forConnection: self
			  withObject: self
			  withObject: element];

	if ([[element namespace] isEqual: XMPP_NS_CLIENT])
		[self XMPP_handleStanza: element];

	if ([[element namespace] isEqual: XMPP_NS_STREAM])
		[self XMPP_handleStream: element];
684
685
686
687
688
689
690
691

692
693
694
695
696
697
698
699
700
701
702
703

704
705
706
707
708
709
710
684
685
686
687
688
689
690

691
692
693
694
695
696
697
698
699
700
701
702

703
704
705
706
707
708
709
710







-
+











-
+







{
	if ([[element name] isEqual: @"proceed"]) {
		/* FIXME: Catch errors here */
		SSLSocket *newSock;

		[delegates broadcastSelector: @selector(
						  connectionWillUpgradeToTLS:)
			       forConnection: self];
				  withObject: self];

		newSock = [[SSLSocket alloc] initWithSocket: sock
					     privateKeyFile: privateKeyFile
					    certificateFile: certificateFile];
		[sock release];
		sock = newSock;

		encrypted = YES;

		[delegates broadcastSelector: @selector(
						  connectionDidUpgradeToTLS:)
			       forConnection: self];
				  withObject: self];

		/* Stream restart */
		[self XMPP_startStream];

		return;
	}

740
741
742
743
744
745
746
747

748
749
750
751
752
753
754
740
741
742
743
744
745
746

747
748
749
750
751
752
753
754







-
+








	if ([[element name] isEqual: @"success"]) {
		[authModule continueWithData: [OFDataArray
		    dataArrayWithBase64EncodedString: [element stringValue]]];

		[delegates broadcastSelector: @selector(
						  connectionWWasAuthenticated:)
			       forConnection: self];
				  withObject: self];

		/* Stream restart */
		[self XMPP_startStream];

		return;
	}

773
774
775
776
777
778
779
780

781
782
783
784
785
786
787
788
789
790
791
792
793

794
795
796
797
798
799
800

801
802
803
804
805
806
807
773
774
775
776
777
778
779

780
781
782
783
784
785
786
787
788
789
790
791
792

793
794
795
796
797
798
799

800
801
802
803
804
805
806
807







-
+












-
+






-
+







		[callback runWithIQ: iq];
		[callbacks removeObjectForKey: [iq ID]];
		return;
	}

	handled = [delegates broadcastSelector: @selector(
						    connection:didReceiveIQ:)
				 forConnection: self
				    withObject: self
				    withObject: iq];

	if (!handled && ![[iq type] isEqual: @"error"] &&
	    ![[iq type] isEqual: @"result"]) {
		[self sendStanza: [iq errorIQWithType: @"cancel"
					    condition: @"service-unavailable"]];
	}
}

- (void)XMPP_handleMessage: (XMPPMessage*)message
{
	[delegates broadcastSelector: @selector(connection:didReceiveMessage:)
		       forConnection: self
			  withObject: self
			  withObject: message];
}

- (void)XMPP_handlePresence: (XMPPPresence*)presence
{
	[delegates broadcastSelector: @selector(connection:didReceivePresence:)
		       forConnection: self
			  withObject: self
			  withObject: presence];
}

- (void)XMPP_handleFeatures: (OFXMLElement*)element
{
	OFXMLElement *starttls = [element elementForName: @"starttls"
					       namespace: XMPP_NS_STARTTLS];
964
965
966
967
968
969
970
971

972
973
974
975
976
977
978
964
965
966
967
968
969
970

971
972
973
974
975
976
977
978







-
+








	if (needsSession) {
		[self XMPP_sendSession];
		return;
	}

	[delegates broadcastSelector: @selector(connection:wasBoundToJID:)
		       forConnection: self
			  withObject: self
			  withObject: JID];
}

- (void)XMPP_sendSession
{
	XMPPIQ *iq;

987
988
989
990
991
992
993
994

995
996
997
998
999
1000
1001
987
988
989
990
991
992
993

994
995
996
997
998
999
1000
1001







-
+








- (void)XMPP_handleSession: (XMPPIQ*)iq
{
	if (![[iq type] isEqual: @"result"])
		assert(0);

	[delegates broadcastSelector: @selector(connection:wasBoundToJID:)
		       forConnection: self
			  withObject: self
			  withObject: JID];
}

- (OFString*)XMPP_IDNAToASCII: (OFString*)domain_
{
	OFString *ret;
	char *cDomain;

Modified src/XMPPMulticastDelegate.h from [44c939a254] to [7474d32e16].

16
17
18
19
20
21
22
23

24
25
26
27
28
29
30
31
32
33


34
35

36
37
38


39
16
17
18
19
20
21
22

23
24
25
26
27
28
29
30
31


32
33
34

35
36


37
38
39







-
+








-
-
+
+

-
+

-
-
+
+

 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#import "XMPPConnection.h"
#import <ObjFW/OFObject.h>

@class OFDataArray;

@interface XMPPMulticastDelegate: OFObject
{
	OFDataArray *delegates;
}

- (void)addDelegate: (id <XMPPConnectionDelegate>)delegate;
- (void)removeDelegate: (id <XMPPConnectionDelegate>)delegate;
- (void)addDelegate: (id)delegate;
- (void)removeDelegate: (id)delegate;
- (BOOL)broadcastSelector: (SEL)selector
	    forConnection: (XMPPConnection*)connection;
	       withObject: (id)object;
- (BOOL)broadcastSelector: (SEL)selector
	    forConnection: (XMPPConnection*)connection
	       withObject: (id)object;
	       withObject: (id)object1
	       withObject: (id)object2;
@end

Modified src/XMPPMulticastDelegate.m from [405f29fa95] to [427c4ecc5d].

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
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







-
+




-
+













-
+












-
+






-
-
+
+












-
+





- (void)dealloc
{
	[delegates release];

	[super dealloc];
}

- (void)addDelegate: (id <XMPPConnectionDelegate>)delegate
- (void)addDelegate: (id)delegate
{
	[delegates addItem: &delegate];
}

- (void)removeDelegate: (id<XMPPConnectionDelegate>)delegate
- (void)removeDelegate: (id)delegate
{
	id *cArray = [delegates cArray];
	size_t i, count = [delegates count];

	for (i = 0; i < count; i++) {
		if (cArray[i] == delegate) {
			[delegates removeItemAtIndex: i];
			return;
		}
	}
}

- (BOOL)broadcastSelector: (SEL)selector
	    forConnection: (XMPPConnection*)connection
	       withObject: (id)object
{
	id *cArray = [delegates cArray];
	size_t i, count = [delegates count];
	BOOL handled = NO;

	for (i = 0; i < count; i++) {
		if (![cArray[i] respondsToSelector: selector])
			continue;

		BOOL (*imp)(id, SEL, id) = (BOOL(*)(id, SEL, id))
		    [cArray[i] methodForSelector: selector];

		handled |= imp(cArray[i], selector, connection);
		handled |= imp(cArray[i], selector, object);
	}

	return handled;
}

- (BOOL)broadcastSelector: (SEL)selector
	    forConnection: (XMPPConnection*)connection
	       withObject: (id)object
	       withObject: (id)object1
	       withObject: (id)object2
{
	id *cArray = [delegates cArray];
	size_t i, count = [delegates count];
	BOOL handled = NO;

	for (i = 0; i < count; i++) {
		if (![cArray[i] respondsToSelector: selector])
			continue;

		BOOL (*imp)(id, SEL, id, id) = (BOOL(*)(id, SEL, id, id))
		    [cArray[i] methodForSelector: selector];

		handled |= imp(cArray[i], selector, connection, object);
		handled |= imp(cArray[i], selector, object1, object2);
	}

	return handled;
}
@end

Modified src/XMPPRoster.h from [ad6d9399e3] to [1412762159].

24
25
26
27
28
29
30

31
32
33
34
35
36
37
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38







+







#import <ObjFW/ObjFW.h>

#import "XMPPConnection.h"

@class XMPPRosterItem;
@class XMPPIQ;
@class XMPPRoster;
@class XMPPMulticastDelegate;

@protocol XMPPRosterDelegate
#ifndef XMPP_ROSTER_M
    <OFObject>
#endif
#ifdef OF_HAVE_OPTIONAL_PROTOCOLS
@optional
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
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







-
+


-
-
-
-






-
-
+
+









@interface XMPPRoster: OFObject
#ifdef OF_HAVE_OPTIONAL_PROTOCOLS
    <XMPPConnectionDelegate>
#endif
{
	XMPPConnection *connection;
	OFMutableDictionary *rosterItems;
	id <XMPPRosterDelegate, OFObject> delegate;
	XMPPMulticastDelegate *delegates;
}

#ifdef OF_HAVE_PROPERTIES
@property (assign) id <XMPPRosterDelegate> delegate;
#endif

- initWithConnection: (XMPPConnection*)conn;
- (OFDictionary*)rosterItems;
- (void)requestRoster;
- (void)addRosterItem: (XMPPRosterItem*)rosterItem;
- (void)updateRosterItem: (XMPPRosterItem*)rosterItem;
- (void)deleteRosterItem: (XMPPRosterItem*)rosterItem;
- (void)setDelegate: (id <XMPPRosterDelegate>)delegate;
- (id <XMPPRosterDelegate>)delegate;
- (void)addDelegate: (id <XMPPRosterDelegate>)delegate;
- (void)removeDelegate: (id <XMPPRosterDelegate>)delegate;
- (void)XMPP_addRosterItem: (XMPPRosterItem*)rosterItem;
- (void)XMPP_updateRosterItem: (XMPPRosterItem*)rosterItem;
- (void)XMPP_deleteRosterItem: (XMPPRosterItem*)rosterItem;
- (void)XMPP_handleInitialRoster: (XMPPIQ*)iq;
- (XMPPRosterItem*)XMPP_rosterItemWithXMLElement: (OFXMLElement*)element;
@end

@interface OFObject (XMPPRosterDelegate) <XMPPRosterDelegate>
@end

Modified src/XMPPRoster.m from [299be9c4c8] to [eaf6a2879a].

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
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







+











+







{
	self = [super init];

	@try {
		rosterItems = [[OFMutableDictionary alloc] init];
		connection = connection_;
		[connection addDelegate: self];
		delegates = [[XMPPMulticastDelegate alloc] init];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[connection removeDelegate: self];
	[delegates release];
	[rosterItems release];

	[super dealloc];
}

- (OFDictionary*)rosterItems
{
103
104
105
106
107
108
109
110
111
112
113




114
115
116
117
118
119
120
105
106
107
108
109
110
111




112
113
114
115
116
117
118
119
120
121
122







-
-
-
-
+
+
+
+







		rosterItem = [self XMPP_rosterItemWithXMLElement: element];

		if ([[rosterItem subscription] isEqual: @"remove"])
			[self XMPP_deleteRosterItem: rosterItem];
		else
			[self XMPP_addRosterItem: rosterItem];

		if ([delegate respondsToSelector:
		    @selector(roster:didReceiveRosterItem:)])
			[delegate         roster: self
			    didReceiveRosterItem: rosterItem];
		[delegates broadcastSelector: @selector(
						  roster:didReceiveRosterItem:)
				  withObject: self
				  withObject: rosterItem];
	}

	[connection_ sendStanza: [iq resultIQ]];

	return YES;
}

168
169
170
171
172
173
174
175

176
177

178
179
180

181
182

183
184
185
186
187
188
189
170
171
172
173
174
175
176

177
178

179
180
181

182
183

184
185
186
187
188
189
190
191







-
+

-
+


-
+

-
+








	[query addChild: item];
	[iq addChild: query];

	[connection sendStanza: iq];
}

- (void)setDelegate: (id <XMPPRosterDelegate>)delegate_
- (void)addDelegate: (id <XMPPRosterDelegate>)delegate
{
	delegate = (id <XMPPRosterDelegate, OFObject>)delegate_;
	[delegates addDelegate: delegate];
}

- (id <XMPPRosterDelegate>)delegate
- (void)removeDelegate: (id <XMPPRosterDelegate>)delegate
{
	return delegate;
	[delegates removeDelegate: delegate];
}

- (void)XMPP_addRosterItem: (XMPPRosterItem*)rosterItem
{
	return [self XMPP_updateRosterItem: rosterItem];
}

254
255
256
257
258
259
260
261
262


263
264
265
266
267
268
269
270
271
272
273
274
275
256
257
258
259
260
261
262


263
264
265
266
267
268
269
270
271
272
273
274
275
276
277







-
-
+
+














		if ([[rosterItem subscription] isEqual: @"remove"])
			[self XMPP_deleteRosterItem: rosterItem];
		else
			[self XMPP_addRosterItem: rosterItem];
	}

	if ([delegate respondsToSelector: @selector(rosterWasReceived:)])
		[delegate rosterWasReceived: self];
	[delegates broadcastSelector: @selector(rosterWasReceived:)
			  withObject: self];
}
@end

@implementation OFObject (XMPPRosterDelegate)
- (void)rosterWasReceived: (XMPPRoster*)roster
{
}

-         (void)roster: (XMPPRoster*)roster
  didReceiveRosterItem: (XMPPRosterItem*)rosterItem
{
}
@end

Modified tests/test.m from [4c51fd299e] to [1465dc8600].

93
94
95
96
97
98
99
100

101
102
103
104
105
106
107
93
94
95
96
97
98
99

100
101
102
103
104
105
106
107







-
+







	    [[stanza from] fullJID], [[stanza to] fullJID], [stanza type],
	    [stanza ID]] isEqual: @"bob@localhost, alice@localhost, get, 42"]));

	conn = [[XMPPConnection alloc] init];
	roster = [[XMPPRoster alloc] initWithConnection: conn];

	[conn addDelegate: self];
	[roster setDelegate: self];
	[roster addDelegate: self];

	if ([arguments count] != 3) {
		of_log(@"Invalid count of command line arguments!");
		[OFApplication terminateWithStatus: 1];
	}

	[conn setDomain: [arguments objectAtIndex: 0]];