예제 #1
0
파일: mcast.c 프로젝트: sshamilton/mspread
/* 5 = Terminate */
int main(int argc, char **argv)
{
	/* Variables */
  unsigned char      ttl_val;
  int                c, ret, num_groups;
  int complete = 0;
  int				response[10];
  char             sender[MAX_GROUP_NAME];
  int              service_type = 0;
  int               rts = 0;
  char mess_buf[MAX_MESS_LEN];
  struct timeval    timeout, start_time, end_time;
  struct initializers *i=malloc(sizeof(struct initializers));
  struct packet_structure *p=malloc(sizeof(struct packet_structure));
  
  sp_time test_timeout;
  sp_time delta_time;
  int16            mess_type;
  int              endian_mismatch=0;
  char             target_groups[MAX_MEMBERS][MAX_GROUP_NAME];
  i->debug = 0; /*Turn on for testing */
  strcpy(group, "shamil22");
  parseargs(argc, argv, i);
  setup(i); /*Setup ports and wait for start process */
     
  printf("Waiting to start.\n");
  while(!rts) { 
    ret = SP_receive( Mbox, &service_type, sender, 100, &num_groups, target_groups, 
                &mess_type, &endian_mismatch, sizeof(mess_buf), mess_buf );
    if (ret > 0) {
      i->packet = (struct packet_structure *)mess_buf; 
      if (i->packet->type == 2) {
         rts =1;
      }
    }
  }
  printf("Begin!\n");
  gettimeofday(&start_time, NULL);          
  starttime1=start_time.tv_sec+(start_time.tv_usec/1000000.0);
  delta_time.sec = 0; delta_time.usec =0;
  E_queue( send_data, 0, NULL, delta_time );
  E_attach_fd( Mbox, READ_FD, receive_packet, 0, NULL, HIGH_PRIORITY );
  printf("Handling events.\n");
  //send_data(); /*Send first chunk of message*/
  E_handle_events();
   
  return 0;  
}
예제 #2
0
void	Handle_network(int __unused1, int __unused2, void *__unused3)
{
  int	ret;
  
  ret = SP_receive( Mbox, &Service_type, Sender, MAX_PSEUDO, &Num_groups, 
		    Target_groups, &Mess_type, &Endian_mismatch, 
		    MAX_MESS_LEN, Mess );
  if( ret < 0 )
    {
      SP_error( ret );
      Spread_reconnect(ret);
    }
  
  if( Is_membership_mess( Service_type ) )
    {
      if( Is_reg_memb_mess( Service_type ) )
	{
	  Handle_membership();
	  Send_local_arp_cache(0, NULL);
	}else{
	  /* Ignore Transitional membership */
	}
    }else if( Is_regular_mess( Service_type ) ){
      
      /* Handle regular messages */
      if	( Mess_type == STATE_MESS    ) Handle_state();
      else if	( Mess_type == MATURING_MESS ) Handle_mature();
      else if	( Mess_type == BALANCE_MESS  ) Handle_balance();
      else if	( Mess_type == OPERATOR_MESS ) Handle_operator();
      else if	( Mess_type == ARPCACHE_MESS ) Handle_arp_cache((arp_entry *)Mess,ret);
      
      /* Ignore messages of other types */
      
    }else{
      wack_alarm(EXIT,"Error: received message of unknown message type %d with ret %d",
	     Service_type, ret );
     }
  
}
예제 #3
0
파일: mcast.c 프로젝트: sshamilton/mspread
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);
     }
  }

};
예제 #4
0
파일: mcast.c 프로젝트: sshamilton/mspread
void receive_packet() {
  /* receiving data */
  int c, ret, num_groups;
  int r=1;
  int16            mess_type;
  int              endian_mismatch=0;
  char mess_buf[MAX_MESS_LEN];
  char             target_groups[MAX_MEMBERS][MAX_GROUP_NAME];
  char             sender[MAX_GROUP_NAME];
  int              service_type = 0;
  struct timeval    end_time;
  struct packet_structure *packet;
  SP_receive( Mbox, &service_type, sender, 100, &num_groups, target_groups, 
                &mess_type, &endian_mismatch, sizeof(mess_buf), mess_buf );
  packet = (struct packet_structure *)mess_buf;
  if (packet->type == 1) /*Data packet, write to log */
  {
    write_log(packet);
  } 
  else if (packet->type == 3 && machine_index == 1)
  {  printf("Checking for termination \n");
    completed[packet->machine_index] = 1;
    for (c=1; c <= total_machines; c++) {
      if (completed[c] == 0) r =0; 
      printf("%d = %d\n", c, completed[c]);
    }
    if (r==1) {
    /*All machines complete.  Send termination */
      packet->type = 5; printf("Complete sending termination\n");
      ret= SP_multicast( Mbox, AGREED_MESS, group, 1, sizeof(struct packet_structure), (char *)packet );
    }
  }
  else if (packet->type == 5) /*Terminate */
  {
      printf("Complete\n");
      gettimeofday(&end_time, NULL);          
      starttime2=end_time.tv_sec+(end_time.tv_usec/1000000.0);
      printf("%.6lf seconds elapsed\n", starttime2-starttime1);
      fclose(logfile);
      Bye();
      exit (0);
  }
  else
  {
    printf("Got packet type %d, mid=%d\n", packet->type, packet->machine_index);
    printf("received message of unknown message type 0x%x with ret %d\n", service_type, ret);

    
  }
  sendcount++; /* Only send when we've received at least what we've sent. */
  if (packets_to_send > 0 && sendcount == FCC) {
    sendcount = 0;
    sp_time delta_time;
    delta_time.sec = 0; delta_time.usec =1250; /*Setting this below 1000 causes problems */
    E_queue( send_data, 0, NULL, delta_time ); /*Queue up the sender */
  }
  
  if (packets_to_send == 0 && completed[machine_index] == 0){ /*Send we are complete if we haven't already */
    printf("Sending completed\n");
    completed[machine_index] = 1;
    packet->type = 3;
    packet->machine_index = machine_index;
    ret= SP_multicast( Mbox, AGREED_MESS, group, 1, sizeof(struct packet_structure), (char *)packet );
  }
}
예제 #5
0
static	void	Read_message()
{

static	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;
	int16		 mess_type;
	int		 endian_mismatch;
	int		 i,j;
	int		 ret;

        service_type = 0;

	ret = SP_receive( Mbox, &service_type, sender, 100, &num_groups, target_groups, 
		&mess_type, &endian_mismatch, sizeof(mess), mess );
	printf("\n============================\n");
	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, MAX_MEMBERS, &num_groups, target_groups, 
                                          &mess_type, &endian_mismatch, sizeof(mess), mess );
                }
        }
        if (ret < 0 )
        {
		if( ! To_exit )
		{
			SP_error( ret );
			printf("\n============================\n");
			printf("\nBye.\n");
		}
		exit( 0 );
	}
	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 from %s, of type %d, (endian %d) to %d groups \n(%d bytes): %s\n",
			sender, mess_type, endian_mismatch, num_groups, ret, mess );
	}else if( Is_membership_mess( service_type ) )
        {
                ret = SP_get_memb_info( mess, service_type, &memb_info );
                if (ret < 0) {
                        printf("BUG: membership message does not have valid body\n");
                        SP_error( ret );
                        exit( 1 );
                }
		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", &target_groups[i][0] );
			printf("grp id is %d %d %d\n",memb_info.gid.id[0], memb_info.gid.id[1], memb_info.gid.id[2] );

			if( Is_caused_join_mess( service_type ) )
			{
				printf("Due to the JOIN of %s\n", memb_info.changed_member );
			}else if( Is_caused_leave_mess( service_type ) ){
				printf("Due to the LEAVE of %s\n", memb_info.changed_member );
			}else if( Is_caused_disconnect_mess( service_type ) ){
				printf("Due to the DISCONNECT of %s\n", memb_info.changed_member );
			}else if( Is_caused_network_mess( service_type ) ){
				printf("Due to NETWORK change with %u VS sets\n", memb_info.num_vs_sets);
                                num_vs_sets = SP_get_vs_sets_info( mess, &vssets[0], MAX_VSSETS, &my_vsset_index );
                                if (num_vs_sets < 0) {
                                        printf("BUG: membership message has more then %d vs sets. Recompile with larger MAX_VSSETS\n", MAX_VSSETS);
                                        SP_error( num_vs_sets );
                                        exit( 1 );
                                }
                                for( i = 0; i < num_vs_sets; i++ )
                                {
                                        printf("%s VS set %d has %u members:\n",
                                               (i  == my_vsset_index) ?
                                               ("LOCAL") : ("OTHER"), i, vssets[i].num_members );
                                        ret = SP_get_vs_set_members(mess, &vssets[i], members, MAX_MEMBERS);
                                        if (ret < 0) {
                                                printf("VS Set has more then %d members. Recompile with larger MAX_MEMBERS\n", MAX_MEMBERS);
                                                SP_error( ret );
                                                exit( 1 );
                                        }
                                        for( j = 0; j < vssets[i].num_members; j++ )
                                                printf("\t%s\n", members[j] );
                                }
			}
		}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, endian_mismatch, num_groups, ret, mess );
	}else printf("received message of unknown message type 0x%x with ret %d\n", service_type, ret);


	printf("\n");
	printf("User> ");
	fflush(stdout);
}
예제 #6
0
파일: flooder.c 프로젝트: Fran89/seiscomp3
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;
}
예제 #7
0
파일: client.c 프로젝트: Ezran/cs437
static	void	Read_message()
{

static	char		 mess[SPREAD_MESS_LEN];
        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;
        int16		 mess_type;
        int		 endian_mismatch;
        int		 i,j;
        int		 ret;

        service_type = 0;

	ret = SP_receive( Mbox, &service_type, sender, 100, &num_groups, target_groups, 
		&mess_type, &endian_mismatch, sizeof(mess), 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, MAX_MEMBERS, &num_groups, target_groups, 
                                          &mess_type, &endian_mismatch, sizeof(mess), mess );
                }
        }
        if (ret < 0 )
        {
		if( ! To_exit )
		{
			SP_error( ret );
			printf("\n============================\n");
			printf("\nBye.\n");
		}
		exit( 0 );
	}
	if( Is_regular_mess( service_type ) )
	{
		//printf("message from %s, of type %d, (endian %d) to %d groups \n(%d bytes): %s\n", sender, mess_type, endian_mismatch, num_groups, ret, mess );
	    
            typecheck* type = (typecheck*)mess;
            if (type->type == TYPE_JOIN_CHATROOM) {
                join_chatroom* dat = (join_chatroom*)mess;
                ret = add_user(dat->u_id,dat->sp_id,dat->chatroom);
            }
            else if (type->type == TYPE_LEAVE_CHATROOM) {
                leave_chatroom* dat = (leave_chatroom*)mess;
                rm_user(dat->sp_id);
            }
            else if (type->type == TYPE_SEND_MSG) {
                message* dat = (message*)mess;
                if (strlen(chatroom) == 0)
                    add_room(chatroom);   
                lamport* ts = malloc(sizeof(lamport));
                ts->server_id = dat->timestamp.server_id;
                ts->index = dat->timestamp.index;
                printf("[%d,%d]\n",ts->server_id,ts->index);
                add_message(ts, dat->u_id, dat->chatroom, dat->mess);             
            }
            else if (type->type == TYPE_LIKE_MSG) {
                like* dat = (like*)mess;
                lamport* lts = malloc(sizeof(lamport));
                lamport* mts = malloc(sizeof(lamport));
                lts->server_id = dat->timestamp.server_id;
                lts->index = dat->timestamp.index;
                mts->server_id = dat->timestamp.server_id;
                mts->index = dat->timestamp.index;

                ret = add_like(dat->like_state, lts, dat->u_id, chatroom, mts); 
                printf("~~~~~~~~ %d:[%d,%d | %d,%d] %d %s %s \n",ret, lts->server_id,lts->index,mts->server_id,mts->index,dat->like_state,dat->u_id,chatroom);
            } 
            master_print();

	}else if( Is_membership_mess( service_type ) )
        {
                ret = SP_get_memb_info( mess, service_type, &memb_info );
                if (ret < 0) {
                        printf("BUG: membership message does not have valid body\n");
                        SP_error( ret );
                        exit( 1 );
                }
		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", &target_groups[i][0] );
			printf("grp id is %d %d %d\n",memb_info.gid.id[0], memb_info.gid.id[1], memb_info.gid.id[2] );

			if( Is_caused_join_mess( service_type ) )
			{
				printf("Due to the JOIN of %s\n", memb_info.changed_member );
			}else if( Is_caused_leave_mess( service_type ) ){
				printf("Due to the LEAVE of %s\n", memb_info.changed_member );
			}else if( Is_caused_disconnect_mess( service_type ) ){
				printf("Due to the DISCONNECT of %s\n", memb_info.changed_member );
			}else if( Is_caused_network_mess( service_type ) ){
				printf("Due to NETWORK change with %u VS sets\n", memb_info.num_vs_sets);
                                num_vs_sets = SP_get_vs_sets_info( mess, &vssets[0], MAX_VSSETS, &my_vsset_index );
                                if (num_vs_sets < 0) {
                                        printf("BUG: membership message has more then %d vs sets. Recompile with larger MAX_VSSETS\n", MAX_VSSETS);
                                        SP_error( num_vs_sets );
                                        exit( 1 );
                                }
                                for( i = 0; i < num_vs_sets; i++ )
                                {
                                        printf("%s VS set %d has %u members:\n",
                                               (i  == my_vsset_index) ?
                                               ("LOCAL") : ("OTHER"), i, vssets[i].num_members );
                                        ret = SP_get_vs_set_members(mess, &vssets[i], members, MAX_MEMBERS);
                                        if (ret < 0) {
                                                printf("VS Set has more then %d members. Recompile with larger MAX_MEMBERS\n", MAX_MEMBERS);
                                                SP_error( ret );
                                                exit( 1 );
                                        }
                                        for( j = 0; j < vssets[i].num_members; j++ )
                                                printf("\t%s\n", members[j] );
                                }
			}
		}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, endian_mismatch, num_groups, ret, mess );
	}else printf("received message of unknown message type 0x%x with ret %d\n", service_type, ret);


	printf("\n");
	fflush(stdout);
}
예제 #8
0
int main(int argc, const char *argv[]) {
    char from[MAX_GROUP_NAME];
    char message[MAX_MESSLEN];
    membership_info memb_info;
    int num_groups;
    char target_groups[100][MAX_GROUP_NAME];
    int16 mess_type;
    int endian_mismatch;

    int service_type;
    int loop = 1;
    int rc;
    int ret;
    int i;

    sqlite3 *mydb;
    char *dbName;
    char *zErrMsg;

    connectionStatus status = UNKNOWN;

    char buffer[MAX_GROUP_NAME];
    char sqlBuffer[1024];
    char *member;
    char *host;
    char *tok;
    char ch;
    extern char *optarg;

    char scratch[255];

    global.connected=0;
    global.Group=(char *)NULL;
    global.configFileName=(char *)NULL;
    global.locked = 0;
    global.rawClient =1;
    global.debug = 0;

#ifdef FICL
    global.appDir=(char *)NULL;
#endif

    char *group=(char *)NULL;

    setSymbol("BUILD",__DATE__,LOCK,LOCAL);
    setSymbol("CLIENT","raw",UNLOCK,GLOBAL);
    setSymbol("SPREAD_SERVER","4803",UNLOCK,GLOBAL);
    setSymbol("GROUP","global",UNLOCK,GLOBAL);
    setSymbol("USER","dbCache",LOCK,GLOBAL);
    setSymbol("DEBUG","false",UNLOCK,GLOBAL);
    setSymbol("MODE","local",UNLOCK,GLOBAL);
    setSymbol("START_FILE","./dbCache.rc",UNLOCK,GLOBAL);

    while((ch = getopt(argc, ( char **)argv,"dhc:")) != -1) {
        switch(ch) {
            case 'c':
                setSymbol("START_FILE", optarg,LOCK,GLOBAL);
                break;
            case 'd':
                global.debug=1;
                break;
            case 'h':
                usage();
                exit(0);
                break;
        }
    }

    loadFile(getSymbol("START_FILE"));

    if( global.debug ) {
        setBoolean("DEBUG",1);
    }

    global.debug = getBoolean("DEBUG");

    if(global.debug) {
        dumpGlobals();
        dumpSymbols();
    }

    dbName = getSymbol("DATABASE");

    connectToSpread();

    while(loop) {
        status = UNKNOWN;
        ret = SP_receive(global.Mbox, &service_type, from, 100,
                &num_groups, target_groups,
                &mess_type, &endian_mismatch, sizeof(message), message);

        if (Is_membership_mess (service_type)) {
            ret = SP_get_memb_info(message, service_type, &memb_info);

            if (Is_caused_join_mess(service_type)) {
                status=JOINED;
            } 
            if (Is_caused_leave_mess (service_type)) {
                status = LEFT;
            } 
            if (Is_caused_disconnect_mess (service_type)) {
                status = DISCONNECTED;
            } 
            if (Is_caused_network_mess (service_type)) {
                status = NETWORK;
            }

            rc = sqlite3_open(dbName, &mydb);
            for (i = 0; i < num_groups; i++) {

                if( global.debug) {
                    printf("\t%s\n",(char *) &target_groups[i][0]);
                }

                strcpy(buffer, &target_groups[i][1]);

                member=strtok(buffer,"#");
                host=strtok(NULL,"#");

                if( global.debug) {
                    printf("\t\tMember:%s\n", member);
                    printf("\t\tHost  :%s\n", host);
                }

                statusToText( status, scratch );

                if (JOINED == status ) {
                    sprintf( sqlBuffer,"insert or replace into status  ( member, host, grp, state ) values ( '%s','%s','%s','%s');", member, host,from,scratch);
                }
                if( global.debug) {
                    printf ("%s\n",sqlBuffer);
                }
                rc = sqlite3_exec(mydb, sqlBuffer, 0, 0, &zErrMsg);
            }
            strcpy(buffer, &memb_info.changed_member[1]);
            member=strtok(buffer,"#");
            host=strtok(NULL,"#");

            statusToText( status, scratch );
            sprintf( sqlBuffer, "update status set state='%s' where member = '%s' and host ='%s';",scratch, member,host);

            if( global.debug) {
                printf("CHANGE: %s %s\n",member,host);
                printf("CHANGE: %s\n", scratch);
                printf ("%s\n",sqlBuffer);
            }

            rc = sqlite3_exec(mydb, sqlBuffer, 0, 0, &zErrMsg);

            sqlite3_close(mydb);

        }

    }


    printf("============ After\n");
    dumpGlobals();

    printf("Sender %s\n",from);
    printf("Message >%s<\n",message);

    toSpread(from,"Message recieved\n");

    spreadJoin("TEST");

    sleep(2);

    spreadLeave("TEST");

    sleep(2);

    spreadDisconnect();
}
예제 #9
0
static  void    Read_message()
{

static  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;
        int16            mess_type;
        int              endian_mismatch;
        int              i,j;
        int              ret, success =0;
	char		 server_group[1];
	char		 *uname;

        service_type = 0;

        ret = SP_receive( Mbox, &service_type, sender, 100, &num_groups, target_groups, 
                &mess_type, &endian_mismatch, sizeof(mess), 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, MAX_MEMBERS, &num_groups, target_groups, 
                                          &mess_type, &endian_mismatch, sizeof(mess), mess );
                }
        }
if (ret < 0 )
        {
                if( ! To_exit )
                {
                        SP_error( ret );
                        printf("\n============================\n");
                        printf("\nBye.\n");
                }
                exit( 0 );
        }
        if( Is_regular_mess( service_type ) )
        {
                mess[ret] = 0;
                //printf("message from %s, of type %d, (endian %d) to %d groups \n(%d bytes): %s\n",
                //        sender, mess_type, endian_mismatch, num_groups, ret, mess ); 
	 	//printf("private group is %s\n", Private_group);
		if (strncmp(Private_group, sender, strlen(Private_group)) != 0)
		{	
		   recv_server_msg((struct chat_packet *) mess, mess_type);
		}
        }else if( Is_membership_mess( service_type ) )
        {
                ret = SP_get_memb_info( mess, service_type, &memb_info );
                if (ret < 0) {
                        printf("BUG: membership message does not have valid body\n");
                        SP_error( ret );
                        exit( 1 );
                }
	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", &target_groups[i][0] );
                        //printf("grp id is %d %d %d\n",memb_info.gid.id[0], memb_info.gid.id[1], memb_info.gid.id[2] );
			sprintf(server_group, "c%d", connected);
			if (strncmp(sender, server_group, 2) == 0)
			{
			  //printf("Server connection message\n");
			  for (i=0; i< num_groups; i++)
			  {
			    if (target_groups[i][1] == server_group[1])
			    {
				printf("Successfully connected to server %d\n>", connected);
				success = 1;
				show_menu();
			    }
			  }
			  if (!success)
			  {
				printf("Server unavailable.  Please try again later.\n>");
				connected = 0;
				SP_leave(Mbox, sender);
			  }
			}
			else
			{ /*Membership message from chatroom.  Show users in chat */
			 /*No longer here*/ 
			}
                        if( Is_caused_join_mess( service_type ) )
                        {
                                //printf("Due to the JOIN of %s\n", memb_info.changed_member );
                        }else if( Is_caused_leave_mess( service_type ) ){
                                //printf("Due to the LEAVE of %s\n", memb_info.changed_member );
                        }else if( Is_caused_disconnect_mess( service_type ) ){
                                //printf("Due to the DISCONNECT of %s\n", memb_info.changed_member );
			        if (strncmp(sender, server_group, 2) == 0 && memb_info.changed_member[1] == server_group[1])
				{
				  printf("Disconnected from server %s\n", server_group);
				  SP_leave(Mbox, sender); /*Disconnect us from the server group */
				  if (chatroom != NULL)
				  {
				    sprintf(chatroom, "%s%d",chatroom, connected);
				    SP_leave(Mbox, chatroom);
				    chatroom[0]='\0';
				    chatroom_start = (struct node *)calloc(1,sizeof(struct node));
				  }
				  connected = 0; /*we are no longer connected */
				}
                        }else if( Is_caused_network_mess( service_type ) ){
                                //printf("Due to NETWORK change with %u VS sets\n", memb_info.num_vs_sets);
                                num_vs_sets = SP_get_vs_sets_info( mess, &vssets[0], MAX_VSSETS, &my_vsset_index );
                                if (num_vs_sets < 0) {
                                        printf("BUG: membership message has more then %d vs sets. Recompile with larger MAX_VSSETS\n", MAX_VSSETS);
                                        SP_error( num_vs_sets );
                                        exit( 1 );
                                }
                                for( i = 0; i < num_vs_sets; i++ )
                                {
                                        printf("%s VS set %d has %u members:\n",
                                               (i  == my_vsset_index) ?
                                               ("LOCAL") : ("OTHER"), i, vssets[i].num_members );
                                        ret = SP_get_vs_set_members(mess, &vssets[i], members, MAX_MEMBERS);
                                        if (ret < 0) {
                                                printf("VS Set has more then %d members. Recompile with larger MAX_MEMBERS\n", MAX_MEMBERS);
                                                SP_error( ret );
                                                exit( 1 );
                                        }
                                        for( j = 0; j < (int) vssets[i].num_members; j++ )
                                                printf("\t%s\n", members[j] );
                                }
                        }
		}
                
		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("Incorrect mess type");
		
        }  else if (Is_reject_mess( service_type)) {
		printf("Rejcted");
	}  else printf("Unknown");
        fflush(stdout);
}