コード例 #1
0
ファイル: peer.c プロジェクト: eniu9350/vanilladht
void boot(localnode* n)
{
				printf("1\n");
				//SockAddr bootAddr;	//addr of bootstrap node
				SockAddr thisaddr_upper;	//addr of this node for upper layer access

				//listen for connections
				n->addr.port = NODELOCAL_DEFAULT_PORT_ROUTING_LAYER_PEER;
				n->bootAddr.port = NODELOCAL_DEFAULT_PORT_ROUTING_LAYER_PEER;
				n->port_upper = NODELOCAL_DEFAULT_PORT_ROUTING_LAYER_UPPER;

				//get bootstrap node address
				//getBootstraplocalnodeIp(&bootAddr);
				if(ipaddr_cmp(&(n->bootAddr.ip), &n->addr.ip)==1 && n->bootAddr.port == n->addr.port)	{
								printf("cmp true, boot = this\n");
								//listen on peer port
								sw_listen(&n->addr);

								thisaddr_upper = n->addr;
								thisaddr_upper.port = n->port_upper;
								//listen on upper port
								sw_listen(&thisaddr_upper);
				}
				else	{
								//connect and save socket fd
								//n->bsSfd = sw_conn(bootAddr);
								//sw_listen(&n->addr);	//mmm:uncomment it! 
								//n->bootAddr = bootAddr;

								printf("before join\n");
								join(n);
				}

}
コード例 #2
0
ファイル: HTTPServer.cpp プロジェクト: BoxOfSnoo/Fairmount
//-------------------------------------------------------------------------
HTTPServer* createServer(const char *device, std::vector<FMFileInfo> fileList)
{
    fprintf(stderr, "createServer device: %s\n", (device ? device : "null"));
    if (!device) return NULL;

    //----------------
    IFileAccess *dw = IFileAccess::createCached(IFileAccess::createCSS(device, fileList));
    fprintf(stderr, "createServer IFileAccess: %p\n", dw);
    if (!dw) return NULL;

    //----------------
    sw_socket *lst;
    int port = gListeningPort;
    int tryCount = 100;
    while(tryCount > 0)
    {
        lst = sw_listen(port);
        if (lst) break;

        port++;
        tryCount--;
    }
    fprintf(stderr, "createServer socket: %p\n", lst);
    if (!lst)
    {
        fprintf(stderr, "No available ports...\n");
        dw->destroy();
        return NULL;
    }

    //----------------
    HTTPServer *server = (HTTPServer *) malloc( sizeof(HTTPServer) );
    fprintf(stderr, "createServer server: %p\n", server);
    if (!server)
    {
        sw_close(lst);
        dw->destroy();
        return NULL;
    }

    server->lst = lst;
    server->port = port;
    server->dw = dw;

    pthread_create(&server->thread, 0, (void *(*)(void*))acceptSocket, server);

    return server;
}