Add capability to register callbacks when sending IQs

This commit is contained in:
Florian Zeitz 2012-01-01 03:42:01 +01:00
parent 7e9672b76f
commit 2f7b349539
5 changed files with 221 additions and 22 deletions

View file

@ -6,6 +6,7 @@ LIB_MINOR = 0
STATIC_LIB = ${OBJXMPP_STATIC_LIB} STATIC_LIB = ${OBJXMPP_STATIC_LIB}
SRCS = XMPPAuthenticator.m \ SRCS = XMPPAuthenticator.m \
XMPPCallback.m \
XMPPConnection.m \ XMPPConnection.m \
XMPPExceptions.m \ XMPPExceptions.m \
XMPPIQ.m \ XMPPIQ.m \

52
src/XMPPCallback.h Normal file
View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
*
* https://webkeks.org/hg/objxmpp/
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* 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 <ObjFW/ObjFW.h>
@class XMPPIQ;
@protocol XMPPCallback <OFObject>
- (void)runWithIQ: (XMPPIQ*)iq;
@end
#ifdef OF_HAVE_BLOCKS
typedef void(^xmpp_callback_block)(XMPPIQ*);
@interface XMPPBlockCallback: OFObject <XMPPCallback>
{
xmpp_callback_block callback;
}
+ callbackWithCallbackBlock: (xmpp_callback_block)callback;
- initWithCallbackBlock: (xmpp_callback_block)callback;
@end
#endif
@interface XMPPObjectCallback: OFObject <XMPPCallback>
{
id object;
SEL selector;
}
+ callbackWithCallbackObject: (id)object
selector: (SEL)selector;
- initWithCallbackObject: (id)object
selector: (SEL)selector;
@end

91
src/XMPPCallback.m Normal file
View file

@ -0,0 +1,91 @@
/*
* Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
*
* https://webkeks.org/hg/objxmpp/
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* 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.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#import "XMPPCallback.h"
#ifdef OF_HAVE_BLOCKS
@implementation XMPPBlockCallback
+ callbackWithCallbackBlock: (xmpp_callback_block)callback_
{
return [[[self alloc] initWithCallbackBlock: callback_] autorelease];
}
- initWithCallbackBlock: (xmpp_callback_block)callback_
{
self = [super init];
callback = [callback_ copy];
return self;
}
- (void)dealloc
{
[callback release];
[super dealloc];
}
- (void)runWithIQ: (XMPPIQ*)iq
{
callback(iq);
}
@end
#endif
@implementation XMPPObjectCallback
+ callbackWithCallbackObject: (id)object_
selector: (SEL)selector_
{
return [[[self alloc] initWithCallbackObject: object_
selector: selector_] autorelease];
}
- initWithCallbackObject: (id)object_
selector: (SEL)selector_
{
self = [super init];
// TODO: Retain or follow delegate paradigm?
object = [object_ retain];
selector = selector_;
return self;
}
- (void)dealloc
{
[object release];
[super dealloc];
}
- (void)runWithIQ: (XMPPIQ*)iq
{
[object performSelector: selector
withObject: iq];
}
@end

View file

@ -77,11 +77,11 @@
XMPPJID *JID; XMPPJID *JID;
uint16_t port; uint16_t port;
id <XMPPConnectionDelegate, OFObject> delegate; id <XMPPConnectionDelegate, OFObject> delegate;
OFMutableDictionary *callbacks;
XMPPAuthenticator *authModule; XMPPAuthenticator *authModule;
BOOL needsSession; BOOL needsSession;
BOOL encryptionRequired, encrypted; BOOL encryptionRequired, encrypted;
unsigned int lastID; unsigned int lastID;
OFString *bindID, *sessionID;
XMPPRoster *roster; XMPPRoster *roster;
} }
@ -158,6 +158,27 @@
*/ */
- (void)sendStanza: (OFXMLElement*)element; - (void)sendStanza: (OFXMLElement*)element;
/**
* Sends an XMPPIQ, registering a callback method
*
* \param object The object that contains the callback method
* \param selector The selector of the callback method,
* must take exactly one parameter of type XMPPIQ*
*/
- (void)sendIQ: (XMPPIQ*)iq
withCallbackObject: (id)object
selector: (SEL)selector;
#ifdef OF_HAVE_BLOCKS
/**
* Sends an XMPPIQ, registering a callback block
*
* \param callback The callback block
*/
- (void)sendIQ: (XMPPIQ*)iq
withCallbackBlock: (void(^)(XMPPIQ*))callback;
#endif
/** /**
* Generates a new, unique stanza ID. * Generates a new, unique stanza ID.
* *

View file

@ -37,6 +37,7 @@
#import <ObjOpenSSL/X509Certificate.h> #import <ObjOpenSSL/X509Certificate.h>
#import "XMPPConnection.h" #import "XMPPConnection.h"
#import "XMPPCallback.h"
#import "XMPPSRVLookup.h" #import "XMPPSRVLookup.h"
#import "XMPPSCRAMAuth.h" #import "XMPPSCRAMAuth.h"
#import "XMPPPLAINAuth.h" #import "XMPPPLAINAuth.h"
@ -64,6 +65,7 @@
sock = [[OFTCPSocket alloc] init]; sock = [[OFTCPSocket alloc] init];
port = 5222; port = 5222;
encrypted = NO; encrypted = NO;
callbacks = [[OFMutableDictionary alloc] init];
} @catch (id e) { } @catch (id e) {
[self release]; [self release];
@throw e; @throw e;
@ -83,9 +85,8 @@
[domain release]; [domain release];
[resource release]; [resource release];
[JID release]; [JID release];
[callbacks release];
[authModule release]; [authModule release];
[bindID release];
[sessionID release];
[roster release]; [roster release];
[super dealloc]; [super dealloc];
@ -352,6 +353,46 @@
[sock writeString: [element XMLString]]; [sock writeString: [element XMLString]];
} }
- (void)sendIQ: (XMPPIQ*)iq
withCallbackObject: (id)object
selector: (SEL)selector
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
@try {
if (![iq ID])
[iq setID: [self generateStanzaID]];
[callbacks setObject: [XMPPObjectCallback
callbackWithCallbackObject: object
selector: selector]
forKey: [iq ID]];
} @finally {
[pool release];
}
[self sendStanza: iq];
}
#ifdef OF_HAVE_BLOCKS
- (void)sendIQ: (XMPPIQ*)iq
withCallbackBlock: (xmpp_callback_block)callback;
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
@try {
if (![iq ID])
[iq setID: [self generateStanzaID]];
[callbacks setObject: [XMPPBlockCallback
callbackWithCallbackBlock: callback]
forKey: [iq ID]];
} @finally {
[pool release];
}
[self sendStanza: iq];
}
#endif
- (OFString*)generateStanzaID - (OFString*)generateStanzaID
{ {
return [OFString stringWithFormat: @"objxmpp_%u", lastID++]; return [OFString stringWithFormat: @"objxmpp_%u", lastID++];
@ -656,14 +697,11 @@
- (void)XMPP_handleIQ: (XMPPIQ*)iq - (void)XMPP_handleIQ: (XMPPIQ*)iq
{ {
BOOL handled = NO; BOOL handled = NO;
id <XMPPCallback> callback;
if ([[iq ID] isEqual: bindID]) { if ((callback = [callbacks objectForKey: [iq ID]])) {
[self XMPP_handleResourceBind: iq]; [callback runWithIQ: iq];
return; [callbacks removeObjectForKey: [iq ID]];
}
if ([[iq ID] isEqual: sessionID]) {
[self XMPP_handleSession: iq];
return; return;
} }
@ -799,9 +837,8 @@
XMPPIQ *iq; XMPPIQ *iq;
OFXMLElement *bind; OFXMLElement *bind;
bindID = [[self generateStanzaID] retain];
iq = [XMPPIQ IQWithType: @"set" iq = [XMPPIQ IQWithType: @"set"
ID: bindID]; ID: [self generateStanzaID]];
bind = [OFXMLElement elementWithName: @"bind" bind = [OFXMLElement elementWithName: @"bind"
namespace: XMPP_NS_BIND]; namespace: XMPP_NS_BIND];
@ -813,7 +850,9 @@
[iq addChild: bind]; [iq addChild: bind];
[self sendStanza: iq]; [self sendIQ: iq
withCallbackObject: self
selector: @selector(XMPP_handleResourceBind:)];
} }
- (void)XMPP_handleResourceBind: (XMPPIQ*)iq - (void)XMPP_handleResourceBind: (XMPPIQ*)iq
@ -832,9 +871,6 @@
namespace: XMPP_NS_BIND]; namespace: XMPP_NS_BIND];
JID = [[XMPPJID alloc] initWithString: [jidElement stringValue]]; JID = [[XMPPJID alloc] initWithString: [jidElement stringValue]];
[bindID release];
bindID = nil;
if (needsSession) { if (needsSession) {
[self XMPP_sendSession]; [self XMPP_sendSession];
return; return;
@ -849,12 +885,13 @@
{ {
XMPPIQ *iq; XMPPIQ *iq;
sessionID = [[self generateStanzaID] retain];
iq = [XMPPIQ IQWithType: @"set" iq = [XMPPIQ IQWithType: @"set"
ID: sessionID]; ID: [self generateStanzaID]];
[iq addChild: [OFXMLElement elementWithName: @"session" [iq addChild: [OFXMLElement elementWithName: @"session"
namespace: XMPP_NS_SESSION]]; namespace: XMPP_NS_SESSION]];
[self sendStanza: iq]; [self sendIQ: iq
withCallbackObject: self
selector: @selector(XMPP_handleSession:)];
} }
- (void)XMPP_handleSession: (XMPPIQ*)iq - (void)XMPP_handleSession: (XMPPIQ*)iq
@ -865,9 +902,6 @@
if ([delegate respondsToSelector: @selector(connection:wasBoundToJID:)]) if ([delegate respondsToSelector: @selector(connection:wasBoundToJID:)])
[delegate connection: self [delegate connection: self
wasBoundToJID: JID]; wasBoundToJID: JID];
[sessionID release];
sessionID = nil;
} }
- (OFString*)XMPP_IDNAToASCII: (OFString*)domain_ - (OFString*)XMPP_IDNAToASCII: (OFString*)domain_