CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/src/Fireworks/Core/bin/cmsShowSendReport.cc

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <arpa/inet.h>
00004 #include "zlib.h"
00005 #include <sys/types.h>
00006 #include <sys/socket.h>
00007 #include <unistd.h>
00008 #include <string.h>
00009 #include <netdb.h>
00010 #include <iostream>
00011 
00012 #define BUFLEN 60000
00013 
00014 // AMT: This code is a substitute of netcat command. The reason it is replaced 
00015 //      is that netcat has limited buffer to 1024 bytes.
00016 //      
00017 //      TODO:: waith for server echo with timeout.
00018 
00019 
00020 void getCompressedBuffer(const char* fname, Bytef** buffPtr, unsigned long& zippedSize)
00021 {
00022    FILE* pFile = fopen ( fname , "r" );
00023    if ( pFile==NULL )  { std::cerr << "Can't open " << fname << std::endl; exit(1); }
00024 
00025    // obtain file size:
00026    fseek (pFile , 0 , SEEK_END);
00027    unsigned int lSize = ftell (pFile);
00028    rewind (pFile);
00029 
00030    // allocate memory to contain the whole file:
00031    void* buffer = malloc (sizeof(Bytef)*(lSize));
00032 
00033    size_t result = fread (buffer, 1, lSize ,pFile);
00034    fclose(pFile);
00035    if ( !result ) { std::cerr << "Failed to read " << fname <<std::endl; exit(1); }
00036 
00037    //
00038    // write a new buffer. First four bytes is integer with  
00039    // value of size of uncompressed data. Remaining content 
00040    // is compressed original buffer.
00041    //
00042    unsigned int deflatedSize =  compressBound(lSize) + 4; // estimation
00043    Bytef * deflatedBuff = (Bytef*) malloc (sizeof(Bytef)*(deflatedSize));
00044    *((unsigned int*)deflatedBuff) = htonl(lSize);
00045 
00046    //set buffer ptr
00047    *buffPtr = deflatedBuff;
00048 
00049    // compress buffer
00050    zippedSize = deflatedSize;
00051    compress(deflatedBuff+4, &zippedSize, (const Bytef *)buffer, lSize);
00052    zippedSize +=4;
00053 
00054    free(buffer);
00055    /*
00056    printf("zipped size %d \n", (int)zippedSize);
00057    FILE* pFileOut = fopen ( "myfile-compressed" , "wb" );
00058    fwrite (deflatedBuff , 1 , zippedSize , pFileOut );
00059    fclose(pFileOut);
00060    */
00061 }
00062 
00063 int main(int argc, char **argv)
00064 {
00065   if (argc != 2)
00066   {
00067       std::cerr << "Uasage: sendCrashReport <fileName>" << std::endl; exit(1);
00068   }
00069 
00070    // socket creation
00071    int sd = socket(AF_INET,SOCK_DGRAM, 0);
00072    if (sd  < 0) { return 1; }
00073 
00074    // printf("bind port\n");
00075    struct sockaddr_in cliAddr;
00076    cliAddr.sin_family = AF_INET;
00077    cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
00078    cliAddr.sin_port = htons(0);
00079 
00080    int rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr)); 
00081    if (rc < 0) {
00082       std::cerr << "Can't bind port %d " << rc << std::endl; exit(1);
00083    }
00084    
00085    // send data 
00086    struct hostent* h = gethostbyname("xrootd.t2.ucsd.edu");
00087    if (!h) {
00088       std::cerr << "Can't get gost ip \n"; exit(1);
00089    }
00090 
00091    struct sockaddr_in remoteServAddr;
00092    remoteServAddr.sin_family = h->h_addrtype;
00093    memcpy((char *) &remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
00094    remoteServAddr.sin_port = htons(9699);
00095 
00096    Bytef* buff;
00097    unsigned long  buffSize;
00098    getCompressedBuffer(argv[1], &buff, buffSize);
00099 
00100    int res = sendto(sd, buff, buffSize, 0, 
00101                     (struct sockaddr *) &remoteServAddr, 
00102                     sizeof(remoteServAddr));
00103    free(buff);
00104    
00105    if (res == -1)
00106       std::cerr << "Sending report has failed." << std::endl;
00107    else
00108       std::cout << "Report has been sent." <<std::endl;
00109 
00110 }