Пример #1
0
static void *
reader(void *aport)
{
	Buffer *bp;
	Port *port;
	Cam *cam;
	uint8_t *dstmac, *srcmac;
	int i, nrd, nref;

	port = (Port *)aport;
	for(;;){
		bp = qget(&port->freeq);

		nrd = read(port->fd, bp->buf, bp->cap);
		if(port->state != PortOpen){
			qput(bp->freeq, bp);
			break;
		}
		bp->len = nrd;

		dstmac = (uint8_t *)bp->buf + 4;
		srcmac = (uint8_t *)bp->buf + 10;

		nref = 0;
		cam = camlook(g_cams, dstmac);
		if(cam != NULL && cam->port != NULL){
			// port found in cam, forward only there...
			nref = bincref(bp);
			if(qput(&cam->port->xmitq, bp) == -1)
				nref = bdecref(bp);
		} else {
			// broadcast..
			for(i = 0; i < nports; i++){
				if(port == (ports+i))
					continue;
				nref = bincref(bp);
				if(qput(&ports[i].xmitq, bp) == -1)
					nref = bdecref(bp);
			}
		}

		// teach the switch about the source address we just saw
		cam = camlook(g_cams, srcmac);
		if(cam != NULL){
			// always update the port, so if an address moves to a different port
			// the cam will point to that port right away.
			copymac(cam->mac, srcmac);
			cam->age = 0;
			cam->port = port;
		} else {
			fprintf(stderr, "cam presumably full..\n");
		}

		// ref is zero after the forward loop. it didn't go anywhere, so drop it.
		if(nref == 0)
			qput(bp->freeq, bp);
	}
	fprintf(stderr, "%s: reader exiting..\n", portname(port));
	return port;
}
Пример #2
0
static void traverse(STri *t, int op) {
	STri tnew,tr;
	qinit();
	qput(t,op);
	while(qany()) {
		op = qget(&tr);
		if (!reg_stri(&tr)) continue;
		if (op!=Q_OP) { Q_refl(&tr,&tnew);  qput(&tnew,Q_OP);	}
		if (op!=P_OP) { P_refl(&tr,&tnew);  qput(&tnew,P_OP);	}
		if (op!=R_OP) { R_refl(&tr,&tnew);  qput(&tnew,R_OP);	}
		}
	}
Пример #3
0
/* remove n from q1 and put it in q2 if nid not in q2 */
int lqremoveputifn(void *q1p, void *q2p, int (*searchfn1)(void* elementp,void* skeyp1),
                    void* skeyp1, int (*searchfn2)(void* elementp,void* skeyp2), void * skeyp2){ 
    if(NULL != q1p && NULL != q2p){
        void * node;
        int ret = -1;
        pthread_mutex_t *q1_lock = &(((lqtype *)q1p)->q_lock);
        pthread_mutex_t *q2_lock = &(((lqtype *)q2p)->q_lock);
        int rc = pthread_mutex_lock(q1_lock);
        rc = pthread_mutex_lock(q2_lock);
        if(NULL == searchfn2 || qsearch(((lqtype *)q2p)->queue, searchfn2, skeyp2) == NULL){
            node = qremove(((lqtype *)q1p)->queue, searchfn1, skeyp1);
            if(NULL != node){
                qput(((lqtype *)q2p)->queue, node);
                ret = 0;
            }   
        }
        rc = pthread_mutex_unlock(q2_lock);
        rc = pthread_mutex_unlock(q1_lock);

        return ret;
    } else {
        printf("should open queues before using it\n");
        return -1;
    }  
}
Пример #4
0
static void *
writer(void *aport)
{
	Port *port = (Port *)aport;
	Buffer *bp;
	int nwr, nref;

	for(;;){
		bp = qget(&port->xmitq);
		if(bp == NULL)
			break;
		if(bp->len > 0){
			*(uint32_t *)bp->buf = 0;
			nwr = write(port->fd, bp->buf, bp->len);
			if(nwr != bp->len){
				fprintf(stderr, "%s: short write, got %d wanted %d\n", portname(port), nwr, bp->len);
				break;
			}
		}
		nref = bdecref(bp);
		if(nref == 0)
			qput(bp->freeq, bp);
	}
	fprintf(stderr, "%s: writer exiting\n", portname(port));
	return port;
}
Пример #5
0
int main(int argc, char * argv[]){
	
	int cclients = 10;
	int sentMsg = 0;
	printf("Server iniciado para %d clientes.\n", cclients);

	message_t incomingMsg;
	message_t msgsent;
	char * word = "Mensaje del Server";
	
	ipc_t server = (ipc_t) fifoServe(cclients);
	server->stop = 0;

	while(sentMsg != 1000){
		if((incomingMsg = qget(server->inbox)) != NULL){
			printf("%d. Server recibio: %s\n", sentMsg, incomingMsg->data);
			
			printf("Manda mensaje a:%d.\n", incomingMsg->header.from);
			qput(server->outbox, (msgsent = mnew(0,incomingMsg->header.from, strlen(word) + 1, word)));
			mdel(msgsent);

			mdel(incomingMsg);
			sentMsg++;
		}
	}
	stopServer(server);
}
Пример #6
0
void * mq_clientLoop(void* ipcarg)
{
	ipc_t ipc;
	ipc = (ipc_t) ipcarg;
	message_t msg;
	
	if(ipc->status == IPCSTAT_DISCONNECTED)
	{
		return NULL;
	}
	
	ipc->status = IPCSTAT_CONNECTED;
	
	
    while (ipc->stop != 1) {
    	
    	msg = mq_getData(ipc, ipc->ipcdata->queuedata.recvPrior);
    	
    	if(msg != NULL)
    	{
    	
			qput(ipc->inbox, msg);
    	}
    	
    	msg = qget(ipc->outbox);
        if(msg != NULL)
        {
        	mq_sendData(ipc,msg,SERVERKEY);
        }
    }
    
    mq_disconnect(ipc);
    free(msg);
    return NULL;
}
Пример #7
0
void lqput(void *qp, void *elementp){
    if(NULL != qp){
        pthread_mutex_t *q_lock = &(((lqtype *)qp)->q_lock);
        int rc = pthread_mutex_lock(q_lock);
        qput(((lqtype *)qp)->queue, elementp);
        rc = pthread_mutex_unlock(q_lock);
    } else {
	printf("should open queue before using it\n");
    }

}
Пример #8
0
/*
 * application entry point
 *
 */
int main(int argc, char *argv[])
{
    char *p;

    struct queue *q = qalloc();

    while (argc--) qput(q, *argv++);

    while ((p = qget(q))) printf("%s\n", p);

    qfree(q);

    return 0;
}
Пример #9
0
void * mq_serverLoop(void* ipcarg)
{
	ipc_t ipc;
	ipc = (ipc_t) ipcarg;
	message_t msg;
	
	if(ipc->status == IPCSTAT_DISCONNECTED)
	{
		return NULL;
	}
	
	ipc->status = IPCSTAT_SERVING;
	
    while (ipc->stop != 1) {
    	
        msg = mq_getData(ipc, SERVERKEY);
        if(msg != NULL)
        {
			if(msg->header.to == SERVERKEY)
		    {
		    /*
		    	//[TODO] ver que pasa si hay un mensaje con to = 0
		    	//pero hay que cambiar el to.. 0 no existe*/
		    	
		    	/* Es esto lo que tiene que hacer?*/
		    	qput(ipc->inbox, msg);
		    }
		    else 
		    {
		    	mq_sendData(ipc,msg,msg->header.to);
		    }
        }
        
        msg = qget(ipc->outbox);
        if(msg != NULL)
        {
        	mq_sendData(ipc,msg,SERVERKEY);
        }
    }
    
    mq_disconnect(ipc);
    free(msg);
    return NULL;
}
Пример #10
0
/********************************************
Writes the LINE with FMT (format) to the FILE at the given log LEVEL.
The different levels of logging are enum'ed in LOG_LEVEL in fd.h.
The log will include the file and line of the log entry, as well as the time
at which the entry occurred.
The line can be a format string like any printf statement to allow for debugging from
within a firedrake application.
********************************************/
void fd_log_write(int level, char *file, int line, char *fmt, ...){
	char output[MAX_LOG_LINE], message[MAX_LOG_LINE];
	va_list args;

	memset(output, 0, MAX_LOG_LINE);
	memset(message, 0, MAX_LOG_LINE);

	time_t now;
	time(&now);

	/* start the line with the appropriate prefix */
	switch (level) {
	case DEBUG:
		snprintf(output, MAX_LOG_LINE, "[DEBUG] %s:%d:%s\t", file, line, ctime(&now));
		break;
	case INFO:
		snprintf(output, MAX_LOG_LINE, "[INFO] %s:%d:%s\t", file, line, ctime(&now));
		break;
	case MESSAGE:
		snprintf(output, MAX_LOG_LINE, "[MESSAGE] %s:%d:%s\t", file, line, ctime(&now));
		break;
	case WARNING:
		snprintf(output, MAX_LOG_LINE, "[WARNING] %s:%d:%s\t", file, line, ctime(&now));
		break;
	case CRITICAL:
		snprintf(output, MAX_LOG_LINE, "[CRITICAL] %s:%d:%s\t", file, line, ctime(&now));
		break;
	case ERROR:
		snprintf(output, MAX_LOG_LINE, "[ERROR] %s:%d:%s\t", file, line, ctime(&now));
		break;
	}
	
	/* copy the message from the user code */
	va_start(args, fmt);
	vsnprintf(message, MAX_LOG_LINE, fmt, args);
	va_end(args);

	/* assemble the complete line */
	strncat(output, message, MAX_LOG_LINE - strlen(output) - 1);

	/* enqueue this line to be written to the log */
	qput(log_queue, (void *) strdup(output));
}
Пример #11
0
static void *calculator(void *args) {
    int i;
    CalculatorInput_t *input = (CalculatorInput_t *)args;
    QueueElement_t *elements[input->m * PARTITION_MULTIPLIER];

    // Create queue
    //qp = qopen();
    
    // Queue loop
    for (i = 0; i < (input->m * PARTITION_MULTIPLIER); i++) {
        elements[i] = (QueueElement_t *)malloc(sizeof(QueueElement_t));
        elements[i]->a = input->start
            + i * ((input->end - input->start) / (input->m * PARTITION_MULTIPLIER));
        elements[i]->b = input->start
            + (i+1) * ((input->end - input->start) / (input->m * PARTITION_MULTIPLIER));
        elements[i]->fp = input->fp;
        elements[i]->p = input->p;

        // CRITICAL SECTION
        pthread_mutex_lock(&q_lock);
        qput(input->queue->qp, (void *)elements[i]);
        input->queue->num_elements++;
        pthread_mutex_unlock(&q_lock);
        //printf("Item %d placed in queue\n", i);
        //fflush(stdout);
        // END CRITICAL SECTION

    }
    // Set calculator complete
    output.calc_complete = true;

    // Wait loop- replacing with a join in generator

    // Cleanup and return
    return NULL;
}
Пример #12
0
int main(void) {
    /* Create two empty queues */
    void *q1;
    void *q2;
    void *pt;
    int a,b,c,d,e,f,g,h,i,j,k;
    int x;
    int ii;
    a = 1;
    b = 4;
    c = 65;
    d = 34;
    f = 6;
    e = 9;
    g = 11;
    h = 23;
    i = 999;
    j = 234;
    k = 94;

    q1 = qopen();
    q2 = qopen();

    /* Fill both */
    qput(q1, &a);
    qput(q1, &b);
    qput(q1, &c);
    qput(q1, &d);
    qput(q1, &e);
    qput(q1, &f);
    qput(q2, &g);
    qput(q2, &h);
    qput(q2, &i);
    qput(q2, &j);
    qput(q2, &k);

    /* Get an item out of q1 */
    pt = qget(q1);
    if (*((int *)pt) != 1) {
        printf("Fail on qget; returned %d.\n", *((int *)pt));
        return -1;

    }

    /* Catenate */
    qconcat(q1, q2);

    /* Remove until null is returned, count number of iterations */
    for (ii = 0; pt != NULL; ii++) {
        pt = qget(q1);

    }
    if (ii != 11) {
        printf("Fail on catenate; queue was of size %d.\n", ii);
        return -1;

    }

    /* Use remove to take out a specified value  */
    qput(q1, &a);
    qput(q1, &b);
    qput(q1, &c);
    qput(q1, &d);
    qput(q1, &e);
    qput(q1, &f);
    qput(q1, &g);
    qput(q1, &h);
    qput(q1, &i);
    qput(q1, &j);
    qput(q1, &k);
    x = 999;
    pt = qremove(q1, searchfn, &x);
    if (pt == NULL || *((int *)pt) != 999) {
        if (pt == NULL) {
            printf("Fail on qsearch; value not found.\n");

        }
        else {
            printf("Fail on qremove / qsearch; removed value was %d.\n", *((int *)pt));

        }
    }
    /* Use qapply to print out remaining list */
    qapply(q1, printfn);
    return 1;

}