/**************************************************************
 *  Name                 : periodic_tasks_exec_100tks
 *	ID					 : TASK_100TKS
 *  Description          : Container for functionality that is 
                           executed periodically.
 *  Parameters           : none
 *  Return               : none
 *  Critical/explanation : no
 **************************************************************/
 void periodic_tasks_exec_100tks(void)
 { 
      str_ctrl();
	  print_str();
	  print_response();
      actionSelector();    
 }
示例#2
0
void dispatch_request(char* from, char* to, int accept_socket, int* keep_alive){
  char* req_str = (char*) malloc(sizeof(char) * (to - from + 1));
  char* char_i;
  int int_i;
  int _ = 0;
  for(char_i = from, int_i = 0; char_i < to; char_i++, int_i++){
    *(req_str + int_i) = *(char_i);
  }
  *(req_str + int_i) = '\0';
  struct request* req = malloc_request();
  parse_request(req, req_str);
  struct response* resp = malloc_response(req);
  //free(req_str);
  write_request(resp, accept_socket);
  if(resp->html == 0){
    read_file(req->resource_name, accept_socket);
  } else{
    _ = write(accept_socket, resp->html, strlen(resp->html));
    
  }
  if(_ == -1){
    printf("scream write %d\n", _);
  }
  *keep_alive = req->keep_alive;
  free(req_str);
  free_request(req);
  print_response(resp);
  free_response(resp); 
}
示例#3
0
/*******************************************************************************
 * this function is called by the client
 * it takes care that the request is processed by the buffer
 *******************************************************************************/
int clientrequest(int server, const message_t *request, message_t **response_ptr) {
    int verbose = 0;

	if (verbose>0) fprintf(stderr, "clientrequest: server = %d\n", server);
	if (verbose>0) print_request(request->def);

	if (server<0) {
		fprintf(stderr, "clientrequest: invalid value for server (%d)\n", server);
		return -1;
	}

	else if (server==0) {
		/* use direct memory acces to the buffer */
		if (dmarequest(request, response_ptr)!=0)
			return -2;
	}

	else if (server>0) {
		/* use TCP connection to the buffer */
		if (tcprequest(server, request, response_ptr)!=0)
			return -3;
	}

	if (verbose>0) print_response((*response_ptr)->def);

	/* everything went fine */
	return 0;
}
示例#4
0
文件: yc.c 项目: Birdflying1005/h2o
int cmd_touch(int argc, char** argv, yrmcds* s) {
    if( argc != 2 ) {
        fprintf(stderr, "Wrong number of arguments.\n");
        return 1;
    }
    const char* key = argv[0];
    uint32_t expire = (uint32_t)strtoull(argv[1], NULL, 0);

    yrmcds_response r[1];
    uint32_t serial;
    yrmcds_error e = yrmcds_touch(s, key, strlen(key), expire, quiet, &serial);
    CHECK_ERROR(e);
    if( quiet ) {
        e = yrmcds_noop(s, &serial);
        CHECK_ERROR(e);
    }
    if( debug )
        fprintf(stderr, "request serial = %u\n", serial);
    while( 1 ) {
        e = yrmcds_recv(s, r);
        CHECK_ERROR(e);
        if( debug )
            print_response(r);
        CHECK_RESPONSE(r);
        if( r->serial == serial )
            break;
    }
    return 0;
}
示例#5
0
文件: yc.c 项目: Birdflying1005/h2o
int cmd_keys(int argc, char** argv, yrmcds* s) {
    const char* prefix = NULL;
    size_t prefix_len = 0;
    if( argc == 1 ) {
        prefix = argv[0];
        prefix_len = strlen(prefix);
    }
    yrmcds_response r[1];
    uint32_t serial;
    yrmcds_error e = yrmcds_keys(s, prefix, prefix_len, &serial);
    CHECK_ERROR(e);
    if( debug )
        fprintf(stderr, "request serial = %u\n", serial);
    while( 1 ) {
        e = yrmcds_recv(s, r);
        CHECK_ERROR(e);
        if( debug )
            print_response(r);
        CHECK_RESPONSE(r);
        if( r->serial != serial )
            continue;
        if( r->key_len == 0 )
            break;
        printf("%.*s\n", (int)r->key_len, r->key);
    }
    return 0;
}
示例#6
0
int
main(int argc, char **argv)
{
	mdata_proto_t *mdp;
	mdata_response_t mdr;
	string_t *data;
	const char *errmsg = NULL;

	if (argc < 2) {
		errx(MDEC_USAGE_ERROR, "Usage: %s <keyname>", argv[0]);
	}

	if (proto_init(&mdp, &errmsg) != 0) {
		fprintf(stderr, "ERROR: could not initialise protocol: %s\n",
		    errmsg);
		return (MDEC_ERROR);
	}

	if (proto_version(mdp) < 2) {
		fprintf(stderr, "ERROR: host does not support DELETE\n");
		return (MDEC_ERROR);
	}

	keyname = strdup(argv[1]);

	if (proto_execute(mdp, "DELETE", keyname, &mdr, &data) != 0) {
		fprintf(stderr, "ERROR: could not execute GET\n");
		return (MDEC_ERROR);
	}

	return (print_response(mdr, data));
}
示例#7
0
文件: yc.c 项目: Birdflying1005/h2o
int cmd_stat(int argc, char** argv, yrmcds* s) {
    yrmcds_response r[1];
    uint32_t serial;
    yrmcds_error e;
    if( argc > 0 ) {
        if( strcmp(argv[0], "settings") == 0 ) {
            e = yrmcds_stat_settings(s, &serial);
        } else if( strcmp(argv[0], "items") == 0 ) {
            e = yrmcds_stat_items(s, &serial);
        } else if( strcmp(argv[0], "sizes") == 0 ) {
            e = yrmcds_stat_sizes(s, &serial);
        } else {
            fprintf(stderr, "No such statistics.\n");
            return 1;
        }
    } else {
        e = yrmcds_stat_general(s, &serial);
    }
    CHECK_ERROR(e);
    while( 1 ) {
        e = yrmcds_recv(s, r);
        CHECK_ERROR(e);
        if( debug )
            print_response(r);
        CHECK_RESPONSE(r);
        if( r->key_len == 0 )
            break;
        if( r->data_len == 0 )
            continue;
        printf("%.*s: %.*s\n", (int)r->key_len, r->key,
               (int)r->data_len, r->data);
    }
    return 0;
}
示例#8
0
文件: yc.c 项目: Birdflying1005/h2o
int cmd_flush(int argc, char** argv, yrmcds* s) {
    uint32_t delay = 0;
    if( argc == 1 )
        delay = (uint32_t)strtoull(argv[0], NULL, 0);

    yrmcds_response r[1];
    uint32_t serial;
    yrmcds_error e = yrmcds_flush(s, delay, quiet, &serial);
    CHECK_ERROR(e);
    if( quiet ) {
        e = yrmcds_noop(s, &serial);
        CHECK_ERROR(e);
    }
    if( debug )
        fprintf(stderr, "request serial = %u\n", serial);
    while( 1 ) {
        e = yrmcds_recv(s, r);
        CHECK_ERROR(e);
        if( debug )
            print_response(r);
        CHECK_RESPONSE(r);
        if( r->serial == serial )
            break;
    }
    return 0;
}
示例#9
0
文件: yc.c 项目: Birdflying1005/h2o
int cmd_unlock(int argc, char** argv, yrmcds* s) {
    if( argc != 1 ) {
        fprintf(stderr, "Wrong number of arguments.\n");
        return 1;
    }
    yrmcds_response r[1];
    uint32_t serial;
    yrmcds_error e = yrmcds_unlock(s, argv[0], strlen(argv[0]), quiet, &serial);
    CHECK_ERROR(e);
    if( quiet ) {
        e = yrmcds_noop(s, &serial);
        CHECK_ERROR(e);
    }
    if( debug )
        fprintf(stderr, "request serial = %u\n", serial);
    while( 1 ) {
        e = yrmcds_recv(s, r);
        CHECK_ERROR(e);
        if( debug )
            print_response(r);
        CHECK_RESPONSE(r);
        if( r->serial == serial )
            break;
    }
    return 0;
}
int main(int argc, char const *argv[])
{	
	char *ip, *port;
	get_input(argc, argv, &ip, &port);
	printf("IP = %s\n", ip);
	printf("Port = %s\n", port);

	int sock;
	sock = create_socket(SOCK_DGRAM, ip);
	

/* PROTOTIPO de como ENVIA mensagens */
	char msg[BUF_SIZE];
	strcat(msg, "GET /index.html HTTP/1.1\r\nHost: ");
	strcat(msg, ip);
	strcat(msg, "\r\n\r\n");
	print_request(msg);

	send_all(sock, msg);

/* PROTOTIPO de como RECEBE mensagens */
	// int recv(int sockfd, void *buf, int len, int flags);
	char *buff;
	size_t by_recv;
	by_recv = recv_all(sock, &buff, BUF_SIZE);

	print_response(buff);
	return 0;
}
示例#11
0
文件: test_getevt.c 项目: 9877/eeglab
int main(int argc, char *argv[]) {
	int server, offset;
	int n;
	messagedef_t request, response;
	event_t event;
	eventsel_t eventsel;
	void *buf = NULL;

	if (argc != 3) {
		fprintf(stderr, "USAGE: application <server_ip> <port>\n");
		exit(1);
	}

	/* open the TCP socket */
	if ((server = open_connection(argv[1], atoi(argv[2]))) < 0) {
		fprintf(stderr, "ERROR; failed to create socket\n");
		exit(1);
	}

	request.version = VERSION;
	request.command = GET_EVT;
	request.bufsize = 0; // sizeof(eventsel_t);

	// eventsel.begevent = 0;
	// eventsel.endevent = 2;

	fprintf(stderr, "------------------------------\n");
	print_request(&request);
	write(server, &request, sizeof(messagedef_t));
	// write(server, &eventsel, sizeof(eventsel_t));

	read(server, &response, sizeof(messagedef_t));
	fprintf(stderr, "------------------------------\n");
	print_response(&response);
	fprintf(stderr, "------------------------------\n");

	if (response.command==GET_OK) {
		buf = malloc(response.bufsize);
		if ((n = bufread(server, buf, response.bufsize)) < response.bufsize) {
			fprintf(stderr, "problem reading enough bytes (%d)\n", n);
		}
		else {
			n = 0;
			offset = 0;
			while (offset<response.bufsize) {
				event.def = buf+offset;
				event.buf = buf+offset+sizeof(eventdef_t);
				fprintf(stderr, "\n");
				print_eventdef(event.def);
				offset += sizeof(eventdef_t) + event.def->bufsize;
				n++;
			}
		}
		FREE(buf);
	}

	close(server);
	exit(0);
}
示例#12
0
int buffer_gethdr(int server, mxArray *plhs[], const mxArray *prhs[])
{
	int verbose = 0;
	int result  = 0;

	message_t *request  = NULL;
	message_t *response = NULL;

	/* this is for the Matlab specific output */
	const char *field_names[NUMBER_OF_FIELDS] = {"nchans", "nsamples", "nevents", "fsample", "data_type", "bufsize"};

	/* allocate the elements that will be used in the communication */
	request      = malloc(sizeof(message_t));
	request->def = malloc(sizeof(messagedef_t));
	request->buf = NULL;
	request->def->version = VERSION;
	request->def->command = GET_HDR;
	request->def->bufsize = 0;
	
	if (verbose) print_request(request->def);
	result = clientrequest(server, request, &response);
	
	if (result == 0) {
		if (verbose) print_response(response->def);

		if (response->def->command==GET_OK) {
			headerdef_t *headerdef = (headerdef_t *) response->buf;
			
			if (verbose) print_headerdef(headerdef);

			plhs[0] = mxCreateStructMatrix(1, 1, NUMBER_OF_FIELDS, field_names);
			mxSetFieldByNumber(plhs[0], 0, 0, mxCreateDoubleScalar((double)(headerdef->nchans)));
			mxSetFieldByNumber(plhs[0], 0, 1, mxCreateDoubleScalar((double)(headerdef->nsamples)));
			mxSetFieldByNumber(plhs[0], 0, 2, mxCreateDoubleScalar((double)(headerdef->nevents)));
			mxSetFieldByNumber(plhs[0], 0, 3, mxCreateDoubleScalar((double)(headerdef->fsample)));
			mxSetFieldByNumber(plhs[0], 0, 4, mxCreateDoubleScalar((double)(headerdef->data_type)));
			mxSetFieldByNumber(plhs[0], 0, 5, mxCreateDoubleScalar((double)(headerdef->bufsize)));
			
			addChunksToMatrix(plhs[0], (const char *) response->buf + sizeof(headerdef_t), headerdef->bufsize, headerdef->nchans);
		}
		else {
			result = response->def->command;
		}
	}

	if (response) {
		FREE(response->def);
		FREE(response->buf);
		FREE(response);
	}

	if (request) {
		FREE(request->def);
		FREE(request->buf);
		FREE(request);
	}

	return result;
}
示例#13
0
void wifi_test_tcp() {
  printf("[WIFI] Test...\n");

  softserial_printf("AT+CIPSTART=\"TCP\",\"%s\",%d\r\n", "173.254.30.60", 80);
  _delay_ms(1000);
  print_response();

  const char *http = "GET /Test.txt HTTP/1.0\r\n\r\nHost: thearduinoguy.org\r\n\r\n";

  softserial_printf("AT+CIPSEND=%d\r\n", strlen(http));
  print_response();

  while(serial_available()) fgetc(serial_input);
  softserial_printf(http);

  _delay_ms(1000);

  char line_buf[128];
  int content_length = -1;

  while(serial_available()) {
    softserial_readline(line_buf, ARRAYSIZE(line_buf));

    if(memcmp(line_buf, "Content-Length: ", 16) == 0) {
      content_length = atoi(&line_buf[16]);
    } else if(content_length >= 0 && line_buf[0] == '\r') {
      break;
    }
  }

  // TODO: Protect against overflow
  char content[128];
  for(int i = 0; i < content_length; ++i) {
    content[i] = softserial_getc();
  }
  content[content_length] = '\0';

  printf("CONTENT: %s\n", content);

  while(serial_available()) fgetc(serial_input);

  // Will error if remote closed connection already, that's fine
  softserial_printf("AT+CIPCLOSE\r\n");
  _delay_ms(100);
  print_response();
}
示例#14
0
文件: bot.cpp 项目: raj61/chatBot
void TechBot::welcome()
{
    eventManager("welcome**");
    chooseOutput();
    Savelog();
    Savelog("TechTron");
    //speak(TB_Response);
    print_response();
}
示例#15
0
文件: common.c 项目: Greeshma/DFS
void print_packet(corefs_packet pkt){
   dprintf(stderr, "--------------------------------------------------------\n");
  dprintf(stderr,"<header>\n");
  dprintf(stderr, "\t| magic[0x%x] | type[0x%x] | sequence[0x%x] | payload_size[0d%u] |\n",pkt.header.magic, pkt.header.type, pkt.header.sequence, pkt.header.payload_size);
  dprintf(stderr,"<\\header>\n");
  dprintf(stderr, "--------------------------------------------------------\n");
  if(pkt.header.type == COREFS_REQUEST) print_request(pkt);
  if(pkt.header.type == COREFS_RESPONSE) print_response(pkt);
}
示例#16
0
文件: bot.cpp 项目: raj61/chatBot
void TechBot::answer() 
{
    SavePrevresponse();
    setEvent("BOT UNDERSTAND**");
    
    if(null_input())
    {
        eventManager("NULL INPUT**");
    }
    else if(null_input_repetition())
    {
        eventManager("NULL INPUT REPETITION**");
    }
    else if(isUserRepeat())
    {
        manageUserRepeat();
    }
    else if(isaskedusername())
    {
        std::cout<<"I THINK IT IS "<<user_name<<std::endl;
        return;
    }
    else
    {
        selectMatch();
    }
    
    if(user_want_to_quit())
    {
        TB_bQuitProgram = 1;
        eventManager("USER WANTS TO QUIT**");
    }
    else if(TB_Input.find("BYE")!=std::string::npos)
        return;

   else if(!isAIunderstand())
    {
        eventManager("BOT DON'T UNDERSTAND**");
       // update_unkown_input_list();
    }
    
    if(RespLISTonse.size() > 0)
    {
        chooseOutput();
        SaveAI_response();
        initPre_response();

        if(isAIRepeat())
        {
            manageRepeat();
        }
        Savelog("TechTron");
        print_response();
        //speak(TB_Response);
    }
}
示例#17
0
文件: client.c 项目: leonshao/apue
int main(int argc, char *argv[])
{
	int					sockfd, ret;
	char				*host;
	uint16_t 			port = DEFAULT_PORT;
	struct addrinfo		*p_addrlist = NULL;
	struct addrinfo		hint;
	struct sockaddr_in 	sa_in;

	if(argc == 1)
	{
		host = get_defaulthost();
	}
	if(argc >= 2)
	{
		host = argv[1];
	}
	if(argc >= 3)
	{
		port = (uint16_t)atoi(argv[2]);
	}

	/*
	 * get remote addr from input host or default host
	 */
	init_addrinfo(&hint, SOCK_STREAM);
	if((ret = getaddrinfo(host, NULL, &hint, &p_addrlist)) != 0)
		err_quit("getaddrinfo error: %s", gai_strerror(ret));

	if(p_addrlist != NULL)
		memcpy((void *)&sa_in, (void *)p_addrlist->ai_addr, sizeof(sa_in));
	else
	{
		sa_in.sin_family = AF_INET;
		sa_in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
	}

	sa_in.sin_port = htons(port);

	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		err_sys("socket error: %s", strerror(errno));

	/*
	 * !!!
	 * pay attention to the alen, it is not correct by input sizeof(struct sockaddr)
	 * because the struct size depends on the machine archtect
	 */
	if(connect_retry(sockfd, (struct sockaddr *)&sa_in, INET_ADDRSTRLEN) < 0)
		err_quit("connect fail: %s", strerror(errno));

	print_response(sockfd);

	return 0;
}
示例#18
0
int
main()
{
	struct sockaddr_in from;
	socklen_t from_size = (socklen_t)sizeof (from);
	int cc;
	int name_length = sizeof (hostname);
	fd_set rfds;
	struct timeval tv;

	(void) sysinfo(SI_HOSTNAME, hostname, name_length);

	for (;;) {
		tv.tv_sec = MAX_LIFE;
		tv.tv_usec = 0;
		FD_ZERO(&rfds);
		FD_SET(0, &rfds);
		if (select(1, &rfds, 0, 0, &tv) <= 0)
			return (0);
		cc = recvfrom(0, (char *)&request, sizeof (request), 0,
		    (struct sockaddr *)&from, &from_size);

		if (cc != sizeof (request)) {
			if (cc < 0 && errno != EINTR) {
				print_error("receive");
			}
		} else {

			if (debug) {
				(void) printf("Request received : \n");
				(void) print_request(&request);
			}

			request = swapmsg(request);
			process_request(&request, &response);

			if (debug) {
				(void) printf("Response sent : \n");
				print_response(&response);
			}

			/*
			 * Can block here, is this what I want?
			 */
			cc = sendto(0, (char *)&response, sizeof (response), 0,
			    (struct sockaddr *)&request.ctl_addr,
			    (socklen_t)sizeof (request.ctl_addr));

			if (cc != sizeof (response)) {
				print_error("Send");
			}
		}
	}
}
示例#19
0
void Eliza::start() {
	time_t ltime; 
	time(&ltime);
	logfile.open("log.txt", std::ios::out | std::ios::app);
	if(logfile.fail()) {
		throw std::string("can't save conversation log");
	}
	logfile << "\n\nConversation log - " << ctime(&ltime) << std::endl;
	response_list = signOn;
	select_response();
	print_response();
	save_log("ELIZA");
}
示例#20
0
static void
got_body (SoupMessage *msg, gpointer user_data)
{
	SoupLogger *logger = user_data;
	SoupLoggerPrivate *priv = SOUP_LOGGER_GET_PRIVATE (logger);

	g_mutex_lock (&priv->lock);

	print_response (logger, msg);
	soup_logger_print (logger, SOUP_LOGGER_LOG_MINIMAL, ' ', "");

	g_mutex_unlock (&priv->lock);
}
示例#21
0
int
main()
{
	/* Create the DNS context for this call */
	struct getdns_context *this_context = NULL;
	getdns_return_t context_create_return =
	    getdns_context_create(&this_context, 1);
	if (context_create_return != GETDNS_RETURN_GOOD) {
		fprintf(stderr, "Trying to create the context failed: %d",
		    context_create_return);
		return (GETDNS_RETURN_GENERIC_ERROR);
	}
	getdns_context_set_resolution_type(this_context, GETDNS_RESOLUTION_STUB);

	struct getdns_dict *response = NULL;
	getdns_return_t ret =
	    getdns_address_sync(this_context, "www.google.com", NULL, &response);

	if (ret != GETDNS_RETURN_GOOD || response == NULL) {
		fprintf(stderr, "Address sync returned error.\n");
		exit(EXIT_FAILURE);
	}
	print_response(response);
	getdns_dict_destroy(response);

	ret =
	    getdns_service_sync(this_context, "www.google.com", NULL, &response);
	if (ret != GETDNS_RETURN_GOOD || response == NULL) {
		fprintf(stderr, "Service sync returned error.\n");
		exit(EXIT_FAILURE);
	}
	print_response(response);
	getdns_dict_destroy(response);

	/* Clean up */
	getdns_context_destroy(this_context);
	/* Assuming we get here, leave gracefully */
	exit(EXIT_SUCCESS);
}				/* main */
示例#22
0
文件: yc.c 项目: cybozu/php-yrmcds
int cmd_version(int argc, char** argv, yrmcds* s) {
    yrmcds_response r[1];
    uint32_t serial;
    yrmcds_error e = yrmcds_version(s, &serial);
    CHECK_ERROR(e);
    e = yrmcds_recv(s, r);
    CHECK_ERROR(e);
    if( debug )
        print_response(r);
    CHECK_RESPONSE(r);
    printf("%.*s\n", (int)r->data_len, r->data);
    return 0;
}
示例#23
0
文件: yc.c 项目: cybozu/php-yrmcds
int cmd_noop(int argc, char** argv, yrmcds* s) {
    yrmcds_response r[1];
    uint32_t serial;
    yrmcds_error e = yrmcds_noop(s, &serial);
    CHECK_ERROR(e);
    if( debug )
        fprintf(stderr, "request serial = %u\n", serial);
    e = yrmcds_recv(s, r);
    CHECK_ERROR(e);
    if( debug )
        print_response(r);
    CHECK_RESPONSE(r);
    printf("OK\n");
    return 0;
}
示例#24
0
文件: rpc.c 项目: copton/ocram
static void print_response(rpc_response_t* response)
{
    printf("\"type\":\"response\", \"seq\":%d, \"fun\":\"%s\", ", response->sequence, rpc_function_to_string(response->function));
    if (0) {
    } else if (response->function == RPC_TELL) {
        printf("\"call\":{");
        print_response(response->data.tell.response);
        printf("}");
    } else if (response->function == RPC_READ_SLOW_SENSOR) {
        printf("\"value\":%d", response->data.read_slow_sensor.value);
    } else if (response->function == RPC_READ_FAST_SENSOR) {
        printf("\"value\":%d", response->data.read_fast_sensor.value);
    } else {
        ASSERT(false);
    }
}
示例#25
0
文件: yc.c 项目: cybozu/php-yrmcds
int cmd_quit(int argc, char** argv, yrmcds* s) {
    yrmcds_response r[1];
    uint32_t serial;
    yrmcds_error e = yrmcds_quit(s, quiet, &serial);
    CHECK_ERROR(e);
    if( debug )
        fprintf(stderr, "request serial = %u\n", serial);
    if( ! quiet ) {
        e = yrmcds_recv(s, r);
        CHECK_ERROR(e);
        if( debug )
            print_response(r);
        CHECK_RESPONSE(r);
    }
    return 0;
}
示例#26
0
int main(int argc, char *argv[]) {
	int server;
	int n;
	messagedef_t request, response;
	header_t header;
	void *buf = NULL;

	if (argc != 3) {
		fprintf(stderr, "USAGE: application <server_ip> <port>\n");
		exit(1);
	}

	/* open the TCP socket */
	if ((server = open_connection(argv[1], atoi(argv[2]))) < 0) {
		fprintf(stderr, "ERROR; failed to create socket\n");
		exit(1);
	}

	request.version = VERSION;
	request.command = GET_HDR;
	request.bufsize = 0;

	fprintf(stderr, "------------------------------\n");
	print_request(&request);
	bufwrite(server, &request, sizeof(messagedef_t));

	bufread(server, &response, sizeof(messagedef_t));
	fprintf(stderr, "------------------------------\n");
	print_response(&response);
	fprintf(stderr, "------------------------------\n");

	if (response.command==GET_OK) {
		buf = malloc(response.bufsize);
		if ((n = bufread(server, buf, response.bufsize)) < response.bufsize) {
			fprintf(stderr, "problem reading enough bytes (%d)\n", n);
		}
		else {
			header.def = (headerdef_t *)buf;
			header.buf = (char *) buf+sizeof(headerdef_t);
			print_headerdef(header.def);
		}
		FREE(buf);
	}

	close(server);
	exit(0);
}
示例#27
0
/** Process the lookup in a child process. The current running child can't do
 * it with a signal, but we need the answerer's ctl_addr to respond... */
void ForwMachine::processLookup(const NEW_CTL_MSG * mp)
{
    if (fork()==0)
    {   // here we are the child
        message("------------- Got LOOKUP : send it to caller (%s)", caller_username);
        // Let's send a LOOK_UP on caller's machine, to make sure he still
        // wants to speak to the callee...
        TalkConnection * tcCaller = new TalkConnection(caller_machine_addr,
                caller_username,
                local_user,
                callerProtocol);
        tcCaller->open_sockets();
        tcCaller->look_for_invite(0/*no error if no invite*/);
        NEW_CTL_RESPONSE rp;
        tcCaller->getResponseItems(&rp.answer, &rp.id_num, &rp.addr);
        message("------------- Done. Forward response to answerer");

        rp.type = LOOK_UP;
        rp.vers = mp->vers;
        rp.id_num = htonl(rp.id_num);
        // Now send the response to the answerer
        if (forwardMethod == FWR)
        {
            // with caller's addr copied in the NEW_CTL_RESPONSE (if FWR),
            // so that they can talk to each other.
            /* rp.addr filled by getResponseItems */
            rp.addr.sa_family = htons(rp.addr.sa_family);
        }
        else // FWT. (FWA doesn't let us get the LOOK_UP)
        {
            // in this case, we copy in the NEW_CTL_RESPONSE the address
            // of the connection socket set up here for the answerer
            rp.addr = tcAnsw->get_addr();
            rp.addr.sa_family = htons(AF_INET);
        }
        print_response("-- => response (processLookup)", &rp);
        if (forwardMethod == FWT)
            tcAnsw->listen(); // start listening before we send the response,
        // just in case the answerer is very fast (ex: answ mach)
        sendResponse(mp->ctl_addr, &rp);
        if (forwardMethod == FWT)
            connect_FWT(tcCaller);
        delete tcCaller;
        _exit(0);
    }
    new_process();
}
示例#28
0
void wifi_sendn(const void *message, int message_len) {
  printf_P(PSTR("[WIFI] Sending...\n"));

  char cmd[64];
  sprintf_P(cmd, AT_CIPSTART_UDP, WIFI_DEST_IP, WIFI_DEST_PORT);
  wifi_repeat_until_ok(cmd);

  fprintf_P(serial_output, AT_CIPSEND, message_len);
  print_response();

  while(serial_available()) fgetc(serial_input);

  const char *message_chars = message;
  for(int i = 0; i < message_len; ++i) {
    fputc(message_chars[i], serial_output);
  }
}
示例#29
0
文件: yc.c 项目: cybozu/php-yrmcds
int cmd_add(int argc, char** argv, yrmcds* s) {
    if( argc < 2 || 5 < argc ) {
        fprintf(stderr, "Wrong number of arguments.\n");
        return 1;
    }
    const char* key = argv[0];
    char* data = NULL;
    size_t data_len = read_data(argv[1], &data);
    if( data == NULL ) {
        fprintf(stderr, "Failed to read data.\n");
        return 2;
    }
    uint32_t expire = 0;
    uint32_t flags = 0;
    uint64_t cas = 0;

    if( argc > 2 )
        expire = (uint32_t)strtoull(argv[2], NULL, 0);
    if( argc > 3 )
        flags = (uint32_t)strtoull(argv[3], NULL, 0);
    if( argc > 4 )
        cas = (uint64_t)strtoull(argv[4], NULL, 0);

    yrmcds_response r[1];
    uint32_t serial;
    yrmcds_error e = yrmcds_add(s, key, strlen(key), data, data_len,
                                flags, expire, cas, quiet, &serial);
    free(data);
    CHECK_ERROR(e);
    if( quiet ) {
        e = yrmcds_noop(s, &serial);
        CHECK_ERROR(e);
    }
    if( debug )
        fprintf(stderr, "request serial = %u\n", serial);
    while( 1 ) {
        e = yrmcds_recv(s, r);
        CHECK_ERROR(e);
        if( debug )
            print_response(r);
        CHECK_RESPONSE(r);
        if( r->serial == serial )
            break;
    }
    return 0;
}
示例#30
0
文件: yc.c 项目: cybozu/php-yrmcds
int cmd_decr(int argc, char** argv, yrmcds* s) {
    if( argc < 2 || 4 < argc ) {
        fprintf(stderr, "Wrong number of arguments.\n");
        return 1;
    }
    const char* key = argv[0];
    uint64_t value = (uint64_t)strtoull(argv[1], NULL, 0);
    uint64_t initial = 0;
    uint32_t expire = ~(uint32_t)0;

    if( argc > 2 ) {
        initial = (uint64_t)strtoull(argv[2], NULL, 0);
        expire = 0;
    }
    if( argc > 3 )
        expire = (uint32_t)strtoull(argv[3], NULL, 0);

    yrmcds_response r[1];
    uint32_t serial;
    yrmcds_error e;
    if( argc == 2 ) {
        e = yrmcds_decr(s, key, strlen(key), value, quiet, &serial);
    } else {
        e = yrmcds_decr2(s, key, strlen(key), value, initial, expire,
                         quiet, &serial);
    }
    CHECK_ERROR(e);
    if( quiet ) {
        e = yrmcds_noop(s, &serial);
        CHECK_ERROR(e);
    }
    if( debug )
        fprintf(stderr, "request serial = %u\n", serial);
    while( 1 ) {
        e = yrmcds_recv(s, r);
        CHECK_ERROR(e);
        if( debug )
            print_response(r);
        CHECK_RESPONSE(r);
        if( r->serial == serial )
            break;
    }
    printf("%" PRIu64 "\n", r->value);
    return 0;
}