Exemplo n.º 1
0
Arquivo: vnc.c Projeto: fdgonthier/kas
/* This function connects to a VNC session. */
int kcd_vnc_connect_session(struct kcd_ticket_mode_state *tms) {
    int error = 0;
    struct kcd_vnc_mode_state vms;
    kstr *query = &tms->query;
    PGresult *pg_res = NULL;
    
    kcd_vnc_mode_state_init(&vms, tms);
    
    do {
        /* Check the right to start/join a VNC session. */
        error = kcd_vnc_check_right(tms);
        if (error) break;
    
        /* Get the extra information from the ticket. */
        error = anp_read_uint64(&tms->ticket.ext, &vms.session_id);
        if (error) break;
    
	/* Get the port of the session. */
	kstr_sf(query, "SELECT port FROM kcd_kws_vnc_session "
                       "WHERE kws_id = "PRINTF_64"u AND session_id = "PRINTF_64"u", tms->kws_id, vms.session_id);
	error = kcd_exec_pg_query(&tms->db_conn, query->data, &pg_res, "select from VNC map");
    	if (error) break;
	
	/* No such session. */
	if (!PQntuples(pg_res)) {
	    kmod_set_error("the sharing session has been closed");
	    error = -2;
	    break;
	}
	
	vms.proxy_port = pg_db_get_uint32(pg_res, 0, 0);
	pg_db_destroy_res(&pg_res);
	
	/* Connect to the proxy. */
	error = ksock_create(&vms.proxy_sock);
	if (error) break;
	
	error = ksock_set_unblocking(vms.proxy_sock);
	if (error) break;
	
	if (ksock_connect(vms.proxy_sock, "127.0.0.1", vms.proxy_port)) {
	    kmod_set_error("the sharing session has been closed");
	    error = -2;
	    break;
	}
	
	/* Send the "OK" to the client. */
        kcd_ticket_mode_new_out_msg(tms, KANP_RES_OK);
        error = kcd_ticket_mode_send_msg(tms);
	if (error) break;
        
	/* Loop communicating with the proxy. */
	error = kcd_vnc_proxy_comm_loop(&vms);
	if (error) break;
	
    } while (0);
    
    kcd_vnc_mode_state_clean(&vms);
    pg_db_destroy_res(&pg_res);
    
    return error;
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
    struct timeval lastcommand, now, diff;
    struct timezone here;

    char *servIP;                    /* Server IP address (dotted quad) */
    unsigned short servPort;         /* Echo server port */
    char echoBuffer[RCVBUFSIZE];     /* Buffer for echo string */
    unsigned int echoStringLen;      /* Length of string to echo */
    int bytesRcvd, totalBytesRcvd;   /* Bytes read in single recv() 
                                        and total bytes read */
    int fd;
    int readevent;
    struct js_event e;

    /* Set signal handler */
    signal(SIGINT, handle_kill);

    /* Set the starting time */
    gettimeofday(&lastcommand,&here);
    
    if (argc < 3)     /* Test for correct number of arguments */
    {
       fprintf(stderr, "Usage: %s <Server IP> <Port>\n", argv[0]);
       exit(1);
    }

    /* Open the Joystick device */
    fd = open ("/dev/input/js0", O_RDONLY|O_NONBLOCK);
    if(fd < 0)
    {
      printf("Cannot open joystick(/dev/input/js0)\r\n");
      exit(1);
    }

    servIP = argv[1];             /* First arg: server IP address (dotted quad) */

    servPort = atoi(argv[2]); /* Use given port, if any */

    ksock_init('\n',2048);
	
    sock = ksock_connect(servIP, servPort);
    if(sock > 0)
      printf("Connected to %s:%d\r\n",servIP,servPort);

    /* Start reading event from the joystick */
    readevent = read (fd, &e, sizeof(struct js_event));

    while (readevent >=0 || sendflag || errno == EAGAIN)
    {
      if(readevent >= 0)
      {
	/* A new event is available */
	e.type &= ~JS_EVENT_INIT;
	switch (e.type)
	{

	  /* Handle joystick button event */
	  case JS_EVENT_BUTTON:
	    //printf("Button %d at pos %d\n", e.number, e.value);
	    handle_joybutton(e.number,e.value,echoBuffer);
	    break;

	  /* Handle joystick axis event */
	  case JS_EVENT_AXIS:
	    //printf("Axis %d at pos %d\n", e.number, e.value);
	    handle_joyaxis(e.number,e.value,echoBuffer);
	    break;
	}
      }

      /* Send the command to the server if required */
      if(sendflag) {
	/* check timing between to commands */
	gettimeofday(&now,&here);
	timersub(&now,&lastcommand,&diff);
	//printf("elapsed: %ld.%.6ld\r\n",diff.tv_sec,diff.tv_usec);

	if(diff.tv_usec > 100000 || !timeflag)
	{
	  lastcommand.tv_sec  = now.tv_sec;
	  lastcommand.tv_usec = now.tv_usec;

	  printf("%s\n",echoBuffer);
	  /* Send the command to the robot */
	  echoStringLen = strlen(echoBuffer);
	  if (send(sock, echoBuffer, echoStringLen, 0) != echoStringLen)
	    DieWithError("send() sent a different number of bytes than expected");
	  /* Receive the same string back from the server */
	  totalBytesRcvd = 0;
	  printf("Received: ");                /* Setup to print the echoed string */
	  while (totalBytesRcvd < echoStringLen)
	  {
	    /* Receive up to the buffer size (minus 1 to leave space for
	       a null terminator) bytes from the sender */
	    if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
	      DieWithError("recv() failed or connection closed prematurely");
	    totalBytesRcvd += bytesRcvd;   /* Keep tally of total bytes */
	    echoBuffer[bytesRcvd] = '\0';  /* Terminate the string! */
	    printf(echoBuffer);            /* Print the echo buffer */
	  }

	  printf("\n");    /* Print a final linefeed */

	  sendflag = 0;
	}
      }

      /* Read the next event */
      readevent = read (fd, &e, sizeof(struct js_event));
    }

    close(fd);
    close(sock);
    exit(0);
}