コード例 #1
0
ファイル: netgame.cpp プロジェクト: mikowiec/harvest
	bool operator()()
	{
		if (conn) {
			obj_dosync();
			ext_send();
			ext_poll();
			if (!conn->good()) {
				conn = 0;
			}
		}
		if (die.try_wait()) {
			in_queue.clear();
			out_queue.clear();
			dead.signal();
			return false;
		} else {
			return true;
		}
	}
コード例 #2
0
/* ###### Main program ################################################### */
int main(int argc, char** argv)
{
   union sockaddr_union           localAddress;
   struct pollfd                  ufds;
   struct SimpleRedBlackTree      objectStorage;
   struct SimpleRedBlackTree      objectDisplay;
   struct SimpleRedBlackTreeNode* node;
   unsigned long long             now;
   unsigned long long             updateInterval = 1000000;
   unsigned long long             purgeInterval  = 30000000;
   unsigned long long             lastUpdate     = 0;
   size_t                         lastElements   = ~0;
   size_t                         elements;
   int                            result;
   int                            reuse;
   int                            sd;
   int                            n;

   if(checkIPv6()) {
      string2address("[::]:0", &localAddress);
      setPort(&localAddress.sa, 2960);
   }
   else {
      string2address("0.0.0.0:0", &localAddress);
      setPort(&localAddress.sa, 2960);
   }
   for(n = 1;n < argc;n++) {
      if(!(strncmp(argv[n], "-localaddress=", 14))) {
         if(string2address((char*)&argv[n][14], &localAddress) == false) {
            fprintf(stderr, "ERROR: Bad local address <%s>\n", (char*)&argv[n][14]);
            exit(1);
         }
      }
      else if(!(strncmp(argv[n], "-updateinterval=", 16))) {
         updateInterval = 1000 * atol((char*)&argv[n][16]);
         if(updateInterval < 100000) {
            updateInterval = 100000;
         }
      }
      else if(!(strncmp(argv[n], "-purgeinterval=", 15))) {
         purgeInterval = 1000 * atol((const char*)&argv[n][15]);
         if(purgeInterval < 1000000) {
            purgeInterval = 1000000;
         }
      }
      else if(!(strncmp(argv[n], "-maxpr=", 7))) {
         maxPRs = atoi((const char*)&argv[n][7]);
      }
      else if(!(strncmp(argv[n], "-maxpe=", 7))) {
         maxPEs = atoi((const char*)&argv[n][7]);
      }
      else if(!(strncmp(argv[n], "-maxpu=", 7))) {
         maxPUs = atoi((const char*)&argv[n][7]);
      }
      else if(!(strncmp(argv[n], "-maxlocationsize=", 17))) {
         maxLocationSize = atoi((const char*)&argv[n][17]);
      }      
      else if(!(strcmp(argv[n], "-compact"))) {
         useCompactMode = true;
      }
      else if(!(strcmp(argv[n], "-full"))) {
         useCompactMode = false;
      }
      else {
         printf("Bad argument \"%s\"!\n" ,argv[n]);
         fprintf(stderr, "Usage: %s {-localaddress=address:port} {-updateinterval=milliseconds} {-purgeinterval=milliseconds} {-compact|-full} {-maxpr=PRs} {-maxpe=PEs} {-maxpu=PUs} {-maxlocationsize=characters}\n", argv[0]);
         exit(1);
      }
   }

   sd = ext_socket(localAddress.sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
   if(sd < 0) {
      perror("Unable to create socket");
      exit(1);
   }
   reuse = 1;
   if(ext_setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
      perror("setsockopt() with SO_REUSEADDR failed");
   }
   if(bindplus(sd, &localAddress, 1) == false) {
      fputs("ERROR: Unable to bind socket to local address\n", stderr);
      exit(1);
   }

   simpleRedBlackTreeNew(&objectStorage, NULL,                  cspObjectStorageComparison);
   simpleRedBlackTreeNew(&objectDisplay, cspObjectDisplayPrint, cspObjectDisplayComparison);

   puts("Component Status Monitor - Version 1.0");
   puts("======================================\n");

   installBreakDetector();
   printf("\x1b[;H\x1b[2J");

   /* The first update should be in 1 second ... */
   lastUpdate = getMicroTime() + 1000000 - updateInterval;
   
   while(!breakDetected()) {
      ufds.fd          = sd;
      ufds.events      = POLLIN;
      now              = getMicroTime();
      while(now - lastUpdate < updateInterval) {
         now = getMicroTime();
         result = ext_poll(&ufds, 1,
                           ((lastUpdate + updateInterval) > now) ?
                              (int)((lastUpdate + updateInterval - now) / 1000) : 0);
         if((result > 0) && (ufds.revents & POLLIN)) {
            handleMessage(sd, &objectStorage, &objectDisplay);
         }
         else if((result < 0) && (errno == EINTR)) {
            goto finished;
         }
      }
      purgeCSPObjects(&objectStorage, &objectDisplay, purgeInterval);

      elements = simpleRedBlackTreeGetElements(&objectStorage);

      if( (elements != lastElements) || (elements > 0) ) {
         printf("\x1b[;H");
         printTimeStamp(stdout);
         printf("Current Component Status -- \x1b[31;1m%u PRs\x1b[0m, \x1b[34;1m%u PEs\x1b[0m, \x1b[32;1m%u PUs\x1b[0m\x1b[0K\n\x1b[0K\n\x1b[0K\x1b[;H\n",
                totalPRs, totalPEs, totalPUs);
         maxObjectLabelSize = 0;
         currentPRs         = 0;
         currentPEs         = 0;
         currentPUs         = 0;

         node = simpleRedBlackTreeGetFirst(&objectDisplay);
         while(node != NULL) {
            cspObjectDisplayPrint(node, stdout);
            node = simpleRedBlackTreeGetNext(&objectDisplay, node);
         }

         currentObjectLabelSize = maxObjectLabelSize;
         printf("\x1b[0J");
         fflush(stdout);
      }

      lastElements = elements;
      lastUpdate   = now;
   }

finished:
   ext_close(sd);
   simpleRedBlackTreeDelete(&objectStorage);
   puts("\nTerminated!");
   return(0);
}