simple_client.c 3.8 KB
/**
 * \file
 *
 * \author	Georg Hopp
 *
 * \copyright
 * Copyright © 2014 Georg Hopp
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdarg.h>

#include <sys/types.h>
#include <stdint.h>

#include "trbase.h"
#include "trio.h"
#include "trevent.h"

#include "tr/simple_client.h"
#include "tr/comm_end_point.h"
#include "tr/comm_manager.h"
#include "tr/comm_manager_poll.h"
#include "tr/comm_manager_epoll.h"
#include "tr/connector.h"
#include "tr/io_handler.h"
#include "tr/protocol_handler.h"


static
int
simpleClientCtor(void * _this, va_list * params)
{
	TR_SimpleClient this = _this;

	this->endpoint         = va_arg(*params, TR_CommEndPoint);

#if 1
	this->comm_manager     = (TR_CommManager)TR_new(TR_CommManagerEpoll);
#else
	this->comm_manager     = (TR_CommManager)TR_new(TR_CommManagerPoll);
#endif
	this->dispatcher       = TR_new(TR_EventDispatcher, TR_EVD_CLIENT, NULL);
	this->io_handler       = TR_new(TR_IoHandler);
	this->protocol_handler = TR_new(TR_ProtocolHandler);
	this->timer            = TR_new(TR_Timer, TR_TBASE_MIL, 1000);

	TR_eventDispatcherRegisterHandler(
			this->dispatcher,
			(TR_EventHandler)this->comm_manager);
	TR_eventDispatcherRegisterHandler(
			this->dispatcher,
			(TR_EventHandler)this->io_handler);
	TR_eventDispatcherRegisterHandler(
			this->dispatcher,
			(TR_EventHandler)this->protocol_handler);
	TR_eventDispatcherRegisterHandler(
			this->dispatcher,
			(TR_EventHandler)this);

	TR_commManagerAddEndpoint(this->comm_manager, this->endpoint);

	return 0;
}

static
void
simpleClientDtor(void * _this)
{
	TR_SimpleClient this = _this;

	TR_delete(this->protocol_handler);
	TR_delete(this->io_handler);
	TR_delete(this->comm_manager);
	TR_delete(this->dispatcher);
	TR_delete(this->timer);
}

static
TR_EventDone
simpleClientUserAction(void * _this, TR_Event event)
{
	TR_SimpleClient this = _this;

	if (this->send_issued) {
		unsigned long missed;

		TR_timerGet(this->timer, &missed);

		if (this->response || missed) {
			TR_eventDispatcherStop((TR_EventDispatcher)event->subject);
		} else {
			TR_eventHandlerIssueEvent(
					(TR_EventHandler)_this,
					TR_eventSubjectEmit(
						event->subject,
						TR_DISPATCHER_EVENT_DATA_WAIT,
						this->timer));
		}
	} else {
		TR_eventHandlerIssueEvent(
				(TR_EventHandler)_this,
				TR_eventSubjectEmit(
					(TR_EventSubject)this->endpoint,
					TR_CEP_EVENT_MSG_READY,
					this->request));
		this->send_issued = TRUE;
	}

	return TR_EVENT_DONE;
}

static
TR_EventDone
simpleClientHandleData(void * _this, TR_Event event)
{
	TR_ProtoMessage data = event->data;

	if (data->remote->handle == -1) {
		TR_delete(data->remote);
	}

	((TR_SimpleClient)_this)->response = event->data;

	return TR_EVENT_DONE;
}

static
void
simpleClientCvInit(TR_class_ptr cls)
{
	TR_EVENT_HANDLER_SET_METHOD(
			cls,
			TR_EventDispatcher,
			TR_DISPATCHER_EVENT_USER_WAIT,
			simpleClientUserAction);
	TR_EVENT_HANDLER_SET_METHOD(
			cls,
			TR_CommEndPoint,
			TR_CEP_EVENT_NEW_MSG,
			simpleClientHandleData);
}

TR_INIT_HANDLER(TR_SimpleClient);
TR_INIT_IFACE(TR_Class, simpleClientCtor, simpleClientDtor, NULL);
TR_CREATE_CLASS(
		TR_SimpleClient,
		TR_EventHandler,
		simpleClientCvInit,
		TR_IF(TR_Class)) = {
	{ TR_HANDLER_CVARS(TR_SimpleClient) }
};

// vim: set ts=4 sw=4: