int main(int argc, char *argv[])
{
    int newsockfd;
    int sockfd, portno;
    t_msg_frame ourFrame={1,2,3,"goodbye cruel world",5};
    ourFrame.bdyLgth=strlen(ourFrame.msgBody);

    if (argc < 2) {
     fprintf(stderr,"ERROR, no port provided\n");
     exit(1);
    }

    if ( argc > 2){
        // todo: kindof msgBodyCpy()
        ourFrame.msgBody=malloc(strlen(argv[2]));
        strcpy(ourFrame.msgBody, argv[2]);
        ourFrame.bdyLgth=strlen(ourFrame.msgBody);
    }

    portno = atoi(argv[1]);

    open_and_listen(&sockfd, portno);

    get_client_connection(&newsockfd, sockfd);

    write_frame_on_socket(&ourFrame, newsockfd);

    // close everything
    close(newsockfd);
    close(sockfd);
    return 0; 
}
예제 #2
0
/* {{{ proto Riak\Search\Output\Output Riak\Search->search(string $index, string $query[, Riak\Search\Input] $parameters)
Create a new Search object */
PHP_METHOD(Riak_Search, search)
{
    riak_connection *connection;
    riack_search_optional_params search_params;
    riack_search_result *search_result;
    riack_string rsquery, rsindex;
    char* index, *query;
    int index_len, query_len, riackstatus;
    zval *zclient, *zresult, *zparams = NULL;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|o", &index, &index_len, &query, &query_len, &zparams) == FAILURE) {
        return;
    }
    zclient = zend_read_property(riak_search_ce, getThis(), "connection", sizeof("connection")-1, 1 TSRMLS_CC);

    connection = get_client_connection(zclient TSRMLS_CC);

    THROW_EXCEPTION_IF_CONNECTION_IS_NULL(connection);

    memset(&search_params, 0, sizeof(riack_search_optional_params));
    rsquery.value = query;
    rsquery.len = query_len;
    rsindex.value = index;
    rsindex.len = index_len;
    riak_search_set_optional_params(connection->client, zparams, &search_params TSRMLS_CC);
    RIACK_RETRY_OP(riackstatus, connection, riack_search(connection->client, &rsquery, &rsindex, &search_params, &search_result));
    riak_search_free_optional_params(connection->client, &search_params TSRMLS_CC);
    CHECK_RIACK_STATUS_THROW_AND_RETURN_ON_ERROR(connection, riackstatus);
    zresult = riak_search_output_from_riack_search_result(search_result TSRMLS_CC);
    riack_free_search_result_p(connection->client, &search_result);

    RETURN_ZVAL(zresult, 0, 1);
}
char *
get_external_command(const char *prompt, char *input, int len)
{
	do {
		// get a connection
		int connection = get_client_connection();
		if (connection < 0)
			return NULL;

		// read until we have a full command
		external_command_message message;
		int toRead = sizeof(message);
		char *buffer = (char*)&message;
		while (toRead > 0) {
			int bytesRead = read(connection, buffer, toRead);
			if (bytesRead < 0) {
				if (errno == EINTR) {
					continue;
				} else {
					fprintf(stderr, "Reading from connection failed: %s\n", strerror(errno));
					break;
				}
			}

			// connection closed?
			if (bytesRead == 0)
				break;
			
			buffer += bytesRead;
			toRead -= bytesRead;
		}

		// connection may be broken: discard it
		if (toRead > 0) {
			close_client_connection();
			continue;
		}

		// get the len of the command
		message.command[sizeof(message.command) - 1] = '\0';
		int commandLen = strlen(message.command) + 1;
		if (commandLen <= 1) {
			fprintf(stderr, "No command given.\n");
			continue;
		}
		if (commandLen > len) {
			fprintf(stderr, "Command too long. Ignored.\n");
			continue;
		}

		// copy the command
		memcpy(input, message.command, commandLen);
		input[len - 1] = '\0';	// always NULL-terminate
		return input;
	
	} while (true);
}