Esempio n. 1
0
/*
 * This function is fully implemented
 *
 * The functions that are called need to be implemented to actually
 * do something
 *
 */
void process(Dragon * dragons, int & currentDragonNumbers, int choice,
	PernThread *& pernPtr)
{
	switch (choice)
	{
	case ADD:
		addDragon(dragons, currentDragonNumbers);
		break;

	case DETECT:
		makeThread(pernPtr);
		break;
	case SELECT_ACTION:
		action(dragons, currentDragonNumbers, pernPtr);
		break;

	case DISPLAY:
		showAll(dragons, currentDragonNumbers, pernPtr);
		break;

	case QUIT:
		// just trap this choice so that it does not show as an error
		break;

	default:
		cout << choice << " is not a valid option" << endl;
		break;
	}
}
ThreadPoolNormal::ThreadPoolNormal(int num_workers) 
  : flagstop_(false) {
  for(int i=0; i<num_workers; i++) {
   Callback<void>* cb = makeCallableOnce(&ThreadPoolNormal::ExecuteTask,this);
   threads_.push_back(makeThread(cb));
  }
}	
Esempio n. 3
0
void makeThread(nodeptr start, nodeptr stop)
{
	int ndesc, i;
	nodeptr desc[NCHILD + 1];
	/* Set link to the next node */
	Next(start) = stop;
	/* If could descendent to thread */
	if (Type(start) == CELL) {
		/* Start counting */
		ndesc = 0;
		/* Loop over all child cells  */
		for (i = 0; i < NCHILD; i++)
			/* If this one is occupied */
			if (Child(start)[i] != NULL)
				/* Store it in the table */
				desc[ndesc++] = Child(start)[i];
		/* Set link to the first one */
		More(start) = desc[0];
		/* Thread last one to next */
		desc[ndesc] = stop;
		/* Loop over descendents */
		for (i = 0; i < ndesc; i++)
			/* Thread them together */
			makeThread(desc[i], desc[i + 1]);
	}
}
Esempio n. 4
0
/**
 * Thread to handle all incoming connection requests
 */
void incomingHandler(void *vargp)
{
	int listenfd; 				// fd we are listening on
	int len;
	int count = 0;
	struct sockaddr addr;
	struct Connection *conn;	// Pointer to connection info

	/* Grab the open listening file descriptor from the args */
	listenfd = *((int *) vargp);
	Free(vargp);

	/* Continuously listen and accept connections */
	for (;;) {
		conn = Malloc(sizeof(struct Connection));

		if ((conn->fd = accept(listenfd, &addr, &len)) == INVALID_SOCKET) {
			Free(conn);
			continue;
		}

		if (verbose) {
			printf("T00: New client connection, request number %d\n", count);
		}

		conn->n = count++;

		/* Spawn a thread to handle the connection */
		makeThread(&connectionHandler, conn);
	}
}
Esempio n. 5
0
ThreadPoolNormal::ThreadPoolNormal(int num_workers) {
  for (int i = 0; i < num_workers; ++i) {
    Callback<void>* body = makeCallableOnce(&ThreadPoolNormal::workerLoop,
                                            this);
    workers_.push_back(makeThread(body));
  }
}
Esempio n. 6
0
void treeInit(cellptr * root, bodyptr bodies, int nbody, real * rootSize)
{
	bodyptr p;
	(*rootSize) = 1.0;
	/* Count the root size */
	(*rootSize) = expandBox(*root, *rootSize, bodies, nbody);
	(*root) = treeCreateCell();
	/* Foreach bodies */
	int i = 0;
	for (p = bodies; p < bodies + nbody; p++) {
		/* Insert into tree */
		treeInsert(root, *rootSize, p);
	}
	calculateCenterOfMass(*root);
	makeThread((nodeptr) * root, NULL);
}
Esempio n. 7
0
int startServer(Server * srv)
{
    //tell the server it's on, this has to be done first because
    //the server thread checks for it
    servOn(srv);
    srv->serverThread       = makeThread(serverThread, srv);

    //if the server thread has not been made
    if(!(srv->serverThread))
    {
        //turn off the server power flag
        servOff(srv);
        return 0;
    }
    //server is now running, return success
    else return 1;
}
Esempio n. 8
0
/**
 * Creates the server component of the RCC. Spawns listening thread and
 * worker threads.
 */
int createServer(int port)
{
	int *listenfd;		// The fd we are listening on
	WSADATA wsaData;	// Things for winsock setup
	WORD version;

	/* Initialize the Windows socket interface */
	version = MAKEWORD(2, 0);
	if (WSAStartup(version, &wsaData) != 0) {
		Error("WSAStartup error");
	}

	if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) {
		WSACleanup();
		Error("WSAStartup error");
	}

	/* Get and store the local IP address */
	getIPAddress();

	listenfd = Malloc(sizeof(int));

	/* Open a port to listen on */
	if ((*listenfd = openListenFD(port)) < 0) {
		return (-1);
	}

	/* Create incoming connection handler */
	makeThread(&incomingHandler, listenfd);

	if (verbose) {
		printf("T00: Server initialized on port %d\n", port);
	}

	/* Success */
	return (0);
}
Esempio n. 9
0
void
ConnCreate(TConn **            const connectionPP,
           TServer *           const serverP,
           TSocket *           const connectedSocketP,
           TThreadProc *       const job,
           TThreadDoneFn *     const done,
           enum abyss_foreback const foregroundBackground,
           abyss_bool          const useSigchld,
           const char **       const errorP) {
    /*----------------------------------------------------------------------------
       Create an HTTP connection.

       A connection carries one or more HTTP transactions (request/response).

       'connectedSocketP' transports the requests and responses.

       The connection handles those HTTP requests.

       The connection handles the requests primarily by running the
       function 'job' once.  Some connections can do that autonomously, as
       soon as the connection is created.  Others don't until Caller
       subsequently calls ConnProcess.  Some connections complete the
       processing before ConnProcess return, while others may run the
       connection asynchronously to the creator, in the background, via a
       TThread thread.  'foregroundBackground' determines which.

       'job' calls methods of the connection to get requests and send
       responses.

       Some time after the HTTP transactions are all done, 'done' gets
       called in some context.
    -----------------------------------------------------------------------------*/
    TConn * connectionP;

    MALLOCVAR(connectionP);

    if (connectionP == NULL)
        xmlrpc_asprintf(errorP, "Unable to allocate memory for a connection "
                        "descriptor.");
    else {
        abyss_bool success;
        uint16_t peerPortNumber;

        connectionP->server     = serverP;
        connectionP->socketP    = connectedSocketP;
        connectionP->buffersize = 0;
        connectionP->bufferpos  = 0;
        connectionP->finished   = FALSE;
        connectionP->job        = job;
        connectionP->done       = done;
        connectionP->inbytes    = 0;
        connectionP->outbytes   = 0;
        connectionP->trace      = getenv("ABYSS_TRACE_CONN");
        connectionP->start      = time(NULL);

        SocketGetPeerName(connectedSocketP,
                          &connectionP->peerip, &peerPortNumber, &success);

        if (success)
            makeThread(connectionP, foregroundBackground, useSigchld, errorP);
        else
            xmlrpc_asprintf(errorP, "Failed to get peer name from socket.");
    }
    *connectionPP = connectionP;
}
Esempio n. 10
0
void
ConnCreate(TConn **            const connectionPP,
           TServer *           const serverP,
           TChannel *          const channelP,
           void *              const channelInfoP,
           TThreadProc *       const job,
           size_t              const jobStackSize,
           TThreadDoneFn *     const done,
           enum abyss_foreback const foregroundBackground,
           bool                const useSigchld,
           const char **       const errorP) {
/*----------------------------------------------------------------------------
   Create an HTTP connection.

   A connection carries one or more HTTP transactions (request/response).

   *channelP transports the requests and responses.

   The connection handles those HTTP requests.

   The connection handles the requests primarily by running the
   function 'job' once.  Some connections can do that autonomously, as
   soon as the connection is created.  Others don't until Caller
   subsequently calls ConnProcess.  Some connections complete the
   processing before ConnProcess return, while others may run the
   connection asynchronously to the creator, in the background, via a
   TThread thread.  'foregroundBackground' determines which.

   'job' calls methods of the connection to get requests and send
   responses.

   Some time after the HTTP transactions are all done, 'done' gets
   called in some context.

   'channelInfoP' == NULL means no channel info supplied.
-----------------------------------------------------------------------------*/
    TConn * connectionP;

    MALLOCVAR(connectionP);

    if (connectionP == NULL)
        xmlrpc_asprintf(errorP, "Unable to allocate memory for a connection "
                        "descriptor.");
    else {
        connectionP->server       = serverP;
        connectionP->channelP     = channelP;
        connectionP->channelInfoP = channelInfoP;
        connectionP->buffer.b[0]  = '\0';
        connectionP->buffersize   = 0;
        connectionP->bufferpos    = 0;
        connectionP->finished     = FALSE;
        connectionP->job          = job;
        connectionP->done         = done;
        connectionP->inbytes      = 0;
        connectionP->outbytes     = 0;
        connectionP->trace        = getenv("ABYSS_TRACE_CONN");

        makeThread(connectionP, foregroundBackground, useSigchld,
                   jobStackSize, errorP);
    }
    *connectionPP = connectionP;
}