HepLib
Server.cpp
Go to the documentation of this file.
1 #include "BASIC.h"
2 #include <netdb.h>
3 #include <sstream>
4 
5 namespace HepLib {
6 
7  string Server::Next(string sip, string sport) {
8  static const int MAXSIZE = 4096;
9  auto port = stoi(sport);
10 
11  int sockfd, n,rec_len;
12  char recvline[MAXSIZE], sendline[MAXSIZE];
13  char buf[MAXSIZE];
14  struct sockaddr_in servaddr;
15 
16  if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
17  ostringstream oss;
18  oss << "create socket error(" << errno << "): " << strerror(errno);
19  throw Error(oss.str());
20  }
21 
22  memset(&servaddr, 0, sizeof(servaddr));
23  servaddr.sin_family = AF_INET;
24  servaddr.sin_port = htons(port);
25  struct hostent *hext;
26  if ( (hext = gethostbyname(sip.c_str())) == NULL ) {
27  ostringstream oss;
28  oss << "gethostbyname error for " << sip << endl;
29  throw Error(oss.str());
30  }
31  memcpy(&servaddr.sin_addr, hext->h_addr_list[0], hext->h_length);
32 
33  if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
34  ostringstream oss;
35  oss << "connect error(" << errno << "): " << strerror(errno) << endl;
36  throw Error(oss.str());
37  }
38 
39  if((rec_len = recv(sockfd, buf, MAXSIZE,0)) == -1) {
40  ostringstream oss;
41  oss << "recv error(" << errno << "): " << strerror(errno) << endl;
42  throw Error(oss.str());
43  }
44 
45  buf[rec_len] = '\0';
46  string ret = buf;
47  close(sockfd);
48  return ret;
49  }
50 
51  void Server::Start() {
52 
53  int port = Port;
54  auto total = Total;
55  auto round = Round;
56 
57  int socket_fd;
58  struct sockaddr_in servaddr;
59 
60  if( (socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) {
61  throw Error(string("Server: ") + strerror(errno));
62  }
63 
64  memset(&servaddr, 0, sizeof(servaddr));
65  servaddr.sin_family = AF_INET;
66  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
67  servaddr.sin_port = (port>0 ? htons(port) : 0);
68 
69  if( bind(socket_fd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) {
70  throw Error(string("Server: ") + strerror(errno));
71  }
72  if(port<=0) {
73  struct sockaddr_in connAddr;
74  socklen_t len = sizeof(connAddr);
75  if(getsockname(socket_fd, (sockaddr*)&connAddr, &len) !=0) throw Error("Server: retrived Port failed!");
76  Port = port = ntohs(connAddr.sin_port);
77  }
78 
79  if( listen(socket_fd, 10) == -1) throw Error(string("Server: ") + strerror(errno));
80  map<string,ex> dict;
81  char hostname[1024];
82  gethostname(hostname, 1024);
83  dict["port"] = Port;
84  dict["DL"] = Symbol(DL);
85  dict["FUNC"] = Symbol(FUNC);
86  garWrite("PRC.gar", dict);
87 
88  if(Verbose>0) cout << endl << "Started @ " << now() << endl;
89  for(int r=0; r<round; r++) {
90  for(int c=0; c<total; c++) {
91  auto current = c;
92  if(Verbose>1) {
93  cout << "\r \r";
94  cout << " Server[" << port << "]: " << current << " / " << (total-1) << " @ " << now(false) << flush;
95  }
96 
97  string skip = Skip;
98  string_replace_all(skip, "[ID]", to_string(current));
99  if(file_exists(skip)) continue;
100 
101  int connect_fd;
102  if( (connect_fd = accept(socket_fd, (struct sockaddr*)NULL, NULL)) == -1) {
103  cout << "Server: " << strerror(errno) << endl;
104  continue;
105  }
106  struct linger so_linger;
107  so_linger.l_onoff = 1;
108  so_linger.l_linger = 0;
109  setsockopt(connect_fd, SOL_SOCKET, SO_LINGER, &so_linger, sizeof so_linger);
110 
111  string data = to_string(current); // data = ID
112  if(send(connect_fd, data.c_str(), data.length(),0) == -1) {
113  if(Verbose>1) cout << "Server: " << strerror(errno) << endl;
114  }
115  close(connect_fd);
116  }
117  if(Verbose>1) cout << endl;
118  }
119  if(Verbose>0) cout << "Finished @ " << now() << endl << endl;
120  close(socket_fd);
121  }
122 
123 }
Basic header file.
class used to wrap error message
Definition: BASIC.h:242
static string Next(string sip, string sport)
Definition: Server.cpp:7
int Verbose
Definition: BASIC.h:883
unsigned Total
Definition: BASIC.h:881
string FUNC
Definition: BASIC.h:886
string DL
Definition: BASIC.h:885
string Skip
Definition: BASIC.h:884
void Start()
Definition: Server.cpp:51
class extended to GiNaC symbol class, represent a positive symbol
Definition: BASIC.h:113
HepLib namespace.
Definition: BASIC.cpp:17
bool file_exists(string fn)
Definition: BASIC.h:289
string now(bool use_date)
date/time string
Definition: BASIC.cpp:525
void string_replace_all(string &str, const string &from, const string &to)
Definition: Functions.cpp:148
void garWrite(const string &garfn, const map< string, ex > &resMap)
garWrite to write the string-key map to the archive
Definition: BASIC.cpp:639