Jpp  pmt_effective_area_update_2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Namespaces | Typedefs | Functions
JLigier.cc File Reference

ControlHost server. More...

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <deque>
#include <set>
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"
#include "JLang/JMemory.hh"
#include "JLang/JSharedCounter.hh"
#include "JNet/JSocket.hh"
#include "JNet/JSocketChannel.hh"
#include "JNet/JServerSocket.hh"
#include "JNet/JSelect.hh"
#include "JNet/JControlHost.hh"
#include "JSystem/JSystemToolkit.hh"
#include "JMath/JConstants.hh"

Go to the source code of this file.

Classes

class  JNET::JMemory_t< T >
 
class  JNET::JDispatch
 Data structure of a ControlHost message. More...
 
class  JNET::JClient
 ControlHost client manager. More...
 
class  JNET::JClientList
 List of ControlHost client managers. More...
 

Namespaces

 JNET
 Interprocess communication.
 

Typedefs

typedef JPrefix JNET::JPrefix_t
 
typedef JSocketInputChannel
< JPrefix_t > 
JNET::JSocketInputChannel_t
 

Functions

int JNET::getSizeOfPacket (const JPrefix_t &prefix)
 Get total size of internet packet. More...
 
void JNET::setSizeOfPacket (const int size, JPrefix_t &prefix)
 Set total size of internet packet. More...
 
std::ostream & JNET::operator<< (std::ostream &out, const JDispatch &message)
 Print message. More...
 
std::ostream & JNET::operator<< (std::ostream &out, const JSocket &socket)
 Print socket. More...
 
std::ostream & JNET::operator<< (std::ostream &out, const JSocketStatus &status)
 Print socket status. More...
 
std::ostream & JNET::operator<< (std::ostream &out, const JSocketInputBuffer &buffer)
 Print socket input buffer. More...
 
int main (int argc, char *argv[])
 

Detailed Description

ControlHost server.

Author
mdejong

Definition in file JLigier.cc.

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 579 of file JLigier.cc.

580 {
581  using namespace std;
582  using namespace JPP;
583 
584  int port;
585  int backlog;
586  int timeout_us;
587  int buffer_size;
588  int debug;
589 
590  try {
591 
592  JParser<> zap("ControlHost server.");
593 
594  zap['P'] = make_field(port) = DISPATCH_PORT;
595  zap['q'] = make_field(backlog) = 1024;
596  zap['T'] = make_field(timeout_us) = 1;
597  zap['s'] = make_field(buffer_size) = GIGABYTE;
598  zap['Q'] = make_field(JClient::QUEUE_LIMIT) = 100;
599  zap['M'] = make_field(JDispatch::MEMORY_LIMIT) = (JSYSTEM::getRAM() >> 1);
600  zap['d'] = make_field(debug) = 0;
601 
602  zap(argc, argv);
603  }
604  catch(const exception &error) {
605  FATAL(error.what() << endl);
606  }
607 
608 
609  JServerSocket server(port, backlog);
610  JSelect select;
611  JClientList clientList;
612 
613 
614  DEBUG("Port " << setw(10) << port << endl);
615  DEBUG("Memory limit " << setw(10) << JDispatch::MEMORY_LIMIT << endl);
616  DEBUG("Queue limit " << setw(10) << JClient::QUEUE_LIMIT << endl);
617 
618 
619  for ( ; ; ) {
620 
621  select.reset();
622  select.setReaderMask(server);
623 
624  for (JClientList::iterator client = clientList.begin(); client != clientList.end(); ++client) {
625 
626  if (!client->in.isReady()) {
627  select.setReaderMask(*client);
628  }
629 
630  if (client->out.isReset()) {
631 
632  if (!client->queue.empty()) {
633 
634  DEBUG("Client" << *client << ".set" << client->queue.front() << endl);
635 
636  client->out.set(client->queue.front());
637  client->decrementRequest();
638 
639  select.setWriterMask(*client);
640  }
641 
642  } else if (client->out.isBusy()) {
643 
644  select.setWriterMask(*client);
645  }
646  }
647 
648  if (select(timeout_us) > 0) {
649 
650  for (JClientList::iterator client = clientList.begin(); client != clientList.end(); ) {
651 
652  try {
653 
654  if (select.hasReaderMask(*client)) {
655 
656  try {
657  client->in.read();
658  }
659  catch(const exception& error) {
660 
661  ERROR("Remove (3) client" << *client << "<" << client->getNickname() << ">: " << error.what() << endl);
662 
663  if (client->getNickname() != "") {
664  clientList.add(JDispatch(DISPTAG_Died, client->getNickname()));
665  }
666 
667  client->shutdown();
668 
669  client = clientList.erase(client);
670 
671  continue;
672  }
673 
674  DEBUG("Client" << *client << ".read" << static_cast<const JSocketInputBuffer&>(client->in) << endl);
675  }
676 
677  if (client->in.isReady()) {
678 
679  DEBUG("Message" << *client << ' ' << client->in.prefix.c_str() << ' ' << client->in.size() << endl);
680 
681  bool special = JControlHost::maybe_special(client->in.prefix);
682 
683  if (special) {
684 
685  client->in.seekg(sizeof(JPrefix_t)); // skip prefix
686 
687  if (client->in.prefix.getTag() == DISPTAG_Subscribe) {
688 
689  client->setSubscription(string(client->in.getRemainingData(), client->in.getRemainingSize()));
690 
691  } else if (client->in.prefix.getTag() == DISPTAG_MyId) {
692 
693  client->setNickname(string(client->in.getRemainingData(), client->in.getRemainingSize()));
694 
695  clientList.add(JDispatch(DISPTAG_Born, client->getNickname()));
696 
697  } else if (client->in.prefix.getTag() == DISPTAG_Gime) {
698 
699  client->incrementRequest();
700 
701  } else if (client->in.prefix.getTag() == DISPTAG_Always) {
702 
703  client->setRequestAll();
704 
705  } else if (client->in.prefix.getTag() == DISPTAG_WhereIs) {
706 
707  string nick_name(client->in.getRemainingData(), client->in.getRemainingSize());
708  string buffer;
709 
710  for (JClientList::iterator i = clientList.begin(); i != clientList.end(); ++i) {
711  if (i->getNickname() == nick_name) {
712  buffer += " " + i->getHostname();
713  }
714  }
715 
716  JControlHost socket(*client);
717 
718  socket.PutFullString(DISPTAG_WhereIs, buffer.substr(buffer.empty() ? 0 : 1));
719 
720  DEBUG("Remove (1) client" << *client << endl);
721 
722  client->shutdown();
723 
724  client = clientList.erase(client);
725 
726  continue; // skip any action
727 
728  } else if (client->in.prefix.getTag() == DISPTAG_ShowStat) {
729 
730  client->shutdown();
731 
732  client = clientList.erase(client);
733 
734  for (JClientList::iterator i = clientList.begin(); i != clientList.end(); ++i) {
735 
736  int total = 0;
737 
738  for (std::deque<JDispatch>::const_iterator message = i->queue.begin(); message != i->queue.end(); ++message) {
739  total += message->size();
740  }
741 
742  cout << "client[" << i->getFileDescriptor() << "] " << i->getNickname() << endl;
743  cout << "tag - all:";
744  for (std::set<JTag>::const_iterator tag = i->getSubscriptionAll().begin(); tag != i->getSubscriptionAll().end(); ++tag) {
745  cout << ' ' << *tag;
746  }
747  cout << endl;
748  cout << "tag - any:";
749  for (std::set<JTag>::const_iterator tag = i->getSubscriptionAny().begin(); tag != i->getSubscriptionAny().end(); ++tag) {
750  cout << ' ' << *tag;
751  }
752  cout << endl;
753  cout << "queue " << i->queue.size() << ' ' << total << "B" << endl;
754  }
755 
756  continue; // skip any action
757 
758  } else if (client->in.prefix.getTag() == DISPTAG_Debug) {
759 
760  istringstream is(string(client->in.getRemainingData(), client->in.getRemainingSize()));
761 
762  is >> debug;
763 
764  } else {
765 
766  special = false; // not a reserved tag.
767  }
768  }
769 
770  if (!special) {
771 
772  clientList.add(JDispatch(client->in.prefix, client->in.data()));
773 
774  if (JDispatch::MEMORY_TOTAL > JDispatch::MEMORY_LIMIT) {
775 
776  WARNING("Memory " << setw(10) << JDispatch::MEMORY_TOTAL << " > " << setw(10) << JDispatch::MEMORY_LIMIT << endl);
777 
778  clientList.drop();
779  }
780  }
781 
782  client->in.reset();
783  }
784 
785  if (select.hasWriterMask(*client)) {
786 
787  client->out.write();
788 
789  DEBUG("Client" << *client << ".write" << static_cast<const JSocketStatus&>(client->out) << endl);
790 
791  if (client->out.isReady()) {
792  client->out.reset();
793  client->queue.pop_front();
794  }
795  }
796 
797  ++client;
798  }
799  catch(const exception& error) {
800 
801  DEBUG("Remove (2) client" << *client << "<" << client->getNickname() << ">: " << error.what() << endl);
802 
803  if (client->getNickname() != "") {
804  clientList.add(JDispatch(DISPTAG_Died, client->getNickname()));
805  }
806 
807  client->shutdown();
808 
809  client = clientList.erase(client);
810  }
811  }
812 
813  if (select.hasReaderMask(server)) {
814 
815  JSocket socket;
816 
817  socket.accept(server.getFileDescriptor());
818 
819  socket.setSendBufferSize (buffer_size);
820  socket.setReceiveBufferSize(buffer_size);
821 
822  socket.setKeepAlive (true);
823  socket.setNonBlocking(true);
824 
825  DEBUG("New client" << socket << endl);
826 
827  clientList.push_back(JClient(socket));
828  }
829  }
830  }
831 }
Utility class to parse command line options.
Definition: JParser.hh:1500
static const JTag DISPTAG_Subscribe("_Subscri")
Special ControlHost tags.
static const int DISPATCH_PORT
Default ControlHost port.
Definition: JHostname.hh:24
#define WARNING(A)
Definition: JMessage.hh:65
static const JTag DISPTAG_Died("Died")
static const JTag DISPTAG_MyId("_MyId")
static const JTag DISPTAG_WhereIs("_WhereIs")
is
Definition: JDAQCHSM.chsm:167
then echo Test string reversed by client(hit< return > to continue)." JProcess -c "JEcho-r" -C fi if (( 1 ))
JPrefix JPrefix_t
Definition: JLigier.cc:29
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
static const size_t buffer_size
#define ERROR(A)
Definition: JMessage.hh:66
int debug
debug level
Definition: JSirene.cc:63
#define FATAL(A)
Definition: JMessage.hh:67
static const JTag DISPTAG_Born("Born")
static const long long int GIGABYTE
Number of bytes in a mega-byte.
unsigned long long int getRAM()
Get RAM of this CPU.
static const JTag DISPTAG_Gime("_Gime")
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG
static const JTag DISPTAG_Debug("_Debug")
static const JTag DISPTAG_Always("_Always")
static const JTag DISPTAG_ShowStat("_ShowSta")