Changeset 10120


Ignore:
Timestamp:
Jan 27, 2011, 8:13:14 AM (9 years ago)
Author:
ogrimm
Message:
Moved OpenOtherSockets() to FADBoard class
Location:
fact/FADctrl
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • fact/FADctrl/FAD.cc

    r10117 r10120  
    6868
    6969  // Construct boards
    70   BoardList = Tokenize(GetConfig("BoardList","129.217.160.119"));
    71   BoardList = Tokenize("192.33.99.225");
     70  BoardList = Tokenize(GetConfig("BoardList"));
    7271
    7372  for (unsigned int i=0; i<BoardList.size(); i++) {
     
    8584  int Ret;
    8685  if ((Ret = pthread_create(&Thread, NULL, (void * (*)(void *)) LaunchEventThread,(void *) this)) != 0) {
    87     Message(ERROR, "pthread_create() failed in FAD::FAD() (%s)", strerror(Ret));
    88         Thread = pthread_self();
     86    Message(FATAL, "pthread_create() failed in FAD::FAD() (%s)", strerror(Ret));
    8987  }
    9088
     
    105103
    106104  // Wait for DIM service thread to quit
    107   if (pthread_equal(Thread, pthread_self()) == 0) {
    108         if ((Ret = pthread_join(Thread, NULL)) != 0) Message(ERROR, "pthread_join() failed in ~FAD() (%s)", strerror(Ret));
    109   }
     105  if ((Ret = pthread_join(Thread, NULL)) != 0) Message(ERROR, "pthread_join() failed in ~FAD() (%s)", strerror(Ret));
    110106
    111107  // Delete all boards (cancels threads automatically)
     
    114110  delete Command;
    115111  delete ConsoleOut;
    116   free(ConsoleText); 
     112  free(ConsoleText);
    117113}
    118114
     
    208204  }
    209205
    210   if (Match(Parameter[1],"com")) PrintMessage("all active boards switched to command mode - socket 0\n");
    211   else if (Match(Parameter[1],"daq")) PrintMessage("all active boards switched to DAQ mode - socket 1..7\n");
     206  if (Match(Parameter[1],"com")) PrintMessage("All active boards switched to command mode - socket 0\n");
     207  else if (Match(Parameter[1],"daq")) PrintMessage("All active boards switched to DAQ mode - socket 1..7\n");
    212208  else PrintUsage();
    213209}
     
    283279  }
    284280
    285   if (Match(Parameter[1],"enable")) PrintMessage("all active boards accept now incoming triggers\n");
    286   else if (Match(Parameter[1],"disable")) PrintMessage("no active board accepts any incoming trigger anymore.\n");
     281  if (Match(Parameter[1],"enable")) PrintMessage("All active boards accept incoming triggers\n");
     282  else if (Match(Parameter[1],"disable")) PrintMessage("No active board accepts incoming triggers\n");
    287283}
    288284
  • fact/FADctrl/FAD.h

    r10117 r10120  
    4848    int NumEvents;                      // Number of event taken           
    4949       
    50         std::vector<std::string> BoardList;
    5150        std::vector<class FADBoard *> Boards;
    5251
     
    8887        int Pipe[2];
    8988    int NumEventsRequested;     // Number of events requested
     89        std::vector<std::string> BoardList;
    9090};
    9191
  • fact/FADctrl/FADBoard.cc

    r10117 r10120  
    2323  ACalibTime = -1;
    2424  Status.Update.tv_sec = -1;
    25   Thread = pthread_self(); // For checking in destructor
    2625
    2726  Name = new char [Server.size()+1]; // Name in permanent memory for DIM service
    2827  strcpy(Name, Server.c_str());
     28
     29  // Initialise mutex for synchronization
     30  pthread_mutexattr_t Attr;
     31
     32  if ((Ret = pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_ERRORCHECK)) != 0) {
     33    m->Message(m->ERROR, "pthread_mutex_settype() failed (%s)", strerror(Ret));
     34  }
     35  if ((Ret = pthread_mutex_init(&Mutex, &Attr)) != 0) {
     36    m->Message(m->FATAL, "pthread_mutex_init() failed (%s)", strerror(Ret));
     37  }
     38
     39  // Initialise condition variable for synchronization
     40  if ((Ret = pthread_cond_init(&CondVar, NULL)) != 0) {
     41    m->Message(m->FATAL, "pthread_cond_init() failed (%s)", strerror(Ret));
     42  }
    2943
    3044  // Resolve hostname
     
    3751  // Open socket descriptor
    3852  if ((Socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    39     m->PrintMessage("Could not open socket for %s (%s)\n", Server.c_str(), strerror(errno));
     53    m->Message(m->ERROR, "Could not open socket for %s (%s)\n", Server.c_str(), strerror(errno));
    4054    return;
    4155  }
     
    6276  DIM_ROI = new DimService((ID.str()+"ROI").c_str(), (char *) "S", NULL, 0);
    6377
    64   // Initialise mutex for synchronization
    65   pthread_mutexattr_t Attr;
    66 
    67   if ((Ret = pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_ERRORCHECK)) != 0) {
    68     m->Message(m->ERROR, "pthread_mutex_settype() failed (%s)", strerror(Ret));
    69   }
    70   if ((Ret = pthread_mutex_init(&Mutex, &Attr)) != 0) {
    71     m->Message(m->ERROR, "pthread_mutex_init() failed (%s)", strerror(Ret));
    72         return;
    73   }
    74 
    75   // Initialise condition variable for synchronization
    76   if ((Ret = pthread_cond_init(&CondVar, NULL)) != 0) {
    77     m->Message(m->ERROR, "pthread_cond_init() failed (%s)", strerror(Ret));
    78         return;
    79   }
    80 
    8178  // Create thread that receives data
    8279  if ((Ret = pthread_create(&Thread, NULL, (void * (*)(void *)) LaunchThread,(void *) this)) != 0) {
    83     m->Message(m->ERROR, "pthread_create() failed in FADBoard() (%s)\n", strerror(Ret));
    84         Thread = pthread_self();
    85         return;
     80    m->Message(m->FATAL, "pthread_create() failed in FADBoard() (%s)", strerror(Ret));
     81  }
     82
     83  // Create thread to connect to other sockets
     84  if ((Ret = pthread_create(&OtherThread, NULL, (void * (*)(void *)) OpenOtherSockets, (void *) Name)) != 0) {
     85    m->Message(m->FATAL, "pthread_create() failed for OpenOtherSockets (%s)", strerror(Ret));
    8686  }
    8787
     
    9696  int Ret;
    9797
    98   // Cancel thread (if it did not quit already) and wait for it to quit
    99   if (pthread_equal(Thread, pthread_self()) == 0) {
    100         if ((Ret = pthread_cancel(Thread)) != 0 && Ret != ESRCH) m->Message(m->ERROR, "pthread_cancel() failed in ~FADBoard() (%s)", strerror(Ret));
    101         if ((Ret = pthread_join(Thread, NULL)) != 0) m->Message(m->ERROR, "pthread_join() failed in ~FADBoard (%s)", strerror(Ret));
     98  // Avoid segmentation faults by chekcing for InitOK
     99  if (InitOK) {
     100        // Terminate thread for other sockets 
     101        if ((Ret = pthread_cancel(OtherThread)) != 0) {
     102          m->Message(m->ERROR, "pthread_cancel() failed in ~FADBoard() for OtherThread (%s)", strerror(Ret));
     103        }
     104        if ((Ret = pthread_join(OtherThread, NULL)) != 0) {
     105          m->Message(m->ERROR, "pthread_join() failed in ~FADBoard for OtherThread (%s)", strerror(Ret));
     106        }
     107
     108        // Cancel thread (if it did not quit already) and wait for it to quit
     109        if ((Ret = pthread_cancel(Thread)) != 0 && Ret != ESRCH) {
     110          m->Message(m->ERROR, "pthread_cancel() failed in ~FADBoard() (%s)", strerror(Ret));
     111        }
     112        if ((Ret = pthread_join(Thread, NULL)) != 0) {
     113          m->Message(m->ERROR, "pthread_join() failed in ~FADBoard (%s)", strerror(Ret));
     114        }
     115
     116        delete DIM_Name;
     117        delete DIM_ID;
     118        delete DIM_Temp;
     119        delete DIM_DAC;
     120        delete DIM_ROI;
     121  }
     122 
     123  delete[] Name;
     124
     125  // Close socket descriptor
     126  if ((Socket != -1) && (close(Socket) == -1)) {
     127        m->PrintMessage("Could not close socket descriptor (%s)", strerror(errno)); 
    102128  }
    103129
     
    108134
    109135  // Delete mutex 
    110   if (InitOK && ((Ret = pthread_mutex_destroy(&Mutex)) != 0)) {
     136  if ((Ret = pthread_mutex_destroy(&Mutex)) != 0) {
    111137        m->Message(m->ERROR, "pthread_mutex_destroy() failed in ~FADBoard (%s)", strerror(Ret));
    112   }
    113 
    114   delete DIM_Name;
    115   delete DIM_ID;
    116   delete DIM_Temp;
    117   delete DIM_DAC;
    118   delete DIM_ROI;
    119   delete[] Name;
    120 
    121   // Close socket descriptor
    122   if ((Socket != -1) && (close(Socket) == -1)) {
    123         m->PrintMessage("Could not close socket descriptor (%s)", strerror(errno)); 
    124138  }
    125139}
     
    524538  }
    525539}
     540
     541
     542//
     543// OpenOtherSockets()
     544//
     545void FADBoard::OpenOtherSockets(char *Hostname) {
     546
     547  int List[] = {5001, 5002, 5003, 5004, 5005, 5006, 5007};
     548  int Socket[sizeof(List)/sizeof(int)], MaxSocketNum=0, Ret;
     549  fd_set DescriptorList;
     550  char Buffer[1000000];
     551
     552  // Resolve hostname
     553  struct hostent *Host = gethostbyname(Hostname);
     554  if (Host == 0) {
     555    printf("OtherSockets: Could not resolve host name for %s\n", Hostname);
     556    return;
     557  }
     558
     559  // Connect to server
     560  struct sockaddr_in SocketAddress;
     561  SocketAddress.sin_family = PF_INET;
     562  SocketAddress.sin_addr = *(struct in_addr*) Host->h_addr;
     563
     564  for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
     565        // Open socket descriptor
     566        if ((Socket[i] = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
     567      printf("OtherSockets: Could not open socket for port %d (%s)\n", List[i], strerror(errno));
     568      return;
     569        }
     570
     571        // Determine highest socket number for select()
     572        if (Socket[i] > MaxSocketNum) MaxSocketNum = Socket[i];
     573         
     574        // Connect to server
     575    SocketAddress.sin_port = htons((unsigned short) List[i]);
     576        if (connect(Socket[i], (struct sockaddr *) &SocketAddress, sizeof(SocketAddress)) == -1) {
     577      printf("OtherSockets: Could not connect to port %d (%s)\n", List[i], strerror(errno));
     578      return;
     579        }
     580  }
     581 
     582  while(true) {
     583    // Wait for data from terminal (stdin) or from sockets
     584    FD_ZERO(&DescriptorList);   
     585    for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) FD_SET(Socket[i], &DescriptorList);
     586    if (select(MaxSocketNum+1, &DescriptorList, NULL, NULL, NULL) == -1) {
     587      perror("OtherSockets: Error with select()");
     588      break;
     589    }
     590       
     591        // Data from socket
     592        for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) if (FD_ISSET(Socket[i], &DescriptorList)) {
     593          Ret = read(Socket[i], Buffer, sizeof(Buffer));
     594      if(Ret == 0) printf("OtherSockets: Connection to port %d not existing anymore\n", List[i]);
     595      else if (Ret == -1) printf("OtherSockets: Error reading from port %d (%s)\n", List[i], strerror(errno));
     596      else ;//printf("OtherSockets: Read %d bytes from port %d\n", Ret, List[i]);
     597    }
     598  }
     599
     600  // Close all sockets
     601  for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
     602        if ((Socket[i] != -1) && (close(Socket[i]) == -1)) {
     603          printf("OtherSockets: Could not close socket of port %d (%s)", List[i], strerror(errno)); 
     604        }
     605  }
     606}
  • fact/FADctrl/FADBoard.h

    r10117 r10120  
    2525        class FAD *m;
    2626        int Socket;
    27         pthread_t Thread;
     27        pthread_t Thread, OtherThread;
    2828        pthread_mutex_t Mutex;
    29         DimService *DIM_Name;
    30         DimService *DIM_ID;
    31         DimService *DIM_Temp;
    32         DimService *DIM_ROI;
    33         DimService *DIM_DAC;
     29        DimService *DIM_Name, *DIM_ID, *DIM_Temp, *DIM_ROI, *DIM_DAC;
    3430
    3531        void ReadLoop();
    3632        static void LaunchThread(class FADBoard *);
     33        static void OpenOtherSockets(char *);
    3734
    3835  public:
  • fact/FADctrl/FADctrl.cc

    r10103 r10120  
    1212
    1313const string READLINE_HIST_FILE = string(getenv("HOME"))+"/.history_FADctrl";
    14 void OpenOtherSockets();
    1514
    1615// ================
     
    2019int main() {
    2120
    22   // Uses getc() for readline library (allows interruption by signal)
     21  // Uses getc() for readline library (allows interruption by signal) and load history buffer
    2322  rl_getc_function = getc;
    24  
    25   // Load history buffer
    2623  read_history(READLINE_HIST_FILE.c_str());
    2724
     
    3532  signal(SIGPIPE,SIG_IGN);
    3633
    37   // Create thread to connect to other sockets
    38   int RetVal;
    39   pthread_t Thread;
    40   if ((RetVal = pthread_create(&Thread, NULL, (void * (*)(void *)) OpenOtherSockets, NULL)) != 0) {
    41     printf("pthread_create() failed for OpenOtherSockets(%s)\n", strerror(RetVal));
    42         exit(EXIT_FAILURE);
    43   }
    44 
    4534  // Initialise all boards
    46   M.PrintMessage("Initalizing all boards...\n");
    47 
    4835  DimClient::sendCommand(SERVER_NAME"/Command", "dwrite off");
    4936  DimClient::sendCommand(SERVER_NAME"/Command", "domino off");
    50   sleep(1);
    51 
    5237  DimClient::sendCommand(SERVER_NAME"/Command", "dac 0 25000");
    5338  DimClient::sendCommand(SERVER_NAME"/Command", "dac 1-3 0");
    5439  DimClient::sendCommand(SERVER_NAME"/Command", "dac 4-7 28800");
    55   sleep (1);
    56 
    57   //DimClient::sendCommand(SERVER_NAME"/Command", "roi all 10");
    58   //DimClient::sendCommand(SERVER_NAME"/Command", "address 44 29");
    59   //sleep (1);
    60 
    61   //DimClient::sendCommand(SERVER_NAME"/Command", "trigger");
    62   //sleep (1);
    63 
    64   //DimClient::sendCommand(SERVER_NAME"/Command", "address 44 30");
    65   //sleep (1);
    66 
    67   //DimClient::sendCommand(SERVER_NAME"/Command", "trigger");
    68   //sleep (1);
    69 
    70   //DimClient::sendCommand(SERVER_NAME"/Command", "address 44 0");
    71   //sleep (1);
    72 
    73   //DimClient::sendCommand(SERVER_NAME"/Command", "trigger");
    7440  DimClient::sendCommand(SERVER_NAME"/Command", "domino on");
    7541  DimClient::sendCommand(SERVER_NAME"/Command", "dwrite on");
    7642  DimClient::sendCommand(SERVER_NAME"/Command", "roi all 1024");
    77 
    7843  DimClient::sendCommand(SERVER_NAME"/Command", "trigger enable");
    79 
    8044  M.PrintMessage("Finished initalizing all boards\n");
    8145 
     
    10468  int Ret = write_history(READLINE_HIST_FILE.c_str());
    10569  if (Ret != 0 ) printf("Error writing history file to '%s' (%s)\n", READLINE_HIST_FILE.c_str(), strerror(Ret));
    106 
    107   // Terminate thread for other sockets 
    108   if ((Ret = pthread_cancel(Thread)) != 0) printf("Error: Could not request thread cancellation in main() (%s)\n", strerror(Ret));
    109   if ((Ret = pthread_join(Thread, NULL)) != 0) printf("pthread_join() failed in main () (%s)\n", strerror(Ret));
    11070}
    111 
    112 
    113 // ====================
    114 //   OpenOtherSockets()
    115 // ====================
    116 
    117 void OpenOtherSockets() {
    118 
    119         //ETHZ
    120   static char Hostname[] = "192.33.99.225";
    121         //TUDO
    122         //static char Hostname[] = "129.217.160.119";
    123   int List[] = {5001, 5002, 5003, 5004, 5005, 5006, 5007};
    124   int Socket[sizeof(List)/sizeof(int)], MaxSocketNum=0, Ret;
    125   fd_set DescriptorList;
    126   char Buffer[1000000];
    127 
    128   // Resolve hostname
    129   struct hostent *Host = gethostbyname(Hostname);
    130   if (Host == 0) {
    131     printf("OtherSockets: Could not resolve host name for %s\n", Hostname);
    132     return;
    133   }
    134 
    135   // Connect to server
    136   struct sockaddr_in SocketAddress;
    137   SocketAddress.sin_family = PF_INET;
    138   SocketAddress.sin_addr = *(struct in_addr*) Host->h_addr;
    139 
    140   for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
    141         // Open socket descriptor
    142         if ((Socket[i] = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    143       printf("OtherSockets: Could not open socket for port %d (%s)\n", List[i], strerror(errno));
    144       return;
    145         }
    146 
    147         // Determine highest socket number for select()
    148         if (Socket[i] > MaxSocketNum) MaxSocketNum = Socket[i];
    149          
    150         // Connect to server
    151     SocketAddress.sin_port = htons((unsigned short) List[i]);
    152         if (connect(Socket[i], (struct sockaddr *) &SocketAddress, sizeof(SocketAddress)) == -1) {
    153       printf("OtherSockets: Could not connect to port %d (%s)\n", List[i], strerror(errno));
    154       return;
    155         }
    156   }
    157  
    158   while(true) {
    159     // Wait for data from terminal (stdin) or from sockets
    160     FD_ZERO(&DescriptorList);   
    161     for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) FD_SET(Socket[i], &DescriptorList);
    162     if (select(MaxSocketNum+1, &DescriptorList, NULL, NULL, NULL) == -1) {
    163       perror("OtherSockets: Error with select()");
    164       break;
    165     }
    166        
    167         // Data from socket
    168         for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) if (FD_ISSET(Socket[i], &DescriptorList)) {
    169           Ret = read(Socket[i], Buffer, sizeof(Buffer));
    170       if(Ret == 0) printf("OtherSockets: Connection to port %d not existing anymore\n", List[i]);
    171       else if (Ret == -1) printf("OtherSockets: Error reading from port %d (%s)\n", List[i], strerror(errno));
    172       else ;//printf("OtherSockets: Read %d bytes from port %d\n", Ret, List[i]);
    173     }
    174   }
    175 
    176   // Close all sockets
    177   for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
    178         if ((Socket[i] != -1) && (close(Socket[i]) == -1)) {
    179           printf("OtherSockets: Could not close socket of port %d (%s)", List[i], strerror(errno)); 
    180         }
    181   }
    182 }
  • fact/FADctrl/History.txt

    r10117 r10120  
    8823/1/2011       Data can be written to disk in M0 format
    9924/1/2011       Integrated amplitude calibration into FADBoard class
     1027/1/2011       Moved OpenOtherSockets() to FADBoard class
Note: See TracChangeset for help on using the changeset viewer.