-const char stats_rcs[] = "$Id: stats.c,v 2.3 2002/07/18 22:06:12 jongfoster Exp $";
+const char stats_rcs[] = "$Id: stats.c,v 2.4 2003/01/06 02:03:13 david__schmidt Exp $";
/*********************************************************************
*
- * File : $Source: /cvsroot/ijbswa/current/src/jcc.c,v $
+ * File : $Source: /cvsroot/ijbswa/current/src/stats.c,v $
*
- * Purpose :
- *
+ * Purpose : Functions and definitions for accumulating and
+ * sending statistics to an "external" stats console
*
* Copyright : Written by and Copyright (C) 2002, 2003 the SourceForge
* Privoxy team. http://www.privoxy.org/
* or write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
+ * Revisions :
+ * $Log: stats.c,v $
+ * Revision 2.4 2003/01/06 02:03:13 david__schmidt
+ * Update stats protocol now that the console is actually running
+ *
+ *
*********************************************************************/
\f
+#include <string.h>
#ifdef unix
#include <sys/signal.h>
+#include <unistd.h>
+#include <pthread.h>
#endif
#include "project.h"
#include "errlog.h"
+#include "miscutil.h"
#include "stats.h"
#include "ipc.h"
+#include "jbsockets.h"
const char stats_h_rcs[] = STATS_H_VERSION;
const char ipc_h_rcs[] = IPC_H_VERSION;
static IPC_MUTEX_LOCK stats_lock;
-struct configuration_spec *latest_config;
-int changed = 0,
- stats_array[STATS_MAX_KEYS];
+stats_struct *main_stats;
/*********************************************************************
*
*********************************************************************/
void init_stats_config(struct configuration_spec * config)
{
- int i;
-#if defined (_WIN32) || defined (__OS2__)
- int child_id;
-#else
+ int i, child_id;
+#ifdef unix
pthread_attr_t attr;
pthread_t thread;
#endif /* def unix */
- log_error(LOG_LEVEL_INFO, "init_stats_config hit.");
+ main_stats = zalloc(sizeof(stats_struct));
IPC_CREATE_MUTEX(stats_lock);
for (i=0; i < STATS_MAX_KEYS; i++)
{
- stats_array[i] = 0;
+ main_stats->stats_array[i] = 0;
}
- latest_config = config;
+ main_stats->config = config;
+
+ accumulate_stats(STATS_PRIVOXY_PORT, config->hport);
/*
- * Start the timing/sending thread - we'll need a lot of work here
- * for each platform. I imagine there is also a possibility of
- * doing this via fork() instead of threads.
+ * Start the timing/sending thread - I stole this from jcc.c.
+ * The idea is to get a mutiplatform thread started.
+ * YMMV - please tweak for your platform!
+ */
+
+/* this is a switch () statment in the C preprocessor - ugh */
+#undef SELECTED_ONE_OPTION
+
+/* Use pthreads in preference to any native code */
+#if defined(FEATURE_PTHREAD) && !defined(SELECTED_ONE_OPTION)
+#define SELECTED_ONE_OPTION
+ signal(SIGALRM, null_routine); /* Ignore the SIGALRM signal */
+ pthread_attr_init(&attr);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ child_id = (pthread_create(&thread, &attr,
+ (void*)forward_stats, main_stats) ? -1 : 0);
+ pthread_attr_destroy(&attr);
+#endif
+
+#if defined(_WIN32) && !defined(_CYGWIN) && !defined(SELECTED_ONE_OPTION)
+#define SELECTED_ONE_OPTION
+ child_id = _beginthread(
+ (void (*)(void *))forward_stats,
+ 64 * 1024,
+ main_stats);
+#endif
+
+#if defined(__OS2__) && !defined(SELECTED_ONE_OPTION)
+#define SELECTED_ONE_OPTION
+ child_id = _beginthread(
+ (void(* _Optlink)(void*))forward_stats,
+ NULL,
+ 64 * 1024,
+ main_stats);
+#endif
+
+#if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
+#define SELECTED_ONE_OPTION
+ thread_id tid = spawn_thread
+ (server_thread, "forward_stats", B_NORMAL_PRIORITY, NULL);
+ if ((tid >= 0) && (resume_thread(tid) == B_OK))
+ {
+ child_id = (int) tid;
+ }
+ else
+ {
+ child_id = -1;
+ }
+#endif
+
+#if defined(AMIGA) && !defined(SELECTED_ONE_OPTION)
+#define SELECTED_ONE_OPTION
+ if((child_id = (int)CreateNewProcTags(
+ NP_Entry, (ULONG)server_thread,
+ NP_Output, Output(),
+ NP_CloseOutput, FALSE,
+ NP_Name, (ULONG)"privoxy child",
+ NP_StackSize, 200*1024,
+ TAG_DONE)))
+ {
+ childs++;
+ Signal((struct Task *)child_id, SIGF_SINGLE);
+ Wait(SIGF_SINGLE);
+ }
+#endif
+
+#if !defined(SELECTED_ONE_OPTION)
+ /* I don't think the IPC will really work in a fork()'d environment,
+ * so proceed with caution. FIXME.
*/
+#error FIXME - stats will not work without pthreads!
+ child_id = fork();
-#ifdef _WIN32
- child_id = _beginthread(
- (void (*)(void *))forward_stats,
- 64 * 1024,
- NULL);
-#elif __OS2__
- child_id = _beginthread(
- (void(* _Optlink)(void*))forward_stats,
- NULL,
- 64 * 1024,
- NULL);
-#else
- /* Generic unix processing */
- signal(SIGALRM, null_routine); /* Ignore the SIGALRM signal */
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
- pthread_create(&thread, &attr, forward_stats, NULL);
+ if (child_id == 0) /* child */
+ {
+ forward_stats(main_stats);
+ _exit(0);
+ }
+ else if (child_id > 0) /* parent */
+ {
+ }
#endif
+#undef SELECTED_ONE_OPTION
+/* end of c preprocessor switch () */
+
}
/*********************************************************************
*********************************************************************/
void update_stats_config(struct configuration_spec * config)
{
- latest_config = config;
+ main_stats->config = config;
}
/*********************************************************************
if (key < STATS_MAX_KEYS)
{
IPC_LOCK_MUTEX(stats_lock);
- stats_array[key] += value;
- changed = 1;
+ main_stats->stats_array[key] += value;
+ main_stats->changed = 1;
IPC_UNLOCK_MUTEX(stats_lock);
}
- /* log_error(LOG_LEVEL_INFO, "Accumulate stats: key %d, value %d, total %d; send to: %s:%d", key, value, stats_array[key], latest_config->activity_address,latest_config->activity_port); */
}
/*********************************************************************
* Returns : N/A
*
*********************************************************************/
-void *forward_stats()
+void *forward_stats(stats_struct *pstats)
{
int local_stats_array[STATS_MAX_KEYS];
-
- log_error(LOG_LEVEL_INFO, "forward_stats ready.");
+
for (;;)
{
- IPC_SLEEP_SECONDS(latest_config->activity_freq);
- if (changed == 1)
+ IPC_SLEEP_SECONDS(pstats->config->activity_freq);
+ if (pstats->changed == 1)
{
IPC_LOCK_MUTEX(stats_lock);
- memcpy(local_stats_array,stats_array,sizeof(stats_array));
- changed = 0;
+ memcpy(local_stats_array,pstats->stats_array,sizeof(pstats->stats_array));
+ pstats->changed = 0;
IPC_UNLOCK_MUTEX(stats_lock);
- send_stats(&local_stats_array);
+ send_stats(local_stats_array);
}
}
}
*
* Function : send_stats
*
- * Description : Attempt to send statistics to the listening console
+ * Description : Attempt to send statistics to the listening console.
+ * Stats are formatted as a clear-text string for now -
+ * no need for any encoding fanciness just yet.
*
- * Parameters : N/A
+ * Parameters :
+ * 1 : local_stats_array, a pointer to a local copy of the
+ * statistics array.
*
* Returns : N/A
*
*********************************************************************/
-void send_stats(int *local_stats_array[])
+void send_stats(int local_stats_array[])
{
+ jb_socket sk;
+ char *msg = NULL, tmp_msg[64];
+ int i;
+
/* Here, we initiate the socket send to the console */
- /*
- log_error(LOG_LEVEL_INFO, "send_stats sending stats: %d %d %d %d %d %d %d %d %d %d",
- local_stats_array[0],local_stats_array[1],local_stats_array[2],local_stats_array[3],local_stats_array[4],local_stats_array[5],local_stats_array[6],local_stats_array[7],local_stats_array[8],local_stats_array[9]);
- */
+ sk = connect_to(main_stats->config->activity_address,main_stats->config->activity_port,NULL);
+ if (sk > 0)
+ {
+ /* max size of a key looks like this: xxxxx:xxxxxb */
+ msg = zalloc(
+ STATS_MAX_KEYS * 64 /* Space for keys - much bigger than necessary for safety */
+ );
+ if (msg)
+ {
+ for (i = 0; i < STATS_MAX_KEYS; i++)
+ {
+ sprintf(tmp_msg,"%d:%d ",i,local_stats_array[i]);
+ strcat(msg,tmp_msg);
+ }
+ write_socket(sk,msg,strlen(msg));
+ freez(msg);
+ }
+ close_socket(sk);
+ }
}
/*********************************************************************