Ignore:
Timestamp:
Jan 27, 2011, 8:13:14 AM (9 years ago)
Author:
ogrimm
Message:
Moved OpenOtherSockets() to FADBoard class
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 }
Note: See TracChangeset for help on using the changeset viewer.