/* ----------------------------------------------------------------------- */
void voole_dispatch(session_t* se)
{
	printf("i am voole_dispatch \n");
	if (!gdsl_list_is_empty(se->recv_queue)){
		result_t *presult = NULL;
		cdnmsg_t * msg = NULL;
		pthread_mutex_lock(&se->recv_lock);
		msg = gdsl_list_remove_head(se->recv_queue);
		pthread_mutex_unlock(&se->recv_lock);
		if (!msg)
			return;
		presult = gdsl_list_search(se->fi->list_result,gdsl_result_seq_cmp,&msg->head.seq);
		if (!presult) printf("i can't find seq %d\n",msg->head.seq);
		else tcp_cmd_download_resp_handle(se,presult,msg);
	}
}
Exemple #2
0
//Remove the tunnel from list and destroy the route object if empty.
void centernode_cancel(centernode_t *cn, cnaccess_t *ca)
{
    cngroup_t *cg = NULL;
    cnchild_t *cc = NULL;

    return ;
    pthread_mutex_lock(&cn->objlock); /* unregister the child node */
    cg = gdsl_hash_search(cn->groups, INT2PTR(xtoi(ca->nid)/1000000));
    if (cg != NULL) {
        cc = gdsl_list_search(cg->childs, (gdsl_compare_func_t)searchChild, ca->nid);
        if (cc != NULL) {
            int i = 0;
            int bfound = 0;
            for (i=0;i<cc->chused;i++) {
                if (cc->channel[i] == ca || bfound == 1){
                    if (i==(cc->chused-1)) {
                        cc->channel[i] = 0;
                        cc->chused --;
                    }
                    else
                        cc->channel[i] = cc->channel[i+1];
                    bfound = 1;
                }
            }
            if (cc->chused == 0) {
                printf("hello %s\n", ca->nid);
                gdsl_list_remove(cg->childs, (gdsl_compare_func_t)searchChild, ca->nid);
                cnchild_destroy(cc);
                cn->cc_sum --;
            }
        }
        //    else gdsl_list_insert_tail(cg->childs, cc);
    }
    else {}
    pthread_mutex_unlock(&cn->objlock);

}
Exemple #3
0
int main (void)
{
    int choix = 0;

    gdsl_list_t l = gdsl_list_alloc ("MY LIST", alloc_string, free_string);

    do
	{
	    printf ("\t\tMENU - LIST\n\n");
	    printf ("\t 1> Create a cell\n");
	    printf ("\t 2> Remove the first cell\n");
	    printf ("\t 3> Remove the last cell\n");
	    printf ("\t 4> Remove a cell\n");
	    printf ("\t 5> Display list in forward order\n");
	    printf ("\t 6> Display list in backward order\n");
	    printf ("\t 7> Flush list\n");
	    printf ("\t 8> Size of list\n");
	    printf ("\t 9> Dump list\n");
	    printf ("\t10> XML dump of list\n");
	    printf ("\t11> Search for a place\n");
	    printf ("\t12> Search for an element\n");
	    printf ("\t13> Sort of list\n");
	    printf ("\t14> Greatest element of list\n");
	    printf ("\t 0> Quit\n\n");
	    printf ("\t\tYour choice: ");
	    scanf ("%d", &choix);

	    switch (choix)
		{
		case 1:
		    {
			char nom[100];
			int done = 0;

			printf ("Nom: ");
			scanf ("%s", nom);

			do
			    {
				int choix;

				printf ("\t\tMENU - CELL INSERTION\n\n");
				printf ("\t1> Insert cell at the beginning of the list\n");
				printf ("\t2> Insert cell at end of list\n");
				printf ("\t3> Insert cell after another cell\n");
				printf ("\t4> Insert cell before another cell\n");
				printf ("\t5> Display the list\n");
				printf ("\t0> RETURN TO MAIN MENU\n\n");
				printf ("\t\tYour choice: ");
				scanf ("%d", &choix );

				switch (choix)
				    {
				    case 1:
					{
					    gdsl_list_insert_head (l, nom);
					    done = 1;
					}
					break;

				    case 2:
					{
					    gdsl_list_insert_tail (l, nom);
					    done = 1;
					}
					break;

				    case 3:
					if (gdsl_list_is_empty (l))
					    {
						printf ("The list is empty.\n");
					    }
					else
					    {
						char Nom[100];
						gdsl_list_cursor_t c = gdsl_list_cursor_alloc (l);

						printf ("Name of cell after which you want to insert: ");
						scanf ("%s", Nom);
			
						if (!gdsl_list_cursor_move_to_value (c, compare_strings, Nom))
						    {
							printf ("The cell '%s' doesn't exist\n", Nom);
						    }
						else
						    {
							gdsl_list_cursor_insert_after (c, nom);
							done = 1;
						    }
						gdsl_list_cursor_free (c);
					    }
					break;

				    case 4:
					if (gdsl_list_is_empty (l))
					    {
						printf ("The list is empty.\n");
					    }
					else
					    {
						char Nom[100];
						gdsl_list_cursor_t c = gdsl_list_cursor_alloc (l);

						printf ("Name of cell before which you want to insert: ");
						scanf ("%s", Nom);
			
						if (!gdsl_list_cursor_move_to_value (c, compare_strings, Nom))
						    {
							printf ("The cell '%s' doesn't exist\n", Nom);
						    }
						else
						    {
							gdsl_list_cursor_insert_before (c, nom);
							done = 1;
						    }
						gdsl_list_cursor_free (c);
					    }
					break;

				    case 5:
					if (gdsl_list_is_empty (l))
					    {
						printf ("The list is empty.\n");
					    }
					else
					    {
						affiche_liste_chaines_fwd (l);
					    }
					break;

				    case 0:
					done = 1;
					break;
				    }
			    } 
			while (!done);
		    }
		    break;

		case 2:
		    if (gdsl_list_is_empty (l))
			{
			    printf ("The list is empty.\n");
			}
		    else
			{
			    gdsl_list_delete_head (l);
			}
		    break;

		case 3:
		    if (gdsl_list_is_empty (l))
			{
			    printf ("The list is empty.\n");
			}
		    else
			{
			    gdsl_list_delete_tail (l);
			}
		    break;

		case 4:
		    {
			char nom[100];

			if (gdsl_list_is_empty (l))
			    {
				printf ("The list is empty.\n");
			    }
			else
			    {
				printf ("Name of cell to remove: ");
				scanf ("%s", nom);

				if (!gdsl_list_delete (l, compare_strings, nom))
				    {
					printf ("The cell '%s' doesn't exist\n", nom);
				    }
				else
				    {
					printf ("The cell '%s' is removed from list\n", nom);
				    }
			    }
		    }
		    break;

		case 5:
		    if (gdsl_list_is_empty (l))
			{
			    printf ("The list is empty.\n");
			}
		    else
			{
			    affiche_liste_chaines_fwd (l);
			}
		    break;

		case 6:
		    if (gdsl_list_is_empty (l))
			{
			    printf ("The list is empty.\n");
			}
		    else
			{
			    affiche_liste_chaines_bwd (l);
			}
		    break;
	  
		case 7:
		    if (gdsl_list_is_empty (l))
			{
			    printf ("The list is empty.\n");
			}
		    else
			{
			    gdsl_list_flush (l);
			}
		    break;

		case 8:
		    printf ("Card( %s ) = %ld\n", gdsl_list_get_name (l), gdsl_list_get_size (l));
		    break;

		case 9:
		    if (gdsl_list_is_empty (l))
			{
			    printf ("The list is empty.\n");
			}
		    else
			{
			    gdsl_list_dump (l, print_string, stdout, NULL);
			}
		    break;

		case 10:
		    if (gdsl_list_is_empty (l))
			{
			    printf ("The list is empty.\n");
			}
		    else
			{
			    gdsl_list_write_xml (l, print_string, stdout, NULL);
			}
		    break;

		case 11:
		    {
			int pos;
			gdsl_element_t e;

			printf ("Enter the position of the place to search for: ");
			scanf ("%d", & pos);

			e = gdsl_list_search_by_position (l, (ulong) pos);
			if (e != NULL)
			    {
				print_string (e, stdout, GDSL_LOCATION_UNDEF, NULL);
			    }
		    }
		    break;
	  
		case 12:
		    {
			char nom [100];
			gdsl_element_t e;

			printf ("Name of cell to search for: ");
			scanf ("%s", nom);
	    
			e = gdsl_list_search (l, compare_strings, nom);
			if (e == NULL)
			    {
				printf ("The cell '%s' doesn't exist\n", nom);
			    }
			else
			    {
				printf ("The cell '%s' was found: ", nom);
				print_string (e, stdout, GDSL_LOCATION_UNDEF, NULL);
				printf ("\n");
			    }
		    }
		    break;

		case 13:
		    gdsl_list_sort (l, compare_strings);
		    break;
	  
		case 14:
		    if (gdsl_list_is_empty (l))
			{
			    printf ("The list is empty.\n");
			}
		    else
			{
			    printf ("Max Element: %s\n", (char*) gdsl_list_search_max (l, compare_strings));
			}
		    break;

		case 15: /* case for my own tests... */
		    {
			int i;
			gdsl_perm_t p = gdsl_perm_alloc ("p", PERMUTATION_NB);
			gdsl_list_t g = gdsl_list_alloc ("MY LIST 2", alloc_string, free_string);

			gdsl_perm_randomize (p);

			for (i = 0; i < PERMUTATION_NB; i++)
			    {
				char c[2];
				c[0] = 65 + gdsl_perm_get_element (p, i);
				c[1] = '\0';
				gdsl_list_insert_tail (g, c);
			    }

			gdsl_perm_free (p);
			affiche_liste_chaines_fwd (g);
			affiche_liste_chaines_bwd (g);
			printf ("SORT\n");
			gdsl_list_sort (g, compare_strings);
			affiche_liste_chaines_fwd (g);
			affiche_liste_chaines_bwd (g);
			gdsl_list_free (g);
		    }

		    {
			int i = 0;
			gdsl_list_cursor_t c = gdsl_list_cursor_alloc (l);

			for (gdsl_list_cursor_move_to_head (c); gdsl_list_cursor_get_content (c); gdsl_list_cursor_step_forward (c))
			    {
				char toto[50];
				sprintf (toto, "%d", i++);

				gdsl_list_cursor_insert_before (c, toto);

				gdsl_list_cursor_step_backward (c);
				gdsl_list_cursor_delete_after (c);
			    }
			
			gdsl_list_cursor_free (c);
		    }
		    break;
		}
	} 
    while (choix != 0);

    gdsl_list_free (l);

    exit (EXIT_SUCCESS);
}
int tcp_cmd_download_resp_handle (session_t* pse,result_t* presult,cdnmsg_t* msg)
{
	uint32_t  host;
	uint8_t   isnewse;
	struct in_addr in;
	session_t *psession;
	cdnmsg_t  *req;
	switch (msg->body.downloadres.status) {
		case 0:
			presult->signal = 1;
			presult->pdata  = (uint8_t*)msg;
			presult->size = ntohl(msg->head.len)-17;
			//	sem_post(presult->psem);
			printf("i will post a sem,seq %d,sem 0x%x, ret %d\n",msg->head.seq, presult->psem,sem_post(presult->psem));
			
			break;
		case 1:
			break;
		case 2:
			break;
		case 3:
			if (presult->trytimes>5){
				presult->signal = 1;
				presult->pdata  = msg;
				presult->size = -1;
				//  sem_post(presult->psem);
				printf("i will post a sem,seq %d,sem 0x%x, ret %d, but i haven't down anything!\n",
						msg->head.seq, presult->psem,sem_post(presult->psem));
				break;
			}
			psession = gdsl_list_get_head(pse->fi->list_session);
			pthread_mutex_lock(&psession->send_lock);
			gdsl_list_insert_tail(psession->send_queue,presult->preq);
			if (!psession->wtev) {
				psession->wtev = mpcalloc(sizeof(struct event));
				event_set(psession->wtev,psession->fd,EV_WRITE|EV_PERSIST,tcp_session_cback,psession);
				event_add(psession->wtev,&psession->tv);
			}
			pthread_mutex_unlock(&psession->send_lock);				
			
			//presult->trytimes ++;
			break;
		case 4:
			host = ntohl(*(uint32_t*)((uint8_t*)msg+17));
			pthread_mutex_lock(&pse->fi->flock);
			psession = gdsl_hash_search(pse->fi->hash_session,host);
			if (!psession) {
				psession = mpcalloc(sizeof(struct _cdn_msg_session));
				if (!psession) {
					printf("i can't malloc psession\n");
					abort();
					return;// -ENOSYS;
				}
					//finfo->fh = (uint64_t)psession;
				psession->fi = pse->fi;
				psession->host = host;
				psession->port = *(uint16_t*)((uint8_t*)msg+21);
				in.s_addr= psession->host;
				printf("redirect host: %s,port:%d\n",inet_ntoa(in),ntohs(psession->port));
				/*psession->fd = */tcp_session_open(psession);
				tcp_session_init(psession);
				if (!psession->pdev){
					psession->pdev = mpcalloc(sizeof(struct event));
					event_set(psession->pdev,psession->fd,EV_READ|EV_PERSIST,tcp_session_cback,psession);
					event_add(psession->pdev,&psession->tv);
				}
				gdsl_hash_insert(pse->fi->hash_session,&host);
				gdsl_list_insert_tail(pse->fi->list_session,psession);
				//gdsl_list_insert_tail(voolefs.access,psession);
			}
			else {
				psession = gdsl_list_search(pse->fi->list_session,gdsl_session_host_cmp,&host);
			}
			pthread_mutex_unlock(&pse->fi->flock);
			req = (cdnmsg_t*)presult->preq;	
			pthread_mutex_lock(&psession->send_lock);
			uint32_t len = ntohl(req->head.len);
			presult->trytimes ++;
			if (!psession->eagain){
				int ret = 0,sum=0;
				do{
					ret = write(psession->fd,(void*)req,len);
					if( ret == -1 && (errno == EAGAIN || errno == EINTR)) {
						psession->eagain = 1;
						psession->besent = len-sum;
						psession->send_incomplete = req;
						printf("i met eagain\n");
						//continue;
						gdsl_list_insert_tail(psession->send_queue,req);
						if (!psession->wtev) {
							psession->wtev = mpcalloc(sizeof(struct event));
							event_set(psession->wtev,psession->fd,EV_WRITE|EV_PERSIST,tcp_session_cback,psession);
							event_add(psession->wtev,&psession->tv);                                                                        
						}
						pthread_mutex_unlock(&psession->send_lock);
					/*	pthread_mutex_lock(&voolefs.reglock),
							gdsl_queue_insert(voolefs.registration, INT2PTR(psession->fd)),
						pthread_mutex_unlock(&voolefs.reglock);
						if (write(voolefs.notify_send_fd, " ", 1) != 1)
							perror("Error writing to notify pipe");
					*/	break;
					}
					else if( ret == -1) {
						pthread_mutex_unlock(&psession->send_lock);
						tcp_session_close(psession);
						mpfree(req);
						//*ppres = NULL;
						abort();
						return -1;
						//do clean connection.
					}
					sum += ret;
					printf("i have send %d from fd:%d\n",sum,psession->fd);
					printf("my pdev is 0x%x, event_base is 0x%x, event is 0x%x\n",psession->pdev,
							(psession->pdev)?((struct event*)psession->pdev)->ev_base:NULL,
							(psession->pdev)?((struct event*)psession->pdev)->ev_events:NULL);
				}while(sum<len);
				
				pthread_mutex_unlock(&psession->send_lock);
			} else{
				gdsl_list_insert_tail(psession->send_queue,req);
				if (!psession->wtev) {
					psession->wtev = mpcalloc(sizeof(struct event));
					event_set(psession->wtev,psession->fd,EV_WRITE|EV_PERSIST,tcp_session_cback,psession);
					event_add(psession->wtev,&psession->tv);                                                                            
				}
				pthread_mutex_unlock(&psession->send_lock);
			/*	pthread_mutex_lock(&voolefs.reglock),
					gdsl_queue_insert(voolefs.registration, INT2PTR(psession->fd)),
				pthread_mutex_unlock(&voolefs.reglock);
				if (write(voolefs.notify_send_fd, " ", 1) != 1)
					perror("Error writing to notify pipe");
			*/
			}
			break;
		default:
			break;
	}
	return 0;
}