Skip to content

Commit

Permalink
Adding ability to rebroadcast myPresence as an atomic operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
robbiehanson committed Mar 20, 2012
1 parent 4421f3a commit d3ea255
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
16 changes: 16 additions & 0 deletions Core/XMPPStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ typedef enum XMPPStreamErrorCode XMPPStreamErrorCode;
* In other words, it represents the presence as others see us.
*
* This excludes presence elements sent concerning subscriptions, MUC rooms, etc.
*
* @see resendMyPresence
**/
@property (strong, readonly) XMPPPresence *myPresence;

Expand Down Expand Up @@ -515,6 +517,20 @@ typedef enum XMPPStreamErrorCode XMPPStreamErrorCode;
**/
- (void)sendElement:(NSXMLElement *)element andGetReceipt:(XMPPElementReceipt **)receiptPtr;

/**
* Fetches and resends the myPresence element (if available) in a single atomic operation.
*
* There are various xmpp extensions that hook into the xmpp stream and append information to outgoing presence stanzas.
* For example, the XMPPCapabilities module automatically appends capabilities information (as a hash).
* When these modules need to update/change their appended information,
* they should use this method to do so.
*
* The alternative is to fetch the myPresence element, and resend it manually using the sendElement method.
* However, that is 2 seperate operations, and the user, may send a different presence element inbetween.
* Using this method guarantees everything is done as an atomic operation.
**/
- (void)resendMyPresence;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Module Plug-In System
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
20 changes: 18 additions & 2 deletions Core/XMPPStream.m
Original file line number Diff line number Diff line change
Expand Up @@ -2289,7 +2289,7 @@ - (void)sendElement:(NSXMLElement *)element withTag:(long)tag
}

/**
* This methods handles sending an XML fragment.
* This methods handles sending an XML stanza.
* If the XMPPStream is not connected, this method does nothing.
**/
- (void)sendElement:(NSXMLElement *)element
Expand All @@ -2311,7 +2311,7 @@ - (void)sendElement:(NSXMLElement *)element
}

/**
* This method handles sending an XML fragment.
* This method handles sending an XML stanza.
* If the XMPPStream is not connected, this method does nothing.
*
* After the element has been successfully sent,
Expand Down Expand Up @@ -2350,6 +2350,22 @@ - (void)sendElement:(NSXMLElement *)element andGetReceipt:(XMPPElementReceipt **
}
}

- (void)resendMyPresence
{
dispatch_block_t block = ^{ @autoreleasepool {

if (myPresence && [[myPresence type] isEqualToString:@"available"])
{
[self sendElement:myPresence];
}
}};

if (dispatch_get_current_queue() == xmppQueue)
block();
else
dispatch_async(xmppQueue, block);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Stream Negotiation
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 1 addition & 5 deletions Extensions/XEP-0115/XMPPCapabilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -737,11 +737,7 @@ - (void)continueCollectMyCapabilities:(NSXMLElement *)query
// and ended up taking so long as to not be available when the presence was sent,
// we should re-broadcast our presence now that we know what our capabilities are.

XMPPPresence *myPresence = [xmppStream myPresence];
if (myPresence)
{
[xmppStream sendElement:myPresence];
}
[xmppStream resendMyPresence];
}

- (void)recollectMyCapabilities
Expand Down
7 changes: 1 addition & 6 deletions Extensions/XEP-0199/XMPPPing.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,7 @@ - (void)setRespondsToQueries:(BOOL)flag
#ifdef _XMPP_CAPABILITIES_H
@autoreleasepool {
// Capabilities may have changed, need to notify others.

XMPPPresence *presence = xmppStream.myPresence;
if (presence)
{
[xmppStream sendElement:presence];
}
[xmppStream resendMyPresence];
}
#endif
}
Expand Down
7 changes: 1 addition & 6 deletions Extensions/XEP-0202/XMPPTime.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,7 @@ - (void)setRespondsToQueries:(BOOL)flag
#ifdef _XMPP_CAPABILITIES_H
@autoreleasepool {
// Capabilities may have changed, need to notify others.

XMPPPresence *presence = xmppStream.myPresence;
if (presence)
{
[xmppStream sendElement:presence];
}
[xmppStream resendMyPresence];
}
#endif
}
Expand Down

0 comments on commit d3ea255

Please sign in to comment.