Esempio n. 1
0
int main(int argc, char ** argv){

	/* variables declaration */
	int i;
	int *size_of_buffer = 0;
	int num_of_ones = 0;
	
	/* Number of rounds that the algorithm will run */
	int rounds = 0;		
	
	/* buffer that master will send */
	char *buffer = NULL;
	
	/* sessionC handler */
	session *s;
	join_session(&argc, &argv, &s, "Slave3.spr");
	role *Master = s->get_role(s, "Master");	
	
	/* receive size of buffer */
	receive_int(Master, &size_of_buffer);
	
	printf("size of buffer is %d\n", *size_of_buffer);
	
	/* Dynamic memory allocation */
	buffer = (char *) malloc( (*size_of_buffer) * sizeof(char));
	
	/* Receive the buffer */
	receive_string(Master, &buffer);
	
/* For 100000 rounds */	
while(rounds++ < 100000){		
	/* Compute the number of ones */
	num_of_ones = number_of_ones(buffer); 
}	
	
	printf("num of ones is %d\n", num_of_ones);
	
	/* Send the result to the master node */
	send_int(Master, num_of_ones);
	
	/* Deallocate memory */
	free(buffer);
	
	/* End current session */
	end_session(s);
		
	return EXIT_SUCCESS;
}
Esempio n. 2
0
/***********************************************************************//**
 * @brief Execute function on server
 *
 * @param[in] request XML request string
 * @return XML response
 ***************************************************************************/
GXml GVOClient::execute(const std::string& request) const
{
    // Initialise response
    GXml xml;

    // Connect to hub
    connect_to_hub();

    // Continue only if connection has been established
    if (m_socket != -1) {

        // Post request
        post_string(request);

        // Receive response
        std::string response = receive_string();

        // Find start of XML text
        size_t start = response.find("<?xml");

        // If found then convert text into XML document
        if (start != std::string::npos) {
            xml = GXml(response.substr(start, std::string::npos));
        }

        // Close socket
        if (m_socket != -1) {
            close(m_socket);
            m_socket = -1;
        }

    } // endif: connection has been established

    // Return response
    return xml;
}
Esempio n. 3
0
//
// will establish a connection, send the
// packet and block till a response is obtained.
// socket will be closed after the response
// is obtained..
//
// the buffer is used for the sent as well
// as the received data.
//
void send_packet_and_wait_for_response(char* buffer, int send_len, char* server_host_address, int server_port_number)
{
  int sockfd, n;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    {
      fprintf(stderr,"Error: could not open client socket\n");
      return;
    }
  
  server = gethostbyname(server_host_address);
  if (server == NULL) 
    {
      fprintf(stderr, "Error: server host %s not found..\n",server_host_address);
      close(sockfd);
      sockfd = -1;
    }
  else
    {
      bzero((char *) &serv_addr, sizeof(serv_addr));
      serv_addr.sin_family = AF_INET;
      bcopy((char *)server->h_addr,(char *) &serv_addr.sin_addr.s_addr,server->h_length);
      serv_addr.sin_port = htons(server_port_number);

#ifdef DEBUG
      fprintf(stderr, "Info: connecting to server %s on port %d .......... \n",
	      server_host_address,
	      server_port_number);
#endif

      n =-1;

      while(n == -1)
      {
#ifdef USE_GNUPTH
	n=pth_connect(sockfd,(struct sockaddr*) &serv_addr,sizeof(serv_addr));
#else
	n=connect(sockfd,(struct sockaddr*) &serv_addr,sizeof(serv_addr));
#endif
      }
      
#ifdef DEBUG
      fprintf(stderr, "Info: successfully connected to server %s on port %d .......... \n",
	      server_host_address,
	      server_port_number);
#endif


      while(1)
	{
	  if(can_write_to_socket(sockfd))
	    break;

	  __SLEEP__(1000);
	}

#ifdef USE_GNUPTH
      pth_send(sockfd,buffer,send_len,0);
#else
      send(sockfd,buffer,send_len,0);
#endif

#ifdef DEBUG
      char payload[4096];
      int pl = extract_payload(buffer,payload,send_len);
      fprintf(stderr, "Info: sent message %s to server (payload of %d bytes)\n", buffer, pl);
      if(pl > 0)
	print_payload(stderr,payload,8,pl);
#endif
      while( (n = receive_string(sockfd,buffer, MAX_BUF_SIZE)) <= 0)
	{
		__SLEEP__(1000);
	}
	  
#ifdef DEBUG
      fprintf(stderr, "Info: received message %s from server\n", buffer);	  
      print_payload(stderr,buffer,8,n);
#endif
      close(sockfd);
    }
}