Skip to content

Commit 24bd4cf

Browse files
author
Andres Canal
committed
added support for XEP-0030 with tests
1 parent cc83f0b commit 24bd4cf

File tree

6 files changed

+365
-1
lines changed

6 files changed

+365
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// XMPPServiceDiscovery.h
3+
// Mangosta
4+
//
5+
// Created by Andres Canal on 4/27/16.
6+
// Copyright © 2016 Inaka. All rights reserved.
7+
//
8+
9+
#import <XMPPFramework/XMPPFramework.h>
10+
11+
@class XMPPIDTracker;
12+
13+
@interface XMPPServiceDiscovery : XMPPModule {
14+
XMPPIDTracker *xmppIDTracker;
15+
}
16+
17+
- (void)discoverInformationAboutJID:(XMPPJID *)jid;
18+
- (void)discoverItemsAssociatedWithJID:(XMPPJID *)jid;
19+
20+
@end
21+
22+
@protocol XMPPServiceDiscoveryDelegate
23+
24+
@optional
25+
26+
- (void)xmppServiceDiscovery:(XMPPServiceDiscovery *)sender didDiscoverInformation:(NSArray *)items;
27+
- (void)xmppServiceDiscovery:(XMPPServiceDiscovery *)sender didDiscoverItems:(NSArray *)items;
28+
29+
- (void)xmppServiceDiscovery:(XMPPServiceDiscovery *)sender didFailToDiscover:(XMPPIQ *)iq;
30+
31+
@end
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//
2+
// XMPPServiceDiscovery.m
3+
// Mangosta
4+
//
5+
// Created by Andres Canal on 4/27/16.
6+
// Copyright © 2016 Inaka. All rights reserved.
7+
//
8+
9+
#define XMLNS_DISCO_ITEMS @"http://jabber.org/protocol/disco#items"
10+
#define XMLNS_DISCO_INFO @"http://jabber.org/protocol/disco#info"
11+
#import "XMPPServiceDiscovery.h"
12+
#import "XMPPIDTracker.h"
13+
14+
@interface XMPPServiceDiscovery()
15+
@property BOOL discoveringInfo;
16+
@end
17+
18+
@implementation XMPPServiceDiscovery
19+
20+
- (BOOL)activate:(XMPPStream *)aXmppStream {
21+
22+
if ([super activate:aXmppStream]) {
23+
xmppIDTracker = [[XMPPIDTracker alloc] initWithDispatchQueue:moduleQueue];
24+
25+
return YES;
26+
}
27+
28+
return NO;
29+
}
30+
31+
- (void)deactivate {
32+
dispatch_block_t block = ^{ @autoreleasepool {
33+
34+
[xmppIDTracker removeAllIDs];
35+
xmppIDTracker = nil;
36+
37+
}};
38+
39+
if (dispatch_get_specific(moduleQueueTag))
40+
block();
41+
else
42+
dispatch_sync(moduleQueue, block);
43+
44+
[super deactivate];
45+
}
46+
47+
- (void) discoverInfoOrItem:(NSString *) infoOrItems jid:(XMPPJID *) jid {
48+
49+
dispatch_block_t block = ^{ @autoreleasepool {
50+
// <iq type='get'
51+
// from='[email protected]/orchard'
52+
// to='shakespeare.lit'
53+
// id='items1'>
54+
// <query xmlns='http://jabber.org/protocol/disco#items'/> // disco#info
55+
// </iq>
56+
57+
NSString *iqID = [XMPPStream generateUUID];
58+
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns: infoOrItems];
59+
XMPPIQ *iq = [XMPPIQ iqWithType:@"get" to:jid elementID:iqID child:query];
60+
61+
[xmppIDTracker addID:iqID
62+
target:self
63+
selector:@selector(handleDiscovery:withInfo:)
64+
timeout:60.0];
65+
66+
[xmppStream sendElement:iq];
67+
}};
68+
69+
if (dispatch_get_specific(moduleQueueTag))
70+
block();
71+
else
72+
dispatch_async(moduleQueue, block);
73+
}
74+
75+
- (void)discoverInformationAbout:(XMPPJID *)jid{
76+
self.discoveringInfo = true;
77+
[self discoverInfoOrItem:XMLNS_DISCO_INFO jid:jid];
78+
}
79+
80+
81+
- (void)discoverItemsAssociatedWith:(XMPPJID *)jid{
82+
self.discoveringInfo = false;
83+
[self discoverInfoOrItem:XMLNS_DISCO_ITEMS jid:jid];
84+
}
85+
86+
- (void)handleDiscovery:(XMPPIQ *)iq withInfo:(id <XMPPTrackingInfo>)info{
87+
88+
if ([[iq type] isEqualToString:@"result"]){
89+
NSXMLElement *query = [iq elementForName:@"query"];
90+
NSArray *items = [query children];
91+
92+
if (self.discoveringInfo) {
93+
[multicastDelegate xmppServiceDiscovery:self didDiscoverInformation:items];
94+
} else {
95+
[multicastDelegate xmppServiceDiscovery:self didDiscoverItems:items];
96+
}
97+
98+
} else {
99+
[multicastDelegate xmppServiceDiscovery:self didFailToDiscover:iq];
100+
}
101+
}
102+
103+
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
104+
{
105+
NSString *type = [iq type];
106+
107+
if ([type isEqualToString:@"result"] || [type isEqualToString:@"error"])
108+
{
109+
return [xmppIDTracker invokeForID:[iq elementID] withObject:iq];
110+
}
111+
112+
return NO;
113+
}
114+
115+
@end

XMPPFramework.podspec

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ s.subspec 'XEP-0363' do |ss|
348348
ss.prefix_header_contents = "#define HAVE_XMPP_SUBSPEC_#{name.upcase.sub('-', '_')}"
349349
end
350350

351+
s.subspec 'XEP-0030' do |ss|
352+
ss.source_files = 'Extensions/XEP-0030/*.{h,m}'
353+
ss.dependency 'XMPPFramework/Core'
354+
ss.prefix_header_contents = "#define HAVE_XMPP_SUBSPEC_#{name.upcase.sub('-', '_')}"
355+
end
356+
351357
s.subspec 'All' do |ss|
352358
ss.dependency 'XMPPFramework/Core'
353359
ss.dependency 'XMPPFramework/BandwidthMonitor'
@@ -396,5 +402,6 @@ s.subspec 'All' do |ss|
396402
ss.dependency 'XMPPFramework/XEP-0357'
397403
ss.dependency 'XMPPFramework/XEP-0363'
398404
ss.dependency 'XMPPFramework/XEP-0313'
405+
ss.dependency 'XMPPFramework/XEP-0030'
399406
end
400407
end

Xcode/Testing-pod/Podfile.lock

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ PODS:
3131
- XMPPFramework/XEP-0009
3232
- XMPPFramework/XEP-0012
3333
- XMPPFramework/XEP-0016
34+
- XMPPFramework/XEP-0030
3435
- XMPPFramework/XEP-0045
3536
- XMPPFramework/XEP-0054
3637
- XMPPFramework/XEP-0059
@@ -91,6 +92,8 @@ PODS:
9192
- XMPPFramework/Core
9293
- XMPPFramework/XEP-0016 (3.6.7):
9394
- XMPPFramework/Core
95+
- XMPPFramework/XEP-0030 (3.6.7):
96+
- XMPPFramework/Core
9497
- XMPPFramework/XEP-0045 (3.6.7):
9598
- XMPPFramework/Core
9699
- XMPPFramework/CoreDataStorage
@@ -184,6 +187,6 @@ SPEC CHECKSUMS:
184187
CocoaAsyncSocket: a18c75dca4b08723628a0bacca6e94803d90be91
185188
CocoaLumberjack: 97fab7ee5f507fe54445cca7ea80f926729cfd15
186189
KissXML: d19dd6dc65e0dc721ba92b3077b8ebdd240f1c1e
187-
XMPPFramework: 231fe0cf84271a0bdf675254653e5747c6ae7f5a
190+
XMPPFramework: 2e6b563c554f3a4c6565b33981c5b527b9c474c4
188191

189192
COCOAPODS: 0.39.0

Xcode/Testing-pod/XMPPFrameworkTests.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
63F50D9F1C6020A100CA0201 /* EncodeDecodeTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 63F50D9C1C6020A100CA0201 /* EncodeDecodeTest.m */; };
1313
63F50DA01C6020A100CA0201 /* XMPPURITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 63F50D9D1C6020A100CA0201 /* XMPPURITests.m */; };
1414
C141EB1A1CF76CE900513A66 /* XMPPMockStream.m in Sources */ = {isa = PBXBuildFile; fileRef = C141EB191CF76CE900513A66 /* XMPPMockStream.m */; };
15+
C1FF91F61CF8906000C88DEA /* XMPPServiceDiscoveryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C1FF91F51CF8906000C88DEA /* XMPPServiceDiscoveryTests.m */; };
1516
D92C57A41CC2E0820032DE59 /* XMPPStorageHintTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D92C57A31CC2E0820032DE59 /* XMPPStorageHintTests.m */; };
1617
E0ED56D11CF34D28004C726B /* XMPPHTTPFileUploadTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E0ED56D01CF34D28004C726B /* XMPPHTTPFileUploadTests.m */; };
1718
E0ED56D61CF4D333004C726B /* XMPPMessageArchiveManagementTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E0ED56D51CF4D333004C726B /* XMPPMessageArchiveManagementTests.m */; };
@@ -29,6 +30,7 @@
2930
B02E767684F690CBF1C43DDA /* Pods_XMPPFrameworkTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_XMPPFrameworkTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3031
C141EB181CF76CE900513A66 /* XMPPMockStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XMPPMockStream.h; sourceTree = "<group>"; };
3132
C141EB191CF76CE900513A66 /* XMPPMockStream.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMPPMockStream.m; sourceTree = "<group>"; };
33+
C1FF91F51CF8906000C88DEA /* XMPPServiceDiscoveryTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMPPServiceDiscoveryTests.m; sourceTree = "<group>"; };
3234
D92C57A31CC2E0820032DE59 /* XMPPStorageHintTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMPPStorageHintTests.m; sourceTree = "<group>"; };
3335
E0ED56D01CF34D28004C726B /* XMPPHTTPFileUploadTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMPPHTTPFileUploadTests.m; sourceTree = "<group>"; };
3436
E0ED56D51CF4D333004C726B /* XMPPMessageArchiveManagementTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMPPMessageArchiveManagementTests.m; sourceTree = "<group>"; };
@@ -81,6 +83,7 @@
8183
63F50D9D1C6020A100CA0201 /* XMPPURITests.m */,
8284
E0ED56D01CF34D28004C726B /* XMPPHTTPFileUploadTests.m */,
8385
E0ED56D51CF4D333004C726B /* XMPPMessageArchiveManagementTests.m */,
86+
C1FF91F51CF8906000C88DEA /* XMPPServiceDiscoveryTests.m */,
8487
637AE2E81C6AC0D50051BF1F /* XMPPPushTests.swift */,
8588
63F50D971C60208200CA0201 /* Info.plist */,
8689
C141EB181CF76CE900513A66 /* XMPPMockStream.h */,
@@ -217,6 +220,7 @@
217220
files = (
218221
63F50D9E1C6020A100CA0201 /* CapabilitiesHashingTest.m in Sources */,
219222
63F50D9F1C6020A100CA0201 /* EncodeDecodeTest.m in Sources */,
223+
C1FF91F61CF8906000C88DEA /* XMPPServiceDiscoveryTests.m in Sources */,
220224
E0ED56D11CF34D28004C726B /* XMPPHTTPFileUploadTests.m in Sources */,
221225
E0ED56D61CF4D333004C726B /* XMPPMessageArchiveManagementTests.m in Sources */,
222226
C141EB1A1CF76CE900513A66 /* XMPPMockStream.m in Sources */,

0 commit comments

Comments
 (0)