/* LICENSE:
  =========================================================================
    CMPack'04 Source Code Release for OPEN-R SDK 1.1.5-r2 for ERS7
    Copyright (C) 2004 Multirobot Lab [Project Head: Manuela Veloso]
    School of Computer Science, Carnegie Mellon University
    All rights reserved.
  ========================================================================= */

#ifndef INCLUDED_WaveServer
#define INCLUDED_WaveServer

#include <pthread.h>
#include <semaphore.h>

#include "../../agent/headers/CircBufPacket.h"
#include "../../agent/headers/WaveLAN.h"

typedef void *(*pthread_start)(void *data);

static const int InSocketIdx=0;
static const int OutSocketIdx=1;
static const int NumAcceptingSockets=2;

struct WaveServer {
  int socket_accept_fd[NumAcceptingSockets];
  int socket_read_fd[NumAcceptingSockets];
  pthread_t InputThread;
  pthread_t OutputThread;

  void init();
};

struct WaveServerConfig {
  // INPUT
  void (*packetCallback)(int robot_id,WaveServerConfig *config,RobotDataPacket *packet);
  void *(*outputProcessor)(WaveServerConfig *config); // NULL means default processor
  // only used if OutputProcessor==NULL
  // if OutputCallback==NULL, then signal on condition variable when
  //   ability to output stuff changes (not currently implemented)
  bool (*outputCallback)(WaveServerConfig *config); 
  const char **hosts;
  const int *ports;
  sem_t *RunSemaphore;
  bool *Run;
  bool runOnce;
  void *userData;
  // OUTPUT
  WaveServer server;
};

bool SendMessage(WaveServer *server,NetMsgHeader *msg);
// data should be a pointer to a WaveServerConfig structure
void *SocketServer(WaveServerConfig *data);
void *SocketServer2(WaveServerConfig *data);

#endif
