Ejemplo n.º 1
0
void print_rec(int place, int level)
{
	if (place >= SIZE)
		return;
		
	for (int i=0; i<level; ++i)
		printf("--");
	printf("->%d\n",heap[place]);
		
	print_rec(2*place, level+1);
	print_rec(2*place+1, level+1);	
}
Ejemplo n.º 2
0
void print_rec(avl_node_t *node, int lvl, int maxlvl, long *count) {
  if(node == NULL) {
    return;
  }
  if(lvl == maxlvl) {
    //printf("Key:%d,D/R:%d%d,LRC:%d,%d,%d ",node->key, node->deleted, node->removed, node->lefth, node->righth, node->localh);
    printf("Key:%d,D/R:%d%d ", node->key, node->deleted, node->removed);
    *count = *count + 1;
    return;
  }
  lvl = lvl + 1;
  print_rec(node->left, lvl, maxlvl, count);
  print_rec(node->right, lvl, maxlvl, count);
}
Ejemplo n.º 3
0
static void store_ntdb(char *keyname, size_t keylen, char* data, size_t datalen)
{
	NTDB_DATA key, dbuf;
	enum NTDB_ERROR ecode;

	if ((keyname == NULL) || (keylen == 0)) {
		terror(NTDB_SUCCESS, "need key");
		return;
	}

	if ((data == NULL) || (datalen == 0)) {
		terror(NTDB_SUCCESS, "need data");
		return;
	}

	key.dptr = (unsigned char *)keyname;
	key.dsize = keylen;
	dbuf.dptr = (unsigned char *)data;
	dbuf.dsize = datalen;

	printf("Storing key:\n");
	print_rec(ntdb, key, dbuf, NULL);

	ecode = ntdb_store(ntdb, key, dbuf, NTDB_REPLACE);
	if (ecode) {
		terror(ecode, "store failed");
	}
}
Ejemplo n.º 4
0
static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
{
	TDB_DATA key, dbuf;

	if ((keyname == NULL) || (keylen == 0)) {
		terror("need key");
		return;
	}

	if ((data == NULL) || (datalen == 0)) {
		terror("need data");
		return;
	}

	key.dptr = (unsigned char *)keyname;
	key.dsize = keylen;
	dbuf.dptr = (unsigned char *)data;
	dbuf.dsize = datalen;

	printf("Storing key:\n");
	print_rec(tdb, key, dbuf, NULL);

	if (tdb_store(tdb, key, dbuf, TDB_REPLACE) != 0) {
		terror("store failed");
	}
}
Ejemplo n.º 5
0
static void show_tdb(void)
{
	char *k = get_token(1);
	TDB_DATA key, dbuf;

	if (!k) {
		help();
		return;
	}

	key.dptr = k;
	key.dsize = strlen(k)+1;

	dbuf = tdb_fetch(tdb, key);
	if (!dbuf.dptr) {
		/* maybe it is non-NULL terminated key? */
		key.dsize = strlen(k); 
		dbuf = tdb_fetch(tdb, key);
		
		if ( !dbuf.dptr ) {
			terror("fetch failed");
			return;
		}
	}
	
	/* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
	print_rec(tdb, key, dbuf, NULL);
	
	free( dbuf.dptr );
	
	return;
}
Ejemplo n.º 6
0
void print_avltree(avl_intset_t *set) {
  long count = 1;
  int lvl = 0;
  while(count != 0) {
    count = 0;
    print_rec(set->root, 0, lvl++, &count);
    printf("\n");
  }

}
Ejemplo n.º 7
0
static void first_record(TDB_CONTEXT *context, TDB_DATA *pkey)
{
  TDB_DATA dbuf;
  *pkey = tdb_firstkey(context);
  
  dbuf = tdb_fetch(context, *pkey);
  if (!dbuf.dptr) terror("fetch failed");
  /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
  print_rec(context, *pkey, dbuf, NULL);
}
Ejemplo n.º 8
0
static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
{
	TDB_DATA dbuf;
	*pkey = tdb_nextkey(the_tdb, *pkey);

	dbuf = tdb_fetch(the_tdb, *pkey);
	if (!dbuf.dptr)
		terror("fetch failed");
	else
		print_rec(the_tdb, *pkey, dbuf, NULL);
}
Ejemplo n.º 9
0
static void print_record( struct Ordered_container* lib_ID )
{
    int ID;
    
    if ( !read_int( &ID ) )
    {
        return;
    }
    
    print_rec( lib_ID, comp_Record_to_ID, &ID, "No record with that ID!\n" );
}
Ejemplo n.º 10
0
static void first_record(struct tdb_context *the_tdb, TDB_DATA *pkey)
{
	TDB_DATA dbuf;
	enum TDB_ERROR ecode;
	ecode = tdb_firstkey(the_tdb, pkey);
	if (!ecode)
		ecode = tdb_fetch(the_tdb, *pkey, &dbuf);
	if (ecode) terror(ecode, "fetch failed");
	else {
		print_rec(the_tdb, *pkey, dbuf, NULL);
	}
}
Ejemplo n.º 11
0
void print( char **S, Vertex *Mtz, unsigned int *indexp, unsigned int *tams, char **resp)
{
	int nums[sizeof(VUTIL)+1];
	unsigned int max = tams[0]*indexp[0]-1;

    memset(nums, 0, sizeof(nums[0]) * (k+1));
    print_rec(S, Mtz, indexp, tams, max, resp, nums);
    for (int i=0; i<k; i++) {
        resp[i][4+2*nums[k]] = '\0';
        puts(resp[i]);
    }
}
Ejemplo n.º 12
0
static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
{
	TDB_DATA dbuf;
	*pkey = tdb_nextkey(the_tdb, *pkey);
	
	dbuf = tdb_fetch(the_tdb, *pkey);
	if (!dbuf.dptr) 
		terror("fetch failed");
	else
		/* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
		print_rec(the_tdb, *pkey, dbuf, NULL);
}
Ejemplo n.º 13
0
static void next_record(struct ntdb_context *the_ntdb, NTDB_DATA *pkey)
{
	NTDB_DATA dbuf;
	enum NTDB_ERROR ecode;
	ecode = ntdb_nextkey(the_ntdb, pkey);

	if (!ecode)
		ecode = ntdb_fetch(the_ntdb, *pkey, &dbuf);
	if (ecode)
		terror(ecode, "fetch failed");
	else
		print_rec(the_ntdb, *pkey, dbuf, NULL);
}
Ejemplo n.º 14
0
void print_rec( char **S, Vertex *Mtz, unsigned int *indexp, unsigned int *tams, unsigned int p, char **resp, int nums[]) {
    if (p) {
        print_rec(S, Mtz, indexp, tams, Vizinho(p, Mtz[p].destino, indexp, Mtz), resp, nums);
        for (int i=0; i<k; i++) {
            if ( Mtz[p].destino & (1<<i) ) {
                resp[i][3+2*nums[k]] = S[i][nums[i]];
                nums[i]++;
            }
            else resp[i][3+2*nums[k]] = '-';
            resp[i][4+2*nums[k]] = ' ';
        }
        nums[k]++;
    }
}
Ejemplo n.º 15
0
static void move_rec(void)
{
	char *k = get_token(1);
	char *file = get_token(0);	
	TDB_DATA key, dbuf;
	TDB_CONTEXT *dst_tdb;

	if (!k) {
		help();
		return;
	}
	
	if ( !file ) {
		terror("need destination tdb name");
		return;
	}

	key.dptr = k;
	key.dsize = strlen(k)+1;

	dbuf = tdb_fetch(tdb, key);
	if (!dbuf.dptr) {
		/* maybe it is non-NULL terminated key? */
		key.dsize = strlen(k); 
		dbuf = tdb_fetch(tdb, key);
		
		if ( !dbuf.dptr ) {
			terror("fetch failed");
			return;
		}
	}
	
	print_rec(tdb, key, dbuf, NULL);
	
	dst_tdb = tdb_open(file, 0, 0, O_RDWR, 0600);
	if ( !dst_tdb ) {
		terror("unable to open destination tdb");
		return;
	}
	
	if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
		terror("failed to move record");
	}
	else
		printf("record moved\n");
	
	tdb_close( dst_tdb );
	
	return;
}
Ejemplo n.º 16
0
int main(int argc, char ** argv)
{
	  int i;
	  print_array();
	  build_heap();
	  print_array();
	  insert(22);
	  print_array();
	  print_rec(1, 0);
	  for (i=1; i<=SIZE; ++i) 
		printf(" %d , ", max_del());
	  printf("\n");
	  return 0;
}
Ejemplo n.º 17
0
void Tree::print_rec()
{
  cout << "depth = " << recdepth << "  "
	   << "node->id = " << recnode->id << (recdepth==fulldepth ? " * " : "   ")
	   << "box: " << recboxes.back().v
	   << "," << recboxes.back().w
	   << endl;

  for (int i=0; i<pow(2,dim); i++)
  {
	if (down(i))
	{
	  print_rec();
	  up();
	}
  }  
}
Ejemplo n.º 18
0
static void move_rec(char *keyname, size_t keylen, char* tdbname)
{
	TDB_DATA key, dbuf;
	TDB_CONTEXT *dst_tdb;

	if ((keyname == NULL) || (keylen == 0)) {
		terror("need key");
		return;
	}

	if ( !tdbname ) {
		terror("need destination tdb name");
		return;
	}

	key.dptr = (unsigned char *)keyname;
	key.dsize = keylen;

	dbuf = tdb_fetch(tdb, key);
	if (!dbuf.dptr) {
		terror("fetch failed");
		return;
	}

	print_rec(tdb, key, dbuf, NULL);

	dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
	if ( !dst_tdb ) {
		terror("unable to open destination tdb");
		return;
	}

	if (tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) != 0) {
		terror("failed to move record");
	}
	else
		printf("record moved\n");

	tdb_close( dst_tdb );

	return;
}
Ejemplo n.º 19
0
static void move_rec(char *keyname, size_t keylen, char* tdbname)
{
	TDB_DATA key, dbuf;
	struct tdb_context *dst_tdb;
	enum TDB_ERROR ecode;

	if ((keyname == NULL) || (keylen == 0)) {
		terror(TDB_SUCCESS, "need key");
		return;
	}

	if ( !tdbname ) {
		terror(TDB_SUCCESS, "need destination tdb name");
		return;
	}

	key.dptr = (unsigned char *)keyname;
	key.dsize = keylen;

	ecode = tdb_fetch(tdb, key, &dbuf);
	if (ecode) {
		terror(ecode, "fetch failed");
		return;
	}
	
	print_rec(tdb, key, dbuf, NULL);
	
	dst_tdb = tdb_open(tdbname, 0, O_RDWR, 0600, NULL);
	if ( !dst_tdb ) {
		terror(TDB_SUCCESS, "unable to open destination tdb");
		return;
	}
	
	ecode = tdb_store( dst_tdb, key, dbuf, TDB_REPLACE);
	if (ecode)
		terror(ecode, "failed to move record");
	else
		printf("record moved\n");
	
	tdb_close( dst_tdb );
}
Ejemplo n.º 20
0
static void show_tdb(char *keyname, size_t keylen)
{
	TDB_DATA key, dbuf;
	enum TDB_ERROR ecode;

	if ((keyname == NULL) || (keylen == 0)) {
		terror(TDB_SUCCESS, "need key");
		return;
	}

	key.dptr = (unsigned char *)keyname;
	key.dsize = keylen;

	ecode = tdb_fetch(tdb, key, &dbuf);
	if (ecode) {
		terror(ecode, "fetch failed");
		return;
	}
	
	print_rec(tdb, key, dbuf, NULL);
	
	free( dbuf.dptr );
}
Ejemplo n.º 21
0
static void store_tdb(void)
{
  char *k = get_token(1);
  char *d = get_token(0);
  TDB_DATA key, dbuf;

  if (!k || !d) {
    help();
    return;
  }

  key.dptr = k;
  key.dsize = strlen(k)+1;
  dbuf.dptr = d;
  dbuf.dsize = strlen(d)+1;

  printf("Storing key:\n");
  print_rec(tdb, key, dbuf, NULL);

  if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
    terror("store failed");
  }
}
Ejemplo n.º 22
0
static void show_tdb(char *keyname, size_t keylen)
{
	TDB_DATA key, dbuf;

	if ((keyname == NULL) || (keylen == 0)) {
		terror("need key");
		return;
	}

	key.dptr = (unsigned char *)keyname;
	key.dsize = keylen;

	dbuf = tdb_fetch(tdb, key);
	if (!dbuf.dptr) {
	    terror("fetch failed");
	    return;
	}

	print_rec(tdb, key, dbuf, NULL);

	free( dbuf.dptr );

	return;
}
Ejemplo n.º 23
0
static int parse_buffer(int fdin, int fdout)
{
	struct dbg_line		*line;
	struct ptldebug_header	*hdr;
	char			 buf[4097];
	char			*ptr;
	unsigned long		 dropped = 0;
	unsigned long		 kept = 0;
	unsigned long		 bad = 0;
	struct dbg_line		**linev = NULL;
	int			 linev_len = 0;
	int			 rc;

	hdr = (void *)buf;

	while (1) {
		int first_bad = 1;
		int count;

		count = HDR_SIZE;
		ptr = buf;
readhdr:
		rc = read(fdin, ptr, count);
		if (rc <= 0)
			goto print;

		ptr += rc;
		count -= rc;
		if (count > 0)
			goto readhdr;

		if (hdr->ph_len > 4094 ||       /* is this header bogus? */
		    hdr->ph_stack > 65536 ||
		    hdr->ph_sec < (1 << 30) ||
		    hdr->ph_usec > 1000000000 ||
		    hdr->ph_line_num > 65536) {
			if (first_bad)
				dump_hdr(lseek(fdin, 0, SEEK_CUR), hdr);
			bad += first_bad;
			first_bad = 0;

			/* try to restart on next line */
			while (count < HDR_SIZE && buf[count] != '\n')
				count++;
			if (buf[count] == '\n')
				count++; /* move past '\n' */
			if (HDR_SIZE - count > 0) {
				int left = HDR_SIZE - count;

				memmove(buf, buf + count, left);
				ptr = buf + left;

				goto readhdr;
			}

			continue;
		}

		if (hdr->ph_len == 0)
			continue;

		count = hdr->ph_len - HDR_SIZE;
readmore:
		rc = read(fdin, ptr, count);
		if (rc <= 0)
			break;

		ptr += rc;
		count -= rc;
		if (count > 0)
			goto readmore;

		first_bad = 1;

		if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
		    (hdr->ph_mask && !(debug_mask & hdr->ph_mask))) {
			dropped++;
			continue;
		}

retry_alloc:
		line = malloc(sizeof(*line));
		if (line == NULL) {
			if (linev) {
				fprintf(stderr, "error: line malloc(%u): "
					"printing accumulated records\n",
					(unsigned int)sizeof(*line));
				print_rec(&linev, kept, fdout);

				goto retry_alloc;
			}
			fprintf(stderr, "error: line malloc(%u): exiting\n",
				(unsigned int)sizeof(*line));
			break;
		}

		line->hdr = malloc(hdr->ph_len + 1);
		if (line->hdr == NULL) {
			free(line);
			if (linev) {
				fprintf(stderr, "error: hdr malloc(%u): "
					"printing accumulated records\n",
					hdr->ph_len + 1);
				print_rec(&linev, kept, fdout);

				goto retry_alloc;
			}
			fprintf(stderr, "error: hdr malloc(%u): exiting\n",
					hdr->ph_len + 1);
			break;
		}

		ptr = (void *)line->hdr;
		memcpy(line->hdr, buf, hdr->ph_len);
		ptr[hdr->ph_len] = '\0';

		ptr += sizeof(*hdr);
		line->file = ptr;
		ptr += strlen(line->file) + 1;
		line->fn = ptr;
		ptr += strlen(line->fn) + 1;
		line->text = ptr;

retry_add:
		if (add_rec(line, &linev, &linev_len, kept) < 0) {
			if (linev) {
				fprintf(stderr, "error: add_rec[%u] failed; "
					"print accumulated records\n",
					linev_len);
				print_rec(&linev, kept, fdout);

				goto retry_add;
			}
			fprintf(stderr, "error: add_rec[0] failed; exiting\n");
			break;
		}
		kept++;
	}

print:
	if (linev)
		print_rec(&linev, kept, fdout);

	printf("Debug log: %lu lines, %lu kept, %lu dropped, %lu bad.\n",
		dropped + kept + bad, kept, dropped, bad);

	return 0;
}
Ejemplo n.º 24
0
int main (int argc, char * argv[] )
{

	FILE *outputFile;
	int sock;                           //This will be our socket
	struct sockaddr_in sin, remote;     //"Internet socket address structure"
	unsigned int remote_length;         //length of the sockaddr_in structure
	char buffer[MAXBUFSIZE];             //a buffer to store our received message
	if(argc<5)
	{
		printf("usage : %s <server_port> <error_rate> <random_seed> <output_file> <receive_log> \n", argv[0]);
		exit(1);
	}


    if ((fp = fopen(argv[5],"w")) == NULL)
    {
    	printf("Unable to open log file %s\n", argv[5]);
    	exit(1);
    }
    if ((outputFile = fopen(argv[4],"w")) == NULL)
    {
    	printf("Unable to open output file %s\n", argv[4]);
    	exit(1);
    }
    fprintf(LOGFILE,"Server starting up!\n");

	/******************
     This code populates the sockaddr_in struct with
     the information about our socket
	 ******************/
	bzero(&sin,sizeof(sin));                    //zero the struct
	sin.sin_family = AF_INET;                   //address family
	sin.sin_port = htons(atoi(argv[1]));        //htons() sets the port # to network byte order
	sin.sin_addr.s_addr = INADDR_ANY;           //supplies the IP address of the local machine


	//Causes the system to create a generic socket of type UDP (datagram)
    if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        perror("client: socket");
        exit(1);
    }

	/******************
     Once we've created a socket, we must bind that socket to the
     local address and port we've supplied in the sockaddr_in struct
	 ******************/
	if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
	{
		perror("unable to bind socket\n");
        exit(1);
	}

	remote_length = sizeof(remote);
    struct sockaddr_in from_addr;
    int addr_length = sizeof(struct sockaddr);
    bzero(buffer,sizeof(buffer));

    MsgRec *rec;
    int i = 0;
    int rws = 6;
    int laf = rws;
    int lfr = 0;
    int totalExpectedPackets = -1;
    MsgRec *receivedMsg;
    int prevPacketId = 0;
    int packetId = 0;
    int expectedPacket = 0;
    int totalReceivedPackets = 0;
    int increasing_mode = 0;

    while (totalReceivedPackets != totalExpectedPackets)
    {
    	fprintf(LOGFILE,"Packet structure:\n");
    	int c;
        for (c = lfr-1; c < laf + 3;c++ )
        {
        	if (c == lfr)
        		fprintf(LOGFILE,"[lfr -> %-2d] ", c);
        	else if (c == laf)
        		fprintf(LOGFILE,"[laf -> %-2d] ", c);
        	else
        		fprintf(LOGFILE,"[%-2d] ", c);
        }
        fprintf(LOGFILE,"\n rws = %d\n", laf - lfr);

        // wait for command from client
        int numbytes;
        bzero(buffer,sizeof(buffer));
        if ((numbytes = recvfrom(sock, buffer, sizeof(buffer)-1, 0,
                                 (struct sockaddr *)&from_addr, &addr_length)) == -1)
        {
            perror("recvfrom");
            exit(1);
        }
    	if (increasing_mode && (laf - lfr) < rws)
    	{
    		laf++;  // keep increasing until it is back up to RWS
    		fprintf(LOGFILE,"Increasing mode activated.   LAF is now at %d\n", laf);
    	}
        rec = (MsgRec *) buffer;
        if (i == 0)
        {
        	// then read the num packets
        	fprintf(LOGFILE,"Total expected number of packets [%s]\n", rec->numPackets);
        	totalExpectedPackets = atoi(rec->numPackets);
        	receivedMsg = malloc(totalExpectedPackets*sizeof(MsgRec));
        }
        fprintf(LOGFILE,"Receiving:\n");
        print_rec(*rec);
        time_t now;
        time(&now);
		fprintf(LOGFILE,"Receive seq=%s free_slots=%d LFRead=%d LFRcvd=%d LAF=%d time=%s\n", rec->packetId, laf - lfr, lfr-1,lfr,laf,ctime(&now));



        packetId = atoi(rec->packetId);
        i++;

        if (i > PACKET_SLOWDOWN && !increasing_mode)
        {
        	if (laf > 0 && (laf - lfr) >= 0)
        	{
        		fprintf(LOGFILE,"reducing frame size %d\n", --laf);
        	}
        }
        if (packetId != expectedPacket)
        {
        	// then packets arrive out of order
        	fprintf(LOGFILE,"Packets arrive out of order, just send the last ack back.  Duplicate ack sent");
        	sprintf(rec->rws,"%d", laf - lfr);
            sprintf(rec->ack,"%d",lfr-1);
            fprintf(LOGFILE,"Sending:\n");
            print_rec(*rec);
        	int len = sizeof(MsgRec);
			char *buf = malloc(len);
			memcpy(buf, rec, len);
			// <Send | Resend | Receive> <Seq #> [Free Slots] <LFRead> <LFRcvd> <LAF> <Time>
	        time_t now;
	        time(&now);
			fprintf(LOGFILE,"Resend seq=%s free_slots=%d LFRead=%d LFRcvd=%d LAF=%d time=%s\n", rec->packetId, laf - lfr, lfr-1,lfr,laf,ctime(&now));
            if ((numbytes = sendto_(sock,buf, len,0,(struct sockaddr *)&from_addr, addr_length)) == -1) {
                perror("talker: sendto");
                exit(1);
            }
            free(buf);
        }
        else if (packetId == expectedPacket && packetId <= laf)
        {
        	fprintf(LOGFILE,"packets are in order, proceed\n");
        	// packets are in order, proceed
        	expectedPacket++;
        	totalReceivedPackets++;
            prevPacketId = packetId;

            memcpy(&receivedMsg[lfr],rec,sizeof(MsgRec));
            fprintf(LOGFILE,"Payload[%d]: [%s]\n",lfr, receivedMsg[lfr].payload);

            sprintf(rec->rws,"%d", laf - lfr);
            sprintf(rec->ack,"%d",lfr);
            lfr++;
            laf++;
            int len = sizeof(MsgRec);
			char *buf = malloc(len);
			memcpy(buf, rec, len);
            fprintf(LOGFILE,"Sending:\n");
            print_rec(*rec);
            time_t now;
            time(&now);
			fprintf(LOGFILE,"send seq=%s free_slots=%d LFRead=%d LFRcvd=%d LAF=%d time=%s\n", rec->packetId, laf - lfr, lfr-1,lfr,laf,ctime(&now));
	        if ((numbytes = sendto_(sock,buf, len,0,(struct sockaddr *)&from_addr, addr_length)) == -1) {
	            perror("talker: sendto");
	            exit(1);
	        }
	        free(buf);
        }
        else if (packetId > laf)
		{
			// then it is outside of the excepted frame, send it back
			fprintf(LOGFILE,"Packets arrive faster than can be processed");
			sprintf(rec->rws,"%d", 0);
			sprintf(rec->ack,"%d",0);

			int len = sizeof(MsgRec);
			char *buf = malloc(len);
			memcpy(buf, rec, len);
			fprintf(LOGFILE,"Sending:\n");
			print_rec(*rec);
	        time_t now;
	        time(&now);
			fprintf(LOGFILE,"resend seq=%s free_slots=%d LFRead=%d LFRcvd=%d LAF=%d time=%s\n", rec->packetId, laf - lfr, lfr-1,lfr,laf,ctime(&now));
			if ((numbytes = sendto_(sock,buf, len,0,(struct sockaddr *)&from_addr, addr_length)) == -1) {
				perror("talker: sendto");
				exit(1);
			}
			free(buf);
			fprintf(LOGFILE,"Sleeping 10 millseconds...\n");
			fflush(LOGFILE);
			fd_set select_fds;
			struct timeval timeout;

			FD_ZERO(&select_fds);
			FD_SET(sock, &select_fds);

			timeout.tv_sec = 0;
			timeout.tv_usec = 10000;  // 10ms

			select(sock+1, &select_fds, NULL,NULL, &timeout);
			fprintf(LOGFILE,"awake!!\n");
			increasing_mode = 1;
		}
        // print out the packets stored
        fprintf(LOGFILE,"Packet storage:\n");
        for (c = 0; c < totalExpectedPackets; c++)
        {
        	if (((c+1) % 10) == 0 )
        	{
        		fprintf(LOGFILE,"[%-2s]\n",receivedMsg[c].packetId);
        	}
        	else
        	{
        		fprintf(LOGFILE,"[%-2s]-",receivedMsg[c].packetId);
        	}
        }
        fprintf(LOGFILE,"\n");


    } // end while


    // then write the packets out to the out file
    fprintf(LOGFILE,"Writing out output file...\n");
    int c;
    for (c = 0; c < totalExpectedPackets; c++)
    {
    	fwrite(receivedMsg[c].payload, sizeof(char), PACKET_SIZE,outputFile);
    }
    fclose(outputFile);
    fclose(fp);

    free(receivedMsg);
	close(sock);
}
Ejemplo n.º 25
0
void Tree::print()
{
  top();
  print_rec();
}
Ejemplo n.º 26
0
int get_hhdr (char *fname, struct Hdr_rec *hdr_rec, char *msg)
{
   char      header[80000];
   char      str[132];
   FILE     *fptr;
   int       i, status, csys, nfailed, first_failed, clockwise;
   double    lon, lat, equinox;
   double    ra2000, dec2000;
   double    ra, dec;
   double    x1, y1, z1;
   double    x2, y2, z2;

   double    dtr = 1.745329252e-2;

   struct WorldCoor *wcs;

   struct stat buf;

   nfailed      = 0;
   first_failed = 0;
   status       = 0;

   fptr = fopen(fname, "r");
 
   if(fptr == (FILE *)NULL)
   {
      sprintf (msg, "Cannot open header file %s", fname);

      if(showbad)
      {
	 printf("[struct stat=\"INFO\", msg=\"Cannot open file\", file=\"%s\"]\n",
	    fname);
	 
	 fflush(stdout);
      }
      return (1);
   }

   stat(fname, &buf);

   hdr_rec->size = buf.st_size;

   hdr_rec->hdu = 1;

   strcpy(header, "");

   while(1)
   {
      if(fgets(str, 80, fptr) == (char *)NULL)
         break;
      
      while(str[strlen(str)-1] == '\n'
         || str[strlen(str)-1] == '\r')
            str[strlen(str)-1] =  '\0';

      for(i=strlen(str); i<80; ++i)
	 str[i] = ' ';

      str[80] = '\0';

      strcat(header, str);
   }

   wcs = wcsinit(header);

   if(wcs == (struct WorldCoor *)NULL) 
   {
      if(hdr_rec->hdu == 1)
	 first_failed = 1;

      ++nfailed;

      if(showbad)
      {
	 printf("[struct stat=\"INFO\", msg=\"WCS lib init failure\", file=\"%s\", hdu=%d]\n",
	    fname, hdr_rec->hdu);
	 
	 fflush(stdout);
      }

      ++hdr_rec->hdu;
      
      return(1);
   } 

   if(checkWCS(wcs, 1) == 1)
   {
      if(debug)
      {
	 printf("Bad WCS for file %s\n", fname);
	 fflush(stdout);
      }

      wcs = (struct WorldCoor *)NULL;

      if(hdr_rec->hdu == 1)
	 first_failed = 1;

      ++nfailed;

      if(showbad)
      {
	 printf("[struct stat=\"INFO\", msg=\"Bad WCS\", file=\"%s\", hdu=%d]\n",
	    fname, hdr_rec->hdu);
	 
	 fflush(stdout);
      }

      ++hdr_rec->hdu;

      return(1);
   }

   hdr_rec->ns = (int) wcs->nxpix;
   hdr_rec->nl = (int) wcs->nypix;

   strcpy(hdr_rec->ctype1, wcs->ctype[0]);
   strcpy(hdr_rec->ctype2, wcs->ctype[1]);

   hdr_rec->crpix1  = wcs->xrefpix;
   hdr_rec->crpix2  = wcs->yrefpix;
   hdr_rec->equinox = wcs->equinox;
   hdr_rec->crval1  = wcs->xref;
   hdr_rec->crval2  = wcs->yref;
   hdr_rec->cdelt1  = wcs->xinc;
   hdr_rec->cdelt2  = wcs->yinc;
   hdr_rec->crota2  = wcs->rot;

   if(hdr_rec->cdelt1 > 0.
   && hdr_rec->cdelt2 > 0.
   && (hdr_rec->crota2 < -90. || hdr_rec->crota2 > 90.))
   {
      hdr_rec->cdelt1 = -hdr_rec->cdelt1;
      hdr_rec->cdelt2 = -hdr_rec->cdelt2;

      hdr_rec->crota2 += 180.;

      while(hdr_rec->crota2 >= 360.)
	 hdr_rec->crota2 -= 360.;

      while(hdr_rec->crota2 <= -360.)
	 hdr_rec->crota2 += 360.;
   }


   /* Convert center of image to sky coordinates */

   csys = EQUJ;

   if(strncmp(hdr_rec->ctype1, "RA",   2) == 0)
      csys = EQUJ;
   if(strncmp(hdr_rec->ctype1, "GLON", 4) == 0)
      csys = GAL;
   if(strncmp(hdr_rec->ctype1, "ELON", 4) == 0)
      csys = ECLJ;

   equinox = hdr_rec->equinox;

   pix2wcs (wcs, hdr_rec->ns/2., hdr_rec->nl/2., &lon, &lat);


   /* Convert lon, lat to EQU J2000 */

   convertCoordinates (csys, equinox, lon, lat,
		       EQUJ, 2000., &ra2000, &dec2000, 0.);

   hdr_rec->ra2000  = ra2000;
   hdr_rec->dec2000 = dec2000;

   clockwise = 0;

   if((hdr_rec->cdelt1 < 0 && hdr_rec->cdelt2 < 0)
   || (hdr_rec->cdelt1 > 0 && hdr_rec->cdelt2 > 0)) clockwise = 1;


   if(clockwise)
   {
      pix2wcs(wcs, -0.5, -0.5, &lon, &lat);
      convertCoordinates (csys, equinox, lon, lat,
			  EQUJ, 2000., &ra, &dec, 0.);

      hdr_rec->ra1 = ra;
      hdr_rec->dec1 = dec;


      pix2wcs(wcs, wcs->nxpix+0.5, -0.5, &lon, &lat);
      convertCoordinates (csys, equinox, lon, lat,
			  EQUJ, 2000., &ra, &dec, 0.);

      hdr_rec->ra2 = ra;
      hdr_rec->dec2 = dec;


      pix2wcs(wcs, wcs->nxpix+0.5, wcs->nypix+0.5, &lon, &lat);
      convertCoordinates (csys, equinox, lon, lat,
			  EQUJ, 2000., &ra, &dec, 0.);

      hdr_rec->ra3 = ra;
      hdr_rec->dec3 = dec;


      pix2wcs(wcs, -0.5, wcs->nypix+0.5, &lon, &lat);
      convertCoordinates (csys, equinox, lon, lat,
			  EQUJ, 2000., &ra, &dec, 0.);

      hdr_rec->ra4 = ra;
      hdr_rec->dec4 = dec;
   }
   else
   {
      pix2wcs(wcs, -0.5, -0.5, &lon, &lat);
      convertCoordinates (csys, equinox, lon, lat,
			  EQUJ, 2000., &ra, &dec, 0.);

      hdr_rec->ra1 = ra;
      hdr_rec->dec1 = dec;


      pix2wcs(wcs, wcs->nxpix+0.5, -0.5, &lon, &lat);
      convertCoordinates (csys, equinox, lon, lat,
			  EQUJ, 2000., &ra, &dec, 0.);

      hdr_rec->ra2 = ra;
      hdr_rec->dec2 = dec;


      pix2wcs(wcs, wcs->nxpix+0.5, wcs->nypix+0.5, &lon, &lat);
      convertCoordinates (csys, equinox, lon, lat,
			  EQUJ, 2000., &ra, &dec, 0.);

      hdr_rec->ra3 = ra;
      hdr_rec->dec3 = dec;


      pix2wcs(wcs, -0.5, wcs->nypix+0.5, &lon, &lat);
      convertCoordinates (csys, equinox, lon, lat,
			  EQUJ, 2000., &ra, &dec, 0.);

      hdr_rec->ra4 = ra;
      hdr_rec->dec4 = dec;
   }


   x1 = cos(hdr_rec->ra2000*dtr) * cos(hdr_rec->dec2000*dtr);
   y1 = sin(hdr_rec->ra2000*dtr) * cos(hdr_rec->dec2000*dtr);
   z1 = sin(hdr_rec->dec2000*dtr);

   x2 = cos(ra*dtr) * cos(dec*dtr);
   y2 = sin(ra*dtr) * cos(dec*dtr);
   z2 = sin(dec*dtr);

   hdr_rec->radius = acos(x1*x2 + y1*y2 + z1*z2) / dtr;

   hdr_rec->cntr = cntr;
   print_rec (hdr_rec);

   status = 0;
   fclose(fptr);

   return(nfailed);
}