Example #1
0
File: mngr.c Project: jiangli/lacp
int
main_loop (void)
{
    fd_set readfds;
    int rc, numfds, sock, kkk;

    rl_callback_handler_install (get_prompt (), rl_read_cli);

    sock = GET_FILE_DESCRIPTOR (&main_sock);

    do {
        numfds = -1;
        FD_ZERO (&readfds);

        kkk = 0;			/* stdin for commands */
        FD_SET (kkk, &readfds);
        if (kkk > numfds)
            numfds = kkk;

        FD_SET (sock, &readfds);
        if (sock > numfds)
            numfds = sock;

        if (numfds < 0)
            numfds = 0;
        else
            numfds++;

        rc = select (numfds, &readfds, NULL, NULL, NULL);
        if (rc < 0) {		// Error
            if (EINTR == errno)
                continue;		// don't break
            printf ("FATAL_MODE:select failed: %s\n", strerror (errno));
            return -2;
        }

        if (FD_ISSET (0, &readfds)) {
            rl_callback_read_char ();
        }

        if (FD_ISSET (sock, &readfds)) {
            shutdown_flag |= read_uid ();
        }

    } while (!shutdown_flag);
    return 0;
}
Example #2
0
int main_loop(void)
{
	fd_set readfds;
	struct timeval tv;
	struct timeval now, earliest;
	int rc, numfds, sock, kkk;

	sock = GET_FILE_DESCRIPTOR(&uid_socket);

	gettimeofday(&earliest, NULL);
	earliest.tv_sec++;

	do {
		numfds = -1;
		FD_ZERO(&readfds);

		kkk = 0; /* stdin for commands */
		FD_SET(kkk, &readfds);
		if (kkk > numfds) {
			numfds = kkk;
		}

		FD_SET(sock, &readfds);
		if (sock > numfds) {
			numfds = sock;
		}

		if (numfds < 0) {
			numfds = 0;
		} else {
			numfds++;
		}

		gettimeofday(&now, 0);
		tv.tv_usec = 0;
		tv.tv_sec = 0;

		if (now.tv_sec < earliest.tv_sec) { /* we must wait more than 1 sec. */
			tv.tv_sec = 1;
			tv.tv_usec = 0;
		} else if (now.tv_sec == earliest.tv_sec) {
			if (now.tv_usec < earliest.tv_usec) {
				if (earliest.tv_usec < now.tv_usec)
					tv.tv_usec = 0;
				else
					tv.tv_usec = earliest.tv_usec
					                - now.tv_usec;
			}
		}

		//printf("wait %ld-%ld\n", (long)tv.tv_sec, (long)tv.tv_usec);
		rc = select(numfds, &readfds, NULL, NULL, &tv);
		if (rc < 0) { // Error
			if (EINTR == errno)
				continue; // don't break 
			printf("FATAL_MODE:select failed: %s\n",
			       strerror(errno));
			return -2;
		}

		if (!rc) { // Timeout expired
			STP_IN_one_second();
			gettimeofday(&earliest, NULL);
			//printf("tick %ld-%ld\n", (long)earliest.tv_sec - 1005042800L, (long)earliest.tv_usec);

			earliest.tv_sec++;
			continue;
		}

		if (FD_ISSET(0, &readfds)) {
			rl_callback_read_char();
		}

		if (FD_ISSET(sock, &readfds)) {
			shutdown_flag |= read_uid(&uid_socket);
		}

	} while (!shutdown_flag);
	
	return 0;
}
Example #3
0
BatchData KaldiStream::read(int N, size_t dim, size_t base) {
  BatchData data;
  data.x.resize(N, dim + 1, 0);
  data.y.resize(N, 1, 0);

  // Read kaldi feature
  FILE* &fis = this->_ffid;
  FILE* &lis = this->_lfid;

  int counter = 0;
  int& r = this->_remained;

  while (true) {

    if (r == 0) {
      string uid1, uid2;
      uid1 = read_uid(fis);
      uid2 = read_uid(lis);

      if (uid1.empty() or uid2.empty()) {
	this->rewind();
	uid1 = read_uid(fis);
	uid2 = read_uid(lis);
      }

      if (uid1 != uid2)
	throw std::runtime_error(RED_ERROR + "uid1 != uid2 (\"" + uid1 + "\" != \"" + uid2 + "\")");

      char s[6]; 
      int frame;
      int dimension;

      CFRE(fread((void*) s, 6, 1, fis));
      CFRE(fread((void*) &frame, 4, 1, fis));
      CFRE(fread((void*) s, 1, 1, fis));
      CFRE(fread((void*) &dimension, 4, 1, fis));

      if (dimension != (int) dim)
	throw std::runtime_error(RED_ERROR + "feature dimension in kaldi archive (=" +
	    to_string(dimension) + ") does not match --input-dim (=" +
	    to_string(dim) + ").");

      r = frame;
    }

    for(int i = 0; i < r; i++) {
      for(int j = 0; j < (int) dim; j++)
	CFRE(fread((void*) &data.x(counter, j), sizeof(float), 1, fis));
      data.x(counter, dim) = 1;

      size_t y;
      CFRE(fscanf(lis, "%lu", &y));
      data.y[counter] = y;

      if (++counter == N) {
	r -= i + 1;
	return data;
      }
    }

    r = 0;
  }

  return data;
}