• Mqtt协议IOS端移植3


    ServerMqFramework.h

    #import "MqttFramework.h"
    
    @interface ServerMqFramework : MqttFramework
    
    
    /**
     * @brief  得到模块控制器的句柄单例
     *
     * @param [in] N/A
     * @param [out] N/A
     * @return void
     * @note
     */
    
    +(ServerMqFramework*)getMQttServerFrameInstance;
    
    - (int)callBusinessProcessEX:(NSString *)capabilityId withMessageType:(NSString *)MessageType WithMessage:(id)messageVector withTopic:(NSString *)topic;
    
    @end
    


    ServerMqFramework.m


    #import "ServerMqFramework.h"
    #import "getMacAddress.h"
    
    @implementation ServerMqFramework
    
    
    
    /**
     * @brief  得到模块控制器的句柄单例
     *
     * @param [in] N/A
     * @param [out] N/A
     * @return void
     * @note
     */
    
    static ServerMqFramework *mqttInstance = nil;
    +(ServerMqFramework*)getMQttServerFrameInstance
    {
        @synchronized(self)
    	{
    		if(mqttInstance==nil)
    		{
    			mqttInstance=[[self alloc]init];
            }
    	}
    	return mqttInstance;
    }
    
    
    #pragma  --mark  BusinessModuleProtocol  delegate
    - (int)initBusinessModule:(BusinessModuleInfo*)info
    {
        
        info.businessModuleIdEX = @"MqttServerFrameWork";//
        businessFrameworks_ = info.businessFramework;
        return 0;
    }
    
    
    //指定发送到远程的响应接收者的主题   (实现宏观上的点对点传输)  //2012/11/30   modify
    
    - (int)callBusinessProcessEX:(NSString *)capabilityId withMessageType:(NSString *)MessageType WithMessage:(id)messageVector withTopic:(NSString *)topic
    {
        NSLog(@"server capabilityId = %@  topic = %@  messageVector = %@",capabilityId,MessageType,messageVector);
        
        AppDelegate  *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    	MQTTClient   *serverMQTTClient = [app serverMqClient];
        
        
       
        
        NSString  *sendStr = [[NSString alloc] initWithData:[XmlAdept makeMqttXML:MessageType withDictionary:(NSDictionary *)messageVector] encoding:NSUTF8StringEncoding];
        
        //    /****************去掉最后换行符********************/
        int index = sendStr.length-1;
        sendStr = [sendStr substringToIndex:index];
        
        NSString *string = [NSString stringWithFormat:@"**%@**",sendStr];
        NSLog(@"server senderMesg = %@",string);
        [serverMQTTClient publishString:sendStr toTopic:topic retain:NO];
        
        return 0;
    }
    
    -(void)requestResult:(NSString *)topic  withData:(id)resultData
    {
        NSLog(@"远程mq服务器发送业务广播 给监听者");
        [businessFrameworks_ broadcastBusinessNotifyEX:topic withInParam:resultData];
    }
    
    
    
    /**
     * @brief 定义mqtt消息主题
     *
     * @param [in] N/A
     * @param [out] N/A
     * @return void
     * @note
     */
    
    -(void)productTheme:(NSString *)theme
    {
        
        NSLog(@"server theme = %@",theme);
        AppDelegate  *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        MQTTClient *mosq = [app serverMqClient];
    
        [mosq setHost:Mqtt_Server_URL];
          
        [mosq connect];
        [mosq subscribe:theme];
    }
    
    /**
     * @brief 重连mqtt服务器
     *
     * @param [in] N/A
     * @param [out] N/A
     * @return void
     * @note
     */
    -(void)reconnectMqtt
    {
        AppDelegate  *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        MQTTClient *mosq = [app serverMqClient];
       
        [mosq setHost:Mqtt_Server_URL];
        
        [mosq reconnect];
        
    }
    
    #pragma --mark  mosquittoclientDelegate
    
    - (void) didConnect:(NSUInteger)code
    {
        if (code == 0)
        {
            NSLog(@"连接远程Mqtt服务器返回码为:%d 连接mqtt成功",code);
        }
        else
        {
            NSLog(@"连接远程Mqtt服务器失败");
        }
        
    	
    }
    
    - (void) didDisconnect
    {
    	NSLog(@"Server mqtt disconnect!");
        AppDelegate  *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        MQTTClient *mosq = [app serverMqClient];
        //断开后自己主动重连
        //3g或wifi任一可达就重连
        if ([Common IsEnable3G] || [Common IsEnableWIFI])
        {
            [mosq reconnect];   //远程MQTT服务器端重连
            NSString  *serverTheme = getMacAddress();
            [mosq subscribe:serverTheme]; //又一次订阅主题
        }
        
    }
    
    //处理各个主题相应的message
    
    - (void) didReceiveMessage: (NSString*)message topic:(NSString*)topic
    {
    	NSLog(@" serverMqtt %@ => %@", topic, message);
      
        NSArray   *resultArr = [[NSArray alloc]initWithObjects:message, nil];
        NSArray   *list = [XmlAdept mqttParseMessageNode:resultArr];
        NSString  *type = [[list objectAtIndex:0] objectForKey:@"type"];
        
        [self requestResult:type withData:list];
        
        
    	
    }
    
    /**
     * @brief 给远程mqtt服务器发送消息   对外接口
     *
     * @param [in] N/A
     * @param [out] N/A
     * @return void
     * @note
     */
    
    +(void)publishMessage:(NSString *)message   withTopicType:(NSString *)topicType
    {
        AppDelegate  *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    	MQTTClient *mosq = [app serverMqClient];
        [mosq publishString:message toTopic:topicType retain:NO];  //消息的retain类型设置为NO让其不在消息缓冲池中保留
    }
    
    -(void)dealloc
    {
        [super dealloc];
    }
    
    
    @end
    


  • 相关阅读:
    当我们谈线(进)程“切换”时
    机器学习算法实现——线性回归
    从cpu加电到加载OS内核的详细过程(清华大学ucore-lab1总结一)
    X86 I/O端口
    【转】实模式和保护模式
    链接器(linker)的作用——CSAPP第7章读书笔记
    【转】Derivation of the Normal Equation for linear regression
    使用PowerShell创建Active Directory用户对象(域用户)
    使用Powershell安装WSFC
    临时自定义数学算符添加正下方下标
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7214395.html
Copyright © 2020-2023  润新知