void	EuclideanDisplacementTest::testConvolve() {
	debug(LOG_DEBUG, DEBUG_LOG, 0, "testConvolve() begin");

	// read testimages/sun.fits
	std::string	name("testimages/orion1.fits");
	FITSinfile<unsigned short>	infile(name);
        Image<unsigned short>	*i = infile.read();
	ImagePtr	image(i);

	// create an adapter so that we see double values
	adapter::TypeConversionAdapter<unsigned short>	tconv(*i);

	// apply the convolution
	simpleconvolution	f;

	// create an image from the transform
	transform::EuclideanDisplacementConvolve<double>	ta(f, 1000);
	Image<double>	*result = ta(tconv);
	ImagePtr	r(result);

	// write the transformed image back to tmp/sun-displace.fits
	std::string	outname("tmp/orion-convolve.fits");
	FITSoutfile<double>       *outfile = new FITSoutfile<double>(outname);
        outfile->setPrecious(false);
        outfile->write(*result);
        delete outfile;
	debug(LOG_DEBUG, DEBUG_LOG, 0, "testConvolve() end");
}
示例#2
0
int64_t
timestamp_us()
{
	struct timeval t;
	int64_t ts;
	gettimeofday(&t, NULL);
	tconv(&t, &ts, true);
	return ts;
}
示例#3
0
int
connect_socket(const char *host, unsigned short port, unsigned long to_us)
{
	int64_t tsend = to_us ? timestamp_us() + to_us : 0;
	struct addrinfo *ai_list = NULL;
	struct addrinfo hints;
	memset(&hints, 0, sizeof hints);
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = 0;
	hints.ai_flags = AI_NUMERICSERV;
	char portstr[6];
	snprintf(portstr, sizeof portstr, "%hu", port);

	int r = getaddrinfo(host, portstr, &hints, &ai_list);

	if (r != 0) {
		W("%s", gai_strerror(r));
		return -1;
	}

	if (!ai_list) {
		W("result address list empty");
		return -1;
	}

	int sck = -1;
	for (struct addrinfo *ai = ai_list; ai; ai = ai->ai_next)
	{
		errno = 0;
		sck = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
		if (sck < 0) {
			WE("cannot create socket");
			continue;
		}
		
		errno = 0;
		if (fcntl(sck, F_SETFL, O_NONBLOCK) == -1) {
			WE("failed to enable nonblocking mode");
			close(sck);
			sck = -1;
			continue;
		}
		
		D("set to nonblocking mode, calling connect() now");
		errno = 0;
		int r = connect(sck, ai->ai_addr, ai->ai_addrlen);

		if (r == -1 && (errno != EINPROGRESS)) {
			WE("connect() failed");
			close(sck);
			sck = -1;
			continue;
		}

		int opt = 1;
		socklen_t optlen = sizeof opt;
		struct timeval tout;
		tout.tv_sec = 0;
		tout.tv_usec = 0;
		int64_t trem = 0;

		for(;;) {
			if (tsend)
			{
				trem = tsend - timestamp_us();
				if (trem <= 0) {
					W("timeout reached while in 3WHS");
					close(sck);
					sck = -1;
					goto outer_bot;
				}

				tconv(&tout, &trem, false);
			}

			fd_set fds;
			FD_ZERO(&fds);
			FD_SET(sck, &fds);

			errno = 0;
			r = select(sck+1, NULL, &fds, NULL, tsend ? &tout : NULL);
			if (r < 0)
			{
				WE("select() failed");
				close(sck);
				sck = -1;
				goto outer_bot;
			}
			if (r == 1) {
				D("select finished successfully");
				break;
			}
		}

		if (getsockopt(sck, SOL_SOCKET, SO_ERROR, &opt, &optlen) != 0) {
			W("getsockopt failed");
			close(sck);
			sck = -1;
			continue;
		}

		if (opt == 0) {
			D("socket connected, setting to blocking mode");
			errno = 0;
			if (fcntl(sck, F_SETFL, 0) == -1) {
				WE("failed to clear nonblocking mode");
				close(sck);
				sck = -1;
				continue;
			}

			break;
		} else {
			WC(opt, "could not connect socket (%d)", opt);
			close(sck);
			sck = -1;
			continue;
		}
outer_bot:;
	}

	freeaddrinfo(ai_list);

	return sck;
}