CMS 3D CMS Logo

cmsShowSendReport.cc
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <arpa/inet.h>
4 #include "zlib.h"
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <netdb.h>
10 #include <iostream>
11 
12 #define BUFLEN 60000
13 
14 // AMT: This code is a substitute of netcat command. The reason it is replaced
15 // is that netcat has limited buffer to 1024 bytes.
16 //
17 // TODO:: waith for server echo with timeout.
18 
19 
20 void getCompressedBuffer(const char* fname, Bytef** buffPtr, unsigned long& zippedSize)
21 {
22  FILE* pFile = fopen ( fname , "r" );
23  if ( pFile==NULL ) { std::cerr << "Can't open " << fname << std::endl; exit(1); }
24 
25  // obtain file size:
26  fseek (pFile , 0 , SEEK_END);
27  unsigned int lSize = ftell (pFile);
28  rewind (pFile);
29 
30  // allocate memory to contain the whole file:
31  void* buffer = malloc (sizeof(Bytef)*(lSize));
32 
33  size_t result = fread (buffer, 1, lSize ,pFile);
34  fclose(pFile);
35  if ( !result ) { std::cerr << "Failed to read " << fname <<std::endl; exit(1); }
36 
37  //
38  // write a new buffer. First four bytes is integer with
39  // value of size of uncompressed data. Remaining content
40  // is compressed original buffer.
41  //
42  unsigned int deflatedSize = compressBound(lSize) + 4; // estimation
43  Bytef * deflatedBuff = (Bytef*) malloc (sizeof(Bytef)*(deflatedSize));
44  *((unsigned int*)deflatedBuff) = htonl(lSize);
45 
46  //set buffer ptr
47  *buffPtr = deflatedBuff;
48 
49  // compress buffer
50  zippedSize = deflatedSize;
51  compress(deflatedBuff+4, &zippedSize, (const Bytef *)buffer, lSize);
52  zippedSize +=4;
53 
54  free(buffer);
55  /*
56  printf("zipped size %d \n", (int)zippedSize);
57  FILE* pFileOut = fopen ( "myfile-compressed" , "wb" );
58  fwrite (deflatedBuff , 1 , zippedSize , pFileOut );
59  fclose(pFileOut);
60  */
61 }
62 
63 int main(int argc, char **argv)
64 {
65  if (argc != 2)
66  {
67  std::cerr << "Uasage: sendCrashReport <fileName>" << std::endl; exit(1);
68  }
69 
70  // socket creation
71  int sd = socket(AF_INET,SOCK_DGRAM, 0);
72  if (sd < 0) { return 1; }
73 
74  // printf("bind port\n");
75  struct sockaddr_in cliAddr;
76  cliAddr.sin_family = AF_INET;
77  cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
78  cliAddr.sin_port = htons(0);
79 
80  int rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
81  if (rc < 0) {
82  std::cerr << "Can't bind port %d " << rc << std::endl; exit(1);
83  }
84 
85  // send data
86  struct hostent* h = gethostbyname("xrootd.t2.ucsd.edu");
87  if (!h) {
88  std::cerr << "Can't get gost ip \n"; exit(1);
89  }
90 
91  struct sockaddr_in remoteServAddr;
92  remoteServAddr.sin_family = h->h_addrtype;
93  memcpy((char *) &remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
94  remoteServAddr.sin_port = htons(9699);
95 
96  Bytef* buff;
97  unsigned long buffSize;
98  getCompressedBuffer(argv[1], &buff, buffSize);
99 
100  sendto(sd, buff, buffSize, 0,
101  (struct sockaddr *) &remoteServAddr,
102  sizeof(remoteServAddr));
103 
104  free(buff);
105 }
void getCompressedBuffer(const char *fname, Bytef **buffPtr, unsigned long &zippedSize)
#define NULL
Definition: scimark2.h:8
int main(int argc, char **argv)
double sd
string fname
main script