Beispiel #1
0
int main(int argc, char** argv) {
    nabto_main_setup* nms = unabto_init_context();

    stream_echo_init();
    
    if (!check_args(argc, argv, nms)) {
        return 1;
    }

    if (!unabto_init()) {
        return 1;
    }
    
    while(true) {
        unabto_tick();
#ifdef WIN32
        Sleep(1);
#endif
#if (_POSIX_C_SOURCE >= 199309L)
        struct timespec sleepTime;
        sleepTime.tv_sec = 0;
        sleepTime.tv_nsec = 10*1000000;
        nanosleep(&sleepTime, NULL);
#endif
    }
    
    unabto_close();
}
unabto_api_status_t unabto_api_platform_stop() {
    if (running) {
        running = false;
        unabto_thread_join(tick_thread, NULL, NULL);
    }
    unabto_close();
    return UNABTO_API_OK;
}
Beispiel #3
0
/**
 * Entry point.
 * @param argc  number of parameters
 * @param argv  the parameters
 * - 1 ip-address
 * - 2 serverid | - 
 * - 3 log
 * @return      the result
 */
int main(int argc, char* argv[])
{
    nabto_main_setup* nms;
    nabto_endpoint localEp;

    // flush stdout
    setvbuf(stdout, NULL, _IONBF, 0);

    nms = unabto_init_context();

    /**
     * Overwrite default values with command line args
     */
    if (!check_args(argc, argv, nms)) {
        return 1;
    }

    if (!unabto_init()) {
        NABTO_LOG_FATAL(("Failed at nabto_main_init"));
    }

	localEp.addr=nms->ipAddress;
	localEp.port=nms->localPort;
	
	if (nms->ipAddress != INADDR_ANY) NABTO_LOG_INFO(("Own IP address: " PRIep, MAKE_EP_PRINTABLE(localEp)));
    else NABTO_LOG_INFO(("Own IP address, local IP is set to INADDR_ANY: " PRIep, MAKE_EP_PRINTABLE(localEp)));

    while (true) {
        /*
         * Here the main application should do it's work
         * and then whenever time is left for Nabto (but at least regularly)
         * the nabto_main_tick() should be called.
         *
         * nabto_main_tick()
         * - polls the socket for incoming messages
         * - administers timers to do its own timer based work
         */
        unabto_tick();
#if NABTO_ENABLE_STREAM
        nabto_stream_test_tick();
#endif
#if (_POSIX_C_SOURCE >= 199309L)
        struct timespec sleepTime;
        sleepTime.tv_sec = 0;
        sleepTime.tv_nsec = 10*1000000;
        nanosleep(&sleepTime, NULL);
#endif
    }

    unabto_close();
	return 0;
}
Beispiel #4
0
void tunnel_loop_epoll() {

    struct epoll_event events[MAX_EPOLL_EVENTS];
    int timeout;
    nabto_stamp_t ne;
    nabto_stamp_t now;
    int nfds;

    if (!unabto_init()) {
        NABTO_LOG_FATAL(("Failed to initialize unabto"));
    }

    if (!init_tunnel_module()) {
        NABTO_LOG_FATAL(("Cannot initialize tunnel module"));
        return;
    }

    unabto_time_auto_update(false);
    // time is updated here and after the select since that's the only blocking point.
    unabto_time_update_stamp();

    while (true) {
        int i;
        unabto_next_event(&ne);
        now = nabtoGetStamp();
        timeout = nabtoStampDiff2ms(nabtoStampDiff(&ne, &now));
        if (timeout < 0) {
            timeout = 0;
        }
        
        nfds = epoll_wait(unabto_epoll_fd, events, MAX_EPOLL_EVENTS, timeout);
        unabto_time_update_stamp();
        
        for (i = 0; i < nfds; i++) {
            
            unabto_epoll_event_handler* handler = (unabto_epoll_event_handler*)events[i].data.ptr;

            if (handler->epollEventType == UNABTO_EPOLL_TYPE_UDP) {
                unabto_network_epoll_read(&events[i]);
            }
#if NABTO_ENABLE_TCP_FALLBACK
            unabto_tcp_fallback_epoll_event(&events[i]);
#endif
            unabto_tunnel_epoll_event(&events[i]);
        }

        unabto_time_event();
    }
    deinit_tunnel_module();
    unabto_close();
}
Beispiel #5
0
/**
 *  main using gopt to check command line arguments
 *  -h for help
 */
int main(int argc, char* argv[])
{
    // Set nabto to default values

    nabto_main_setup* nms = unabto_init_context();

    // Overwrite default values with command line args
    if (!check_args(argc, argv, nms)) {
        return 1;
    }
    NABTO_LOG_INFO(("Identity: '%s'", nms->id));
    NABTO_LOG_INFO(("Program Release %i.%i", RELEASE_MAJOR, RELEASE_MINOR));
    NABTO_LOG_INFO(("Buffer size: %i", nms->bufsize));

    // Initialize nabto
    if (!unabto_init()) {
        NABTO_LOG_FATAL(("Failed at nabto_main_init"));
    }

    nabto_stamp_t attachExpireStamp;
    // 20 seconds
    nabtoSetFutureStamp(&attachExpireStamp, 1000*20);
        
    // The main loop gives nabto a tick from time to time.
    // Everything else is taken care of behind the scenes.
    while (true) {
        unabto_tick();
        nabto_yield(10);
        if (nabtoIsStampPassed(&attachExpireStamp)) {
            NABTO_LOG_ERROR(("Attach took too long."));
            exit(4);
        }
        if (unabto_is_connected_to_gsp()) {
            NABTO_LOG_INFO(("Successfully attached to the gsp, now exiting."));
            exit(0);
        }
    }

    NABTO_LOG_ERROR(("we should not end here"));
    exit(5);
    
    unabto_close();
    return 0;
}
Beispiel #6
0
void handle_signal(int signum) {
    if (signum == SIGTERM) {
        // Terminate application with exit,
        // This makes libprofiler extra happy
        NABTO_LOG_INFO(("Exit in signal handler: SIGTERM"));
        exit(1);
    }
    if (signum == SIGINT) {
        // Terminate application with exit,
        // This makes libprofiler extra happy
        if (nice_exit) {
            NABTO_LOG_INFO(("Exit in signal handler: SIGINT."));
            NABTO_LOG_INFO(("Cleanup nicely (since --nice_exit was specified)"));
            unabto_close();
            NABTO_LOG_INFO(("Cleanup done. Exiting"));
            exit(1);
        }
        NABTO_LOG_INFO(("Exit in signal handler: SIGINT. No --nice_exit argument -> no cleanup"));
        exit(1);
    }
}
Beispiel #7
0
int main(int argc, char** argv) {
    nabto_main_setup* nms = unabto_init_context();

    stream_echo_init();
    
    if (!check_args(argc, argv, nms)) {
        return 1;
    }

    if (!unabto_init()) {
        return 1;
    }
    
    unabto_time_auto_update(false);
    while(true) {
        unabto_time_update_stamp();
        wait_event();
    }
    
    unabto_close();
}
Beispiel #8
0
/**
 *  main using gopt to check command line arguments
 *  -h for help
 */
int main(int argc, char* argv[])
{

    // Set nabto to default values
    nabto_main_setup* nms = unabto_init_context();


    // Initialise application
    if (!application_init()){
        NABTO_LOG_FATAL(("Unable to initialise serial connection"));
    }

    // Optionally set alternative url to html device driver
    //nmc.nabtoMainSetup.url = "https://dl.dropbox.com/u/15998645/html_dd_demo.zip";

    // Overwrite default values with command line args
    if (!check_args(argc, argv, nms)) {
        return 1;
    }
    NABTO_LOG_INFO(("Identity: '%s'", nms->id));
    NABTO_LOG_INFO(("Program Release %" PRIu32 ".%" PRIu32, RELEASE_MAJOR, RELEASE_MINOR));
    NABTO_LOG_INFO(("Buffer size: %d" , nms->bufsize));

    // Initialize nabto
    if (!unabto_init()) {
        NABTO_LOG_FATAL(("Failed at nabto_main_init"));
    }

    // The main loop gives nabto a tick from time to time.
    // Everything else is taken care of behind the scenes.
    while (true) {
        unabto_tick();
        nabto_yield(10);
    }

    unabto_close();

    return 0;
}
Beispiel #9
0
void tunnel_loop_epoll() {

    struct epoll_event events[MAX_EPOLL_EVENTS];
    int timeout;
    nabto_stamp_t ne;
    nabto_stamp_t now;
    int nfds;
    int i;

    if (!unabto_init()) {
        NABTO_LOG_FATAL(("Failed to initialize unabto"));
    }

    if (!init_tunnel_module()) {
        NABTO_LOG_FATAL(("Cannot initialize tunnel module"));
        return;
    }

    unabto_time_auto_update(false);
    // time is updated here and after the select since that's the only blocking point.
    unabto_time_update_stamp();

    while (true) {
        int i;
        unabto_next_event(&ne);
        now = nabtoGetStamp();
        timeout = nabtoStampDiff2ms(nabtoStampDiff(&ne, &now));
        if (timeout < 0) {
            timeout = 0;
        }
        
        nfds = epoll_wait(unabto_epoll_fd, events, MAX_EPOLL_EVENTS, timeout);
        unabto_time_update_stamp();
        
        for (i = 0; i < nfds; i++) {
            
            unabto_epoll_event_handler* handler = (unabto_epoll_event_handler*)events[i].data.ptr;

            if (handler->epollEventType == UNABTO_EPOLL_TYPE_UDP) {
                unabto_epoll_event_handler_udp* udpHandler = (unabto_epoll_event_handler_udp*)handler;
                bool status;
                do {
                    status = unabto_read_socket(udpHandler->fd);
                } while (status);
            }
#if NABTO_ENABLE_TCP_FALLBACK
            if (handler->epollEventType == UNABTO_EPOLL_TYPE_TCP_FALLBACK) {

                nabto_connect* con = (nabto_connect*)handler;
                if (events[i].events & EPOLLIN) {
                    unabto_tcp_fallback_read_ready(con);
                }
                if (events[i].events & EPOLLOUT) {
                    unabto_tcp_fallback_write_ready(con);
                }
            }
#endif
            if (handler->epollEventType == UNABTO_EPOLL_TYPE_TCP_TUNNEL) {
                tunnel* tunnelPtr = (tunnel*)handler;
                if (tunnelPtr->sock != INVALID_SOCKET) {
                    tunnel_event(tunnelPtr, TUNNEL_EVENT_SOURCE_TCP_READ);
                }
                if (tunnelPtr->sock != INVALID_SOCKET) {
                    tunnel_event(tunnelPtr, TUNNEL_EVENT_SOURCE_TCP_WRITE);
                }
            }
        }

        unabto_time_event();
    }
    deinit_tunnel_module();
    unabto_close();
}