<commit_before>#include <libmary/libmary.h>


using namespace M;

namespace {

TcpConnection *test_conn = NULL;
Timers test_timers;
Timers::TimerKey conn_timeout_key;

void testDoInput ()
{
    logD_ (_func_);

    Byte buf [4096];
    Size nread;
    for (;;) {
	AsyncIoResult const res = test_conn->read (Memory::forObject (buf), &nread);
	switch (res) {
	    case AsyncIoResult::Again:
		logD_ (_func, "read(): Again");
		return;
	    case AsyncIoResult::Eof:
		// TODO Quit program by breaking main loop.
		logD_ (_func, "read(): Eof");
		return;
	    case AsyncIoResult::Error:
		logE_ (_func, "read() failed: ", exc->toString());
		return;
	    case AsyncIoResult::Normal:
		break;
	    default:
		assert (0);
	}

	logD_ (_func, "read(): ", nread, " bytes");

	errs->print (ConstMemory (buf, nread));
    }
}

void testProcessInput (void * const /* cb_data */)
{
    logD_ (_func_);
    testDoInput ();
}

void testProcessError (Exception * const exc_,
		       void      * const /* cb_data */)
{
    if (exc_) {
	logD_ (_func, exc_->toString());
	return;
    }

    logD_ (_func_);
}

Connection::InputFrontend const input_frontend = {
    testProcessInput,
    testProcessError
};

void testProcessOutput (void * const /* cb_data */)
{
    logD_ (_func_);
}

Connection::OutputFrontend const output_frontend = {
    testProcessOutput
};

void testConnected (Exception * const exc_,
		    void      * const /* cb_data */)
{
    logD_ (_func_);

    test_timers.deleteTimer (conn_timeout_key);

    if (exc_) {
	logE_ (_func, exc_->toString());
	return;
    }

    testDoInput ();
}

void testConnectionTimeout (void * const /* cb_data */)
{
    logD_ (_func, "connection timeout");
    // TODO Release 'conn_timeout_key' timer.
//    test_conn->close ();
}

TcpConnection::Frontend tcp_frontend = {
    testConnected
};

Result doTest ()
{
  {
    updateTime ();

    TcpConnection conn (NULL);
    test_conn = &conn;
    conn.Connection::setInputFrontend (Cb<Connection::InputFrontend> (
	    &input_frontend, NULL /* cb_data */, NULL /* coderef_container */));
    conn.Connection::setOutputFrontend (Cb<Connection::OutputFrontend> (
	    &output_frontend, NULL /* cb_data */, NULL /* coderef_container */));
    conn.setFrontend (Cb<TcpConnection::Frontend> (
	    &tcp_frontend, NULL /* frontend_cb_data */, NULL /* coderef_container */));

    IpAddress addr;
    if (!setIpAddress ("127.0.0.1:8080", &addr)) {
	logE_ (_func, "setIpAddress() failed");
	return Result::Failure;
    }

    if (!conn.connect (addr))
	goto exc_;

    conn_timeout_key = test_timers.addTimer (testConnectionTimeout, NULL, NULL, 5 /* time_seconds */);

    DefaultPollGroup poll_group (NULL);
    if (!poll_group.open ())
	goto exc_;

    if (!poll_group.addPollable (conn.getPollable()))
	goto exc_;<commit_msg>api change comment<commit_after>#include <libmary/libmary.h>


using namespace M;

namespace {

TcpConnection *test_conn = NULL;
Timers test_timers;
Timers::TimerKey conn_timeout_key;

void testDoInput ()
{
    logD_ (_func_);

    Byte buf [4096];
    Size nread;
    for (;;) {
	AsyncIoResult const res = test_conn->read (Memory::forObject (buf), &nread);
	switch (res) {
	    case AsyncIoResult::Again:
		logD_ (_func, "read(): Again");
		return;
	    case AsyncIoResult::Eof:
		// TODO Quit program by breaking main loop.
		logD_ (_func, "read(): Eof");
		return;
	    case AsyncIoResult::Error:
		logE_ (_func, "read() failed: ", exc->toString());
		return;
	    case AsyncIoResult::Normal:
		break;
	    default:
		assert (0);
	}

	logD_ (_func, "read(): ", nread, " bytes");

	errs->print (ConstMemory (buf, nread));
    }
}

void testProcessInput (void * const /* cb_data */)
{
    logD_ (_func_);
    testDoInput ();
}

void testProcessError (Exception * const exc_,
		       void      * const /* cb_data */)
{
    if (exc_) {
	logD_ (_func, exc_->toString());
	return;
    }

    logD_ (_func_);
}

Connection::InputFrontend const input_frontend = {
    testProcessInput,
    testProcessError
};

void testProcessOutput (void * const /* cb_data */)
{
    logD_ (_func_);
}

Connection::OutputFrontend const output_frontend = {
    testProcessOutput
};

void testConnected (Exception * const exc_,
		    void      * const /* cb_data */)
{
    logD_ (_func_);

    test_timers.deleteTimer (conn_timeout_key);

    if (exc_) {
	logE_ (_func, exc_->toString());
	return;
    }

    testDoInput ();
}

void testConnectionTimeout (void * const /* cb_data */)
{
    logD_ (_func, "connection timeout");
    // TODO Release 'conn_timeout_key' timer.
//    test_conn->close ();
}

TcpConnection::Frontend tcp_frontend = {
    testConnected
};

Result doTest ()
{
  {
    updateTime ();

    TcpConnection conn (NULL);
    test_conn = &conn;
    conn.Connection::setInputFrontend (Cb<Connection::InputFrontend> (
	    &input_frontend, NULL /* cb_data */, NULL /* coderef_container */));
    conn.Connection::setOutputFrontend (Cb<Connection::OutputFrontend> (
	    &output_frontend, NULL /* cb_data */, NULL /* coderef_container */));
    conn.setFrontend (Cb<TcpConnection::Frontend> (
	    &tcp_frontend, NULL /* frontend_cb_data */, NULL /* coderef_container */));

    IpAddress addr;
    if (!setIpAddress ("127.0.0.1:8080", &addr)) {
	logE_ (_func, "setIpAddress() failed");
	return Result::Failure;
    }

    // TODO TcpConnection::connect() API has changed.
    if (!conn.connect (addr))
	goto exc_;

    conn_timeout_key = test_timers.addTimer (testConnectionTimeout, NULL, NULL, 5 /* time_seconds */);

    DefaultPollGroup poll_group (NULL);
    if (!poll_group.open ())
	goto exc_;

    if (!poll_group.addPollable (conn.getPollable()))
	goto exc_;
{'fraction_non_alphanumeric': 0.1446229913473424, 'fraction_numerical': 0.005650715168638531, 'mean_word_length': 3.4598425196850395, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '83325', 'n_tokens_mistral': 1993, 'n_tokens_neox': 1896, 'n_words': 440}