int main (void) { initialise_network (); // Check that we have local networking via ZeroMQ void *ctx = zmq_ctx_new (); assert (ctx); void *dealer = zmq_socket (ctx, ZMQ_DEALER); if (zmq_bind (dealer, "tcp://127.0.0.1:5670") == -1) { printf ("E: Cannot find 127.0.0.1 -- your system does not have local\n"); printf ("E: networking. Please fix this before running libzmq checks.\n"); return -1; } // Check that we can create 1,000 sockets int handle [1000]; int count; for (count = 0; count < 1000; count++) { handle [count] = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); if (handle [count] == -1) { printf ("W: Only able to create %d sockets on this box\n", count); printf ("I: Tune your system to increase maximum allowed file handles\n"); #if defined (ZMQ_HAVE_OSX) printf ("I: On OS/X, run 'ulimit -n 1200' in bash"); #elif defined (ZMQ_HAVE_LINUX) printf ("I: On Linux, run 'ulimit -n 1200' in bash"); #endif return -1; } } // Release the socket handles for (count = 0; count < 1000; count++) { close(handle[count]); } }
// // // Main procedure // // int main(int argc, char **argv) { int shutdown = 0; // Shutdown flag struct timer_list timers; // Timers int payload_len; // length of payload returned struct payload_pkt app_data; // App payload data char node_name[HOSTNAME_LEN]; // Node name parse_options(argc, argv); // Parse command line parameters debug(DEBUG_ESSENTIAL, "Mesh starting in mode: %d\n", operating_mode); initialise_network(sizeof(struct payload_pkt),notify_link_up, notify_link_down); // Initialise the network details with callbacks initialise_timers(&timers); // and set all timers switch (operating_mode) { case OPMODE_MASTER: // Only Master nodes are responsible for broadcasting add_timer(TIMER_BROADCAST, 5); // Set to refresh network in y seconds break; case OPMODE_SLAVE: // add_timer(TIMER_APPLICATION, 15); // Set to refresh network in y seconds // now done when link comes up break; } add_timer(TIMER_STATS, timeto1hour()); // Report Network Efficiency stats hourly while (!shutdown) { // While NOT shutdown wait_on_network_timers(&timers); // Wait for message or timer expiory if (check_network_msg()) { // If a message is available handle_network_msg(&node_name[0], (char *)&app_data, &payload_len); // handle the network message handle_app_msg(&app_data, payload_len); // handle application specific messages } switch (check_timers(&timers)) { // check which timer has expired case TIMER_BROADCAST: // On Broadcast timer broadcast_network(); // send out broadcast message to contact other nodes add_timer(TIMER_BROADCAST, 20); // and set to broadcast again in y seconds break; case TIMER_PING: if (check_live_nodes()) { // On Ping check the network add_timer(TIMER_REPLY, 2); // Expire replies if not received within x secoonds add_timer(TIMER_PING, 20); // and set to Ping again in y seconds } break; case TIMER_REPLY: expire_live_nodes(); // Expire other nodes where reply has timed out break; case TIMER_STATS: report_network_stats(); // Report network efficiency stats hourly add_timer(TIMER_STATS, timeto1hour()); // Set to refresh network in 1 hour break; case TIMER_PAYLOAD_ACK: timeout_payload(); break; case TIMER_APPLICATION: handle_app_timer(); // Handle the timer for the App break; default: break; } DEBUG_FUNCTION( DEBUG_DETAIL, display_live_network()); DEBUG_FUNCTION( DEBUG_DETAIL, display_timers(&timers)); } debug(DEBUG_ESSENTIAL, "Mesh node shut down\n"); return 0; }