-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for XEP-0030 with tests
- Loading branch information
Andres Canal
committed
May 27, 2016
1 parent
cc83f0b
commit 46ff0b0
Showing
6 changed files
with
365 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// XMPPServiceDiscovery.h | ||
// Mangosta | ||
// | ||
// Created by Andres Canal on 4/27/16. | ||
// Copyright © 2016 Inaka. All rights reserved. | ||
// | ||
|
||
#import <XMPPFramework/XMPPFramework.h> | ||
|
||
@class XMPPIDTracker; | ||
|
||
@interface XMPPServiceDiscovery : XMPPModule { | ||
XMPPIDTracker *xmppIDTracker; | ||
} | ||
|
||
- (void)discoverInformationAboutJID:(XMPPJID *)jid; | ||
- (void)discoverItemsAssociatedWithJID:(XMPPJID *)jid; | ||
|
||
@end | ||
|
||
@protocol XMPPServiceDiscoveryDelegate | ||
|
||
@optional | ||
|
||
- (void)xmppServiceDiscovery:(XMPPServiceDiscovery *)sender didDiscoverInformation:(NSArray *)items; | ||
- (void)xmppServiceDiscovery:(XMPPServiceDiscovery *)sender didDiscoverItems:(NSArray *)items; | ||
|
||
- (void)xmppServiceDiscovery:(XMPPServiceDiscovery *)sender didFailToDiscover:(XMPPIQ *)iq; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// | ||
// XMPPServiceDiscovery.m | ||
// Mangosta | ||
// | ||
// Created by Andres Canal on 4/27/16. | ||
// Copyright © 2016 Inaka. All rights reserved. | ||
// | ||
|
||
#define XMLNS_DISCO_ITEMS @"http://jabber.org/protocol/disco#items" | ||
#define XMLNS_DISCO_INFO @"http://jabber.org/protocol/disco#info" | ||
#import "XMPPServiceDiscovery.h" | ||
#import "XMPPIDTracker.h" | ||
|
||
@interface XMPPServiceDiscovery() | ||
@property BOOL discoveringInfo; | ||
@end | ||
|
||
@implementation XMPPServiceDiscovery | ||
|
||
- (BOOL)activate:(XMPPStream *)aXmppStream { | ||
|
||
if ([super activate:aXmppStream]) { | ||
xmppIDTracker = [[XMPPIDTracker alloc] initWithDispatchQueue:moduleQueue]; | ||
|
||
return YES; | ||
} | ||
|
||
return NO; | ||
} | ||
|
||
- (void)deactivate { | ||
dispatch_block_t block = ^{ @autoreleasepool { | ||
|
||
[xmppIDTracker removeAllIDs]; | ||
xmppIDTracker = nil; | ||
|
||
}}; | ||
|
||
if (dispatch_get_specific(moduleQueueTag)) | ||
block(); | ||
else | ||
dispatch_sync(moduleQueue, block); | ||
|
||
[super deactivate]; | ||
} | ||
|
||
- (void) discoverInfoOrItem:(NSString *) infoOrItems jid:(XMPPJID *) jid { | ||
|
||
dispatch_block_t block = ^{ @autoreleasepool { | ||
// <iq type='get' | ||
// from='[email protected]/orchard' | ||
// to='shakespeare.lit' | ||
// id='items1'> | ||
// <query xmlns='http://jabber.org/protocol/disco#items'/> // disco#info | ||
// </iq> | ||
|
||
NSString *iqID = [XMPPStream generateUUID]; | ||
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns: infoOrItems]; | ||
XMPPIQ *iq = [XMPPIQ iqWithType:@"get" to:jid elementID:iqID child:query]; | ||
|
||
[xmppIDTracker addID:iqID | ||
target:self | ||
selector:@selector(handleDiscovery:withInfo:) | ||
timeout:60.0]; | ||
|
||
[xmppStream sendElement:iq]; | ||
}}; | ||
|
||
if (dispatch_get_specific(moduleQueueTag)) | ||
block(); | ||
else | ||
dispatch_async(moduleQueue, block); | ||
} | ||
|
||
- (void)discoverInformationAboutJID:(XMPPJID *)jid{ | ||
self.discoveringInfo = true; | ||
[self discoverInfoOrItem:XMLNS_DISCO_INFO jid:jid]; | ||
} | ||
|
||
|
||
- (void)discoverItemsAssociatedWithJID:(XMPPJID *)jid{ | ||
self.discoveringInfo = false; | ||
[self discoverInfoOrItem:XMLNS_DISCO_ITEMS jid:jid]; | ||
} | ||
|
||
- (void)handleDiscovery:(XMPPIQ *)iq withInfo:(id <XMPPTrackingInfo>)info{ | ||
|
||
if ([[iq type] isEqualToString:@"result"]){ | ||
NSXMLElement *query = [iq elementForName:@"query"]; | ||
NSArray *items = [query children]; | ||
|
||
if (self.discoveringInfo) { | ||
[multicastDelegate xmppServiceDiscovery:self didDiscoverInformation:items]; | ||
} else { | ||
[multicastDelegate xmppServiceDiscovery:self didDiscoverItems:items]; | ||
} | ||
|
||
} else { | ||
[multicastDelegate xmppServiceDiscovery:self didFailToDiscover:iq]; | ||
} | ||
} | ||
|
||
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq | ||
{ | ||
NSString *type = [iq type]; | ||
|
||
if ([type isEqualToString:@"result"] || [type isEqualToString:@"error"]) | ||
{ | ||
return [xmppIDTracker invokeForID:[iq elementID] withObject:iq]; | ||
} | ||
|
||
return NO; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.