CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/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(char)*(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    /*
00055    printf("zipped size %d \n", (int)zippedSize);
00056    FILE* pFileOut = fopen ( "myfile-compressed" , "wb" );
00057    fwrite (deflatedBuff , 1 , zippedSize , pFileOut );
00058    fclose(pFileOut);
00059    */
00060 }
00061 
00062 int main(int argc, char **argv)
00063 {
00064   if (argc != 2)
00065   {
00066       std::cerr << "Uasage: sendCrashReport <fileName>" << std::endl; exit(1);
00067   }
00068 
00069    // socket creation
00070    int sd = socket(AF_INET,SOCK_DGRAM, 0);
00071    if (sd  < 0) { return 1; }
00072 
00073    // printf("bind port\n");
00074    struct sockaddr_in cliAddr;
00075    cliAddr.sin_family = AF_INET;
00076    cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
00077    cliAddr.sin_port = htons(0);
00078 
00079    int rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr)); 
00080    if (rc < 0) {
00081       std::cerr << "Can't bind port %d " << rc << std::endl; exit(1);
00082    }
00083    
00084    // send data 
00085    struct hostent* h = gethostbyname("xrootd.t2.ucsd.edu");
00086    if (!h) {
00087       std::cerr << "Can't get gost ip \n"; exit(1);
00088    }
00089 
00090    struct sockaddr_in remoteServAddr;
00091    remoteServAddr.sin_family = h->h_addrtype;
00092    memcpy((char *) &remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
00093    remoteServAddr.sin_port = htons(9699);
00094 
00095    Bytef* buff;
00096    unsigned long  buffSize;
00097    getCompressedBuffer(argv[1], &buff, buffSize);
00098 
00099    int res = sendto(sd, buff, buffSize, 0, 
00100                     (struct sockaddr *) &remoteServAddr, 
00101                     sizeof(remoteServAddr));
00102 
00103    if (res == -1)
00104       std::cerr << "Sending report has failed." << std::endl;
00105    else
00106       std::cout << "Report has been sent." <<std::endl;
00107 
00108 }