rllib  1
rleventlogserver.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                        rleventlogserver.cpp  -  description
00003                              -------------------
00004     begin                : Wed Dec 18 2002
00005     copyright            : (C) 2002 by Rainer Lehrig
00006     email                : lehrig@t-online.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This library is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as        *
00013  *   published by the Free Software Foundation                             *
00014  *                                                                         *
00015  ***************************************************************************/
00016 #include "rleventlogserver.h"
00017 #include "rlsocket.h"
00018 #include "rlcutil.h"
00019 #include "rltime.h"
00020 #include "stdio.h"
00021 #include "string.h"
00022 
00023 rlEventLogServer::rlEventLogServer(const char *file, int max)
00024 {
00025   for(int i=0; i<rlMAX_MESSAGES; i++) // init memory
00026   {
00027     sprintf(&memory[i*rlMAX_EVENT]," ");
00028   }
00029   front = -1;
00030   cnt   = 0;
00031   if(file != NULL)
00032   {
00033     filename = new char [strlen(file)+1];
00034     strcpy(filename,file);
00035   }
00036   fp = NULL;
00037   max_events = max;
00038   num_events = 0;
00039 }
00040 
00041 rlEventLogServer::~rlEventLogServer()
00042 {
00043   if(filename != NULL) delete [] filename;
00044   if(fp != NULL) fclose((FILE *) fp);
00045 }
00046 
00047 const char *rlEventLogServer::getEvent(char *buf, int *num)
00048 {
00049   char *cptr;
00050 
00051   mutex.lock();
00052   if(*num == -1 && front != -1) *num = front+1;   // read all
00053   if(*num >= rlMAX_MESSAGES) *num = -1;
00054   if(*num == front || front == -1)                // nothing to read
00055   {
00056     buf[0] = '\0';
00057     mutex.unlock();
00058     return NULL;
00059   }
00060   else                                            // read next record
00061   {
00062     (*num)++;
00063     if(*num >= rlMAX_MESSAGES) 
00064     {
00065       *num = 0;
00066       if(*num == front)
00067       {
00068         buf[0] = '\0';
00069         mutex.unlock();
00070         return NULL;
00071       }  
00072     }
00073     strcpy(buf,&memory[(*num)*rlMAX_EVENT]);
00074     cptr = strchr(buf,'\n');
00075     if(cptr != NULL) *cptr = '\0';                // delete newline
00076     mutex.unlock();
00077     if(buf[0] < ' ') return "";
00078     return buf;
00079   }
00080 }
00081 
00082 void rlEventLogServer::putEvent(const char *event)
00083 {
00084   mutex.lock();
00085   front++;
00086   if(front >= rlMAX_MESSAGES) front = 0;
00087   rlstrncpy(&memory[front*rlMAX_EVENT],event,rlMAX_EVENT-1);
00088   if(fp == NULL && filename != NULL)
00089   {
00090     char name[1024],time[50];
00091     rlTime t;
00092 
00093     t.getLocalTime();
00094     strcpy(time,t.getTimeString()); time[10] = '_'; time[13] = '_'; time[16] = '\0';
00095     strcpy(name,filename);
00096     strcat(name,time);
00097     strcat(name,".rlEventLog");
00098     //printf("openEventlog(%s)\n",name);
00099 #ifdef __VMS
00100     fp = (void *) fopen(name,"w","shr=get");
00101 #else
00102     fp = (void *) fopen(name,"w");
00103 #endif  
00104   }
00105   if(fp != NULL && filename != NULL)
00106   {
00107     fprintf((FILE *) fp,"%s",event);
00108     fflush((FILE *) fp);
00109     num_events++;
00110   }
00111   if(num_events > max_events && filename != NULL)
00112   {
00113     num_events = 0;
00114     fclose((FILE *) fp);
00115     fp = NULL;
00116   }
00117   mutex.unlock();
00118 }
00119 
00120 //------------------------------------------------------------------------------------------
00121 typedef struct
00122 {
00123   rlEventLogServerThreads *thread;
00124   int socket;
00125 }
00126 WORKER_DATA;
00127 
00128 static void *workerThread(void *arg)
00129 {
00130   char                     message[rlMAX_EVENT];
00131   int                      s,ret;
00132   WORKER_DATA             *worker_data;
00133   rlEventLogServerThreads *thread;
00134   rlSocket                *socket;
00135 
00136   THREAD_PARAM *p = (THREAD_PARAM *) arg;
00137   worker_data     = (WORKER_DATA *) p->user;
00138   s               = worker_data->socket;
00139   thread          = worker_data->thread;
00140   socket          = new rlSocket(s);
00141 
00142   while(socket->isConnected())
00143   {
00144     ret = socket->readStr(message,sizeof(message)-1);
00145     if(ret > 0) thread->event_log_server->putEvent(message);
00146   }
00147   delete socket;
00148   return NULL;
00149 }
00150 
00151 static void *rlAcceptThread(void *arg)
00152 {
00153   int port,s;
00154   rlThread worker;
00155   WORKER_DATA worker_data;
00156   rlEventLogServerThreads *thread;
00157   rlSocket *socket;
00158   THREAD_PARAM *p = (THREAD_PARAM *) arg;
00159 
00160   thread = (rlEventLogServerThreads *) p->user;
00161   port = thread->getPort();
00162   socket = new rlSocket("localhost",port,0);
00163   while(1)
00164   {
00165     s = socket->connect();
00166     if(s == -1) break;
00167     worker_data.thread = thread;
00168     worker_data.socket = s;
00169     worker.create(workerThread,&worker_data);
00170     rlsleep(100);
00171   }
00172   delete socket;
00173   return NULL;
00174 }
00175 
00176 rlEventLogServerThreads::rlEventLogServerThreads(int Port, rlEventLogServer *EventLogServer)
00177 {
00178   rlwsa();
00179   port = Port;
00180   event_log_server = EventLogServer;
00181 }
00182 
00183 rlEventLogServerThreads::~rlEventLogServerThreads()
00184 {
00185 }
00186 
00187 void rlEventLogServerThreads::start()
00188 {
00189   if(port <= 0)                return;
00190   if(port >= 256*256)          return;
00191   if(event_log_server == NULL) return;
00192   acceptThread.create(rlAcceptThread,this);
00193 }
00194 
00195 int rlEventLogServerThreads::getPort()
00196 {
00197   return port;
00198 }