Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:42

0001 
0002 #include "remote_msg_buffer.h"
0003 
0004 // in the constructor we allocate the specified amount of bytes for
0005 // the output array.
0006 
0007 remote_msg_buffer::remote_msg_buffer(const char *host, const int port, const int msglen)
0008   : msg_buffer(msglen)
0009 {
0010   // in the constructor we replace COUT's streambuf with this one.
0011   // we remember the old one because we may want to replace it.
0012 
0013 #ifdef RDBUF_ACCEPTS_STREAMBUF
0014   original_streambuf = COUT.rdbuf ( (STREAMBUF *) this);
0015 #else
0016   original_streambuf = COUT.rdbuf ();
0017   COUT = (STREAMBUF *) this;
0018 #endif
0019 
0020   ThePort = port;
0021   TheHost = new char [ strlen(host) +1 ];
0022   strcpy (TheHost, host);
0023 
0024   p_host = gethostbyname(TheHost);
0025   COUT << p_host->h_name << ENDL;
0026 
0027   memset ( (void*) &server_addr, 0, sizeof(server_addr) );
0028   server_addr.sin_family = AF_INET;
0029 
0030   //bcopy(p_host->h_addr, &(server_addr.sin_addr.s_addr), p_host->h_length);
0031   memcpy (&(server_addr.sin_addr.s_addr), p_host->h_addr, p_host->h_length);
0032 
0033   server_addr.sin_port = htons(ThePort);
0034 
0035 }
0036 
0037 // in the destructor, we restore the original streambuf for COUT.
0038 remote_msg_buffer::~remote_msg_buffer()
0039 {
0040 #ifdef RDBUF_ACCEPTS_STREAMBUF
0041   COUT.rdbuf ( original_streambuf);
0042 #else
0043   COUT= original_streambuf;
0044 #endif
0045   delete [] TheHost;
0046 
0047 }
0048 
0049 // this is the function which is called as a result of the << std::endl 
0050 // and flush() operations
0051 #if defined(LVL2_WINNT) || defined(STREAMBUF_NEW_IOSTREAM)
0052 int remote_msg_buffer::pubsync ()
0053 #else
0054 int remote_msg_buffer::sync ()
0055 #endif
0056 { 
0057 
0058   msgProfile mp;
0059   int length, ix;
0060 
0061 
0062   char *x = format(&length, &mp);
0063   int ret = 0;
0064 
0065 #if defined(LVL2_WINNT) || defined(STREAMBUF_NEW_IOSTREAM)
0066   for (ix =0; ix < length; ix++)
0067     {
0068       original_streambuf->sputc( x[ix] );
0069     }
0070   ret = original_streambuf->pubsync();
0071 #else
0072   for (ix =0; ix < length; ix++)
0073     {
0074       original_streambuf->overflow( x[ix] );
0075     }
0076   ret = original_streambuf->sync();
0077 #endif
0078 
0079   send_string(x, length);
0080 
0081   delete [] x;
0082 
0083   pos = 0;
0084   
0085   return ret;
0086 }
0087 
0088 void remote_msg_buffer::send_string(const char *x, const int len)
0089 {
0090   
0091 
0092   if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0) ) < 0 )
0093     {
0094       return;
0095     }
0096 
0097   if ( connect(sockfd, (struct sockaddr*) &server_addr
0098            , sizeof(server_addr)) < 0 ) 
0099     {
0100       return;
0101     }
0102   writen (sockfd, x, len);
0103 
0104   close (sockfd);
0105 }
0106 
0107 int remote_msg_buffer::writen (int fd, const char *ptr, int nbytes)
0108 {
0109   int nleft, nwritten;
0110   nleft = nbytes;
0111   while ( nleft>0 )
0112     {
0113       nwritten = write (fd, ptr, nleft);
0114       if ( nwritten < 0 ) 
0115     return nwritten;
0116 
0117       nleft -= nwritten;
0118       ptr += nwritten;
0119     }
0120   return (nbytes-nleft);
0121 }