From 77ebaecf895b8b863e3e80382bd6ec0e18305dcd Mon Sep 17 00:00:00 2001 From: chatty Date: Tue, 27 Jul 1993 13:51:56 +0000 Subject: Incorporated changes from mbl@prl and mbl@europarc --- comm/OLD/PortServer.cc | 85 +++++++++++++++++++---------------- comm/error.cc | 118 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 155 insertions(+), 48 deletions(-) (limited to 'comm') diff --git a/comm/OLD/PortServer.cc b/comm/OLD/PortServer.cc index db4b020..3c7d0a5 100644 --- a/comm/OLD/PortServer.cc +++ b/comm/OLD/PortServer.cc @@ -24,6 +24,11 @@ #include #include +#define DEFKEY "%s:" + +// *** horrible patch +bool PortServerInquireSysError = FALSE; + /*?class UchPortServer The class \typ{UchPortServer} described here implements the communication with the port server. Both servers and clients need to use this class. @@ -51,7 +56,8 @@ UchPortServer :: UchPortServer (const char* serv, const char* host) if (port) portno = atoi (port); else - Error (ErrFatal, "getservbyname", "service not available"); + portno = 3003; +// Error (ErrFatal, "getservbyname", "service not available"); } /* open datagram socket */ @@ -121,22 +127,13 @@ UchPortServer :: Remove (const char* key, UchInetAddress& addr, int id) SendRequest (Serv, req); } -#if 0 -static jmp_buf JmpTimeOut; - -static void -Timeout (int) -{ - longjmp (JmpTimeOut, 1); -} -#else static bool expired = FALSE; static void Expire (Millisecond) { expired = TRUE; } -#endif + /*? Get a server address from the port server. @@ -152,20 +149,12 @@ This function is to be used by clients to retrieve the address of the server the UchInetAddress* UchPortServer :: Inquire (const char* key, int timeout) { +// *** horrible patch +PortServerInquireSysError = FALSE; UchPortServerReq req (PortServInquire, 0, 0, 0, MakeKey (key)); SendRequest (Serv, req); UchMsgBuffer buf (256); -#if 0 - if (setjmp (JmpTimeOut)) { - alarm (0); - Error (ErrWarn, "UchPortServer::Inquire", "timeout"); - return 0; - } - signal (SIGALRM, Timeout); - alarm (timeout); - int n = Serv->Receive (buf); - alarm (0); -#else + expired = FALSE; CcuTimer timer (timeout * 1000, Expire); int n = Serv->Receive (buf); @@ -173,9 +162,11 @@ UchPortServer :: Inquire (const char* key, int timeout) Error (ErrWarn, "UchPortServer::Inquire", "timeout"); return 0; } -#endif + if (n <= 0) { SysError (ErrWarn, "Inquire"); +// *** horrible patch +PortServerInquireSysError = TRUE; return 0; } if (! buf.Get (&req)) { @@ -215,25 +206,15 @@ UchPortServer :: Match (const char* key, PortServMatchFun f, int timeout) UchMsgBuffer buf (1024); for (;;) { buf.Flush (); -#if 0 - if (setjmp (JmpTimeOut)) { - alarm (0); - Error (ErrWarn, "UchPortServer::Match", "timeout"); - return 0; - } - signal (SIGALRM, Timeout); - alarm (timeout); - int n = Serv->Receive (buf); - alarm (0); -#else expired = FALSE; CcuTimer timer (timeout * 1000, Expire); int n = Serv->Receive (buf); + if (expired) { Error (ErrWarn, "UchPortServer::Match", "timeout"); return 0; } -#endif + if (n <= 0) { SysError (ErrWarn, "Match"); break; @@ -359,9 +340,19 @@ Be sure however that the address is an internet address (and not a Unix address void PortServerRegister (const char* service, const char* name, UchInetAddress& addr) { +#if 0 UchPortServer ps (service); +#else + UchPortServer ps ("agentman"); +#endif + char key [256]; - strcpy (key, "%s:%u:"); +#if 0 + strcpy (key, DEFKEY); +#else + strcpy (key, service); + strcpy (key, name); +#endif strcat (key, name); ps.Register (key, addr, getpid ()); } @@ -385,9 +376,18 @@ Be sure however that the address is an internet address (and not a Unix address void PortServerRemove (const char* service, const char* name, UchInetAddress& addr) { +#if 0 UchPortServer ps (service); +#else + UchPortServer ps ("agentman"); +#endif char key [256]; - strcpy (key, "%s:%u:"); +#if 0 + strcpy (key, DEFKEY); +#else + strcpy (key, service); + strcpy (key, name); +#endif strcat (key, name); ps.Remove (key, addr, getpid ()); } @@ -400,9 +400,18 @@ It connects to the port server on the given host (default is local). UchInetAddress* PortServerInquire (const char* service, const char* name, const char* host) { +#if 0 UchPortServer ps (service, (host && *host) ? host : 0); +#else + UchPortServer ps ("agentman", (host && *host) ? host : 0); +#endif char key [256]; - strcpy (key, "%s:%u:"); +#if 0 + strcpy (key, DEFKEY); +#else + strcpy (key, service); + strcpy (key, name); +#endif strcat (key, name); return ps.Inquire (key); } diff --git a/comm/error.cc b/comm/error.cc index fc02eb9..aed7c0b 100644 --- a/comm/error.cc +++ b/comm/error.cc @@ -17,7 +17,12 @@ #include #include #include +#ifdef sun +extern "C" int rename (const char*, const char*); +#endif #include +#include +#include "ccu/Time.h" /*?class UchERROR The set of global functions described here are designed to handle errors. @@ -60,8 +65,13 @@ char* ErrorTable [] = { "Subclass should implement virtual member", // ErrShouldImplement }; -static char ProgName [128], LogFile [128]; +static char ProgName [256], LogFile [256]; static bool LogOn =FALSE; +static int LogFd = -1; +static off_t LogSize = 0; +static off_t LogHalf = 0; + +#define LOG_HALF 25000 static errtype DefaultHandler (errtype how, const char* /*who*/, const char* /*what*/, const char* msg) @@ -82,7 +92,7 @@ HandleError (errtype how, const char* who, const char* what) char *msg = MakeErrorString (how, who, what); - if (how >= ErrLog && LogOn) { + if (how >= ErrLog) { LogMessage (msg); if (how == ErrLog) return TRUE; @@ -133,20 +143,77 @@ ProgramName (const char* name) /*? Set the log file name. All messages are appended to the logfile. -If \var{reset} is TRUE, the file is cleared. -NOTE : logging is not currently implemented. +If \var{reset} is TRUE, the file is renamed to \var{file\~}. +If this fails, the file is simply cleared. +If the \com{LOGDIR} environment variable is defined, and if \var{file} +does not start with \com{/} or \com{./}, the value of \com{LOGDIR} is +prepended to \var{file}, followed by a slash if necessary. +Otherwise, the directory \com{$HOME/.log} is used. ?*/ void LogfileName (const char* file, bool reset) { if (file) { - strncpy (LogFile, file, sizeof (LogFile) -1); + if (LogOn && LogFd >= 0) + close (LogFd); + if (*file == '/' || strncmp (file, "./", 2) == 0) { + LogFile [0] = '\0'; + } else { + // try $LOGDIR + char* dir = getenv ("LOGDIR"); + if (dir) { + strcpy (LogFile, dir); + } else { + // try $HOME/.log + char* home = getenv ("HOME"); + if (home) { + strcpy (LogFile, home); + strcat (LogFile, "/.log"); + if (access (LogFile, F_OK) != 0) { + // try to create the directory + if (mkdir (LogFile, 0755) != 0) { + Error (ErrWarn, "LogFileName", "logging to /tmp directory"); + strcpy (LogFile, "/tmp"); + } + } + } + } + if (LogFile [strlen (LogFile) - 1] != '/') + strcat (LogFile, "/"); + } + + strcat (LogFile, file); LogOn = TRUE; + int flags = O_RDWR | O_CREAT; if (reset) { - // to do: clear logfile + flags |= O_TRUNC; + // try to save previous version in LogFile~ + if (access (LogFile, F_OK) == 0) { + char old [256]; + strcpy (old, LogFile); + strcat (old, "~"); + rename (LogFile, old); + } + } + LogFd = open (LogFile, flags, 0644); + if (LogFd < 0) { + SysError (ErrWarn, "LogfileName"); + LogOn = FALSE; + } else { + LogHalf = 0; + if (reset) + LogSize = 0; + else { + LogSize = lseek (LogFd, 0L, 2 /*SEEK_END*/); + LogSize += write (LogFd, "\n", 1); + } + LogMessage ("-------- logging started --------\n"); } - } else + } else { + if (LogOn && LogFd >= 0) + close (LogFd); LogOn = FALSE; + } } /*?nodoc?*/ @@ -158,12 +225,43 @@ CleanUp (CleanUpProc) /*? Append a message to the logfile, if one has been specified. -NOTE : logging is not currently implemented. ?*/ void -LogMessage (const char*) +LogMessage (const char* msg) { - // to be done + if (! LogOn || LogFd < 0) + return; + + CcuTimeStamp now; + // format of time: Sun Sep 16 01:03:52 1973\n\0 + // 123456789012345678901234 + LogSize += write (LogFd, now.AsString (), 20); // strip year + LogSize += write (LogFd, msg, strlen (msg)); + + // limit the size of the log file + if (LogSize > LOG_HALF && LogHalf == 0) + LogHalf = LogSize; + + if (LogSize < 2 * LOG_HALF) + return; + + // copy bytes from LogHalf to LogSize to beginning of file + off_t src, dst; + int n; + char buf [4096]; + lseek (LogFd, 0L, 0 /*SEEK_SET*/); + write (LogFd, "...\n", 4); + for (src = LogHalf, dst = 4; src < LogSize; src += n, dst += n) { + lseek (LogFd, src, 0 /*SEEK_SET*/); + n = read (LogFd, buf, sizeof (buf)); + if (n <= 0) + break; + lseek (LogFd, dst, 0 /*SEEK_SET*/); + write (LogFd, buf, n); + } + lseek (LogFd, dst, 0 /*SEEK_SET*/); +////// ftruncate (LogFd, dst); /////// this seems not to be a POSIX call + LogHalf = LogSize = dst; } /*? -- cgit v1.1