/*
 *
 *
 *    muxudpsocket.cc
 *
 *    C++ source code for class my::multiplexed::udpsocket member functions
 *    and/or static member data.
 *
 *    =========================================================================
 *
 *    Copyright (C) 1999-2003 by Matt Roberts, KK5JY.
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    =========================================================================
 *
 *
 */


#include <my++/muxudpsocket.h>

#define DEFAULT_BUFFER_LENGTH (65536)

my::multiplexed::udpsocket::udpsocket (void)
	: buffer_len(0), c_buffer(0), tx_ok(false) {
	buffer(DEFAULT_BUFFER_LENGTH);
}

my::multiplexed::udpsocket::udpsocket (const inet_address &a, int p)
	: my::udpsocket(a,p), buffer_len(0), c_buffer(0), tx_ok(false) {
	buffer(DEFAULT_BUFFER_LENGTH);
}

my::multiplexed::udpsocket::udpsocket (const address &a)
	: my::udpsocket(a), buffer_len(0), c_buffer(0), tx_ok(false) {
	buffer(DEFAULT_BUFFER_LENGTH);
}

my::multiplexed::udpsocket::udpsocket (const std::string &a, int p)
	: my::udpsocket(a,p), buffer_len(0), c_buffer(0), tx_ok(false) {
	buffer(DEFAULT_BUFFER_LENGTH);
}

void my::multiplexed::udpsocket::buffer (size_t len) {
	if (len > 0) {
		if (c_buffer != 0)
			delete [] c_buffer;
		buffer_len = len;
		c_buffer = new unsigned char [buffer_len];
	}
}


bool my::multiplexed::udpsocket::should_watch_read(void) {
	// probably should change this to watch for a bad file descriptor
	return true;
}

bool my::multiplexed::udpsocket::should_watch_write(void) {
	// this too
	return ( ! tx_ok );
}

bool my::multiplexed::udpsocket::read_action (void) {
	my::multiplexed::udpsocket::address a;
	ssize_t rtn = -1; 

	do {
		try {
			rtn = my::udpsocket::get_from (a, c_buffer, buffer_len);
		} catch (const udpsocket::exception &e) {
			if (e.errnum() != EINTR)
				throw;
			rtn = 1;
		}
	} while (rtn < 0);

	// now put the rec'd packet on the queue
	rx_buffer.push (c_buffer, rtn);
	rx_address.push_back (a);
	return true;
}

bool my::multiplexed::udpsocket::write_action (void) {
	if (tx_buffer.size() > 0) {
		tx_ok = false;
		address a(tx_address.front());
		tx_address.pop_front();
		size_t len = tx_buffer.pop(c_buffer, buffer_len);
		ssize_t rtn = -1;

		do {
			try {
				rtn = my::udpsocket::put_to(a, c_buffer, len);
			} catch (const udpsocket::exception &e) {
				if (e.errnum() != EINTR)
					throw;
				rtn = -1;
			}
		} while (rtn < 0);
	} else {
		tx_ok = true;
	}

	// no need to bother the application, we took care of it
	return false;
}

size_t my::multiplexed::udpsocket::get_from (my::socket::address &a, unsigned char *buf, size_t len) {
	my::multiplexed::udpsocket::address &a2 =
		dynamic_cast<my::multiplexed::udpsocket::address &>(a);
	a2 = rx_address.front();
	rx_address.pop_front();
	return rx_buffer.pop ((byte_t*)(buf), len);
}

size_t my::multiplexed::udpsocket::get_from (my::socket::address &a, char *buf, size_t len) {
	my::multiplexed::udpsocket::address &a2 =
		dynamic_cast<my::multiplexed::udpsocket::address &>(a);
	a2 = rx_address.front();
	rx_address.pop_front();
	return rx_buffer.pop ((byte_t*)buf, len);
}

size_t my::multiplexed::udpsocket::get_from (my::socket::address &a, void *buf, size_t len) {
	my::multiplexed::udpsocket::address &a2 =
		dynamic_cast<my::multiplexed::udpsocket::address &>(a);
	a2 = rx_address.front();
	rx_address.pop_front();
	return rx_buffer.pop ((byte_t*)(buf), len);
}

size_t my::multiplexed::udpsocket::put_to (const my::socket::address &a, const unsigned char *buf, size_t len) {
	const my::multiplexed::udpsocket::address &a2 =
		dynamic_cast<const my::multiplexed::udpsocket::address &>(a);
	size_t rtn = len;

	if (tx_ok) {
		tx_ok = false;
		rtn = my::udpsocket::put_to(a, (byte_t*)(buf), len);
	} else {
		tx_buffer.push((byte_t*)(buf), len);
		tx_address.push_back(a2);
	}

	return rtn;
}

size_t my::multiplexed::udpsocket::put_to (const my::socket::address &a, const char *buf, size_t len) {
	const my::multiplexed::udpsocket::address &a2 =
		dynamic_cast<const my::multiplexed::udpsocket::address&>(a);
	size_t rtn = len;

	if (tx_ok) {
		tx_ok = false;
		rtn = my::udpsocket::put_to(a, (byte_t*)buf, len);
	} else {
		tx_buffer.push((byte_t*)(buf), len);
		tx_address.push_back(a2);
	}

	return rtn;
}

size_t my::multiplexed::udpsocket::put_to (const my::socket::address &a, const void *buf, size_t len) {
	const my::multiplexed::udpsocket::address &a2 =
		dynamic_cast<const my::multiplexed::udpsocket::address&>(a);
	size_t rtn = len;

	if (tx_ok) {
		tx_ok = false;
		rtn = my::udpsocket::put_to(a, (byte_t*)buf, len);
	} else {
		tx_buffer.push(static_cast<const byte_t*>(buf), len);
		tx_address.push_back(a2);
	}

	return rtn;
}
