/**************************** CPPHeaderFile ************************************

* FileName [Blade.h]

* PackageName [blade]

* Synopsis [Header file for Blade and BladeParams classes.]

* Description [This file contains the declarations for the classes
* Blade and BladeParams. The Blade class encapsulates the broker. The
* BladeParams class encapsulates the parameters of the broker.]

* SeeAlso [BladeParams.cpp Blade.cpp]

* Author [Sagar Chaki]

* Copyright [ Copyright (c) 2001 by Carnegie Mellon University.  All
* Rights Reserved. This software is for educational purposes only.
* Permission is given to academic institutions to use, copy, and
* modify this software and its documentation provided that this
* introductory message is not removed, that this software and its
* documentation is used for the institutions' internal research and
* educational purposes, and that no monies are exchanged. No guarantee
* is expressed or implied by the distribution of this code. Send
* bug-reports and/or questions to: chaki+@cs.cmu.edu. ]

******************************************************************************/

#ifndef __BLADE_H__
#define __BLADE_H__

#include <string>
using namespace std;

//other classes needed
class BladeDatabase;
class BladeFrontend ;
class BladeBackend ;
class BladeNotifier;

//the type of the return value and argument of the routine at which a
//new thread starts
#ifdef BLADE_OS_LINUX
typedef void *threadStartRoutineRetType;
typedef void *threadStartRoutineArgType;
#endif
#ifdef BLADE_OS_WIN32
typedef DWORD threadStartRoutineRetType;
typedef LPVOID threadStartRoutineArgType;
#endif

//the type of the routine at which a new thread starts
typedef threadStartRoutineRetType (*threadStartRoutineType)(threadStartRoutineArgType);

/**************************************************************/
//the class encapsulating parameters to the broker
/**************************************************************/

class BladeParams
{
 private:
  //the matching semantics
  string matchSem;

  //the default integer value and flag indicating if it is valid
  int defaultInt;

  //the default double value and flag indicating if it is valid
  double defaultDbl;

  //the default string value and flag indicating if it is valid
  string defaultStr;

  //subscription update parameters
  int subUpdateInterval;
  int minSubUpdate;
  int minUnsubUpdate;

  //the flag indicating whether notifications must be generated
  bool notify;

  //the port on which the broker listens
  unsigned short port;
  
  //the backlog of the broker server
  int backlog;

  //maximum lengths of various queues
  int maxPubQLen;
  int maxSubQLen;
  int maxUnsubQLen;
  int maxNotQLen;
  int maxNotTQLen;

  //the file in which current subscriptions are stored
  string currSubFile;

  //the number of threads in the notifier thread pool
  int notThreadNum;

 public:
  BladeParams();
  BladeParams(const BladeParams &rhs) { *this = rhs; }

  BladeParams &operator = (const BladeParams &rhs);

  void SetMatchSem(const string m) { matchSem = m; }

  void SetDefaultInt(const int d) { defaultInt = d; }
  void SetDefaultDbl(const double d) { defaultDbl = d; }
  void SetDefaultStr(const string d) { defaultStr = d; }

  //set subscription update parameters
  void SetSubUpdateInterval(const int s);
  void SetMinSubUpdate(const int m);
  void SetMinUnsubUpdate(const int m);

  void SetNotify(const bool n) { notify = n; }

  void SetPort(const unsigned short p) { port = p; }
  void SetBacklog(const int b);

  void SetMaxPubQLen(const int m);
  void SetMaxSubQLen(const int m);
  void SetMaxUnsubQLen(const int m);
  void SetMaxNotQLen(const int m);
  void SetMaxNotTQLen(const int m);

  void SetCurrSubFile(const string s) { currSubFile = s; }

  void SetNotThreadNum(const int n);

  string GetMatchSem() const { return matchSem; }

  int GetDefaultInt() const { return defaultInt; }
  double GetDefaultDbl() const { return defaultDbl; }
  string GetDefaultStr() const { return defaultStr; }

  //get subscription update parameters
  int GetSubUpdateInterval() const { return subUpdateInterval; }
  int GetMinSubUpdate() const { return minSubUpdate; }
  int GetMinUnsubUpdate() const { return minUnsubUpdate; }

  bool GetNotify() const { return notify; }

  unsigned short GetPort() const { return port; }
  int GetBacklog() const { return backlog; }

  int GetMaxPubQLen() const { return maxPubQLen; }
  int GetMaxSubQLen() const { return maxSubQLen; }
  int GetMaxUnsubQLen() const { return maxUnsubQLen; }
  int GetMaxNotQLen() const { return maxNotQLen; }
  int GetMaxNotTQLen() const { return maxNotTQLen; }

  string GetCurrSubFile() const { return currSubFile; }

  int GetNotThreadNum() const { return notThreadNum; }
};

/**************************************************************/
//the class encapsulating the actual broker
/**************************************************************/

class Blade
{
 private:
  //the port and backlog of server socket
  unsigned short port;
  int backlog;

  //the components
  BladeDatabase *database;
  BladeFrontend *frontend;
  BladeBackend *backend;
  BladeNotifier *notifier;
  
  /**************************************************************/
  //private methods
  /**************************************************************/

  //start the various components
  void StartFrontend();
  void StartBackend();
  void StartNotifier();
  
  //spawn a new thread with specified procedure and argument
  int SpawnThread(threadStartRoutineType startRoutine,threadStartRoutineArgType arg);

 public:
  Blade();
  Blade(BladeParams &params);
  ~Blade();
  void Start();
};

#endif

/**************************************************************/
//end of Blade.h
/**************************************************************/

