Newer
Older
tuved / main.c
@reddawg reddawg on 18 Nov 2007 2 KB sGetCon error?
/*
  Tuved Server

  $Id$
*/

#include <stdio.h>
#include <stdarg.h>
#include <sys/select.h>
#include <time.h>

#include "tuved.h"

FILE *logFile = 0x0;
int logLevel = 1;
pthread_mutex_t chanMutex = PTHREAD_MUTEX_INITIALIZER;
time_t startTime;
int userCount = 0;
int channelCount = 0;
int songCount = 0;
int playCount = 0;

void usage();

int main(int argc,char **argv) {
  fd_set readset;
  int ch;
  int readSocks = 0x0;
  int doFork    = 0x1;
  int rc = 0x0;
  int t = 0x0;
  time_t seconds = 0;
  struct timeval timeout = {30,0};

  /* Get our start time */
  startTime = time(NULL);

  while ((ch = getopt(argc,argv,"sl:nd:")) != -1) {
    switch (ch) {
      case 's':
        logFile = stdout;
        break;
      case 'l':
        logFile = fopen(optarg,"a");
        break;
      case 'n':
        doFork = 0;
        break;
      case 'd':
        logLevel = atoi(optarg);
        if (logLevel < 1)
          logLevel = 1;
        break;
      default:
        usage();
      }
    }
  argc -= optind;
  argv += optind;


  /* Prepare log file */
  if (logFile == 0x0)
    logFile = fopen(LOG_FILE,"a");

  /* Fork into background unless specified not to do so */
  if (doFork == 1)
    if (fork() != 0x0)
      exit(1);

  writeLog(0,"Podz Daemon Starting: %i\n",startTime);

  dbInit();

  srandom(time(NULL));

  pthread_mutex_init(&chanMutex, NULL);
  
  pthread_t threads[NUM_THREADS];
  rc = pthread_create(&threads[t], NULL, tuveBotThread, (void *)t);
 


  sStartListener();

  while (1) {
  	if (sGetConnections(&readset) == 0x0) {

  	  writeLog(0,"Error: sGetConnections Failed\nShuttown Down\n");
  	  exit(0x0);
  	  }

    readSocks = select(highSock+1,&readset,0x0,0x0,&timeout);

    if (readSocks < 0) {
      perror("select failed");
      exit(0x1);
      }
    else if (readSocks > 0) {
      sProcessConnections(&readset);
      }

    if ((seconds + PING_INTERVAL) < time(NULL)) {
      sSendPing(seconds);
      seconds = time(NULL);
      }
      
    /* Clean up old connections */
    sCleanConnections();
    
    /* Flush the log file */
    fflush(logFile);
    }
    
  return(0);
  }

void usage() {
  printf("%s\n","usage: tuved -n -s [-l logfile] [-d debuglevel]");
  exit(1);
  }


int writeLog(int level,char const * __restrict fmt, ...) {
  int ret;
  va_list ap;

  if (level > logLevel)
    return(0x0);

  va_start(ap, fmt);
  ret = vfprintf(logFile, fmt, ap);
  va_end(ap);
  
  return(ret);
  } /* End writeLog(); */