smcp_status_t smcp_outbound_begin_response(coap_code_t code) { smcp_status_t ret = SMCP_STATUS_OK; smcp_t const self = smcp_get_current_instance(); require_action_string(!self->did_respond, bail, ret = SMCP_STATUS_RESPONSE_NOT_ALLOWED, "Attempted to send more than one response!" ); // If we have already started responding, don't bother. require(!self->is_responding,bail); if(self->is_processing_message) self->outbound.next_tid = smcp_inbound_get_msg_id(); smcp_outbound_begin(self,code,(self->inbound.packet->tt==COAP_TRANS_TYPE_NONCONFIRMABLE)?COAP_TRANS_TYPE_NONCONFIRMABLE:COAP_TRANS_TYPE_ACK); self->is_responding = true; if(self->is_processing_message) smcp_outbound_set_msg_id(smcp_inbound_get_msg_id()); if(self->is_processing_message) { #if SMCP_USE_BSD_SOCKETS ret = smcp_outbound_set_destaddr( self->inbound.saddr, self->inbound.socklen ); #elif CONTIKI ret = smcp_outbound_set_destaddr( &self->inbound.toaddr, self->inbound.toport ); #endif require_noerr(ret, bail); } bail: return ret; }
smcp_status_t smcp_outbound_begin_response(coap_code_t code) { smcp_status_t ret; smcp_t const self = smcp_get_current_instance(); ret = SMCP_STATUS_OK; require_action_string(!self->did_respond, bail, ret = SMCP_STATUS_RESPONSE_NOT_ALLOWED, "Attempted to send more than one response!" ); // If we have already started responding, don't bother. require_quiet(!self->is_responding, bail); if (self->is_processing_message) { self->outbound.next_tid = smcp_inbound_get_msg_id(); } ret = smcp_outbound_begin( self, code, (self->inbound.packet->tt==COAP_TRANS_TYPE_NONCONFIRMABLE)?COAP_TRANS_TYPE_NONCONFIRMABLE:COAP_TRANS_TYPE_ACK ); require_noerr(ret, bail); self->is_responding = true; if (self->is_processing_message) { require_noerr(ret=smcp_outbound_set_msg_id(smcp_inbound_get_msg_id()),bail); } bail: if (ret != SMCP_STATUS_OK) { self->is_responding = false; } return ret; }
int tool_cmd_test( smcp_t smcp_instance, int argc, char* argv[] ) { if((2 == argc) && (0 == strcmp(argv[1], "--help"))) { printf("Help not yet implemented for this command.\n"); return ERRORCODE_HELP; } smcp_t smcp; smcp_t smcp2; struct smcp_timer_node_s timer_node = {}; struct smcp_variable_node_s device_node = {}; struct smcp_node_s async_response_node = {}; smcp2 = smcp_create(12345); if(smcp_get_port(smcp_instance) == SMCP_DEFAULT_PORT) smcp = smcp_instance; else smcp = smcp_create(SMCP_DEFAULT_PORT); //printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children)); #if HAVE_LIBCURL struct smcp_curl_proxy_node_s proxy_node = {}; // curl_global_init(CURL_GLOBAL_ALL); // CURLM* curl_multi_handle = curl_multi_init(); smcp_curl_proxy_node_init( &proxy_node, smcp_get_root_node(smcp), "proxy" ); #endif //printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children)); // smcp_pairing_init( // smcp_get_root_node(smcp), // SMCP_PAIRING_DEFAULT_ROOT_PATH // ); // // smcp_pairing_init( // smcp_get_root_node(smcp2), // SMCP_PAIRING_DEFAULT_ROOT_PATH // ); //printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children)); smcp_variable_node_init( &device_node, smcp_get_root_node(smcp), "device" ); device_node.func = device_func; //printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children)); smcp_variable_node_init( NULL, smcp_get_root_node(smcp2), "device" )->func = device_func; //printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children)); smcp_timer_node_init( &timer_node, smcp_get_root_node(smcp), "timer" ); //printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children)); smcp_node_init((smcp_node_t)&async_response_node, smcp_get_root_node(smcp), "async_response" ); async_response_node.request_handler = &async_request_handler; //printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children)); #if !SMCP_CONF_OBSERVING_ONLY { char url[256]; snprintf(url, sizeof(url), "smcp://127.0.0.1:%d/device/action", smcp_get_port(smcp2)); smcp_pair_with_uri(smcp, "device/loadavg", url, 0, NULL); printf("EVENT_NODE PAIRED WITH %s\n", url); } { char url[256]; snprintf(url, sizeof(url), "smcp://[::1]:%d/device/loadavg", smcp_get_port(smcp)); smcp_pair_with_uri(smcp2, "device/action", url, 0, NULL); printf("ACTION_NODE PAIRED WITH %s\n", url); } #endif //printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children)); // Just adding some random nodes so we can browse thru them with another process... { smcp_node_t subdevice = smcp_node_init(NULL, smcp_get_root_node(smcp), "lots_of_devices"); unsigned char i = 0; #if 1 for(i = i * 97 + 101; i; i = i * 97 + 101) { #else for(i = 0; i != 255; i++) { #endif char *name = NULL; asprintf(&name, "subdevice_%d", i); // Will leak, but we don't care. smcp_node_init(NULL, (smcp_node_t)subdevice, name); } // { // unsigned int i = bt_rebalance( // (void**)&((smcp_node_t)subdevice)->children); // printf("Balance operation took %u rotations\n", i); // } // { // unsigned int i = bt_rebalance( // (void**)&((smcp_node_t)subdevice)->children); // printf("Second balance operation took %u rotations\n", i); // } // { // unsigned int i = bt_rebalance( // (void**)&((smcp_node_t)subdevice)->children); // printf("Third balance operation took %u rotations\n", i); // } // { // unsigned int i = bt_rebalance( // (void**)&((smcp_node_t)subdevice)->children); // printf("Fourth balance operation took %u rotations\n", i); // } } //printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children)); { coap_msg_id_t tid = smcp_get_next_msg_id(smcp2); char url[256]; #if 0 snprintf(url, sizeof(url), "smcp://["COAP_MULTICAST_IP6_ALLDEVICES"]:%d/device/", smcp_get_port(smcp)); #else snprintf(url, sizeof(url), "smcp://[::1]:%d/device/", smcp_get_port(smcp)); #endif smcp_begin_transaction_old( smcp2, tid, 5 * MSEC_PER_SEC, 0, // Flags NULL, &list_response_handler, NULL ); smcp_outbound_begin(smcp2,COAP_METHOD_GET,COAP_TRANS_TYPE_CONFIRMABLE); smcp_outbound_set_msg_id(tid); smcp_outbound_set_uri(url, 0); smcp_outbound_send(); fprintf(stderr, " *** Sent LIST request...\n"); } //printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children)); int i; for(i = 0; 1/*i < 3000000*/; i++) { #if 1 if((i - 1) % 250 == 0) { fprintf(stderr, " *** Forcing variable refresh...\n"); smcp_variable_node_did_change(&device_node,0,NULL); } #endif smcp_process(smcp); smcp_process(smcp2); #if HAVE_LIBCURL smcp_curl_proxy_node_process(&proxy_node); #endif } bail: return 0; }