Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

communication.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2001, John Lambert jlambert@jlambert.com
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without modification, are 
00006 permitted provided that the following conditions are met:
00007 
00008 Redistributions of source code must retain the above copyright notice, this list of 
00009 conditions and the following disclaimer.
00010 
00011 Redistributions in binary form must reproduce the above copyright notice, this list
00012 of conditions and the following disclaimer in the documentation and/or other
00013 materials provided with the distribution. 
00014 
00015 Neither the name of the xdProf project nor the names of its contributors may be used
00016 to endorse or promote products derived from this software without specific prior 
00017 written permission. 
00018 
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
00020 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00021 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00022 SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00023 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00024 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
00025 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00026 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
00027 WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 /*
00030 $Header: /xdprof-dll/communication.cpp 13    5/09/01 5:29a Admin $ 
00031  */
00032 #include "xdprof.h"
00033 
00037 
00038 
00042 static int Server;
00043 
00045 static int BytesSent = 0;
00046 
00048 static int SendCalls = 0;
00049 
00055 int Send(const char* msg, int len)
00056 {
00057     if (Server > 0)
00058     {
00059         BytesSent += len;
00060         ++SendCalls;
00061         return send(Server, msg, len, 0); 
00062     }
00063     else
00064         return 0;
00065 }
00066 
00071 void Send(const string & s)
00072 {
00073     Send(s.c_str(), s.length() );
00074 }
00075 
00079 bool IsConnected()
00080 {
00081     return (Server > 0);
00082 }
00083 
00091 static int GetSocketDescriptor(const char *hostname, unsigned short port)
00092 {
00093     struct hostent *hentry;
00094     struct sockaddr_in s;
00095     int fd;
00096     
00097     if (port <= 0 || port > 65535) 
00098     {
00099         fprintf(stderr, "DPROF ERROR: bad port number\n");
00100         return -1;
00101     }
00102     
00103     if (hostname == NULL) 
00104     {
00105         fprintf(stderr, "DPROF ERROR: hostname is NULL\n");
00106         return -1;
00107     }
00108     
00109     /* create a socket */
00110     fd = socket(AF_INET, SOCK_STREAM, 0);
00111     
00112     /* find remote host's addr from name */
00113     if ((hentry = gethostbyname(hostname)) == NULL)
00114     {
00115 #ifdef WIN32
00116         debug("Bad gethostbyname value.", WSAGetLastError());
00117 #endif
00118         return -1;
00119     }
00120     
00121     memset((char *)&s, 0, sizeof(s));
00122     /* set remote host's addr; its already in network byte order */
00123     memcpy(&s.sin_addr.s_addr, *(hentry->h_addr_list), 
00124         sizeof(s.sin_addr.s_addr));
00125     /* set remote host's port */
00126     s.sin_port = htons(port);
00127     s.sin_family = AF_INET;
00128     
00129     /* now try connecting */
00130     if (-1 == connect(fd, (struct sockaddr*)&s, sizeof(s))) 
00131     {
00132         debug("Unable to connect!");
00133         return -1;
00134     }
00135     else 
00136     {
00137         return fd;
00138     }
00139     
00140 }
00141 
00148 bool InitializeCommunications(string hostname, int port)
00149 {
00150 #ifdef WIN32    
00151     WORD wVersionRequested;
00152     WSADATA wsaData;
00153     int err;
00154  
00155     wVersionRequested = MAKEWORD( 1, 1 );
00156  
00157     err = WSAStartup( wVersionRequested, &wsaData );
00158     if ( err != 0 ) {
00159         debug("WSA error", WSAGetLastError());
00160         Server = -1;
00161         return false;
00162     }
00163 #endif
00164 
00165     Server = GetSocketDescriptor(hostname.c_str(), static_cast<unsigned short>(port));
00166     if (IsConnected())
00167         debug("connected!");
00168     else
00169         debug("couldn't connect");
00170     return IsConnected();
00171 }
00172 
00173 static int PacketCount = 0;
00174 static unsigned long int PacketBytes = 0;
00175 
00176 void RecordPacket(unsigned long packetsize)
00177 {
00178     ++PacketCount;
00179     PacketBytes += packetsize;
00180 }
00181 
00185 void ShutdownCommunications()
00186 {
00187     Send("/*done*/");
00188     unsafe_debug("Bytes sent:", BytesSent);
00189     unsafe_debug("SendCalls: ", SendCalls);
00190     //fprintf(stderr, "bytes=%d calls=%d\n", BytesSent, SendCalls);
00191     fprintf(stdout, "\npackets\tbytes\trefresh\n");
00192     //fprintf(stderr, "packets\t%d \nbytes\t%d\nrefresh\t%d\n", PacketCount, PacketBytes,RefreshTime);
00193     fprintf(stdout, "%d\t%d\t%d\n", PacketCount, PacketBytes,RefreshTime);
00194 
00195 fprintf(stdout, "Methods=%d, Classes=%d, MethodCache=%d,ClassCache=%d\n", Methods.size(), Classes.size(), MethodCache.size(), ClassCache.size());
00196 
00197     fflush(stdout);
00198     unsafe_debug("Average: ", BytesSent / static_cast<double>(SendCalls));
00199     if (Server > 0)
00200         closesocket(Server);
00201     Server = -1;
00202 #ifdef WIN32
00203     WSACleanup();
00204 #endif
00205 }

Generated at Sun Jun 24 20:57:14 2001 for xdprof by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001