Ejemplo n.º 1
0
DWORD __declspec(dllexport) Init(SOCKET fd)
{
	// In the case of metsrv payloads, the parameter passed to init is NOT a socket, it's actually
	// a pointer to the metserv configuration, so do a nasty cast and move on.
	MetsrvConfig* metConfig = (MetsrvConfig*)fd;
	DWORD result = server_setup(metConfig);

	dprintf("[METSRV] Exiting with %08x", metConfig->session.exit_func);

	// We also handle exit func directly in metsrv now because the value is added to the
	// configuration block and we manage to save bytes in the stager/header as well.
	switch (metConfig->session.exit_func)
	{
	case EXITFUNC_SEH:
		SetUnhandledExceptionFilter(NULL);
		break;
	case EXITFUNC_THREAD:
		ExitThread(0);
		break;
	case EXITFUNC_PROCESS:
		ExitProcess(0);
		break;
	default:
		break;
	}
	return result;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
    context_t c;

    mrp_clear(&c);

    if (!parse_cmdline(&c, argc, argv))
        exit(1);

    mrp_log_set_mask(c.log_mask);
    mrp_log_set_target(c.log_target);

    if (c.server)
        mrp_log_info("Running as server, using D-BUS '%s'...", c.busaddr);
    else
        mrp_log_info("Running as client, using D-BUS '%s'...", c.busaddr);

    c.ml = mrp_mainloop_create();
    mrp_add_sighandler(c.ml, SIGINT , signal_handler, &c);

    if (c.server)
        server_setup(&c);
    else
        client_setup(&c);

    mrp_mainloop_run(c.ml);

    if (c.server)
        server_cleanup(&c);
    else
        client_cleanup(&c);

    return 0;
}
Ejemplo n.º 3
0
int main(int argc, char** argv){
  if(!argv[1]){
    puts("No IP input found. Please initiate in the form ' ./Player <Server IP> '");
    exit(1);
  }

  //fgets from stdin to get clientname (and pass that into run, ezpz)
  printf("Please enter your name: ");

  fgets(clientname, 64, stdin);
  clientname[strlen(clientname) - 1] = '\0';

  printf("Hello %s!\n", clientname);

  strcat(clientname, " : ");
  namelen = strlen(clientname);
  ///


  SDL_Surface* screen = initScreen(); 

  //Initiate the semaphores and chatserver thread
  msgbuf_sem = SDL_CreateSemaphore(1);
  textSem = SDL_CreateSemaphore(1);
  chatserv = SDL_CreateThread(chatserver, screen);


  //Initiate the server connection
  int socket_id = server_setup(argv[1]);
    
  
  //Initiate the client's sprite
  Uint32 colorkey = SDL_MapRGB( screen->format, 255, 255, 255);
  initiate(socket_id, colorkey); 



  TTF_Init();
  run(screen, socket_id);


  //CEASE AND DESIST

  SDL_KillThread(chatserv);

  SDL_FreeSurface(screen);
  SDL_Quit();
  TTF_Quit();

  int i;
  for(i=0; i<numMessages; i++)
    if(text[i] != NULL)
      SDL_FreeSurface(text[i]);

  SDL_DestroySemaphore(msgbuf_sem);
  SDL_DestroySemaphore(textSem);

  return 1;
}
Ejemplo n.º 4
0
int main(int argc, const char *argv[])
{
    int opt;
    poptContext pc;
    struct main_context *main_ctx;
    int ret;
    uid_t uid;
    gid_t gid;

    struct poptOption long_options[] = {
        POPT_AUTOHELP
        SSSD_MAIN_OPTS
        SSSD_SERVER_OPTS(uid, gid)
        POPT_TABLEEND
    };

    /* Set debug level to invalid value so we can decide if -d 0 was used. */
    debug_level = SSSDBG_INVALID;

    umask(DFL_RSP_UMASK);

    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
    while((opt = poptGetNextOpt(pc)) != -1) {
        switch(opt) {
        default:
            fprintf(stderr, "\nInvalid option %s: %s\n\n",
                  poptBadOption(pc, 0), poptStrerror(opt));
            poptPrintUsage(pc, stderr, 0);
            return 1;
        }
    }

    poptFreeContext(pc);

    DEBUG_INIT(debug_level);

    /* set up things like debug, signals, daemonization, etc... */
    debug_log_file = "sssd_pac";

    ret = server_setup("sssd[pac]", 0, uid, gid,
                       CONFDB_PAC_CONF_ENTRY, &main_ctx);
    if (ret != EOK) return 2;

    ret = die_if_parent_died();
    if (ret != EOK) {
        /* This is not fatal, don't return */
        DEBUG(SSSDBG_OP_FAILURE, "Could not set up to exit when parent process does\n");
    }

    ret = pac_process_init(main_ctx,
                           main_ctx->event_ctx,
                           main_ctx->confdb_ctx);
    if (ret != EOK) return 3;

    /* loop on main */
    server_loop(main_ctx);

    return 0;
}
Ejemplo n.º 5
0
int main( int argc, const char **argv )
{
  long serspeed;
  
  if( argc < 4 )
  {
    fprintf( stderr, "Usage: %s <port> <speed> <dirname> [-v]\n", argv[ 0 ] );
    fprintf( stderr, "(use -v for verbose output).\n");
    return 1;
  }
  if( secure_atoi( argv[ SPEED_ARG_IDX ], &serspeed ) == 0 )
  {
    fprintf( stderr, "Invalid speed\n" );
    return 1;
  } 
  if( !os_isdir( argv[ DIRNAME_ARG_IDX ] ) )
  {
    fprintf( stderr, "Invalid directory %s\n", argv[ DIRNAME_ARG_IDX ] );
    return 1;
  }
  if( ( argc >= 5 ) && !strcmp( argv[ VERBOSE_ARG_IDX ], "-v" ) )
    log_init( LOG_ALL );
  else
    log_init( LOG_NONE );

  // Setup RFS server
  server_setup( argv[ DIRNAME_ARG_IDX ] );

  // Setup serial port
  if( ( ser = ser_open( argv[ PORT_ARG_IDX ] ) ) == ( ser_handler )-1 )
  {
    fprintf( stderr, "Cannot open port %s\n", argv[ PORT_ARG_IDX ] );
    return 1;
  }
  if( ser_setup( ser, ( u32 )serspeed, SER_DATABITS_8, SER_PARITY_NONE, SER_STOPBITS_1 ) != SER_OK )
  {
    fprintf( stderr, "Unable to initialize serial port\n" );
    return 1;
  }
  flush_serial();
  
  // User report
  printf( "Running RFS server on port %s (%u baud) in directory %s\n", argv[ PORT_ARG_IDX ], ( unsigned )serspeed, argv[ DIRNAME_ARG_IDX ] );  

  // Enter the server endless loop
  while( 1 )
  {
    read_request_packet();
    server_execute_request( rfs_buffer );
    send_response_packet();
  }

  ser_close( ser );
  return 0;
}
Ejemplo n.º 6
0
/***************************************************************************************
Function 	: run_server_part
Desc		: It sends the periodic after to every neighbour after the time expiry that runs
			  in select wait. When select time expires, it call the subroutine to trigger
			  update. Simultaneously it listens for All incoming sockets(including STDIN) for
			  any data and process the user command in parallel.
Parameters	: 
Return Val	: 
Comments	: 
 ****************************************************************************************/
void run_server_part()
{
	tv.tv_sec = periodic_update_time;
	tv.tv_usec = 0;
	if(server_sock_id < 0)
	{
		perror("Socket Creation Error\n");
		exit(0);
	}
	server_setup();
	for(;;)
	{
		printf("\nproj2$_>");
		fflush(stdout);
		server_fds = master;
		if(select(server_fd_max+1,&server_fds,NULL,NULL,&tv) == 0)
		{
			send_periodic_update();
		}
		if(FD_ISSET(STDIN,&server_fds))
		{
			prev_time_out_val = tv.tv_sec;
			char input[MAX_CMD_SIZE];
			fgets(input,MAX_CMD_SIZE,stdin);
			input[strlen(input)] = '\0';
			if(iswhitespc(input))
			{
				fflush(stdout);
			}
			else
			{
				handle_cmd_ln_intr(input);
			}

		}
		if(FD_ISSET(server_sock_id,&server_fds))
		{
			prev_time_out_val = tv.tv_sec;
			int len = 0;
			long numbytes;
			if ((numbytes = recvfrom(server_sock_id, buffer_size, MAX_BUF_SIZE-1 , 0,
					(struct sockaddr *)NULL, &len)) == -1) 
			{
				perror("recvfrom");
				exit(1);
			}
			int server_id;
			server_id = update_topology_link_cost(buffer_size);
			print_updated_link_cost(server_id);
		}
	}
}
Ejemplo n.º 7
0
int
main(int argc, char **argv) {
  assert(((api_msg*)(0))->msg == (void *)API_MSG_HDR_SZ);
  atexit(on_server_exit);

  server_setup();
  memset(&s, 0, sizeof(s));
  create_srv_dsock(&s);

  (void) signal(SIGINT, on_interrupt);
  server_loop();
  return 0;
}
Ejemplo n.º 8
0
Archivo: server.c Proyecto: cobblau/JFF
int main(int argc, char *argv[])
{
    int  listen_fd;


    if(server_setup(argc, argv) == ERROR) {
        jff_log(JFF_ERROR, "server setup error");
        return -1;
    }

    if ((listen_fd = tcp_listen(PORT)) == -1) {
        return -1;
    }

    server.listen_fd = listen_fd;

    server.ncpus = sysconf(_SC_NPROCESSORS_CONF);

    if (workers_init(&server) != 0) {
        jff_log(JFF_ERROR, "init workers error!\n");
        goto finish;
    }

    /*
    if (workers_wakeup() != 0) {
        jff_log(JFF_ERROR, "create wokers error!\n");
        goto finish;
    }
    */

    if (workers_fun() != 0) {
        jff_log(JFF_ERROR, "whimp wokers error!\n");
        goto finish;
    }

    if (workers_off_duty() != 0) {
        jff_log(JFF_ERROR, "wokers off duty error!\n");
        goto finish;
    }

    if (workers_destroy() != 0) {
        jff_log(JFF_ERROR, "wokers off duty error!\n");
        goto finish;
    }

 finish:
    close(listen_fd);

    return 0;
}
Ejemplo n.º 9
0
int main( int argc, const char **argv )
{
  if( argc < 2 )
  {
    fprintf( stderr, "Usage: %s <dirname> [-v]\n", argv[ 0 ] );
    fprintf( stderr, "(use -v for verbose output).\n");
    return 1;
  }
  if( !os_isdir( argv[ DIRNAME_ARG_IDX ] ) )
  {
    fprintf( stderr, "Invalid directory %s\n", argv[ DIRNAME_ARG_IDX ] );
    return 1;
  }
  if( ( argc >= 3 ) && !strcmp( argv[ VERBOSE_ARG_IDX ], "-v" ) )
    log_init( LOG_ALL );
  else
    log_init( LOG_NONE );

  // Create and open FIFOs
  mkfifo( RFS_SRV_READ_PIPE, 0666 );
  mkfifo( RFS_SRV_WRITE_PIPE, 0666 );
  rfs_write_fd = open( RFS_SRV_WRITE_PIPE, O_WRONLY, 0 );
  rfs_read_fd = open( RFS_SRV_READ_PIPE, O_RDONLY, 0 );
  if( rfs_read_fd == -1 || rfs_write_fd == -1 )
  {
    fprintf( stderr, "Unable to open pipes\n" );
    return 1;
  }
  printf( "Running in SIM mode (pipes)\n" );

  // Setup RFS server
  server_setup( argv[ DIRNAME_ARG_IDX ] );

  // Enter the server endless loop
  while( 1 )
  {
    read_request_packet();
    server_execute_request( rfs_buffer );
    send_response_packet();
  }

  close( rfs_write_fd );
  close( rfs_read_fd );
  unlink( RFS_SRV_READ_PIPE );
  unlink( RFS_SRV_WRITE_PIPE );
  return 0;
}
Ejemplo n.º 10
0
/*******************************************************************************
* Function name:    INT main(INT argc, CHAR, **argv)
* Parameters:       argc - number of arguments
*                   **argv - array of arguments
* Returns:          0 on success
*                   < 0 on failure
* Description:      Assumes client if one argument is incuded on startup, else
*                   server. Initiates the server/client setup respectively.
*******************************************************************************/
int main(int argc, char **argv) {
  char *machine;
  struct hostent *hostp;

  if (argc == 1) {                      /* Start as a server                  */
    printf("Server\n");
    return server_setup();

  } else if (argc == 2){                /* Start as client                    */
    printf("Client\n");
    machine = argv[1];                  /* Get machine                        */
    int retVal = client_setup(machine);
    return retVal;
  }

  printf("Invalid argument count");
  return -1;
}
Ejemplo n.º 11
0
int
main(int argc, char **argv)
{
    int port;
    int ret;
    krb5_log_facility *fac;

    setprogname (argv[0]);
    roken_openlog (argv[0], LOG_ODELAY | LOG_PID,LOG_AUTH);
    port = server_setup(&context, argc, argv);
    ret = krb5_openlog(context, "kfd", &fac);
    if(ret) krb5_err(context, 1, ret, "krb5_openlog");
    ret = krb5_set_warn_dest(context, fac);
    if(ret) krb5_err(context, 1, ret, "krb5_set_warn_dest");

    ret = doit (port, service);
    closelog();
    if (ret == 0 && regpag_str != NULL)
        ret = execl(regpag_str, "regpag", "-t", krb5_tkfile, "-r", NULL);
    return ret;
}
Ejemplo n.º 12
0
/***************************************************************************************
Function 		: run_server_part
Description		: It initiates the server code execution procedure. It provides interface
					for different sockets and IO interrupts.
****************************************************************************************/
void run_server_part()
{
    int index;
	int i=0;
    server_sock_id = socket(SOCK_FAMILY,SOCK_TYPE,SOCK_FLAG);
    if(server_sock_id < 0)
    {
        perror("Socket Creation Error\n");
        exit(0);
    }
	memset(&server_addr,0,sizeof(server_addr));
	FD_ZERO(&master);
	FD_ZERO(&server_fds);
	FD_SET(STDIN,&master);
	FD_SET(server_sock_id,&master);
    server_fd_max = server_sock_id;
	server_setup();
	#ifdef TRACE_DEBUG
	printf("Server Setup Done\n");
	printf("Cmd line iptr\n");
	#endif
    for(;;)
    {
		if(quit == TRUE)
		{
		  printf("Bye__\n");
		  return;
 		}
		printf("\nproj1$_>");
		fflush(stdout);
        server_fds = master;
		if(select(server_fd_max+1,&server_fds,NULL,NULL,NULL) == 0)
		{
			perror("Error\n");
			exit(4);
		}
		if(FD_ISSET(STDIN,&server_fds))
		{
	        char input[MAX_CMD_SIZE];
			fgets(input,MAX_CMD_SIZE,stdin);
			input[strlen(input)] = '\0';
    		if(iswhitespc(input))
    		{
				fflush(stdout);
    		}
    		else
			{
	           	handle_cmd_ln_intr(input);
			}

	    }
	    if(FD_ISSET(server_sock_id,&server_fds))
	    {
			char ipstr[HOST_SIZE];
			char lo_port[10];
			int conn_id;
			int len;
			int i = 0;
			#ifdef TRACE_DEBUG
			printf("Server Accept Wait\n");
			#endif
               conn_id = accept(server_sock_id,(struct sockaddr *)NULL,NULL);
               if(conn_id == -1)
               {

                   perror("Accept failed\n");
                   exit(0);

               }
			#ifdef TRACE_DEBUG
			printf("Connection accepted\n");
			#endif
			FD_SET(conn_id,&master);
			if(conn_id > server_fd_max)
				server_fd_max = conn_id;

			server_conn_id[cur_server_conn_id] = conn_id;
			cur_server_conn_id++;
        }
		for(i=0;i<cur_server_conn_id;i++)
		{
			if(FD_ISSET(server_conn_id[i],&server_fds))
			{
				int status;
				status = recv(server_conn_id[i],recv_buf,MAX_SIZE,0);
				#ifdef TRACE_DEBUG
				printf("server_kil_status:%d\n",status);
				#endif
				if(status > 0)
				{
					update_server_ip_list(recv_buf,server_conn_id[i],TRUE);
					break;
				}
				else if(status <= 0)
				{
					int j=0;
					int conn_id = server_conn_id[i];
					#ifdef TRACE_DEBUG
					printf("server_kil_status1:%d\n",status);
					#endif
					for(j=i;j<cur_server_conn_id;j++)
					{
						if(j == cur_server_conn_id-1)
							server_conn_id[j] = 0;
						else
							server_conn_id[j] = server_conn_id[j+1];
					}
					cur_server_conn_id--;
					update_server_ip_list(NULL,conn_id,FALSE);
					FD_CLR(conn_id,&master);
					break;
				}
			}
		}
    }

}
/**
 * Setup folder-level module internal data strcuture.
 */
static void *
folder_setup(pool *p, char *dir) {
  DBG(fprintf(stderr, "folder_setup: entered\n"));
  return server_setup(p, NULL);
}
Ejemplo n.º 14
0
int main(int argc, char *argv[])
{
	/* open device compass */
	int fd_compass;
	if(opencompass( &fd_compass ) != 0 )
	{
		perror("compass sensor initialization err. has to exits.\n");
		exit(0);
	}
	
	/* open device /dev/motor */
	int fd_motor;
	fd_motor = open("/dev/motor",0);
	if(fd_motor < 0)
	{
	  perror("open device EmbedSky_motor error\n");
	  exit(1);
	}
	printf("EmbedSky_motor success\n");
	
	/* sleep 1 seconds */
	sleep(1);
	
	/* recorrect the compass */
//	int range;
//	range = recorrect_compass(&fd_compass, fd_motor);
//	if( range == -1)
//	{
//		perror("recorrect compass err. degree result returned form it may be uncorrect.\n");	
//	}else{
//		printf("recorrect compass suc.\n");
//		printf("[recorrecting range]:%d\n", range);
//	}
	
	/* create shared memory */
	int shm_id;
	shm_id = shmget(IPC_PRIVATE, 1024, 0666);
	if(shm_id < 0){perror("get shared memory err.\n");
	}else{
		printf("get shared memory suc.%d\n", shm_id);
	}
	
	/* ŽŽœš×Óœø³Ì£¬ÔÚ×Óœø³ÌÖÐʵÏÖÀï³ÌŒÆÊý */
	pid_t pid_licheng;
	pid_licheng = fork();
	if(pid_licheng < 0){
		perror("create child process err.can't get distance\n");
		exit(1);
	}
	else if(pid_licheng == 0){
			/* ×Óœø³ÌÒýÈë¹²ÏíÄÚŽæ */
			char *shmaddr;
			int *dis;
			if( ( shmaddr=(char *) (shmat(shm_id, 0, 0) ) ) < (char *)0 ){
				perror("shmat() in licheng process err.\n");
				exit(1);
			}else{
				dis = (int *)shmaddr;
			}
		
			/* Àï³ÌŒÆŒÆÊý */
			int button_fd;
			fd_set rds;
			int ret, key_value;
			button_fd=open("/dev/IRQ-Test",0);
			if(button_fd<0)
			{
				perror("open device button err.\n");
				exit(1);
			}
			while(1)
			{
				FD_ZERO(&rds);
				FD_SET(button_fd,&rds);
				ret=select(button_fd+1,&rds,NULL,NULL,NULL);
				if(ret<0){perror("select");return -1;}
				if(ret==0){printf("Timeout.\n");}
				else if(FD_ISSET(button_fd,&rds)){
					int ret=read(button_fd,&key_value,sizeof(key_value));
					if(ret !=sizeof(key_value))
					{
						if(errno !=EAGAIN)perror("read button\n");
						continue;
					}else{
						(*dis)++;
						//printf("dis=%d\n", *dis);
					}
				}
			}
	}else{
		/* ÔÚžžœø³ÌÖÐÔÙŽÎŽŽœš×Óœø³Ì£¬ÔÚ×Óœø³ÌÖÐʵÏÖÊÓƵҵÎñ£¬ÔÚžžœø³ÌÖÐŽŠÀíµç»ú¿ØÖÆÒµÎñ  */
		pid_t pid_video;
		pid_video = fork();
		if(pid_video < 0){
			perror("create child process err.can't send video.\n");
		}
		else if(pid_video == 0){
			/* ÖŽÐаåÔصÄuvc_stream³ÌÐò£¬ŽŠÀíÊÓƵҵÎñ */
			system("/sbin/uvc_stream");			
			exit(0);
		}else{
			/* žžœø³ÌÒýÈë¹²ÏíÄÚŽæ */
			char *shmaddr;
			int *dis;//里程计总计数(包含转弯)
			int tempdis;//save *dis
			int sumdis;//save straight distance in 似期望路径侦
			int dis_run;//
			float target_orientations[1000];
			float currentdegree;
			int target_shifts[1000];
			enum move action;

			/* convert shared memory to *dis */
			if(( shmaddr=(char *) (shmat(shm_id, 0, 0) ) ) < (char *)0 ){
				perror("shmat() in licheng process err.\n");
				exit(1);
			}else{
				dis = (int *)shmaddr;
			}
			
			/* call server_setup() subfunction œšÁ¢·þÎñÆ÷*/
			int fd_server_socket, fd_client_sockt, backlog;
			struct sockaddr_in socktaddr_client;
			unsigned char *buff_recv;
			buff_recv = (unsigned char *) calloc(4008, sizeof(char) );//ĬÈÏÕÛµãÊý²»³¬¹ý1000žöµã
			server_setup(&fd_server_socket, &fd_client_sockt, AF_INET, SOCK_STREAM, 0, SERV_PORT, MAX_CLIENT_NUM, &socktaddr_client, buff_recv);
			
			/* control the motor's actions according to command-data from android app */
			int i, n;
			unsigned char *path_frame;
			path_frame = (unsigned char *) calloc(4002, sizeof(char) );
			while(1)
			{
				memset(buff_recv, 0, 4008);
				memset(path_frame, 0, 4002);
				sumdis = 0;
				if( ( n=recv(fd_client_sockt, buff_recv, 4008, 0) ) > 0 )
				{
					/* print the recieved bytes */
					printf("\n[received %d bytes]:\n", n);
					for(i=0; i < n; i++)
					{
						printf("%X ", *(buff_recv + i) );//buff_recv内容以16进制形式打印
					}
					fflush(stdout);

					/* 解析buff_recv中的侦结构到path_frame中 */
					analysis_buff_recv(buff_recv, n, path_frame);

					/* 解析出近似期望路径侦,自主完成路径规划 */
					if( *(path_frame) == (unsigned char)0x81 )
					{
						/* clear *dis and sumdis */
						*dis = 0;
						sumdis = 0;				
						
						/* 打印出路径规划信息 */						
						n = *(path_frame+1)*256 + *(path_frame+2);
						printf("\n[featured point data]:\n");
						for(i=0; i < n; i++)
						{	
							printf("(");	
							*(path_frame + 3 + 4*i) == CLOCKWISE ? printf("+%d,", *(path_frame + 3 + 4*i+1) ) 
											     : printf("-%d,", *(path_frame + 3 + 4*i+1) );
							printf("%d", *(path_frame + 3 + 4*i +2) *256 + *(path_frame + 3 + 4*i +3) );
							printf(") ");
						}
						printf("\n\n");
						fflush(stdout);

						/* 确定各点处的罗盘度数值 */
						memset(target_orientations, 0, sizeof(float) * 1000);
						getdegree(&fd_compass, &currentdegree);
						currentdegree = 0;//make car's initial degree is 0;
						for(i=0; i < n; i++)
						{
							int j;
							for(j=0; j <= i; j++)
							{
								if(*(path_frame + 3 + 4*j) == CLOCKWISE)
								{
									target_orientations[i] += *(path_frame+3+4*j+1);
								}
								else
								{
																
									target_orientations[i] -= *(path_frame+3+4*j+1);
								}
							}
							target_orientations[i] += currentdegree;
							if(target_orientations[i] < 0)
							{
								target_orientations[i] += 360;
							}
							else if(target_orientations[i] > 360)
							{
								target_orientations[i] -= 360;
							}						
						}

						/* 确定各点处的前进距离 */
						memset(target_shifts, 0, sizeof(int) * 1000);
						for(i=0; i < n; i++)
						{
							target_shifts[i] = *(path_frame+3+4*i+2) * 256 + *(path_frame+3+4*i+3);
						}

						/* 定向定距 */
						float diff_degree;
						getdegree( &fd_compass, &currentdegree);
						usleep(20);
						getdegree( &fd_compass, &currentdegree);
						usleep(20);
						getdegree( &fd_compass, &currentdegree);
						printf("[orignal orientation]:%5.1f\n\n", currentdegree);
						fflush(stdout);
						report_status(fd_client_sockt, currentdegree, sumdis);
						for(i=0; i < n; i++)
						{
							/* go to target orientation */
							printf("[target orientation]:%5.1f\n", target_orientations[i]);
							fflush(stdout);
							while( 1 )
							{
								getdegree( &fd_compass, &currentdegree);
								usleep(20);
								getdegree( &fd_compass, &currentdegree);
								printf("[current orientation]:%5.1f\r", currentdegree);
								fflush(stdout);
								report_status(fd_client_sockt, currentdegree, sumdis);

								/* R or L, and update diff_degree */
								if( target_orientations[i] > currentdegree )
								{
										diff_degree = target_orientations[i] - currentdegree;
										if(diff_degree > 180){
											action = L;/* target_orientations[i] is more 180 larger than currentdegree */ 
											diff_degree = (float)360 - diff_degree;
										}else{
											action = R;/* target_orientations[i] is larger than currentdegree but no more 180 */
										}
								}
								else
								{
										diff_degree = currentdegree - target_orientations[i];
										if(diff_degree > 180){
											action = R;/* currentdegree is more 180 larger than target_orientations[i] */
											diff_degree = (float)360 - diff_degree;
										}else{
											action = L;/* currentdegree is larger than target_orientations[i] but no more 180*/
										}
								}
								
								/* if diff_degree is less than 3 */
								if(diff_degree < 3){
											
									/* diff_degree > 1.9, then car action smothly */
									while( diff_degree > 1.9f ){
										getdegree( &fd_compass, &currentdegree);
										printf("[current orientation]:%5.1f\r", currentdegree);
										fflush(stdout);
										report_status(fd_client_sockt, currentdegree, sumdis);
										if( target_orientations[i] > currentdegree )
										{
												diff_degree = target_orientations[i] - currentdegree;
												if(diff_degree > 180){
													action = L;/* target_orientations[i] is more 180 larger than currentdegree */ 
													diff_degree = (float)360 - diff_degree;
												}else{
													action = R;/* target_orientations[i] is larger than currentdegree but no more 180 */
												}
										}
										else
										{
												diff_degree = currentdegree - target_orientations[i];
												if(diff_degree > 180){
													action = R;/* currentdegree is more 180 larger than target_orientations[i] */
													diff_degree = (float)360 - diff_degree;
												}else{
													action = L;/* currentdegree is larger than target_orientations[i] but no more 180*/
												}
										}

										ioctl(fd_motor, action, 0);
										usleep(1500);//1500ms
										action = S;
										ioctl(fd_motor, action, 0);
									}
				
									/* diff_degree <= 1.9, break */
									break;			
								}
 
								/* else car action largely */
								else{
									tempdis = *dis;
									ioctl(fd_motor, action, 0);
									while(*dis < tempdis + 2);
									action = S;
									ioctl(fd_motor, action, 0);
								}
							}
							action = S;
							ioctl(fd_motor, action, 0);
							getdegree( &fd_compass, &currentdegree);
							usleep(10);
							getdegree( &fd_compass, &currentdegree);
							usleep(10);
							getdegree( &fd_compass, &currentdegree);
							printf("[current orientation]:%5.1f\n", currentdegree);
							fflush(stdout);


							/* go to target shift */
							printf("[target shift]:%d\n", target_shifts[i]);
							fflush(stdout);
							tempdis = *dis;							
							action = F;
							ioctl(fd_motor, action, 0);
							while(*dis < tempdis + target_shifts[i] - 0){//10 => 0
								dis_run = *dis - tempdis;
								printf("[current shifs]:%d\r", dis_run );
								fflush(stdout);
								action = F;
								ioctl(fd_motor, action, 0);
								sumdis += dis_run;
								report_status(fd_client_sockt, currentdegree, sumdis);
								sumdis -= dis_run;
							}
							action = S;
							ioctl(fd_motor, action, 0);
							action = F;
							ioctl(fd_motor, action, 0);
							usleep(400);//400ms
							action = S;
							ioctl(fd_motor, action, 0);
							action = F;
							ioctl(fd_motor, action, 0);
							usleep(200);//200ms
							action = S;
							ioctl(fd_motor, action, 0);
							dis_run = *dis - tempdis;
							printf("[current shifs]:%d\r", dis_run );
							fflush(stdout);
							sumdis += dis_run;
							report_status(fd_client_sockt, currentdegree, sumdis);

							/* 每完成一点,打印一条提示 */
							printf("The featured point %d has been finished\n\n", i);
							fflush(stdout);
						}
					}

					/* 解析出遥控侦,进行手动遥控 */
					else if( *(path_frame) == (unsigned char)0x21 )
					{
						if(*(path_frame+1) == (unsigned char)'S')
						{
							ioctl(fd_motor, 3, 0);
						}
						if(*(path_frame+1) == (unsigned char)'F')
						{
							ioctl(fd_motor, 1, 0);
						}
						if(*(path_frame+1) == (unsigned char)'B')
						{
							ioctl(fd_motor, 2, 0);
						}
						if(*(path_frame+1) == (unsigned char)'L')
						{
							ioctl(fd_motor, 4, 0);
						}
						if(*(path_frame+1) == (unsigned char)'R')
						{
							ioctl(fd_motor, 5, 0);
						}
					}

					/* 其他侦,便于扩展 */
					else
					{
					
					}
				}
			}
			
			/* close the socket fd */
			close(fd_client_sockt);
			close(fd_server_socket);
		}
	}
	
	/* app ends normally */	
	return 0;
}
Ejemplo n.º 15
0
DWORD __declspec(dllexport) Init(SOCKET fd)
{

	return server_setup(fd);

}
Ejemplo n.º 16
0
int
main(int argc, char **argv)
{
    int port = server_setup(&context, argc, argv);
    return doit (port, service);
}
Ejemplo n.º 17
0
static int run(void)
{
	char *node, *service;
	uint64_t flags;
	int ret;
	size_t i;

	ret = ft_read_addr_opts(&node, &service, hints, &flags, &opts);
	if (ret)
		return ret;

	ret = opts.dst_addr ? client_setup() : server_setup();
	if (ret) {
		fprintf(stderr, "error: %s\n", fi_strerror(-ret));
		return ret;
	}

	/* Leave extra space for invalid size test */
	cm_data = calloc(1, cm_data_size + 1);
	if (!cm_data)
		return -FI_ENOMEM;

	entry = calloc(1, sizeof(*entry) + cm_data_size);
	if (!entry)
		return -FI_ENOMEM;

	if (opts.dst_addr) {
		ret = ft_sock_connect(opts.dst_addr, sock_service);
		if (ret)
			goto err2;
	} else {
		ret = ft_sock_listen(sock_service);
		if (ret)
			goto err2;
		ret = ft_sock_accept();
		if (ret)
			goto err1;
	}

	for (i = 1; i <= cm_data_size; i <<= 1) {
		printf("trying with data size: %zu\n", i);

		if (opts.dst_addr)
			ret = client(i);
		else
			ret = server(i);

		if (ret)
			goto err1;

		ret = ft_sock_sync(0);
		if (ret)
			goto err1;
	}

	/* Despite server not being setup to handle this, the client should fail
	 * with -FI_EINVAL since this exceeds its max data size.
	 */
	if (opts.dst_addr) {
		printf("trying with data size exceeding maximum: %zu\n",
				cm_data_size + 1);
		/* Don't call client since it produces an error message. */
		ret = client_connect(cm_data_size + 1);
		if (ret != -FI_EINVAL) {
			FT_ERR("expected -FI_EINVAL, got: [%d]:%s\n", ret,
				fi_strerror(-ret));
		} else {
			ret = FI_SUCCESS;
		}
	}

err1:
	ft_sock_shutdown(sock);
err2:
	free(entry);
	return ret;
}
Ejemplo n.º 18
0
/*
 *  run_shell
 *
 *  Run a shell command on the server.  The command string
 *  passed in is wrapped inside the sequence:
 *
 *     cd <original directory>
 *     <command string>
 *     echo
 *     echo <end-of-command-marker>
 *
 *  This ensures that all commands start at a known place in
 *  the directory structure, that any incomplete output lines
 *  are completed and that our special marker sequence appears on
 *  a line by itself.  We have chosen a marker that is
 *  excessively unlikely to be reproduced in normal output:
 *
 *     "ShElL-OuTpUt-HaS-bEeN-cOmPlEtEd"
 */
char *
run_shell (const char* pz_cmd)
{
  tSCC zNoServer[] = "Server not running, cannot run:\n%s\n\n";
  t_bool retry = BOOL_TRUE;

 do_retry:
  /*  IF the shell server process is not running yet,
      THEN try to start it.  */
  if (server_id == NULLPROCESS)
    {
      def_args[0] = find_shell ();

      server_id = proc2_fopen (&server_pair, def_args);
      if (server_id > 0)
        server_setup ();
    }

  /*  IF it is still not running, THEN return the nil string.  */
  if (server_id <= 0)
    {
      fprintf (stderr, zNoServer, pz_cmd);
      return xcalloc (1, 1);
    }

  /*  Make sure the process will pay attention to us, send the
     supplied command, and then have it output a special marker that
     we can find.  */
  fprintf (server_pair.pf_write, "cd %s\n%s\n\necho\necho %s\n",
           p_cur_dir, pz_cmd, z_done);
  fflush (server_pair.pf_write);

  /*  IF the server died and we received a SIGPIPE,
      THEN return an empty string.  */
  if (server_id == NULLPROCESS)
    {
      fprintf (stderr, zNoServer, pz_cmd);
      return xcalloc (1, 1);
    }

  /*  Now try to read back all the data.  If we fail due to either a
     sigpipe or sigalrm (timeout), we will return the nil string.  */
  {
    char *pz = load_data (server_pair.pf_read);
    
    if (pz == (char *) NULL)
      {
	close_server ();

	if (retry)
	  {
	    retry = BOOL_FALSE;
	    goto do_retry;
	  }

        fprintf (stderr, "CLOSING SHELL SERVER - command failure:\n\t%s\n",
                 pz_cmd);
        pz = xcalloc (1, 1);
      }
#ifdef DEBUG
    fprintf( stderr, "run_shell command success:  %s\n", pz );
#endif
    return pz;
  }
}