rllib  1
rleventlogserver.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rleventlogserver.cpp - description
3  -------------------
4  begin : Wed Dec 18 2002
5  copyright : (C) 2002 by R. Lehrig
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #include "rlsocket.h"
17 #include "rleventlogserver.h"
18 #include "rlcutil.h"
19 #include "rltime.h"
20 #include "stdio.h"
21 #include "string.h"
22 
23 rlEventLogServer::rlEventLogServer(const char *file, int max)
24 {
25  for(int i=0; i<rlMAX_MESSAGES; i++) // init memory
26  {
27  sprintf(&memory[i*rlMAX_EVENT]," ");
28  }
29  front = -1;
30  cnt = 0;
31  if(file != NULL)
32  {
33  filename = new char [strlen(file)+1];
34  strcpy(filename,file);
35  }
36  fp = NULL;
37  max_events = max;
38  num_events = 0;
39 }
40 
42 {
43  if(filename != NULL) delete [] filename;
44  if(fp != NULL) fclose((FILE *) fp);
45 }
46 
47 const char *rlEventLogServer::getEvent(char *buf, int *num)
48 {
49  char *cptr;
50 
51  mutex.lock();
52  if(*num == -1 && front != -1) *num = front+1; // read all
53  if(*num >= rlMAX_MESSAGES) *num = -1;
54  if(*num == front || front == -1) // nothing to read
55  {
56  buf[0] = '\0';
57  mutex.unlock();
58  return NULL;
59  }
60  else // read next record
61  {
62  (*num)++;
63  if(*num >= rlMAX_MESSAGES)
64  {
65  *num = 0;
66  if(*num == front)
67  {
68  buf[0] = '\0';
69  mutex.unlock();
70  return NULL;
71  }
72  }
73  strcpy(buf,&memory[(*num)*rlMAX_EVENT]);
74  cptr = strchr(buf,'\n');
75  if(cptr != NULL) *cptr = '\0'; // delete newline
76  mutex.unlock();
77  // if(buf[0] < ' ') return "";
78  return buf;
79  }
80 }
81 
82 void rlEventLogServer::putEvent(const char *event)
83 {
84  mutex.lock();
85  front++;
86  if(front >= rlMAX_MESSAGES) front = 0;
88  if(fp == NULL && filename != NULL)
89  {
90  char name[1024],time[50];
91  rlTime t;
92 
93  t.getLocalTime();
94  strcpy(time,t.getTimeString()); time[10] = '_'; time[13] = '_'; time[16] = '\0';
95  strcpy(name,filename);
96  strcat(name,time);
97  strcat(name,".rlEventLog");
98  //printf("openEventlog(%s)\n",name);
99 #ifdef __VMS
100  fp = (void *) fopen(name,"w","shr=get");
101 #else
102  fp = (void *) fopen(name,"w");
103 #endif
104  }
105  if(fp != NULL && filename != NULL)
106  {
107  fprintf((FILE *) fp,"%s",event);
108  fflush((FILE *) fp);
109  num_events++;
110  }
111  if(num_events > max_events && filename != NULL)
112  {
113  num_events = 0;
114  fclose((FILE *) fp);
115  fp = NULL;
116  }
117  mutex.unlock();
118 }
119 
120 //------------------------------------------------------------------------------------------
121 typedef struct
122 {
124  int socket;
125 }
127 
128 static void *workerThread(void *arg)
129 {
130  char message[rlMAX_EVENT];
131  int s,ret;
132  WORKER_DATA *worker_data;
133  rlEventLogServerThreads *thread;
134  rlSocket *socket;
135 
136  THREAD_PARAM *p = (THREAD_PARAM *) arg;
137  worker_data = (WORKER_DATA *) p->user;
138  s = worker_data->socket;
139  thread = worker_data->thread;
140  socket = new rlSocket(s);
141 
142  while(socket->isConnected())
143  {
144  ret = socket->readStr(message,sizeof(message)-1);
145  if(ret > 0) thread->event_log_server->putEvent(message);
146  }
147  delete socket;
148  return NULL;
149 }
150 
151 static void *rlAcceptThread(void *arg)
152 {
153  int port,s;
154  rlThread worker;
155  WORKER_DATA worker_data;
156  rlEventLogServerThreads *thread;
157  rlSocket *socket;
158  THREAD_PARAM *p = (THREAD_PARAM *) arg;
159 
160  thread = (rlEventLogServerThreads *) p->user;
161  port = thread->getPort();
162  socket = new rlSocket("localhost",port,0);
163  while(1)
164  {
165  s = socket->connect();
166  if(s == -1) break;
167  worker_data.thread = thread;
168  worker_data.socket = s;
169  worker.create(workerThread,&worker_data);
170  rlsleep(100);
171  }
172  delete socket;
173  return NULL;
174 }
175 
177 {
178  rlwsa();
179  port = Port;
180  event_log_server = EventLogServer;
181 }
182 
184 {
185 }
186 
188 {
189  if(port <= 0) return;
190  if(port >= 256*256) return;
191  if(event_log_server == NULL) return;
193 }
194 
196 {
197  return port;
198 }
const char * getTimeString()
Definition: rltime.cpp:106
void getLocalTime()
Definition: rltime.cpp:342
void putEvent(const char *event)
rlEventLogServer(const char *filename=NULL, int max_events=10000)
#define rlMAX_EVENT
Definition: rlevent.h:26
void * user
Definition: rlthread.h:30
int lock()
Definition: rlthread.cpp:105
rlEventLogServerThreads * thread
char memory[rlMAX_MESSAGES *rlMAX_EVENT]
int create(void *(*func)(void *), void *argument)
Definition: rlthread.cpp:35
static void * workerThread(void *arg)
rlEventLogServerThreads(int port, rlEventLogServer *event_log_server)
int readStr(char *buf, int len, int timeout=0)
Definition: rlsocket.cpp:224
virtual ~rlEventLogServer()
const char * getEvent(char *buf, int *num)
char * rlstrncpy(char *dest, const char *source, int n)
Definition: rlcutil.cpp:169
static void * rlAcceptThread(void *arg)
void rlsleep(long msec)
Definition: rlwthread.cpp:396
int unlock()
Definition: rlthread.cpp:110
#define rlMAX_MESSAGES
int connect()
Definition: rlsocket.cpp:321
int isConnected()
Definition: rlsocket.cpp:559
rlDataProviderThreads * thread
rlEventLogServer * event_log_server
int rlwsa()
Definition: rlsocket.cpp:68
Definition: rltime.h:25