PlasCom2  1.0
XPACC Multi-physics simluation application
TestTCPInterface.C
Go to the documentation of this file.
1 #include "UnixUtils.H"
7 #include "NetUtils.H"
8 #include "FDUtils.H"
9 
10 namespace ix {
11 
12 
20  int TestTCPInterface(int argc,char *argv[])
21  {
22  if(!argv[1])
23  std::exit(1);
24  std::string mode(argv[1]);
25  if(mode == "server"){
26  sys::net::Server server;
27  server.SimpleInit();
28  int mother_descriptor = server.Descriptor();
29  int client_descriptor = server.Accept();
30  std::cout << "Accepted connection on descriptor: " << client_descriptor
31  << std::endl;
32  sys::FDSetMan setman;
33  setman.AddInDescriptor(mother_descriptor);
34  setman.AddIODescriptor(client_descriptor);
35  bool done = false;
36  int nsend = 0;
37  int nrecv = 0;
38  bool do_output = false;
39  bool do_input = false;
40  sys::fdistream clientInStr(client_descriptor);
41  sys::fdostream clientOutStr(client_descriptor);
42  while(!done){
43  int nio = setman.Select(4.0); // select timeout is 2 seconds
44  // std::cout << nio << " descriptors are ready for io." << std::endl;
45  if(setman.ReadyForInput(client_descriptor)){
46  std::cout << "Client has input to process." << std::endl;
47  do_input = true;
48  std::cout << "Receiving message number " << ++nrecv
49  << " from client." << std::endl;
50  std::string clientstring;
51  std::getline(clientInStr,clientstring);
52  std::cout << "Client sent: " << clientstring << std::endl;
53  if(clientstring.find("final") != std::string::npos)
54  done = true;
55  if(!done)
56  clientOutStr << "Server recvd " << nrecv << ", sent "
57  << ++nsend << std::endl;
58  else
59  clientOutStr << "Server recvd " << nrecv << ", send "
60  << ++nsend << " <final>" << std::endl;
61  }
62  }
63  }
64  else{
65  if(!argv[2])
66  std::exit(1);
67  std::string host(argv[2]);
68  sys::net::Client client;
69  if(client.Connect(host)){
70  std::cerr << "Client failed to connect to " << host << std::endl;
71  std::exit(1);
72  }
73  std::cout << "CLient is connected to " << host << " on "
74  << client.Descriptor() << std::endl;
75  sys::FDSetMan setman;
76  setman.AddIODescriptor(client.Descriptor());
77  sys::fdostream fdOut(client.Descriptor());
78  sys::fdistream fdIn(client.Descriptor());
79  fdOut << "I am the client, and this is my initial message." << std::endl;
80  int nsend = 0;
81  int nrecv = 0;
82  int nlines = 0;
83  bool done = false;
84  bool done_sending = false;
85  while(!done){
86  setman.Select(2.0);
87  bool recv_time = (setman.ReadyForInput(client.Descriptor()) > 0);
88  bool send_time = (setman.ReadyForOutput(client.Descriptor()) > 0);
89  if(recv_time){
90  std::cout << "Ready to receive message " << ++nrecv
91  << " from server." << std::endl;
92  std::string line;
93  std::getline(fdIn,line);
94  if(!line.empty())
95  std::cout << "line(" << ++nlines << ") from server: " << line << std::endl;
96  else
97  std::cout << "No line received." << std::endl;
98  if(!done_sending){
99  std::cout << "Sending message " << ++nsend << " to server." << std::endl;
100  fdOut << "I am the client, and I am sending message number " << nsend
101  << std::endl;
102  if(nsend == 2)
103  done_sending = true;
104  }
105  else{
106  std::cout << "Sending final" << std::endl;
107  fdOut << "final" << std::endl;
108  done = true;
109  }
110  }
111  }
112  std::cout << "These lines were left over: " << std::endl;
113  std::string line;
114  while(std::getline(fdIn,line))
115  std::cout << line << std::endl;
116  sleep(30);
117  }
118  std::exit(0);
119  }
120 };
121 
122 int
123 main(int argc,char *argv[])
124 {
125  return(ix::TestTCPInterface(argc,argv));
126 }
int SimpleInit(int port=0)
Definition: NetUtils.H:51
int Select(float seconds=0.0, bool reset=true)
Definition: FDUtils.H:378
int Connect(const std::string &server_address, int port=0)
Definition: NetUtils.H:115
int AddIODescriptor(int infd)
Definition: FDUtils.H:273
Defines MPI-specific parallel global and program classes.
int AddInDescriptor(int infd)
Definition: FDUtils.H:265
int TestTCPInterface(int argc, char *argv[])
Test utility for the TCP Interface.
int main(int argc, char *argv[])
int ReadyForOutput(int infd=-1)
Definition: FDUtils.H:345
int & Descriptor()
Definition: NetUtils.H:47
TCP/IP Network Utilities Interface.
Unix System Tools interface.
int ReadyForInput(int infd=-1)
Definition: FDUtils.H:356
Implementation of stream object for Unix file descriptors.