void conn_handler(xmpp_conn_t *conn, xmpp_conn_event_t status, const int error, xmpp_stream_error_t *stream_error, void *userdata) { xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; if (status == XMPP_CONN_CONNECT) { fprintf(stderr, "DEBUG: connected\n"); // 上线 xmpp_stanza_t *pres = xmpp_stanza_new(ctx); xmpp_stanza_set_name(pres, "presence"); xmpp_send(conn, pres); xmpp_stanza_release(pres); // 消息处理器 xmpp_handler_add(conn, handle_txt_msg, NULL, "message", NULL, ctx); // 启动控制台解析器 int err = pthread_create(&console_thread, NULL, console_routine, conn); if (err != 0) { fprintf(stderr, "can't create console thread"); } } else { fprintf(stderr, "DEBUG: disconnected\n"); xmpp_stop(ctx); } }
/** * This connection handler is called after connection has been etablized. * All initial messages can therefore be put here. */ static void sdvp_HandleConnection(xmpp_conn_t * const conn, const xmpp_conn_event_t status, const int error, xmpp_stream_error_t * const stream_error, void * const userdata) { xmpp_ctx_t *ctx = (xmpp_ctx_t *) userdata; xmpp_stanza_t* pres; char packetId[PACKET_ID_LENGTH_INC_TERM]; if (status == XMPP_CONN_CONNECT) { syslog(LOG_DEBUG, "Connected\n"); xmpp_handler_add(conn, HandleRpcCall, "jabber:iq:rpc", "iq", "get", ctx); xmpp_handler_add(conn, PresenceHandler, NULL, "presence", NULL, ctx); xmpp_handler_add(conn, HandlePing, "urn:xmpp:ping", "iq", "get", ctx); xmpp_id_handler_add(conn, HandlePingResult, "ping", ctx); // Send initial <presence/> so that we appear online to contacts // This is very important as the server don't route messages/iq's if not online! // pres = xmpp_stanza_new(ctx); xmpp_stanza_set_name(pres, "presence"); _GeneratePacketId(packetId, PACKET_ID_LENGTH_INC_TERM); xmpp_stanza_set_id(pres, packetId); xmpp_send(conn, pres); xmpp_stanza_release(pres); // Send request for roaster _SendRosterRequest(conn, ctx); if (callbackOnConnectionChange != NULL ) { callbackOnConnectionChange(SDVP_CONN_CONNECT); } } else { syslog(LOG_WARNING, "conn_handler disconnected\n"); sdvpIsRunning = false; xmpp_stop(ctx); } }
/* define a handler for connection events */ void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status, const int error, xmpp_stream_error_t * const stream_error, void * const userdata) { xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; if (status == XMPP_CONN_CONNECT) { xmpp_stanza_t* pres; fprintf(stderr, "DEBUG: connected\n"); xmpp_handler_add(conn, version_handler, "jabber:iq:version", "iq", NULL, ctx); xmpp_handler_add(conn, message_handler, NULL, "message", NULL, ctx); xmpp_handler_add(conn, presence_handler, NULL, "presence", NULL, ctx); /* Send initial <presence/> so that we appear online to contacts */ pres = xmpp_stanza_new(ctx); xmpp_stanza_set_name(pres, "presence"); xmpp_send(conn, pres); xmpp_stanza_release(pres); } else { fprintf(stderr, "DEBUG: disconnected\n"); xmpp_stop(ctx); } }
void xmpp_ua_uninit(void) { xmpp_stop(_xmpp_ctx); WaitForSingleObject(_xmpp_thread, INFINITE); CloseHandle(_xmpp_thread); CloseHandle(_mutex_4_conn); CloseHandle(_mutex_4_id); xmpp_ctx_free(_xmpp_ctx); xmpp_shutdown(); }
/* define a handler for connection events */ void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status, const int error, xmpp_stream_error_t * const stream_error, void * const userdata) { xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; if (status == XMPP_CONN_CONNECT) { fprintf(stderr, "DEBUG: connected\n"); xmpp_disconnect(conn); } else { fprintf(stderr, "DEBUG: disconnected\n"); xmpp_stop(ctx); } }
static void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status, const int error, xmpp_stream_error_t * const stream_error, void * const userdata) { conflate_handle_t *handle = (conflate_handle_t *)userdata; if (status == XMPP_CONN_CONNECT) { xmpp_stanza_t* pres = NULL, *priority = NULL, *pri_text = NULL; CONFLATE_LOG(handle, LOG_LVL_INFO, "Connected."); xmpp_handler_add(conn, version_handler, "jabber:iq:version", "iq", NULL, handle); xmpp_handler_add(conn, command_handler, "http://jabber.org/protocol/commands", "iq", NULL, handle); xmpp_handler_add(conn, disco_items_handler, "http://jabber.org/protocol/disco#items", "iq", NULL, handle); xmpp_handler_add(conn, message_handler, NULL, "message", NULL, handle); xmpp_timed_handler_add(conn, keepalive_handler, 60000, handle); xmpp_timed_handler_add(conn, alarmqueue_handler, 10000, handle); /* Send initial <presence/> so that we appear online to contacts */ pres = xmpp_stanza_new(handle->ctx); assert(pres); xmpp_stanza_set_name(pres, "presence"); priority = xmpp_stanza_new(handle->ctx); assert(priority); xmpp_stanza_set_name(priority, "priority"); add_and_release(pres, priority); pri_text = xmpp_stanza_new(handle->ctx); assert(pri_text); xmpp_stanza_set_text(pri_text, "5"); add_and_release(priority, pri_text); xmpp_send(conn, pres); xmpp_stanza_release(pres); /* Store the bound jid */ if (!conflate_save_private(handle, STORED_JID_KEY, xmpp_conn_get_bound_jid(conn), handle->conf->save_path)) { CONFLATE_LOG(handle, LOG_LVL_WARN, "Failed to save the bound jid"); } } else { CONFLATE_LOG(handle, LOG_LVL_INFO, "disconnected."); xmpp_stop(handle->ctx); } }
/* define a handler for connection events */ void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status, const int error, xmpp_stream_error_t * const stream_error, void * const userdata) { xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; int secured; if (status == XMPP_CONN_CONNECT) { fprintf(stderr, "DEBUG: connected\n"); secured = xmpp_conn_is_secured(conn); fprintf(stderr, "DEBUG: connection is %s.\n", secured ? "secured" : "NOT secured"); xmpp_disconnect(conn); } else { fprintf(stderr, "DEBUG: disconnected\n"); xmpp_stop(ctx); } }
void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status, const int error, xmpp_stream_error_t * const stream_error, void * const userdata) { xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; xmpp_stanza_t *iq, *query; if (status == XMPP_CONN_CONNECT) { fprintf(stderr, "DEBUG: connected\n"); /* create iq stanza for request */ iq = xmpp_stanza_new(ctx); xmpp_stanza_set_name(iq, "iq"); xmpp_stanza_set_type(iq, "get"); xmpp_stanza_set_id(iq, "active1"); xmpp_stanza_set_attribute(iq, "to", "xxxxxxxxx.com"); query = xmpp_stanza_new(ctx); xmpp_stanza_set_name(query, "query"); xmpp_stanza_set_ns(query, XMPP_NS_DISCO_ITEMS); xmpp_stanza_set_attribute(query, "node", "sessions"); xmpp_stanza_add_child(iq, query); /* we can release the stanza since it belongs to iq now */ xmpp_stanza_release(query); /* set up reply handler */ xmpp_id_handler_add(conn, handle_reply, "active1", ctx); /* send out the stanza */ xmpp_send(conn, iq); /* release the stanza */ xmpp_stanza_release(iq); } else { fprintf(stderr, "DEBUG: disconnected\n"); xmpp_stop(ctx); } }
void main(int argc, char **argv) { xmpp_conn_t* conn = NULL; xmpp_ctx_t *ctx = NULL; char *jid, *pass, *host; int errornum=0; void *ptr; pthread_attr_init(&attr1); pthread_attr_setstacksize(&attr1, PTHREAD_STACK_MIN * 2); /* take a jid and password on the command line */ if (argc < 3 || argc > 4) { fprintf(stderr, "Usage: xep0047-test <jid> <pass> [<host>]\n\n"); return ; } jid = argv[1]; pass = argv[2]; host = NULL; if (argc == 4) host = argv[3]; conn = XMPP_Init(jid , pass, host, &ctx); #if 1 if(ctx !=NULL) pthread_create(&pid1, \ &attr1, (void*)xmpp_run, (void*)ctx ); #endif xmpp_ibb_session_t* IBB_HANDLE; IBB_HANDLE = XMPP_IBB_Init(); while(1) { sleep(5); if(IBB_HANDLE->sid != NULL) { printf("XMPP IBB sesseion open sid=%s, blksz=%s\n ", IBB_HANDLE->sid, IBB_HANDLE->szblock_size); while(IBB_HANDLE->ibb_data_queue != NULL) { printf("Recv IBB seq=%s\n, data=%s\n", IBB_HANDLE->ibb_data_queue->seq_num, IBB_HANDLE->ibb_data_queue->recv_data); XMPP_IBB_Del_Session_Data_Queue(); } } } // xmpp_run(ctx); xmpp_stop(ctx); sleep(1); XMPP_Close(ctx, conn); if ((errornum = pthread_cancel(pid1)) != 0) fprintf(stderr, "pthread_cancel: %s", strerror(errornum)); if ((errornum = pthread_join(pid1, &ptr)) != 0) fprintf(stderr, "pthread_join: %s", strerror(errornum)); }
/** * Stop a running connection * @param sdvpConfig The connection object */ void sdvp_ConnectionStop(sdvp_config_t *sdvpConfig) { sdvpIsRunning = false; xmpp_stop(ctx); }
int main( int argc, char* argv[] ) { uint8_t addr[20] = {0}; uint8_t* paddr = NULL; uint16_t port = USE_RANDOM_PORT; xmpp_ctx_t *ctx = NULL; xmpp_conn_t* conn = NULL; int reg_act = DMC_REGISTER; int errornum=0; void *ptr; pthread_attr_init(&attr1); pthread_attr_setstacksize(&attr1, PTHREAD_STACK_MIN * 2); if(!setPIN("2345678901")) { fprintf(stderr, "PIN read error\n"); return 1; } else printf("\t\tRead PIN : %s\n", getPIN()); if( argc < 2 ) { exit(-1); } uint8_t *ifname = (uint8_t *)argv[1]; /*Get Ip address on defined interface and initialize coap on it with random port number * this port number will be used as a source port in all coap communications*/ if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr)) == ERR_SUCCESS) { OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr); paddr = addr; } /* Initialize OCStack*/ if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack init error"); return 0; } time(&last_discovery_time); InitDiscovery(); // conn = XMPP_Init( xmpp_un , xmpp_pw , xmpp_server, &ctx); // Break from loop with Ctrl+C OC_LOG(INFO, TAG, "Entering occlient main loop..."); signal(SIGINT, handleSigInt); while (!gQuitFlag) { if( cmd_head != NULL ) { switch( cmd_head->command ) { case COMMAND_DISCOVERY: InitDiscovery(); break; case COMMAND_GET: InitGetRequest(cmd_head); break; case COMMAND_PUT: InitPutRequest(cmd_head); break; case COMMAND_POST: InitPutRequest(cmd_head); break; case COMMAND_OBSERVE: break; default: break; } struct cmd *cmd_temp = cmd_head; cmd_head = cmd_head->next; free(cmd_temp); } else { //do discovery every 10 seconds time_t now; time(&now); if( now - last_discovery_time > DMC_INTERVAL ) { struct dmc_result *res = dmc_discovery_finish(reg_act); if( res->state == REG_SUCCEED ) { reg_act = DMC_UPDATE; snprintf( xmpp_un, sizeof(xmpp_un), "%s", res->username ); snprintf( xmpp_pw, sizeof(xmpp_pw), "%s", res->password ); snprintf( xmpp_server, sizeof(xmpp_server), "%s", res->server ); free( res ); printf("%s\n%s\n%s\n", xmpp_server, xmpp_un, xmpp_pw); if(conn==NULL) { conn = XMPP_Init( \ xmpp_un , xmpp_pw, xmpp_server, &ctx); if(ctx !=NULL) pthread_create(&pid1, \ &attr1, (void*)xmpp_run, (void*)ctx ); else printf("ctx null\n"); } } else free(res); OC_LOG_V( INFO, TAG, "DM client returns state %s", get_dmcresult_char(res->state) ); InitDiscovery(); time(&last_discovery_time); } } if (OCProcess() != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack process error"); return 0; } usleep(200*1000); } printf("Quit\n"); OC_LOG(INFO, TAG, "Exiting occlient main loop..."); if (OCStop() != OC_STACK_OK) { OC_LOG_V(ERROR, TAG, "OCStack stop error"); } xmpp_stop(ctx); sleep(1); XMPP_Close(ctx, conn); if ((errornum = pthread_cancel(pid1)) != 0) OC_LOG_V(INFO, TAG, "pthread_cancel: %s", strerror(errornum)); if ((errornum = pthread_join(pid1, &ptr)) != 0) OC_LOG_V(INFO, TAG, "pthread_join: %s", strerror(errornum)); printf("Done\n"); return 0; }