Beispiel #1
0
/**
 * FUNCTION NAME: main
 *
 * DESCRIPTION:
 */
int main(int argc, const char * argv[]) {
    // Check arguments
    if (argc != 2 && argc != 3) {
        usage("Incorrent number of arguments");
        exit(EXIT_FAILURE);
    }

    DNode *node;
    
    if (argc == 2) { // Start a new chat group
        std::string name(argv[1]);
        node = new DNode(name);
    } else { // Join an existing chat group
        std::string name(argv[1]);
        std::string ip_port(argv[2]);
        if (ip_port.find(":") == std::string::npos) {
            usage("Error parsing ADDR:PORT");
            exit(EXIT_FAILURE);
        }
        node = new DNode(name, ip_port);
    }

    // Node is up, introduced to the group
    if (node->nodeStart() == FAILURE) {
        delete node;
        return 0;
    }
    
    // Thread: Listening for input
    std::thread thread_sendMsg(sendMsg, node);
    // Thread: Receive chat messages
    std::thread thread_recvMsg(recvMsg, node);
    // Thread: Receive chat messages
    std::thread thread_displayMsg(displayMsg, node);
    // Thread: Track heartbeat
    std::thread thread_heartbeat(heartBeatRoutine, node);
    // Thread: Send message to leader
    std::thread thread_sendMsgToLeader(sendMsgToLeader, node);
    // Thread: Monitor congestion
    std::thread thread_traffic(trafficCheck, node);

    thread_sendMsg.join();
    thread_recvMsg.join();
    thread_displayMsg.join();
    thread_heartbeat.join();
    thread_sendMsgToLeader.join();
    thread_traffic.join();
    
    // Clean up and quit
    node->nodeLeave();
    delete node;
    return 0;
}
Beispiel #2
0
void connect(fffProcessor& processor, int argc, char **argv)
{
    CommandSocket* commandSocket = new CommandSocket(&processor);
    std::string ip;
    int port = 49674;
    
    std::string ip_port(argv[2]);
    if (ip_port.find(':') != std::string::npos)
    {
        ip = ip_port.substr(0, ip_port.find(':'));
        port = std::stoi(ip_port.substr(ip_port.find(':') + 1).data());
    }

    
    for(int argn = 3; argn < argc; argn++)
    {
        char* str = argv[argn];
        if (str[0] == '-')
        {
            for(str++; *str; str++)
            {
                switch(*str)
                {
                case 'v':
                    cura::increaseVerboseLevel();
                    break;
                case 'j':
                    argn++;
                    if (SettingRegistry::getInstance()->loadJSON(argv[argn]))
                    {
                        cura::logError("ERROR: Failed to load json file: %s\n", argv[argn]);
                    }
                    break;
                default:
                    cura::logError("Unknown option: %c\n", *str);
                    break;
                }
            }
        }
    }
    
    commandSocket->connect(ip, port);
}
void SupernodeDistributerDll::TotallyRemoveSuperNode(char* source, int ip, int port)
{
	// Find the item that we are removing
	IPAddress ip_address;
	ip_address.m_ip = ip;
	ip_address.m_port = port;
	vector<IPAddress>::iterator result = NULL;
	result = find(v_supernode_list.begin(),v_supernode_list.end(),ip_address);
	if(result != v_supernode_list.end() && result != NULL)
	{
		v_supernode_list.erase(result); //remove it from the main list
	}

	for(UINT i=0;i<v_rack_list.size();i++) //remove the supernode from v_rack_list
	{
		bool found = false;
		if(v_rack_list[i].m_rack_name.compare(source)==0)
		{
			found = true;
			vector<IPAndPort>::iterator iter = v_rack_list[i].v_supernodes.begin();
			while(iter != v_rack_list[i].v_supernodes.end())
			{
				if(iter->IsEqual(ip, port))
				{
					v_rack_list[i].v_supernodes.erase(iter);
					break;
				}
				iter++;
			}
		}
		if(found)
			break;
	}

	//remove the supernode from the dialog
	IPAndPort ip_port(ip, port);
	p_dlg->m_supernode_distributer_page.RemoveSuperNode(ip_port);
}
Beispiel #4
0
int
main (int argc, char **argv)
{
  /*local variables */
  extern char *optarg;
  int sock, server_len, len, opt;	//,n;
  char opcode, filename[196], mode[12] = "octet";
  struct hostent *host;		/*for host information */
  struct sockaddr_in server;	//, client; /*the address structure for both the server and client */
  FILE *fp;			/*a pointer to a file that the client will send or get from the server */

  if (argc < 2)
    {
      help (argv[0]);
      return 0;
    }
  if (!(host = gethostbyname (argv[1])))
    {
      perror ("Client could not get host address information");
      exit (2);
    }

/* All of the following deals with command line switches */
  while ((opt = getopt (argc, argv, "dnoh:P:p:g:l:w:")) != -1)	/* this function is handy */
    {
      switch (opt)
	{
	case 'd':		/* debug mode (no opts) */
	  debug = 1;
	  break;
	case 'P':		/* Port (opt required) */
	  port = atoi (optarg);
	  if (debug)
	    {
	      printf ("Client: The port number is: %d\n", port);
	    }
	  break;
	case 'p':		/* put a file on the server */
	  strncpy (filename, optarg, sizeof (filename) - 1);
	  opcode = WRQ;
	  fp = fopen (filename, "r");	/*opened the file for reading */
	  if (fp == NULL)
	    {
	      printf ("Client: file could not be opened\n");
	      return 0;
	    }
	  if (debug)
	    {
	      printf ("Client: The file name is: %s and can be read",
		      filename);
	    }
	  fclose (fp);
	  break;
	case 'g':		/*get a file from the server */
	  strncpy (filename, optarg, sizeof (filename) - 1);
	  opcode = RRQ;
	  fp = fopen (filename, "w");	/*opened the file for writting */
	  if (fp == NULL)
	    {
	      printf ("Client: file could not be created\n");
	      return 0;
	    }
	  if (debug)
	    {
	      printf ("Client: The file name is: %s and it has been created",
		      filename);
	    }
	  fclose (fp);
	  break;
	case 'w':		/* Get the window size */
	  ackfreq = atoi (optarg);
	  if (debug)
	    {
	      printf ("Client: Window size is: %i\n", ackfreq);
	    }
	  //ackfreq = atoi (optarg);
	  if (ackfreq > MAXACKFREQ)
	    {
	      printf
		("Client: Sorry, you specified an ack frequency higher than the maximum allowed (Requested: %d Max: %d)\n",
		 ackfreq, MAXACKFREQ);
	      return 0;
	    }
	  else if (w_size == 0)
	    {
	      printf ("Client: Sorry, you have to ack sometime.\n");
	      return 0;
	    }
	  break;
	case 'l':		/* packet length */
	  datasize = atoi (optarg);
	  if (debug)
	    {
	      printf ("Client: Packet length is: %i bytes\n", datasize);
	    }
	  if (datasize > MAXDATASIZE)
	    {
	      printf
		("Client: Sorry, you specified a data size higher than the maximum allowed (Requested: %d Max: %d)\n",
		 datasize, MAXDATASIZE);
	      return 0;
	    }
	  break;
	case 'h':		/* Help (no opts) */
	  help (argv[0]);
	  return (0);
	  break;
	case 'o':
	  strncpy (mode, "octet", sizeof (mode) - 1);
	  if (debug)
	    {
	      printf ("Client: The mode is set to octet\n");
	    }
	  break;
	case 'n':
	  strncpy (mode, "netascii", sizeof (mode) - 1);
	  if (debug)
	    {
	      printf ("Client: The mode is set to netascii\n");
	    }
	  break;
	default:		/* everything else */
	  help (argv[0]);
	  return (0);
	  break;
	}			//end of switch
    }				//end of while loop

/*check for valid input*/
  if (argc < 5 || argc > 12)
    {
      printf ("Client: wrong number of arguments: %d\n", argc);
      help (argv[0]);
      exit (ERROR);
    }

/* Done dealing with switches and the command line*/


  /*Create the socket, a -1 will show us an error */
  if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
    {
      printf ("Client: Socket could not be created");
      return 0;
    }


/*set the address values for the server */
  memset (&server, 0, sizeof (server));	/*Clear the structure */
  server.sin_family = AF_INET;	/*address family for TCP and UDP */
  memcpy (&server.sin_addr, host->h_addr, host->h_length);	/*set address of server taken from gethostbyname function */
  //server.sin_addr.s_addr = htonl (INADDR_ANY); /*use any address */
  server.sin_port = htons (port);	/*pick a free port */


  server_len = sizeof (server);	/*get the length of the server address */

  if (debug)
    printf ("Client: size of server_address is : %d bytes\n", server_len);

  memset (buf, 0, BUFSIZ);	/*clear the buffer */
  /* this is the first request message */
  len = req_packet (opcode, filename, mode, buf);

  if (debug)
    printf ("Client: The request packt's length is: %d bytes\n", len);

  if (sendto (sock, buf, len, 0, (struct sockaddr *) &server, server_len) !=
      len)
    {
      perror ("Client: sendto has returend an error");
      exit (ERROR);
    }
  if (debug)
    ip_port (server);
  switch (opcode)
    {
    case RRQ:			/*read from the server, download */
      tget (filename, server, mode, sock);
      break;
    case WRQ:			/*write to the server, upload */
      tsend (filename, server, mode, sock);
      break;
    default:
      printf ("Invalid opcode detected. Ignoring packet.");
    }
  close (sock);			/* close the socket */
  return 1;
}				//end of main
Beispiel #5
0
/*
*This function is called when the client would like to upload a file to the server.
*/
void
tsend (char *pFilename, struct sockaddr_in server, char *pMode, int sock)
{
  int len, server_len, opcode, ssize = 0, n, i, j, bcount = 0, tid;
  unsigned short int count = 0, rcount = 0, acked = 0;
  unsigned char filebuf[MAXDATASIZE + 1];
  unsigned char packetbuf[MAXACKFREQ][MAXDATASIZE + 12],
    recvbuf[MAXDATASIZE + 12];
  char filename[128], mode[12], *bufindex;	//fullpath[196],
  struct sockaddr_in ack;

  FILE *fp;			/* pointer to the file we will be sending */

  strcpy (filename, pFilename);	//copy the pointer to the filename into a real array
  strcpy (mode, pMode);		//same as above

  if (debug)
    printf ("Client: branched to file send function\n");

/*At this point I have to wait to recieve an ACK from the server before I start sending the file*/
  /*open the file to read */
  fp = fopen (filename, "r");
  if (fp == NULL)
    {				//if the pointer is null then the file can't be opened - Bad perms OR no such file
      if (debug)
	printf ("Client: sending bad file: file not found (%s)\n", filename);
      return;
    }
  else
    {
      if (debug)
	printf ("Client: Sending file... (source: %s)\n", filename);

    }
//get ACK for WRQ
/* The following 'for' loop is used to recieve/timeout ACKs */
  for (j = 0; j < RETRIES - 2; j++)
    {
      server_len = sizeof (ack);
      errno = EAGAIN;
      n = -1;
      for (i = 0; errno == EAGAIN && i <= TIMEOUT && n < 0; i++)
	{
	  n = recvfrom (sock, recvbuf, sizeof (recvbuf), MSG_DONTWAIT,
			(struct sockaddr *) &ack, (socklen_t *) & server_len);
	  usleep (1000);
	}


      /* if(debug)
         ip_port (ack);    print the vlaue recived from the server */


      tid = ntohs (ack.sin_port);	//get the tid of the server.
      server.sin_port = htons (tid);	//set the tid for rest of the transfer


      if (n < 0 && errno != EAGAIN)
	{
	  if (debug)
	    printf
	      ("Client: could not receive from the server (errno: %d n: %d)\n",
	       errno, n);
	  //resend packet
	}
      else if (n < 0 && errno == EAGAIN)
	{
	  if (debug)
	    printf ("Client: Timeout waiting for ack (errno: %d n: %d)\n",
		    errno, n);
	  //resend packet
	}
      else
	{			/*changed client to server here */
	  if (server.sin_addr.s_addr != ack.sin_addr.s_addr)	/* checks to ensure send to ip is same from ACK IP */
	    {
	      if (debug)
		printf
		  ("Client: Error recieving ACK (ACK from invalid address)\n");
	      j--;		/* in this case someone else connected to our port. Ignore this fact and retry getting the ack */
	      continue;
	    }
	  if (tid != ntohs (server.sin_port))	/* checks to ensure get from the correct TID */
	    {
	      if (debug)
		printf
		  ("Client: Error recieving file (data from invalid tid)\n");
	      len = err_packet (5, err_msg[5], buf);
	      if (sendto (sock, buf, len, 0, (struct sockaddr *) &server, sizeof (server)) != len)	/* send the data packet */
		{
		  printf
		    ("Client: Mismatch in number of sent bytes while trying to send mode error packet\n");
		}
	      /* if (debug)
	         ip_port(server); */
	      j--;
	      continue;		/* we aren't going to let another connection spoil our first connection */
	    }

/* this formatting code is just like the code in the main function */
	  bufindex = (char *) recvbuf;	//start our pointer going
	  if (bufindex++[0] != 0x00)
	    printf ("Client: bad first nullbyte!\n");
	  opcode = *bufindex++;

	  rcount = *bufindex++ << 8;
	  rcount &= 0xff00;
	  rcount += (*bufindex++ & 0x00ff);
	  if (opcode != 4 || rcount != count)	/* ack packet should have code 4 (ack) and should be acking the packet we just sent */
	    {
	      if (debug)
		printf
		  ("Client: Remote host failed to ACK proper data packet # %d (got OP: %d Block: %d)\n",
		   count, opcode, rcount);
/* sending error message */
	      if (opcode > 5)
		{

		  len = err_packet (4, err_msg[4], buf);
		  if (sendto (sock, buf, len, 0, (struct sockaddr *) &server, sizeof (server)) != len)	/* send the data packet */
		    {
		      printf
			("Client: Mismatch in number of sent bytes while trying to send mode error packet\n");
		    }
		}
	      /* from here we will loop back and resend */
	      /* if (debug)
	         ip_port(server); */
	    }
	  else
	    {
	      if (debug)
		printf ("Client: Remote host successfully ACK'd (#%d)\n",
			rcount);
	      break;
	    }
	}			//end of else
      if (debug)
	printf ("Client: Ack(s) lost. Resending complete.\n");

    }
/* The ack sending 'for' loop ends here */



  memset (filebuf, 0, sizeof (filebuf));	//clear the filebuf
  while (1)			/* our break statement will escape us when we are done */
    {
      acked = 0;
      ssize = fread (filebuf, 1, datasize, fp);
      if (debug)
	{
	  printf
	    ("The first data block has been read from the file and will be sent to the server\n");
	  printf ("The size read from the file is: %d\n", ssize);
	}

      count++;			/* count number of datasize byte portions we read from the file */
      if (count == 1)		/* we always look for an ack on the FIRST packet */
	bcount = 0;
      else if (count == 2)	/* The second packet will always start our counter at zreo. This special case needs to exist to avoid a DBZ when count = 2 - 2 = 0 */
	bcount = 0;
      else
	bcount = (count - 2) % ackfreq;

      sprintf ((char *) packetbuf[bcount], "%c%c%c%c", 0x00, 0x03, 0x00, 0x00);	/* build data packet but write out the count as zero */
      memcpy ((char *) packetbuf[bcount] + 4, filebuf, ssize);
      len = 4 + ssize;
      packetbuf[bcount][2] = (count & 0xFF00) >> 8;	//fill in the count (top number first)
      packetbuf[bcount][3] = (count & 0x00FF);	//fill in the lower part of the count
      if (debug)
	printf ("Client: Sending packet # %04d (length: %d file chunk: %d)\n",
		count, len, ssize);
      /* send the data packet */
      if (sendto
	  (sock, packetbuf[bcount], len, 0, (struct sockaddr *) &server,
	   sizeof (server)) != len)
	{
	  if (debug)
	    printf ("Client: Mismatch in number of sent bytes\n");
	  return;
	}
      if (debug)
	{
	  ip_port (server);
	  printf ("==count: %d  bcount: %d  ssize: %d  datasize: %d\n", count,
		  bcount, ssize, datasize);
	}
      //if ((count - 1) == 0 || ((count - 1) % ackfreq) == 0 || ssize != datasize)
      if (((count) % ackfreq) == 0 || ssize != datasize)
	{
	  if (debug)
	    printf ("-- I will get an ACK\n");
/* The following 'for' loop is used to recieve/timeout ACKs */
	  for (j = 0; j < RETRIES; j++)
	    {
	      server_len = sizeof (ack);
	      errno = EAGAIN;
	      n = -1;
	      for (i = 0; errno == EAGAIN && i <= TIMEOUT && n < 0; i++)
		{
		  n =
		    recvfrom (sock, recvbuf, sizeof (recvbuf), MSG_DONTWAIT,
			      (struct sockaddr *) &ack,
			      (socklen_t *) & server_len);
		  /* if (debug)
		     ip_port(ack); */
		  usleep (1000);
		}
	      if (n < 0 && errno != EAGAIN)
		{
		  if (debug)
		    printf
		      ("Client: could not receive from the server (errno: %d n: %d)\n",
		       errno, n);
		  //resend packet
		}
	      else if (n < 0 && errno == EAGAIN)
		{
		  if (debug)
		    printf
		      ("Client: Timeout waiting for ack (errno: %d n: %d)\n",
		       errno, n);
		  //resend packet
		}
	      else
		{		/* checks to ensure send to ip is same from ACK IP */
		  if (server.sin_addr.s_addr != ack.sin_addr.s_addr)
		    {
		      if (debug)
			printf
			  ("Client: Error recieving ACK (ACK from invalid address)\n");
		      /* in this case someone else connected to our port. Ignore this fact and retry getting the ack */
		      j--;
		      continue;
		    }
		  if (tid != ntohs (server.sin_port))	/* checks to ensure get from the correct TID */
		    {
		      if (debug)
			printf
			  ("Client: Error recieving file (data from invalid tid)\n");
		      len = err_packet (5, err_msg[5], buf);
		      /* send the data packet */
		      if (sendto
			  (sock, buf, len, 0, (struct sockaddr *) &server,
			   sizeof (server)) != len)
			{
			  printf
			    ("Client: Mismatch in number of sent bytes while trying to send mode error packet\n");
			}
		      /*if (debug)
		         ip_port(server);  */
		      j--;

		      continue;	/* we aren't going to let another connection spoil our first connection */
		    }

/* this formatting code is just like the code in the main function */
		  bufindex = (char *) recvbuf;	//start our pointer going
		  if (bufindex++[0] != 0x00)
		    printf ("Client: bad first nullbyte!\n");
		  opcode = *bufindex++;

		  rcount = *bufindex++ << 8;
		  rcount &= 0xff00;
		  rcount += (*bufindex++ & 0x00ff);
		  if (opcode != 4 || rcount != count)	/* ack packet should have code 4 (ack) and should be acking the packet we just sent */
		    {
		      if (debug)
			printf
			  ("Client: Remote host failed to ACK proper data packet # %d (got OP: %d Block: %d)\n",
			   count, opcode, rcount);
		      /* sending error message */
		      if (opcode > 5)
			{
			  len = err_packet (4, err_msg[4], buf);
			  if (sendto (sock, buf, len, 0, (struct sockaddr *) &server, sizeof (server)) != len)	/* send the data packet */
			    {
			      printf
				("Client: Mismatch in number of sent bytes while trying to send mode error packet\n");
			    }
			  /*if (debug)
			     ip_port(server); */
			}
		      /* from here we will loop back and resend */
		    }
		  else
		    {
		      if (debug)
			printf
			  ("Client: Remote host successfully ACK'd (#%d)\n",
			   rcount);
		      break;
		    }
		}
	      for (i = 0; i <= bcount; i++)
		{
		  if (sendto (sock, packetbuf[i], len, 0, (struct sockaddr *) &server, sizeof (server)) != len)	/* resend the data packet */
		    {
		      if (debug)
			printf ("Client: Mismatch in number of sent bytes\n");
		      return;
		    }
		  if (debug)
		    {
		      printf ("Client: Ack(s) lost. Resending: %d\n",
			      count - bcount + i);
		      ip_port (server);
		    }
		}
	      if (debug)
		printf ("Client: Ack(s) lost. Resending complete.\n");

	    }
/* The ack sending 'for' loop ends here */

	}
      else if (debug)
	{
	  printf
	    ("Client: Not attempting to recieve ack. Not required. count: %d\n",
	     count);
	  n = recvfrom (sock, recvbuf, sizeof (recvbuf), MSG_DONTWAIT, (struct sockaddr *) &ack, (socklen_t *) & server_len);	/* just do a quick check incase the remote host is trying with ackfreq = 1 */
	  /*if (debug)
	     ip_port(ack); */
	}

      if (j == RETRIES)
	{
	  if (debug)
	    printf ("Client: Ack Timeout. Aborting transfer\n");
	  fclose (fp);

	  return;
	}
      if (ssize != datasize)
	break;

      memset (filebuf, 0, sizeof (filebuf));	/* fill the filebuf with zeros so that when the fread fills it, it is a null terminated string */
    }

  fclose (fp);
  if (debug)
    printf ("Client: File sent successfully\n");

  return;
}				//end of tsend function