summaryrefslogtreecommitdiff
path: root/apps/JAWS/stress_testing/connection.cpp
blob: a8f38074d721c9fe9975a92c8842e39050d04b92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// $Id$

#include "connection.h"


// Make the connection to the WEB server

int connection::connect(char *hostname_opt_port, int tcp_nodelay, int sockbufsiz) {
  if(!hostname_opt_port) return 1;
  
  char *hostname_with_port;
  // Check to see if portnumber is specified in the hostnameport
  // If not, append :80
  if(!ACE_OS::strchr(hostname_opt_port,':')) {
	  hostname_with_port = new char[ACE_OS::strlen(hostname_opt_port) + 3];
	  ACE_OS::sprintf(hostname_with_port, "%s:%d", hostname_opt_port, 80);
  }
  else {
    hostname_with_port = hostname_opt_port;
  }
	
  // Beyond this point, hostname_with_port is of the form hostname:port

  ACE_INET_Addr server_addr(hostname_with_port);
  
  // Connect to server

  ACE_SOCK_Connector con;

  if(con.connect(stream_, server_addr) == -1) {
    perror("ACE_SOCK_Connector::connect");
    return 1;
  }

  
  // tcp_nodelay processing.

  // turn off weird ack things
  if(tcp_nodelay) {
	  struct protoent *p = ACE_OS::getprotobyname ("tcp");
    int one = 1;
    
    if (p && stream_.set_option (p->p_proto,
				 TCP_NODELAY,
				 (char *)& one, 
				 sizeof (one))) {
      perror("tcp_nodelay");
      return 1;
    }
  }
  
  if(sockbufsiz) 
    if (stream_.set_option (SOL_SOCKET,
			    SO_RCVBUF,
			    (char *) &sockbufsiz,
			    sizeof sockbufsiz) == -1) {
      perror("socket_queue_size");
      return 1;
    }
  
  return 0;
}

int connection::read(void *buffer, size_t maxlen, unsigned int timeout_seconds) {
  ACE_UNUSED_ARG (timeout_seconds);
  return stream_.recv(buffer, maxlen);
}

int connection::write(const void *buffer, size_t maxlen, unsigned int timeout_seconds) {
  ACE_UNUSED_ARG (timeout_seconds);
  return stream_.send(buffer, maxlen);
}

int connection::write_n(const void *buffer, size_t len, unsigned int timeout_seconds) {
  ACE_UNUSED_ARG (timeout_seconds);
  if(stream_.send_n(buffer, len) == -1)
    ACE_ERROR_RETURN((LM_ERROR, "Write failed for %s", buffer),1);
  return 0;
}

int connection::read_n(void *buffer, size_t maxlen, unsigned int timeout_seconds) {
  ACE_UNUSED_ARG (timeout_seconds);
  if(stream_.recv_n(buffer, maxlen) == -1)
    ACE_ERROR_RETURN((LM_ERROR, "Read failed.."),1);
  return 1;
}

int connection::close(void) {
  stream_.close_reader();
  stream_.close_writer();
  stream_.close();
  return 0;
}
  
connection::~connection(void) {
  this->close();
}