Beispiel #1
0
int valid_leave(object me, string dir)
{
	int count;

	if (member_array(dir, dirs) != -1)
	{
		if (dir == "east")
		{
//			write("*水生木*\n");
			count = me->query_temp("wuxing/木");
			count++;
			me->set_temp("wuxing/木", count);
			if (check_out(me))
				return notify_fail("你顺利地走出了五行迷宫。\n");
		}
		else if (dir == "north")
		{
//			write("*水克火*\n");
			me->delete_temp("wuxing");
			me->move(__DIR__"jianyu1");
			return notify_fail("你掉进机关,落入僧监。\n");
		}
	}
	return ::valid_leave(me, dir);
}
Beispiel #2
0
int main() {
  struct hotel plaza;
  int ans,i;
  init_hotel(&plaza);

  // Asks the user to check out rooms.
  printf("Would you like check in(1), or check out(2) or quit(4)?");
  scanf("%d", &ans);

  // Execute the user's choices until they quit.
  while (ans != 4) {

    if (ans == 1) 
      get_room(&plaza);
    else if (ans == 2)
      check_out(&plaza);
    else if (ans == 3) 
      print_all(&plaza);

    // Get the user's next choice.
    printf("Would you like check in(1), or check out(2) or quit(3)?");
    scanf("%d", &ans);
  }

  return 0;
}
Beispiel #3
0
/*
**  LONG RANGE OF SCANNERS
**
**	A summary of the quadrants that surround you is printed.  The
**	hundreds digit is the number of Klingons in the quadrant,
**	the tens digit is the number of starbases, and the units digit
**	is the number of stars.  If the printout is "///" it means
**	that that quadrant is rendered uninhabitable by a supernova.
**	It also updates the "scanned" field of the quadrants it scans,
**	for future use by the "chart" option of the computer.
*/
void
lrscan()
{
	register int			i, j;
	register struct quad		*q;

	if (check_out(LRSCAN))
	{
		return;
	}
	printf("Long range scan for quadrant %d,%d\n\n", Ship.quadx, Ship.quady);

	/* print the header on top */
	for (j = Ship.quady - 1; j <= Ship.quady + 1; j++)
	{
		if (j < 0 || j >= NQUADS)
			printf("      ");
		else
			printf("     %1d", j);
	}

	/* scan the quadrants */
	for (i = Ship.quadx - 1; i <= Ship.quadx + 1; i++)
	{
		printf("\n  -------------------\n");
		if (i < 0 || i >= NQUADS)
		{
			/* negative energy barrier */
			printf("  !  *  !  *  !  *  !");
			continue;
		}

		/* print the left hand margin */
		printf("%1d !", i);
		for (j = Ship.quady - 1; j <= Ship.quady + 1; j++)
		{
			if (j < 0 || j >= NQUADS)
			{
				/* negative energy barrier again */
				printf("  *  !");
				continue;
			}
			q = &Quad[i][j];
			if (q->stars < 0)
			{
				/* supernova */
				printf(" /// !");
				q->scanned = 1000;
				continue;
			}
			q->scanned = q->klings * 100 + q->bases * 10 + q->stars;
			printf(" %3d !", q->scanned);
		}
	}
	printf("\n  -------------------\n");
}
Beispiel #4
0
int do_select(fd_set in, fd_set out, fd_set ex,
	fd_set *inp, fd_set *outp, fd_set *exp)
{
	int count;
	select_table wait_table;
	int i;
	fd_set mask;

	mask = in | out | ex;
	for (i = 0 ; i < NR_OPEN ; i++,mask >>= 1) {
		if (!(mask & 1))
			continue;
		if (!current->filp[i])
			return -EBADF;
		if (!current->filp[i]->f_inode)
			return -EBADF;
		if (current->filp[i]->f_inode->i_pipe)
			continue;
		if (S_ISCHR(current->filp[i]->f_inode->i_mode))
			continue;
		if (S_ISFIFO(current->filp[i]->f_inode->i_mode))
			continue;
		return -EBADF;
	}
repeat:
	wait_table.nr = 0;
	*inp = *outp = *exp = 0;
	count = 0;
	mask = 1;
	for (i = 0 ; i < NR_OPEN ; i++, mask += mask) {
		if (mask & in)
			if (check_in(&wait_table,current->filp[i]->f_inode)) {
				*inp |= mask;
				count++;
			}
		if (mask & out)
			if (check_out(&wait_table,current->filp[i]->f_inode)) {
				*outp |= mask;
				count++;
			}
		if (mask & ex)
			if (check_ex(&wait_table,current->filp[i]->f_inode)) {
				*exp |= mask;
				count++;
			}
	}
	if (!(current->signal & ~current->blocked) &&
	    (wait_table.nr || current->timeout) && !count) {
		current->state = TASK_INTERRUPTIBLE;
		schedule();
		free_wait(&wait_table);
		goto repeat;
	}
	free_wait(&wait_table);
	return count;
}
Beispiel #5
0
void init()
{       
	object me=this_player();

	if( me->query_temp("xmaze2") == 0 )
	{
		me->set_temp("xmaze2", 1);
	}
	else if( me->query_temp("xmaze2") == 1 )
	{
		reset_path(me);
	}
	
	if( check_out(me) ) me->move(__DIR__"xueshan4");
}
Beispiel #6
0
void init()
{       
   object me=this_player();

   add_action("do_jump", "jump");

   if( me->query_temp("wzgmaze1") == 0 )
   {
     me->set_temp("wzgmaze1", 1);
   }
   else if( me->query_temp("wzgmaze1") == 1 )
   {
     reset_path(me);
   }
   
   if( check_out(me) ) me->move(__DIR__"northpool");
}
Beispiel #7
0
void init()
{       
        object me=this_player();

        add_action("do_hujiu", "hujiu");

        if( me->query_temp("xmaze10") == 0 )
        {
                me->set_temp("xmaze10", 1);
        }
        else if( me->query_temp("xmaze10") == 1 )
        {
                reset_path(me);
        }
        
        if( check_out(me) ) me->move(__DIR__"baozang0");
}
Beispiel #8
0
void  init()
{              
	object  me=this_player();

	if(  me->query_temp("tree7")  ==  0  )
	{
		me->set_temp("tree7",  1);
	}
	else  if(  me->query_temp("tree7")  ==  1  )
	{
		reset_path(me);
	}
	
	if(  check_out(me)  ) 
	{ 
        set("exits/north", __DIR__"shijie");
        call_out("close_exit_shijie", 5);
	}
}
Beispiel #9
0
void init()
{       
   object me=this_player();

   if( me->query_temp("xmaze7") == 0 )
   {
     me->set_temp("xmaze7", 1);
   }
   else if( me->query_temp("xmaze7") == 1 )
   {
     reset_path(me);
   }
   
   if( check_out(me) )
   { 
     if( random(10) < 5 ) me->move(__DIR__"xueshan4");
     else me->move(__DIR__"xueshan3");
   }
}
Beispiel #10
0
/*ARGSUSED*/
void
torped(int v __unused)
{
	int		ix, iy;
	double		x, y, dx, dy;
	double		angle;
	int		course, course2;
	int		k;
	double		bigger;
	double		sectsize;
	int		burst;
	int		n;

	if (Ship.cloaked) {
		printf("Federation regulations do not permit attack while "
		       "cloaked.\n");
		return;
	}
	if (check_out(TORPED))
		return;
	if (Ship.torped <= 0) {
		printf("All photon torpedos expended\n");
		return;
	}

	/* get the course */
	course = getintpar("Torpedo course");
	if (course < 0 || course > 360)
		return;
	burst = -1;

	/* need at least three torpedoes for a burst */
	if (Ship.torped < 3) {
		printf("No-burst mode selected\n");
		burst = 0;
	} else {
		/* see if the user wants one */
		if (!testnl()) {
			k = ungetc(getchar(), stdin);
			if (k >= '0' && k <= '9')
				burst = 1;
		}
	}
	if (burst < 0) {
		burst = getynpar("Do you want a burst");
	}
	if (burst) {
		burst = getintpar("burst angle");
		if (burst <= 0)
			return;
		if (burst > 15) {
			printf("Maximum burst angle is 15 degrees\n");
			return;
		}
	}
	sectsize = NSECTS;
	n = -1;
	if (burst) {
		n = 1;
		course -= burst;
	}
	for (; n && n <= 3; n++) {
		/* select a nice random course */
		course2 = course + randcourse(n);
		/* convert to radians */
		angle = course2 * 0.0174532925;
		dx = -cos(angle);
		dy =  sin(angle);
		bigger = fabs(dx);
		x = fabs(dy);
		if (x > bigger)
			bigger = x;
		dx /= bigger;
		dy /= bigger;
		x = Ship.sectx + 0.5;
		y = Ship.secty + 0.5;
		if (Ship.cond != DOCKED)
			Ship.torped -= 1;
		printf("Torpedo track");
		if (n > 0)
			printf(", torpedo number %d", n);
		printf(":\n%6.1f\t%4.1f\n", x, y);
		while (1) {
			ix = x += dx;
			iy = y += dy;
			if (x < 0.0 || x >= sectsize ||
			    y < 0.0 || y >= sectsize) {
				printf("Torpedo missed\n");
				break;
			}
			printf("%6.1f\t%4.1f\n", x, y);
			switch (Sect[ix][iy]) {
			  case EMPTY:
				continue;

			  case HOLE:
				printf("Torpedo disappears into a black "
				       "hole\n");
				break;

			  case KLINGON:
				for (k = 0; k < Etc.nkling; k++) {
					if (Etc.klingon[k].x != ix ||
					    Etc.klingon[k].y != iy)
						continue;
					Etc.klingon[k].power -= 500 + ranf(501);
					if (Etc.klingon[k].power > 0) {
						printf("*** Hit on Klingon at "
						       "%d,%d: extensive "
						       "damages\n",
							ix, iy);
						break;
					}
					killk(ix, iy);
					break;
				}
				break;

			  case STAR:
				nova(ix, iy);
				break;

			  case INHABIT:
				kills(ix, iy, -1);
				break;

			  case BASE:
				killb(Ship.quadx, Ship.quady);
				Game.killb += 1;
				break;

			  default:
				printf("Unknown object %c at %d,%d destroyed\n",
					Sect[ix][iy], ix, iy);
				Sect[ix][iy] = EMPTY;
				break;
			}
			break;
		}
		if (damaged(TORPED) || Quad[Ship.quadx][Ship.quady].stars < 0)
			break;
		course += burst;
	}
	Move.free = 0;
}
int write_to_dev(char * buf, int buf_len, int len, SOCKET sockfd,
		HANDLE devfd, OVERLAPPED *ov)
{
	int ret;
	unsigned long out=0, in_len, iso_len;
	struct usbip_header * u = (struct usbip_header *)buf;

	if(len!=sizeof(*u)){
		err("read from sock ret %d not equal a usbip_header", len);
#ifdef DEBUG
		usbip_dump_buffer(buf,len);
#endif
		return -1;
	}
	if(usbip_header_correct_endian(u, 0)<0)
		return -1;
	dbg_file("recv seq %d\n", u->base.seqnum);
	if ((u->base.seqnum%100)==0)
		fprintf(stderr,"Receive sequence:    %d\r", u->base.seqnum);

#ifdef DEBUG
	usbip_dump_header(u);
#endif

	if(check_out(htonl(u->base.seqnum)))
		in_len=0;
	else
		in_len=u->u.ret_submit.actual_length;

	iso_len = u->u.ret_submit.number_of_packets
			* sizeof(struct usbip_iso_packet_descriptor);

	if(in_len==0&&iso_len==0){
		ret=WriteFile(devfd, (char *)u, sizeof(*u), &out, ov);
		if(!ret||out!=sizeof(*u)){
			err("last error:%ld",GetLastError());
			err("out:%ld ret:%d",out,ret);
			err("write dev failed");
			return -1;
		}
		return 0;
	}
	len = sizeof(*u) + in_len + iso_len;
	if(len>buf_len){
		err("too big len %d %ld %ld", len, in_len,iso_len);
		return -1;
	}
	ret=usbip_recv(sockfd, buf+sizeof(*u),
		in_len+iso_len);
	if(ret != in_len + iso_len){
		err("recv from sock failed %d %ld",
				ret,
				in_len + iso_len);
		return -1;
	}

	if(iso_len)
		fix_iso_desc_endian(sock_read_buf+sizeof(*u)+in_len,
					u->u.ret_submit.number_of_packets);
	ret=WriteFile(devfd, buf, len, &out, ov);
	if(!ret||out!=len){
		err("last error:%ld\n",GetLastError());
		err("out:%ld ret:%d len:%d\n",out,ret,len);
		err("write dev failed");
		return -1;
	}
	return 0;
}
Beispiel #12
0
computer()
{
	int			ix, iy;
	register int		i, j;
	int			numout;
	int			tqx, tqy;
	struct cvntab		*r;
	int			cost;
	int			course;
	double			dist, time;
	double			warpfact;
	struct quad		*q;
	register struct event	*e;

	if (check_out(COMPUTER))
		return;
	while (1)
	{
		r = getcodpar("\nRequest", Cputab);
		switch ((int)r->value)
		{

		  case 1:			/* star chart */
			printf("Computer record of galaxy for all long range sensor scans\n\n");
			printf("  ");
			/* print top header */
			for (i = 0; i < NQUADS; i++)
				printf("-%d- ", i);
			printf("\n");
			for (i = 0; i < NQUADS; i++)
			{
				printf("%d ", i);
				for (j = 0; j < NQUADS; j++)
				{
					if (i == Ship.quadx && j == Ship.quady)
					{
						printf("$$$ ");
						continue;
					}
					q = &Quad[i][j];
					/* 1000 or 1001 is special case */
					if (q->scanned >= 1000)
						if (q->scanned > 1000)
							printf(".1. ");
						else
							printf("/// ");
					else
						if (q->scanned < 0)
							printf("... ");
						else
							printf("%3d ", q->scanned);
				}
				printf("%d\n", i);
			}
			printf("  ");
			/* print bottom footer */
			for (i = 0; i < NQUADS; i++)
				printf("-%d- ", i);
			printf("\n");
			break;

		  case 2:			/* trajectory */
			if (check_out(SRSCAN))
			{
				break;
			}
			if (Etc.nkling <= 0)
			{
				printf("No Klingons in this quadrant\n");
				break;
			}
			/* for each Klingon, give the course & distance */
			for (i = 0; i < Etc.nkling; i++)
			{
				printf("Klingon at %d,%d", Etc.klingon[i].x, Etc.klingon[i].y);
				course = kalc(Ship.quadx, Ship.quady, Etc.klingon[i].x, Etc.klingon[i].y, &dist);
				prkalc(course, dist);
			}
			break;

		  case 3:			/* course calculation */
			if (readdelim('/'))
			{
				tqx = Ship.quadx;
				tqy = Ship.quady;
			}
			else
			{
				ix = getintpar("Quadrant");
				if (ix < 0 || ix >= NSECTS)
					break;
				iy = getintpar("q-y");
				if (iy < 0 || iy >= NSECTS)
					break;
				tqx = ix;
				tqy = iy;
			}
			ix = getintpar("Sector");
			if (ix < 0 || ix >= NSECTS)
				break;
			iy = getintpar("s-y");
			if (iy < 0 || iy >= NSECTS)
				break;
			course = kalc(tqx, tqy, ix, iy, &dist);
			if (r->value2)
			{
				warp(-1, course, dist);
				break;
			}
			printf("%d,%d/%d,%d to %d,%d/%d,%d",
				Ship.quadx, Ship.quady, Ship.sectx, Ship.secty, tqx, tqy, ix, iy);
			prkalc(course, dist);
			break;

		  case 4:			/* score */
			score();
			break;

		  case 5:			/* phaser effectiveness */
			dist = getfltpar("range");
			if (dist < 0.0)
				break;
			dist *= 10.0;
			cost = pow(0.90, dist) * 98.0 + 0.5;
			printf("Phasers are %d%% effective at that range\n", cost);
			break;

		  case 6:			/* warp cost (time/energy) */
			dist = getfltpar("distance");
			if (dist < 0.0)
				break;
			warpfact = getfltpar("warp factor");
			if (warpfact <= 0.0)
				warpfact = Ship.warp;
			cost = (dist + 0.05) * warpfact * warpfact * warpfact;
			time = Param.warptime * dist / (warpfact * warpfact);
			printf("Warp %.2f distance %.2f cost %.2f stardates %d (%d w/ shlds up) units\n",
				warpfact, dist, time, cost, cost + cost);
			break;

		  case 7:			/* impulse cost */
			dist = getfltpar("distance");
			if (dist < 0.0)
				break;
			cost = 20 + 100 * dist;
			time = dist / 0.095;
			printf("Distance %.2f cost %.2f stardates %d units\n",
				dist, time, cost);
			break;

		  case 8:			/* distresslist */
			j = 1;
			printf("\n");
			/* scan the event list */
			for (i = 0; i < MAXEVENTS; i++)
			{
				e = &Event[i];
				/* ignore hidden entries */
				if (e->evcode & E_HIDDEN)
					continue;
				switch (e->evcode & E_EVENT)
				{

				  case E_KDESB:
					printf("Klingon is attacking starbase in quadrant %d,%d\n",
						e->x, e->y);
					j = 0;
					break;

				  case E_ENSLV:
				  case E_REPRO:
					printf("Starsystem %s in quadrant %d,%d is distressed\n",
						Systemname[e->systemname], e->x, e->y);
					j = 0;
					break;
				}
			}
			if (j)
				printf("No known distress calls are active\n");
			break;

		}

		/* skip to next semicolon or newline.  Semicolon
		 * means get new computer request; newline means
		 * exit computer mode. */
		while ((i = cgetc(0)) != ';')
		{
			if (i == '\0')
				exit(1);
			if (i == '\n')
			{
				ungetc(i, stdin);
				return;
			}
		}
	}
}
Beispiel #13
0
void create_tcp_packet(struct tcphdr2 *tcp,struct ip* ip, int size_payload,const u_char* packet){

    char errbuf[LIBNET_ERRBUF_SIZE];
    libnet_ptag_t tag=0,tag_tcp=0,ipv=0;    /* libnet protocol block */
    libnet_t * libnet_context;
    int set=0;
    int port_out,port_in;
    char* payload ;
    if(!strcmp("192.168.1.3",inet_ntoa(ip->ip_src)))set=1;
    else set=2;

    if(set==1){
       libnet_context = libnet_init(LIBNET_RAW4,"eth0", errbuf);
       port_out=check_out(ntohs(tcp->source));
    }
    else{
       libnet_context = libnet_init(LIBNET_RAW4,"eth1", errbuf);
       //if(ntohs(tcp->dest)!=9999){exit(0);}
       port_in=check_in(ntohs(tcp->dest));
       if(!port_in)return;//exit(0);return;}
    }

    if (libnet_context == NULL)
    {
        fprintf(stderr, "libnet_init() failed: %s", errbuf);
        exit(1);
    }
    fprintf(stderr, "libnet_init() seccess\n");
    payload=(char*) (packet+54);
    char* flags=(char*)(tcp+13);
    if((tcp->doff)*4 > 20)
    {
      fprintf(stderr, "(tcp->doff)*4 > 20\n");
      if(libnet_build_tcp_options(/*(u_int8_t *)(tcp+20)*/(u_int8_t *)packet+54,(tcp->doff)*4 -20,libnet_context,0)==-1)
      {
        printf("tcp_option error\n\n");
      }
      //payload=(char*)( packet+(tcp->doff)*4-20+54);
      printf("tcp_option\n");
    }
     fprintf(stderr, "libnet_init() seccess2\n");
    if (size_payload == 0) payload = NULL;
    if(set==1){
       // port_test=ntohs(tcp->source);
    tag_tcp = libnet_build_tcp (
                                //ntohs(tcp->source),                             /* src port */
                                //9999,
                                port_out,
                                ntohs(tcp->dest),   			    /* destination port */
                                ntohl(tcp->seq),    	                    /* sequence number */
                                ntohl(tcp->ack_seq), 			            /* acknowledgement */
                                tcp->th_flags,
                                //(*flags), 			    /* control flags */
                                ntohs(tcp->window),   			    /* window */
                                   0 ,                                                /* checksum - 0 = autofill */
                                ntohs(tcp->urg_ptr),                               /* urgent */
                                (tcp->doff)*4+size_payload,                        /* total tcp size */
                                payload,                                          /* payload */
                                size_payload,                                     /* payload length */
                                libnet_context,                                   /* libnet context */
                                tag);
                                                                   /* protocol tag */

    }
    else{

     tag_tcp = libnet_build_tcp (
                                ntohs(tcp->source),                             /* src port */
                                //ntohs(tcp->dest),   			    /* destination port */
                                port_in,
                                //port_test,
                                ntohl(tcp->seq),    	                    /* sequence number */
                                ntohl(tcp->ack_seq), 			            /* acknowledgement */
                                tcp->th_flags, 			    /* control flags */
                                ntohs(tcp->window),   			    /* window */
                                   0 ,                                                /* checksum - 0 = autofill */
                                ntohs(tcp->urg_ptr),                               /* urgent */
                                (tcp->doff)*4+size_payload,                        /* total tcp size */
                                payload,                                          /* payload */
                                size_payload,                                     /* payload length */
                                libnet_context,                                   /* libnet context */
                                tag);


    }
    if (tag_tcp == -1)
    {
        fprintf (stderr,
                 "Unable to build TCP header: %s\n", libnet_geterror (libnet_context));
        exit (1);
    }
    //libnet_do_checksum(&libnet_context,tcp,IPPROTO_TCP,(tcp->doff));
    fprintf (stderr,"tcp_build OK\n");
    struct in_addr* tmp_src = &ip->ip_src;
    struct in_addr* tmp_dst = &ip->ip_dst;

    if(set==1){
    ipv = libnet_build_ipv4(
                ntohs(ip->ip_len),			  /* length */
                (ip->ip_tos),                           /* TOS */
                ntohs(ip->ip_id),                     /* IP ID */
                ntohs(ip->ip_off),                    /* IP Frag */
                (ip->ip_ttl),                           /* TTL */
               (ip->ip_p),                             /* protocol */
			    //IPPROTO_TCP,
			    0,                                    /* checksum */
                libnet_name2addr4(libnet_context,"140.114.195.31",LIBNET_DONT_RESOLVE),
			    *((u_int32_t*)(tmp_dst)), /* destination IP */
			    //libnet_name2addr4(libnet_context,"192.168.1.3",LIBNET_DONT_RESOLVE),
                            // *((u_int32_t*)(tmp_src)), /*src IP*/
                            NULL,                                 /* payload */
                            0,             	                       /* payload size */
                            libnet_context,                       /* libnet context */
                            tag);                                 /* ptag */

    }else if(set==2){
         ipv = libnet_build_ipv4(
                ntohs(ip->ip_len),			  /* length */
                (ip->ip_tos),                           /* TOS */
                ntohs(ip->ip_id),                     /* IP ID */
                ntohs(ip->ip_off),                    /* IP Frag */
                (ip->ip_ttl),                           /* TTL */
               (ip->ip_p),                             /* protocol */
			    //IPPROTO_TCP,
			    0,                                    /* checksum */
               // libnet_name2addr4(libnet_context,"192.168.1.0",LIBNET_DONT_RESOLVE),

                            *((u_int32_t*)(tmp_src)), /*src IP*/
                libnet_name2addr4(libnet_context,"192.168.1.3",LIBNET_DONT_RESOLVE),
                            //*((u_int32_t*)(tmp_dst)), /* destination IP */
                            NULL,                                 /* payload */
                            0,             	                       /* payload size */
                            libnet_context,                       /* libnet context */
                            tag);
    }
    else {
        printf("error iph\n");exit(0);
    }
    if (ipv == -1)
    {
        fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(libnet_context));

    }
    if (set==1) {
        //flag为1,带表从内网到外网,目的MAC:gateway  源MAC:localMACw
        libnet_build_ethernet(
        gatewayMAC, /* ethernet destination */
        LOCAL_MAC_OUTTER, /* ethernet source */
        ETHERTYPE_IP, /* protocol type */
        NULL, /* payload */
        0, /* payload size */
        libnet_context, /* libnet handle */
        0); /* libnet id */
    } else {
        //flag为0,代表从外网到内网,目的MAC:targetMAC 源MAC:localMACn
        libnet_build_ethernet(
        targetMAC, /* ethernet destination */
        LOCAL_MAC_INNER, /* ethernet source */
        ETHERTYPE_IP, /* protocol type */
        NULL, /* payload */
        0, /* payload size */
        libnet_context, /* libnet handle */
        0); /* libnet id */
    }
    if (libnet_write(libnet_context) == -1)
        fprintf(stderr, "Write error: %s\n", libnet_geterror(libnet_context));
    fprintf(stderr, "Send %u\n",(u_int16_t)tcp->doff);

    libnet_clear_packet(libnet_context);
    libnet_destroy(libnet_context);


}
Beispiel #14
0
int work(void)
{
	static int retcnt = 0;
	int i = 0;
	static pid_t ID[100];
	static int workcnt = 0;
	int runid = 0;
	int jobs[max_running * 2 + 1];
	pid_t tmp_pid = 0;

	/* get the database info */
	//获取判题任务
	if (!get_jobs(jobs)) {
		retcnt = 0;
	}

	/* exec the submit */
	int j = 0;
	for (j = 0; jobs[j] > 0; j++) {
		runid = jobs[j];
		if (runid % oj_tot != oj_mod) {
			continue;
		}
		write_log("judging solution %d.\n", runid);
		if (workcnt >= max_running) {	// if no more client can running
			//总共有4个判题的进程,等待任何一个退出,可以在配置
			//文件中设置个数
			tmp_pid = waitpid(-1, NULL, 0);	// wait 4 one child exit
			workcnt--;
			retcnt++;
			for (i = 0; i < max_running; i++) {	// get the client id
				if (ID[i] == tmp_pid) {
					break;	// got the client id
				}
			}
			ID[i] = 0;
		} else {	// have free client
			for (i = 0; i < max_running; i++)	// find the client id
				if (ID[i] == 0) {
					break;		// got the client id
				}
		}
		if (workcnt < max_running && check_out(runid, OJ_CI)) {
			workcnt++;
			ID[i] = fork();		// start to fork
			if (ID[i] == 0) {
				write_log("judge solution %d in client%d.\n",
						runid, i);
				// 子进程运行判题客户端
				run_client(runid, i);	// if the process is the son, run it
				exit(EXIT_SUCCESS);
			}
		} else {
			ID[i] = 0;
		}
	}
	while ((tmp_pid = waitpid(-1, NULL, WNOHANG)) > 0) {
		workcnt--;
		retcnt++;
		for (i = 0; i < max_running; i++) {	// get the client id
			if (ID[i] == tmp_pid) {
				break;	// got the client id
			}
		}
		ID[i] = 0;
		write_log("client%d judge done.\n", i);
	}
	mysql_free_result(res);	// free the memory
	executesql("commit");
	write_log("total %d solution judge done.\n", retcnt);

	return retcnt;
}