// XMPPManager. h
// InstantMessage
//
// Created by ChenJungang on 14-1-3.
// Copyright (c) 2014 chenjungang. All rights reserved.
//
# Import <Foundation/Foundation. h>
# Import "XMPPFramework. h"
@ Class XMPPManager;
@ Class BuddyItem;
@ Class MessageItem;
@ Protocol XMPPManagerDelegate <NSObject>
// Receives the status information of friends in XMPPManager. The friends list page is passed through proxy.
-(Void) XMPPManager :( XMPPManager *) manager didReceiveBuddyItem :( BuddyItem *) item;
//-(Void) xmppManager :( XMPPManager *) manager didReceiveNewMessageWithMessageItem :( MessageItem *) item;
@ End
@ Interface XMPPManager: NSObject <XMPPStreamDelegate, XMPPRosterDelegate>
@ Property (nonatomic, strong) XMPPStream * xmppStream; // the xmpp service class is used to manage basic operations of xmpp; connection server verification login registration and sending information, etc.
@ Property (nonatomic, strong) XMPPRoster * xmppRoster; // xmpp friend list class
@ Property (nonatomic, weak) id <XMPPManagerDelegate> delegate;
@ Property (nonatomic, copy) void (^ receiveMessageBlock) (XMPPManager *, MessageItem *);
+ (XMPPManager *) defaultManager; // Singleton Initialization Method
-(BOOL) connectToServer; // connect to the server
-(Void) disConnectToserver; // disconnect the server
-(Void) xmppReceiveMessageWithCompletionHandler :( void (^) (XMPPManager *, MessageItem *) messageBlock;
@ End
// XMPPManager. m
// InstantMessage
//
// Created by ChenJungang on 14-1-3.
// Copyright (c) 2014 chenjungang. All rights reserved.
//
# Import "XMPPManager. h"
# Import "BuddyItem. h"
# Import "MessageItem. h"
@ Interface XMPPManager ()
@ Property (nonatomic, strong) NSString * password;
-(Void) _ setup; // initialize the object of the xmpp class
-(Void) _ userOnline; // The User goes online.
-(Void) _ urerOffline; // deprecate the user
@ End
@ Implementation XMPPManager
# Pragma mark-public method public
+ (XMPPManager *) defaultManager; // Singleton Initialization Method
{
Static XMPPManager * instance;
Static dispatch_once_t onceToKen;
Dispatch_once (& onceToKen, ^ {
Instance = [[[self class] alloc] init]; // In the instance method, self is the object that calls this method.
});
Return instance;
}
-(BOOL) connectToServer; // connect to the server
{
// Initialize the XMPP operation class
[Self _ setup];
// First judgment. If you already have a connection to the server,
If (! [_ XmppStream isDisconnected]) {
Return YES;
}
NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
NSString * userName = [userDefault objectForKey: XMPPUserName]; // User Name
_ Password = [userDefault objectForKey: XMPPUserPassword]; // password
// Second judgment, no jidhe password, transient, not for you
If (userName = nil & _ password = nil ){
Return NO;
}
// Configure xmppStream, JID, server, and port number.
NSString * jidString = [NSString stringWithFormat: @ "% @ lanouserver", userName];
[UserDefault setObject: jidString forKey: XMPPUserJID];
NSString * serverIP = [userDefault objectForKey: XMPPServerIP];
XMPPJID * jid = [XMPPJID jidWithString: jidString resource: @ ""]; // concatenate jid
[_ XmppStream setMyJID: jid]; // set jid
[_ XmppStream setHostName: serverIP]; // sets the server
[_ XmppStream setHostPort: 5222]; // set the server port number;
// Start Link
NSError * error = nil;
If (! [Self. xmppStream connectWithTimeout: 30366f error: & error]) {
NSLog (@ "link failed: % @", error. description );
Return NO;
}
Return YES;
}
-(Void) disConnectToserver; // disconnect the server
{
}
# Pragma mark-Private method Private
-(Void) _ setup; // initialize the object of the xmpp class
{
// Initialize xmppStream
If (! _ XmppStream ){
Self. xmppStream = [[XMPPStream alloc] init];
[_ XmppStream addDelegate: self delegateQueue: dispatch_get_main_queue ()];
}
// Initialize xmppRoster
If (! _ XmppRoster ){
XMPPRosterCoreDataStorage * rosterStorage = [[XMPPRosterCoreDataStorage alloc] init];
Self. xmppRoster = [[XMPPRoster alloc] initWithRosterStorage: rosterStorage dispatchQueue: dispatch_get_main_queue ()];
[_ XmppRoster addDelegate: self delegateQueue: dispatch_get_main_queue ()];
[_ XmppRoster activate: _ xmppStream];
}
}
-(Void) _ userOnline; // The User goes online.
{
XMPPPresence * onlinePresence = [XMPPPresence presenceWithType: @ "avalible"];
[_ XmppStream sendElement: onlinePresence];
}
-(Void) _ urerOffline; // deprecate the user
{
XMPPPresence * presence = [XMPPPresence presenceWithType: @ "unavaliable"];
[_ XmppStream sendElement: presence];
}
# Pragma mark-XMPPStream Delegate Methods-
-(Void) xmppStreamDidConnect :( XMPPStream *) sender
{
NSLog (@ "successfully linked to the server ");
BOOL isLogin = [[NSUserDefaults standardUserDefaults] boolForKey: XMPPUserLogin];
If (isLogin ){
// Server authentication user (LOGIN)
[_ XmppStream authenticateWithPassword: _ password error: nil];
} Else {
// Register a user with the server
[_ XmppStream registerWithPassword: _ password error: nil];
}
}
-(Void) xmppStreamDidDisconnect :( XMPPStream *) sender withError :( NSError *) error
{
NSLog (@ "failed to connect to the server: % @", error. description );
}
-(Void) xmppStreamDidAuthenticate :( XMPPStream *) sender
{
NSLog (@ "Logon successful ");
[Self _ userOnline];
[[Nsicationcenter center defacenter center] postNotificationName: XMPPLoginSuccessedNotification object: nil];
}
-(Void) xmppStream :( XMPPStream *) sender didNotAuthenticate :( DDXMLElement *) error {
NSLog (@ "Logon Failed: % @", error. description );
}
-(Void) xmppStreamDidRegister :( XMPPStream *) sender
{
NSLog (@ "registered successfully ");
// After successful registration, You need to log in
[_ XmppStream authenticateWithPassword: _ password error: nil];
}
-(Void) xmppStream :( XMPPStream *) sender didNotRegister :( DDXMLElement *) error
{
NSLog (@ "registration failed % @", error );
}
// Obtain the status of a friend in real time
-(Void) xmppStream :( XMPPStream *) sender didReceivePresence :( XMPPPresence *) presence
{
NSLog (@ "% @", presence );
// 1. encapsulate the response model class through the obtained Present
// If you do not want to display yourself in the list,
NSString * fromUserName = presence. from. user;
NSString * myUserName = [[NSUserDefaults standardUserDefaults] objectForKey: XMPPUserName];
If (! [FromUserName isinclutostring: myUserName]) {
BuddyItem * item = [[BuddyItem alloc] initWithUserName: presence. from. user UserStatus: presence. status userResource: presence. from. resource];
// 2. Pass the encapsulated model class to the friend list page.
If (_ delegate & [_ delegate respondsToSelector: @ selector (XMPPManager: didReceiveBuddyItem :)]) {
[_ Delegate XMPPManager: self didReceiveBuddyItem: item];
}
}
}
-(Void) xmppRoster :( XMPPRoster *) sender didReceiveRosterItem :( DDXMLElement *) item
{
NSLog (@ "DDXMLElementItem = % @", item );
}
-(Void) xmppStream :( XMPPStream *) sender didReceiveMessage :( XMPPMessage *) message {
NSLog (@ "message = % @", message );
MessageItem * item = [[MessageItem alloc] init];
Item. userName = message. from. user;
Item. chatMessage = message. body;
Item. isReceive = YES;
Self. receiveMessageBlock (self, item );
}
-(Void) xmppReceiveMessageWithCompletionHandler :( void (^) (XMPPManager *, MessageItem *) messageBlock
{
NSLog (@ "Accept block object (implement block )");
// The block attribute is declared as copy to copy the block object from the stack to the heap. We use the arc, so we do not need to use this block.
Self. receiveMessageBlock = messageBlock;
}
@ End