Beispiel #1
0
void    Spread_reconnect(int ret){

  if(ret != -8 && ret != -11 && ret != -2)
    wack_alarm(EXIT, "Spread_reconnnect: Unexpected Error (%d)", ret);
  
  wack_alarm(PRINT,"connecting to %s", Spread_name);
    
  Clean_up();

  if(Mbox >= 0) {
    SP_disconnect(Mbox);
    E_detach_fd( Mbox, READ_FD );
    Mbox = -1;
  }
  /* connecting to the relevant Spread daemon, asking for group info */
  if(spread_lock) {
    handle_reconnect(0, NULL);
    return;
  }
  ret = SP_connect( Spread_name, User, 0, 1, &Mbox, Private_group ) ;
  if(ret == ACCEPT_SESSION) {
    ret = SP_join( Mbox, Spread_group );  
    if( ret < 0 ) {
      SP_error( ret );
      SP_disconnect( Mbox );
      Mbox = -1;
      wack_alarm(PRINT, "Spread join on reconnect failed [%d].", ret);
      handle_reconnect(0, NULL);
      return;
    }
  } else {
    wack_alarm(PRINT, "Spread connect failed [%d].", ret);
    handle_reconnect(0, NULL);
    return;
  }

  /* State initializations */
  State = BOOT;
  Old_maturity = 0;
  Maturity = 0;
  E_queue( Turn_mature, 0, 0, Maturity_timeout );
  My.num_allocated = 0;
  
  strcpy(My.private_group_name, Private_group);

  E_attach_fd( Mbox, READ_FD, Handle_network, 0, NULL, HIGH_PRIORITY );
  E_set_active_threshold( HIGH_PRIORITY );
}
Beispiel #2
0
void join_server(char *server_id)
{
	int ret;
	struct chat_packet *c;
	struct chat_packet *r;
	char             mess[MAX_MESSLEN];
        char             sender[MAX_GROUP_NAME];
        char             target_groups[MAX_MEMBERS][MAX_GROUP_NAME];
        membership_info  memb_info;
        vs_set_info      vssets[MAX_VSSETS];
        unsigned int     my_vsset_index;
        int              num_vs_sets;
        char             members[MAX_MEMBERS][MAX_GROUP_NAME];
        int              num_groups;
        int              service_type=0;
        int16            mess_type;
        int              endian_mismatch;
	char		group[80];
	c = (struct chat_packet *)calloc(1,sizeof(struct chat_packet));
	r = (struct chat_packet *)calloc(1,sizeof(struct chat_packet));
	c->type = 2;//Request to join server
        strncpy(c->text, Private_group, strlen(Private_group));
        c->server_id = atoi(server_id);
        //printf("Sending private grup %s to server", Private_group);
	strncpy(c->client_group, Private_group, MAX_GROUP_NAME);
        /* Send Message */
	if (connected > 0)
	{
	  /*Leave current group*/
	  sprintf(group, "c%d", connected);
	  SP_leave(Mbox, group);
	}
        /* ret = SP_multicast(Mbox, AGREED_MESS, server_id, 2, sizeof(struct chat_packet), (char *)c); */
	/* That was the old way.  New way is join the server group */
	sprintf(group, "c%d", atoi(server_id));
	ret = SP_join(Mbox, group);
	connected = atoi(server_id); /*Set connected.  Note we still need response from server, but we need to pass this to the recv connect */
        if( ret < 0 )
        {
                SP_error( ret );
        }
}
Beispiel #3
0
void setup(struct initializers *i) {
  /* Sets up all ports */
  /* and waits for the start_mcast message to start the actual process */
   int              mcast_addr;
   int              start = 0;
   int              bytes;
   int              num, c, num_groups, r, ret;
   int              service_type = 0;
   int              responded=0;
   int16            mess_type;
   int              endian_mismatch=0;
   int				response[10];   
   struct		    timeval timeout;
   unsigned char      ttl_val;
   char             target_groups[MAX_MEMBERS][MAX_GROUP_NAME];
   char             logfilename[10];
   char             sender[MAX_GROUP_NAME];
   char             groups[10][MAX_GROUP_NAME];
   char mess_buf[MAX_MESS_LEN];
   sp_time test_timeout;
   struct packet_structure *p=malloc(sizeof(struct packet_structure));
   snprintf(logfilename, 10, "%d.out", machine_index);
    
   logfile = fopen(logfilename, "w");
   ret = SP_connect_timeout( Spread_name, User, 0, 1, &Mbox, Private_group, test_timeout );
   if( ret != ACCEPT_SESSION ) 
   {
	 SP_error( ret );
	 Bye();
   }
   printf("User: connected to %s with private group %s\n", Spread_name, Private_group );
   E_init();
   ret = SP_join(Mbox, group); printf("Join group %s:%d\n", group, ret);
   if (machine_index == 1) {
    for (c=1; c <= total_machines; c++) {
       response[c] = 0;
       completed[c] = 0;
    }
     /* Collect up the users, and send start message when everyone is in the group */
    response[1]=1;
    while (responded < 1) {
	  ret = SP_receive( Mbox, &service_type, sender, 100, &num_groups, target_groups, 
                &mess_type, &endian_mismatch, sizeof(mess_buf), mess_buf );
      p = (struct packet_structure *)mess_buf;
      if (p->type == 4) {
          printf("ret = %d Got machine id %d\n", ret, p->machine_index);
          /* Add this machine to the array and check to see if we are done */
          response[p->machine_index] = 1; 
          printf("Got response from %d\n", p->machine_index);
          r = 1;
          for (c=1; c <= total_machines; c++) {
                if (response[c] == 0) r =0; 
                }
          if (r==1) responded = 1;
          }
      else if (p->type == 3 && machine_index == 1)
      {
        completed[p->machine_index] = 1;
      }
      }
      /* Send start sending message to everyone */
      if (r=1) /*All ready */ {
        p->type=  2; printf("Ready to go..\n");
        ret= SP_multicast( Mbox, AGREED_MESS, group, 1, sizeof(struct packet_structure), (char *)p );
      }
    }
    else { /*We are not machine index 1*/
    /*Send ready to begin message */
    p->type = 4;
    p->machine_index = machine_index;    
    
    ret= SP_multicast( Mbox, AGREED_MESS, group, 1, sizeof(struct packet_structure), (char *)p );
    printf("Join=%d, group %s\n", ret, group);
    if( ret < 0 ) 
    {
            SP_error( ret );
            Bye();
    } 
    else
    {
      printf("Sent: %d\n", ret);
     }
  }

};
Beispiel #4
0
static	void	User_command()
{
	char	command[130];
	char	mess[MAX_MESSLEN];
	char	group[80];
	char	groups[10][MAX_GROUP_NAME];
	int	num_groups;
	unsigned int	mess_len;
	int	ret;
	int	i;

	for( i=0; i < sizeof(command); i++ ) command[i] = 0;
	if( fgets( command, 130, stdin ) == NULL ) 
            Bye();

	switch( command[0] )
	{
		case 'j':
			ret = sscanf( &command[2], "%s", group );
			if( ret < 1 ) 
			{
				printf(" invalid group \n");
				break;
			}
			ret = SP_join( Mbox, group );
			if( ret < 0 ) SP_error( ret );

			break;

		case 'l':
			ret = sscanf( &command[2], "%s", group );
			if( ret < 1 ) 
			{
				printf(" invalid group \n");
				break;
			}
			ret = SP_leave( Mbox, group );
			if( ret < 0 ) SP_error( ret );

			break;

		case 's':
			num_groups = sscanf(&command[2], "%s%s%s%s%s%s%s%s%s%s", 
						groups[0], groups[1], groups[2], groups[3], groups[4],
						groups[5], groups[6], groups[7], groups[8], groups[9] );
			if( num_groups < 1 ) 
			{
				printf(" invalid group \n");
				break;
			}
			printf("enter message: ");
			if (fgets(mess, 200, stdin) == NULL)
				Bye();
			mess_len = strlen( mess );
#ifdef _REENTRANT
#ifdef __bsdi__		/* bsdi bug - doing a close when another thread blocks on the socket causes a seg fault */
	ret = send( Mbox, mess, 0, 0 );
	if( ret < 0 )
	{
		SP_error( CONNECTION_CLOSED );
		Bye();
	}
#endif /* __bsdi__ */
#endif /* _REENTRANT */
			ret= SP_multigroup_multicast( Mbox, SAFE_MESS, num_groups, (const char (*)[MAX_GROUP_NAME]) groups, 1, mess_len, mess );
			if( ret < 0 ) 
			{
				SP_error( ret );
				Bye();
			}
			Num_sent++;

			break;

		case 'b':
			ret=sscanf( &command[2], "%s", group );
			if( ret != 1 ) strcpy( group, "dummy_group_name" );
			printf("enter size of each message: ");
			if (fgets(mess, 200, stdin) == NULL)
				Bye();
			ret=sscanf(mess, "%u", &mess_len );
			if( ret !=1 ) mess_len = Previous_len;
                        if( mess_len > MAX_MESSLEN ) mess_len = MAX_MESSLEN;
			Previous_len = mess_len;
			printf("sending 10 messages of %u bytes\n", mess_len );
			for( i=0; i<10; i++ )
			{
				Num_sent++;
				sprintf( mess, "mess num %d ", Num_sent );
#ifdef _REENTRANT
#ifdef __bsdi__		/* bsdi bug - doing a close when another thread blocks on the socket causes a seg fault */
	ret = send( Mbox, mess, 0,0 );
	if( ret < 0 )
	{
		SP_error( CONNECTION_CLOSED );
		Bye();
	}
#endif /* __bsdi__ */
#endif /* _REENTRANT */
				ret= SP_multicast( Mbox, FIFO_MESS, group, 2, mess_len, mess );

				if( ret < 0 ) 
				{
					SP_error( ret );
					Bye();
				}
				printf("sent message %d (total %d)\n", i+1, Num_sent );
			}
			break;
#ifndef _REENTRANT
		case 'r':

			Read_message();
			break;

		case 'p':

			ret = SP_poll( Mbox );
			printf("Polling sais: %d\n", ret );
			break;

		case 'e':

			E_attach_fd( Mbox, READ_FD, Read_message, 0, NULL, HIGH_PRIORITY );

			break;

		case 'd':

			E_detach_fd( Mbox, READ_FD );

			break;
#endif	/* _REENTRANT */
		case 'q':
			Bye();
			break;

		default:
			printf("\nUnknown commnad\n");
			Print_menu();

			break;
	}
	printf("\nUser> ");
	fflush(stdout);

}
Beispiel #5
0
int main( int argc, char *argv[] )
{
	int		ret;
	int		service_type, num_groups;
	char		sender[MAX_GROUP_NAME];
	int16		mess_type;
	int		dummy_endian_mismatch;
        int             joined_members;
        int             sender_index;
	int		i,j;

	Usage( argc, argv );

        if (Num_members == 0) {
            /* connecting to the relevant Spread daemon, no need for group info */
            printf("flooder: connecting to %s\n", Spread_name );
            ret = SP_connect( Spread_name, User, 0, 0, &Mbox, Private_group ) ;
        } else {
            /* connecting to the relevant Spread daemon, DO need group info */
            printf("flooder: connecting to %s with group membership\n", Spread_name );
            ret = SP_connect( Spread_name, User, 0, 1, &Mbox, Private_group ) ;
        }
        if(ret < 0) 
	{
		SP_error( ret );
                exit(1) ;
        }
	/* 
	 * Joining the process group.
	 *
	 * Note that this is not necessary in order to multicast the
	 * messages, but only to demonstrate end-to-end behaviour.
	 */
	if( Read_only )
	{
		printf("flooder: Only receiving messages\n");
		SP_join( Mbox, "flooder" );
	}else if( Write_only ) {
		printf("flooder: starting  multicast of %d messages, %d bytes each (self discarding).\n", Num_messages, Num_bytes);
	}else{
		SP_join( Mbox, "flooder" );
		printf("flooder: starting  multicast of %d messages, %d bytes each.\n", Num_messages, Num_bytes);
	}

        /* Wait for all members to join */
        joined_members = 0;
        while( joined_members < Num_members)
        {
            service_type = 0;
            ret = SP_receive( Mbox, &service_type, sender, FLOODER_MAX_GROUPS, &num_groups, ret_groups, 
                              &mess_type, &dummy_endian_mismatch, sizeof(recv_mess), recv_mess );
            if( ret < 0 ) 
            {
                if ( (ret == GROUPS_TOO_SHORT) || (ret == BUFFER_TOO_SHORT) ) {
                    printf("\n========Buffers or Groups too Short=======\n");
                    printf("Should NOT happen in wait for members! Program quitting\n");
                    exit(1);
                }       
            }

            if( Is_regular_mess( service_type ) )
            {
		mess[ret] = 0;
		if     ( Is_unreliable_mess( service_type ) ) printf("received UNRELIABLE ");
		else if( Is_reliable_mess(   service_type ) ) printf("received RELIABLE ");
		else if( Is_fifo_mess(       service_type ) ) printf("received FIFO ");
		else if( Is_causal_mess(     service_type ) ) printf("received CAUSAL ");
		else if( Is_agreed_mess(     service_type ) ) printf("received AGREED ");
		else if( Is_safe_mess(       service_type ) ) printf("received SAFE ");
		printf("message during wait for members, from %s, of type %d, (endian %d) to %d groups \n(%d bytes): %s\n",
			sender, mess_type, dummy_endian_mismatch, num_groups, ret, recv_mess );
            }else if( Is_membership_mess( service_type ) )
            {
		if     ( Is_reg_memb_mess( service_type ) )
		{
			printf("Received REGULAR membership for group %s with %d members, where I am member %d:\n",
				sender, num_groups, mess_type );
			for( i=0; i < num_groups; i++ )
				printf("\t%s\n", &ret_groups[i][0] );
                        /* update count of joined members */
                        joined_members = num_groups;

		}else if( Is_transition_mess(   service_type ) ) {
			printf("received TRANSITIONAL membership for group %s\n", sender );
		}else if( Is_caused_leave_mess( service_type ) ){
			printf("received membership message that left group %s\n", sender );
		}else printf("received incorrecty membership message of type 0x%x\n", service_type );
            } else if ( Is_reject_mess( service_type ) )
            {
		printf("REJECTED message from %s, of servicetype 0x%x messtype %d, (endian %d) to %d groups \n(%d bytes): %s\n",
			sender, service_type, mess_type, dummy_endian_mismatch, num_groups, ret, recv_mess );
            }else printf("received message of unknown message type 0x%x with ret %d\n", service_type, ret);
           
        } /* joined_members < Num_members */

        /* Update My_Counter_index field based on location of my name in last membership message */
        if (Num_members)
        {
            My_Counter_index = mess_type;
            memcpy(&mess[0], &My_Counter_index, sizeof(int));
        }

	for( i=1; i <= Num_messages; i++ )
	{
		/* multicast a message unless Read_only */
		if( !Read_only )
		{
                    if (Num_members) 
                    {
                        ret = SP_multicast( Mbox, FIFO_MESS, "flooder", 0, Num_bytes, mess );
                        Send_Counter++;
                    } else {
                        ret = SP_multicast( Mbox, RELIABLE_MESS, "flooder", 0, Num_bytes, mess );
                    }
		    if( ret != Num_bytes ) 
		    {
			if( ret < 0 )
			{
				SP_error( ret );
				exit(1);
			}
			printf("sent a different message %d -> %d\n", Num_bytes, ret );
		    }
		}

		/* receive a message (Read_only) or one of my messages */
		if( Read_only || ( i > 200 && !Write_only ) )
		{
                    int notdone;

		    do{
                        service_type = 0;

			ret = SP_receive( Mbox, &service_type, sender, FLOODER_MAX_GROUPS, &num_groups, ret_groups, 
					  &mess_type, &dummy_endian_mismatch, sizeof(recv_mess), recv_mess );
                        if( ret < 0 ) 
                        {
                                if ( (ret == GROUPS_TOO_SHORT) || (ret == BUFFER_TOO_SHORT) ) {
                                        service_type = DROP_RECV;
                                        printf("\n========Buffers or Groups too Short=======\n");
                                        ret = SP_receive( Mbox, &service_type, sender, FLOODER_MAX_GROUPS, &num_groups, ret_groups, 
                                                          &mess_type, &dummy_endian_mismatch, sizeof(recv_mess), recv_mess );
                                }
                        }
			if( ret < 0 )
			{
				SP_error( ret );
				exit(1);
			}
                        if (Num_members) {
                            /* update counters of received messages */
                            memcpy(&sender_index, &recv_mess[0], sizeof(int));
                            Recv_Counters[sender_index]++;
                            /* 
                               printf("DEBUG: updated counter %d to value %d\n", sender_index, Recv_Counters[sender_index]);
                            */
                            if (Recv_Counters[sender_index] == (Lowest_Recv_Counter + 1)) {
                                /* Update Lowest_Recv_Counter */
                                Lowest_Recv_Counter = INT_MAX;
                                for (j=0; j < Num_members; j++) {
                                    if (Recv_Counters[j] == 0) continue;
                                    if (Recv_Counters[j] < Lowest_Recv_Counter)
                                        Lowest_Recv_Counter = Recv_Counters[j];
                                }
                                if (Lowest_Recv_Counter == INT_MAX) 
                                    Lowest_Recv_Counter = 0;
                            }
                            /* Read loop is done if we send messages and we have received all 
                             * other senders messages upto 100 less then our current send count 
                             */
                            notdone = ( Lowest_Recv_Counter < (Send_Counter - 200) && !Read_only );
                        } else {
                            notdone = (strcmp( sender, Private_group ) != 0 && !Read_only);
                        }

		    } while( notdone );
		}

		/* report some progress... */
		if( i%1000 == 0 ) printf("flooder: completed %6d messages of %d bytes\n",i, ret);
	}
	printf("flooder: completed multicast of %d messages, %d bytes each.\n", Num_messages, Num_bytes);

	return 0;
}
Beispiel #6
0
static	void	User_command()
{
	char	command[130];
        char    client_group[13];
	char	mess[SPREAD_MESS_LEN];
        char    group[MAX_ROOM_LEN - strlen("CR#_")];
	char	groups[10][MAX_GROUP_NAME];
	int	num_groups;
	unsigned int	mess_len;
	int	ret;
        int     srv;
	int	i;
	for( i=0; i < sizeof(command); i++ ) command[i] = 0;
	if( fgets( command, 130, stdin ) == NULL ) 
            Bye();

	switch( command[0] )
	{
                case 'u':
                        ret = sscanf( &command[2], "%s", u_id);
                        if (ret < 1 )
                            printf("invalid username\n");
                        break;
                case 'c':
                       ret = sscanf( &command[2], "%d", &srv);
                        if ( ret < 1 || isValidServer(srv) != 1) { //isValid sets server_num
                            printf("invalid server index\n");
                            break;
                        }
                        ret = sprintf( client_group, "client_group%d",server_num);
                        if (ret < 1) {
                            printf("Error connecting to server-client group\n");
                            break;
                        }
                        ret = SP_join(Mbox, client_group);
                        if (ret < 0) SP_error( ret );
                        sprintf( server_priv_group, "server%d", server_num);
                        break;  
		case 'j':
                        //check if already in room
                        if (strlen(chatroom) > 0) {
                            clear_room(chatroom);
                        }
                        else { 
                            ret = sscanf( &command[2], "%s", chatroom );
                            if( ret < 1 ) {
                                printf(" invalid chatroom name \n");
                                break;}

                            add_room(chatroom);
                        }
                        if (strlen(u_id) > 0 && isValidServer(server_num)) { //need valid username + server for chatroom
                            ret = sprintf ( group, "%s%d_%s","CR",server_num,chatroom);
                            if (ret < 1) {
                                printf("Error creating chatroom name <%s> from group <%s>\n",chatroom,group);
                                break;
                            }
                            ret = SP_join( Mbox, group );  //join CR#_<group> spread group
                            if( ret < 0 ) SP_error( ret );
                        }
                        else {
                            printf("Invalid username or server number, cannot join chatroom\n"); break;}
                        join_chatroom* send = (join_chatroom*)mess;
                        send->type = TYPE_JOIN_CHATROOM;
                        send->source = SOURCE_CLIENT;
                        strncpy(send->u_id,u_id,MAX_NAME_LEN);
                        strncpy(send->chatroom,chatroom,MAX_ROOM_LEN); //send <group> as chatroom spread name
			ret = SP_multicast( Mbox, AGREED_MESS, server_priv_group, 1, sizeof(join_chatroom), mess );
			if( ret < 0 ) 
			{
				SP_error( ret );
				Bye();
			}
			break;
/*
                case 'x':{
                        char ms[6];
                        add_room(chatroom);
                        set_max_msgs(25);
                        int maxi = 40;
                        for (int i = 1; i <= maxi;i++) {
                            struct lamport* ts = malloc(sizeof(lamport));
                            ts->server_id = 1;
                            ts->index = i;
                            sprintf(ms,"%s%d","asdfd",i);
                            add_message(ts,u_id,chatroom,ms);
                            if (i % 3 == 0 || i % 4 == 1 || i % 6 == 2) {
                                i++; maxi++;
                                struct lamport* ls = malloc(sizeof(lamport));
                                ls->server_id = 1;
                                ls->index = i; 
                                add_like(LIKE,ls,u_id,chatroom,ts);
                                if (i % 6 == 2){
                                    i++;maxi++;
                                    char newu[MAX_NAME_LEN];
                                    sprintf(newu,"%sx",u_id); 
                                    struct lamport* qs = malloc(sizeof(lamport));
                                    qs->server_id = 1;
                                    qs->index = i;
                                    add_like(LIKE,qs,newu,chatroom,ts);
                                }
                            }
                            if (i % 4 == 1) {
                                i++; maxi++;
                                struct lamport* ls = malloc(sizeof(lamport));
                                ls->server_id = 1;
                                ls->index = i; 
                                add_like(UNLIKE,ls,u_id,chatroom,ts);
                            }
                        }
                        

                        printf("== %s ==\n",chatroom);
                        print_room(chatroom); 
    
                        break;}
*/
		case 'a': {
                        char text[MAX_MESS_LEN];
			printf("enter message: ");
			if (fgets(text, MAX_MESS_LEN, stdin) == NULL)
				Bye();
                        if (strlen(chatroom) < 1){
                            printf("Please join a chatroom\n");
                            break;
                        }
                        message* dat = (message*)mess;
                        dat->type = TYPE_SEND_MSG;
                        dat->source = SOURCE_CLIENT;
                        strncpy(dat->u_id,u_id,MAX_NAME_LEN);
                        strncpy(dat->chatroom,chatroom,MAX_ROOM_LEN);
                        strncpy(dat->mess,text,MAX_MESS_LEN);
			ret= SP_multicast( Mbox, AGREED_MESS, server_priv_group, 1, sizeof(message), mess );
			if( ret < 0 ) 
		        {
				SP_error( ret );
				Bye();
                        }
			break;
                }
		case 'l': 
                {
                        if (strlen(chatroom) < 1){
                            printf("Please join a chatroom\n");
                            break;}

                        int linenum;        
                        ret = sscanf( &command[2], "%d", &linenum );
                        printf("Liking line %d.....\n",linenum);
			if( ret < 1 ) {
				printf(" invalid linenum \n");
				break;}
			lamport* stamp = get_msg_stamp(linenum);
                        printf("%d. index %d?\n",linenum,stamp->index);
                        like* dat = (like*)mess;
                        dat->type = TYPE_LIKE_MSG;
                        dat->source = SOURCE_CLIENT;
                        dat->msg_timestamp.server_id = stamp->server_id;
                        dat->msg_timestamp.index = stamp->index;
                        strncpy(dat->u_id,u_id,MAX_NAME_LEN);
                        strncpy(dat->chatroom,chatroom,MAX_ROOM_LEN);
                        dat->like_state = LIKE;                      

                        ret = SP_multicast( Mbox, AGREED_MESS, server_priv_group, 1, sizeof(like), mess);
                        if (ret < 0) {
                            SP_error(ret); Bye; }

			break;
                }
                case 'r':
                {
                        if (strlen(chatroom) < 1){
                            printf("Please join a chatroom\n");
                            break;}

                        int linenum;        
                        ret = sscanf( &command[2], "%d", linenum );
			if( ret < 1 ) {
				printf(" invalid linenum \n");
				break;}
			lamport* stamp = get_msg_stamp(linenum);
                        printf("%d. index %d?\n",linenum,stamp->index);
                        like* dat = (like*)mess;
                        dat->type = TYPE_LIKE_MSG;
                        dat->source = SOURCE_CLIENT;
                        dat->msg_timestamp.server_id = stamp->server_id;
                        dat->msg_timestamp.index = stamp->index;
                        strncpy(dat->u_id,u_id,MAX_NAME_LEN);
                        strncpy(dat->chatroom,chatroom,MAX_ROOM_LEN);
                        dat->like_state = UNLIKE;                      

                        ret = SP_multicast( Mbox, AGREED_MESS, server_priv_group, 1, sizeof(like), mess);
                        if (ret < 0) {
                            SP_error(ret); Bye; }

			break;
                }
                case 'h':
                        printf("history not implemented\n");
                        break;
                case 'v':
                        printf("network view not implemented\n");
                        break;
		case 'q':
			Bye();
			break;

		default:
			printf("\nUnknown commnad\n");
			Print_menu();

			break;
	}

    printf(" \n > ");
    fflush(stdout);
}
Beispiel #7
0
void recv_server_msg(struct chat_packet *c, int16 mess_type) {
   int ret;
   struct node *temp, *temp2;
   if (mess_type == 0) { // If the mess_type is 0, that means we already have the chat_packet, we just want to update the likes.
	temp = chatroom_start->next;
	while (temp != NULL) {
		if (temp->data->sequence == c->sequence) {
		  temp->data->num_likes = c->num_likes;
		  break;
		}
		temp = temp->next;
	}
	refresh_display();
   }
   else if (mess_type == 13) {
	temp = chatroom_start;
	int count= 0;
	/* We need to put the merged chats in the correct order */
	while (temp->next != NULL) {
	  temp->next->sequence = count;/* for resequencing*/
	  if (c->sequence == temp->next->data->sequence)
	  {
	     /*already received this LTS, so don't add it. */
	     break;
          }
	  else if (c->sequence < temp->next->data->sequence)
	  {
	     temp2 = temp->next;
             temp->next = (struct node *)calloc(1,sizeof(struct node));
             temp->next->data = (struct chat_packet *)calloc(1, sizeof(struct chat_packet));
             memcpy(temp->next->data, c, sizeof(struct chat_packet));
	     temp->next->sequence = count;
	     temp->next->previous = temp; /*Double link list added for traversal on 25 chat lines */ 
	     temp2->previous = temp->next;
	     temp->next->next = temp2;
	     break;
	  }
    	  temp = temp->next;
	}
        if (temp->next == NULL) /*We are at the end, so add the message */
	{
	   temp2 = temp->next;
           temp->next = (struct node *)calloc(1,sizeof(struct node));
           temp->next->data = (struct chat_packet *)calloc(1,sizeof(struct chat_packet));
           memcpy(temp->next->data, c, sizeof(struct chat_packet));
	   temp->next->sequence = count;
	   temp->next->previous = temp; /*Double link list added for traversal on 25 chat lines */
           temp->next->next = temp2;
	   chatroom_latest = temp->next; /*set latest to this new packet */
	    
	}
	/*Renumber */
	temp = chatroom_start;
        while (temp->next !=NULL)
	{
 	  count++;
	  temp->next->sequence = count;
	  temp = temp->next;
	}
	line_number = count;
        refresh_display();
	//print_after(c->sequence); /*Print out where we added the packet (for dispaly consistency)*/
   }
   else if (c->type == 0 || c->type == 3) /*Message packet */
   {
	if (c->type == 0) line_number++; /*Only update line number on text message */
	chatroom_latest->next = (struct node *)calloc(1,sizeof(struct node));	
	chatroom_latest->next->previous = chatroom_latest; 
	chatroom_latest = chatroom_latest->next; /*Advance the pointer */
        chatroom_latest->data = (struct chat_packet *)calloc(1,sizeof(struct chat_packet));
        chatroom_latest->sequence = line_number;
	memcpy(chatroom_latest->data, c, sizeof(struct chat_packet));
        if (c->type == 0) /* Live message, display it */
	{
          refresh_display();
	}
   }
   else if (c->type == 2)
   {
	printf("Successful connection to server %d\n>", c->server_id);
	connected = c->server_id;
   }
   else if (c->type == 5) /* Received response to join group */
   {
     ret = SP_join( Mbox, c->group );
     if( ret < 0 ) SP_error( ret );
     bzero(chatroom, MAX_GROUP_NAME);
     strncpy(chatroom, c->group, strlen(c->group)-1); /*Remove server id from chatroom group name */
     chatroom_start = (struct node *)calloc(1, sizeof(struct node));
     chatroom_latest = chatroom_start;
     printf("Successfully joined group %s\n>", chatroom); /* don't display server index at end of group */
     line_number = 0; /*Refresh line number */
   }
   else if (c->type == 6) /*Refresh screen all after lamport timestamp */  /*No longer necessary*/
   {
     struct node *i;
     i = chatroom_start->next; /* Setup iterator */
     line_number = 0;
     while ((i !=NULL) && ((i->data->sequence != c->sequence) || (i->data->server_id != c->server_id)))
     {  line_number++;
	i = i->next;
     }
     while (i != NULL)
    {
	line_number++;
	printf("%d:%s> %s",line_number, i->data->name, i->data->text);
    }
   }
   else if (c->type == 8) /*Display servers online */
   {
	printf("Servers that are online are: %s\n>", c->text);
   }
   else if (c->type == 9) /*Updated name list for chatroom */
   {
	bzero(attendees, 80);
	strncpy(attendees, c->text, strlen(c->text));
	//printf("Members in chatroom are now: %s\n>", c->text); Moved to display.
	refresh_display();
   }

}