2016年6月27日 星期一

客製化的 mosquitto

進入 src/mosquitto.c

main function裡面有從開啟server之後的動作

首先他先載入 config 也就是我們輸入的命令


接著
_mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "mosquitto version %s (build date %s) starting", VERSION, TIMESTAMP);
    _mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "add-plus Mosquitto-server-saving");

開始的message

/*J-強制載入 /etc/mosquitto/mosquitto.conf*/

config.config_file = "/etc/mosquitto/mosquitto.conf";
bool config_exist = file_exists(config.config_file);

if (config_exist){
   _mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "Config loaded from %s.", config.config_file);
}else{
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Can't find config at %s.", config.config_file);
}


在handle_read中

讓mosquitto把接收到的資訊存下來

    /* J-"Payload"" is the message what publisher are sending */
_mosquitto_log_printf(NULL, MOSQ_LOG_DEBUG, "Received PUBLISH from %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", context->id, dup, qos, retain, mid, topic, (long)payloadlen);
_mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "Topic : %s, From : %s", topic, context->id);
_mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "Message here : %s", payload);

/*將檔案寫入使用者目錄,檔名=TOPIC、內容=PAYLOAD*/
FILE *pFile;
    char buffer[50];
sprintf(buffer,"/home/cche/mqtt_state/%s.txt",topic);
pFile = fopen( buffer,"w" );
if( NULL == pFile ){
        _mosquitto_log_printf(NULL, MOSQ_LOG_DEBUG,"open failure" );
    }else{
        fwrite(payload,1,sizeof(payload)-1,pFile);
_mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "File context : %s", payload);
    }
fclose(pFile);
/*-J*/



mosquitto 執行命令後面輸入的東西會出現在

conf.c 裡面的 mqtt3_config_parse_args 裡面




沒有留言:

張貼留言