CMS 3D CMS Logo

DTSpyHelper.cc
Go to the documentation of this file.
1 #include "DTSpyHelper.h"
2 #include <cerrno>
3 #include <sys/time.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <arpa/inet.h>
9 #include <cstring>
10 
11 #ifdef __wasAPPLE__
12 typedef int socklen_t;
13 #endif
14 
16 {
17  DTCtcp(0);
18 }
19 
20 DTCtcp::DTCtcp(int localport)
21 {
22 // struct sockaddr_in myaddr;
23 
24  connected=false;
25 
26  printf("zeroing...\n");
27  bzero ((char *) &myaddr, sizeof(myaddr));
28  printf("zeroing done..\n");
29 
30 
31  sock = socket (AF_INET, SOCK_STREAM, 0);
32  printf("create socket..\n");
33 
34  if (sock < 0)
35  {
36  printf("no socket...\n");
37  throw DTtcpExcp(errno);
38  }
39 
40 
41  myaddr.sin_family = AF_INET;
42  myaddr.sin_port = htons (localport);
43 
44  //int blen = 65536;
45  int blen = 65536*8;
46 
47 // printf("setting socket opts buf...\n");
48 // if(setsockopt(sock,SOL_SOCKET,SO_SNDBUF,(char *)&blen,sizeof(blen))<0)
49 // throw DTtcpExcp(errno);
50 // if(setsockopt(sock,SOL_SOCKET,SO_RCVBUF,(char *)&blen,sizeof(blen))<0)
51 // throw DTtcpExcp(errno);
52  printf("setting socket opts reuse...\n");
53  if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&blen,sizeof(blen))<0)
54  throw DTtcpExcp(errno);
55 // printf("setting socket opts nodelay...\n");
56 // if(setsockopt(sock,SOL_SOCKET,TCP_NODELAY,(char *)&blen,sizeof(blen))<0)
57 // throw;
58  printf("binding...\n");
59 
60  port = localport;
61 
62  if (port)
63  if(bind(sock,(struct sockaddr *)&myaddr,sizeof (myaddr)) < 0)
64  { perror ("bind failed");
65  throw DTtcpExcp(errno);
66  }
67 
68 
69 }
70 
71 DTCtcp::DTCtcp(int snew, int opt)
72 {
73 
74  connected = true;
75  port =0;
76 
77  sock = snew;
78 }
79 
81 {
82 
83  connected = aconn->connected;
84  port = aconn->port;
85 
86  sock = aconn->sock;
87 }
88 
90 {
91  printf("deleting DTCtcp\n");
92  //if (connected) shutdown(sock,2);
93  shutdown(sock,SHUT_RDWR);
94  close (sock);
95 }
96 
97 short
99 {
100  long maddr = clientAddr.sin_addr.s_addr;
101  maddr = htonl (maddr);
102  return maddr&0xff;
103 }
104 
105 unsigned long
107 {
108  unsigned long maddr = clientAddr.sin_addr.s_addr;
109  maddr = htonl (maddr);
110  return maddr;
111 }
112 
113 int
115 {
116  connected = false;
117  return shutdown(sock,SHUT_RDWR);
118 }
119 
120 DTCtcp *
122 {
123 
124  // struct sockaddr_in clientAddr; /* client's address */
125 
126  bzero ((char *) &clientAddr, sizeof (clientAddr));
127 
128  if (listen (sock, 2) < 0)
129  {
130  perror ("listen failed");
131  throw DTtcpExcp(errno);
132  }
133 
134  int len = sizeof (clientAddr);
135 
136  int snew = accept (sock, (struct sockaddr *) &clientAddr,(socklen_t *) &len);
137  if (snew <=0)
138  {
139  perror ("accept failed");
140  throw DTtcpExcp(errno);
141  }
142 
143  return new DTCtcp(snew,0);
144 }
145 
146 void
147 DTCtcp::Connect(unsigned long in,int toport)
148 {
149  clientAddr.sin_family = AF_INET;
150  clientAddr.sin_addr.s_addr = htonl (in);
151  clientAddr.sin_port = htons (toport);
152 
153  if (connect (sock, (struct sockaddr *)&clientAddr, sizeof (clientAddr)) < 0)
154 {
155  perror ("connect failed");
156  throw DTtcpExcp(errno);
157 }
158  connected = true;
159 }
160 
161 void
162 DTCtcp::Connect(const char *host,int toport)
163 {
164  clientAddr.sin_family = AF_INET;
165  clientAddr.sin_addr.s_addr = inet_addr (host);
166  clientAddr.sin_port = htons (toport);
167 
168  if (connect (sock, (struct sockaddr *)&clientAddr, sizeof (clientAddr)) < 0)
169 {
170  perror ("connect failed");
171  throw DTtcpExcp(errno);
172 }
173  connected = true;
174 }
175 
176 int
178 {
179 // return read (sock, buffer,size) ;
180 
181  int howmany = 0;
182  int toberead = size;
183  do
184  {
185  //int readnow = recv (sock, &buffer[howmany], toberead,MSG_WAITALL) ;
186  int readnow = recv (sock, &buffer[howmany], toberead,0) ;
187  //if (readnow < 0 ) {printf("some rrorrs...%d\n",errno); return -1;}
188  if (readnow <= 0 )
189  {printf("some rrorrs...%d\n",errno); throw DTtcpExcp(errno);}
190  else {
191  howmany+=readnow; toberead-=readnow;}
192  } while (toberead>0);
193  return howmany;
194 }
195 
196 int
198 {
199  if (connected==false) throw DTtcpExcp(EPIPE);
200  int myret = write (sock, buffer, size) ;
201  if (myret<0) throw DTtcpExcp(errno);
202  return myret;
203 }
size
Write out results.
host
Definition: query.py:115
int Receive(char *buffer, int size)
Definition: DTSpyHelper.cc:177
int Send(char *buffer, int size)
Definition: DTSpyHelper.cc:197
int Disconnect()
Definition: DTSpyHelper.cc:114
struct sockaddr_in myaddr
Definition: DTSpyHelper.h:28
bool accept(const edm::Event &event, const edm::TriggerResults &triggerTable, const std::string &triggerPath)
Definition: TopDQMHelpers.h:30
short Id()
Definition: DTSpyHelper.cc:98
void Connect(const char *hostaddr, int port)
Definition: DTSpyHelper.cc:162
int sock
Definition: DTSpyHelper.h:24
int connected
Definition: DTSpyHelper.h:26
int port
Definition: DTSpyHelper.h:23
DTCtcp * Accept()
Definition: DTSpyHelper.cc:121
unsigned long addr()
Definition: DTSpyHelper.cc:106
def write(self, setup)
struct sockaddr_in clientAddr
Definition: DTSpyHelper.h:27