/*

Copyright © 2003, Russell C. Hay
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

i. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer. 

ii. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution. 

iii. Neither the name of the dead[protocol]society nor the names of its
     contributors may be used to endorse or promote products derived from this
     software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <unistd.h>
#include <termios.h>
#include "stl.h"
#include "mtxtype.h"
#include "action.h"
#include "device.h"
#include "protocol.h"
#include "connection.h"
#include "connectionMgr.h"

connectionMgr::connectionMgr(protocol *p, device *d)
: m_protocol(p)
, m_device(d)
, m_sock(-1)
, m_done(0)
, m_queue(2)
{
}

connectionMgr::~connectionMgr()
{
	connection_queue_t::iterator iter;

	for(iter=m_queue.begin();iter!=m_queue.end();iter++)
	{
		if ((*iter)!=0)
			delete (*iter);
	}
}

int connectionMgr::InitializeEphemeril(char *dev)
{
	struct sockaddr_un sun;

	m_sock = socket(PF_LOCAL, SOCK_STREAM, 0);
	if (m_sock < 0)
		return -1;

	sun.sun_len = sizeof(sun);
	sun.sun_family = AF_LOCAL;
	strcpy(sun.sun_path, dev);

	if (bind(m_sock, (struct sockaddr *)&sun, sizeof(sun))<0)
		return -1;

	if (listen(m_sock, 5))
		return -1;
}

int connectionMgr::Run()
{
	int blah;
	struct sockaddr_un sun;
	unsigned int size = sizeof(sun);
	fd_set fdset;
	int maxfd = 0;
	connection_queue_t::iterator iter;
	connection *conn;
	char key = 0;

	// Repeat Forever
	//   add ephemeril socket to fdset
	//   add serial socket to fdset
	//   add all connection sockets to fdset
	//   select on fdset
	//   if serial socket has data, read data, and schedule dispatch
	//   if ephemeril socket has data, accept connection and add to list
	//   if any connection socket has data, call HandleConnection for that
	//      connection, and then call KeyNotification if we have key data to
	//      dispatch
	//   
	// End Repeat
	while(!m_done)
	{
		maxfd = m_sock+1;
		FD_ZERO(&fdset);
		FD_SET(m_sock, &fdset);
		m_device->AddToFdSet(&fdset, &maxfd);

		for (iter=m_queue.begin();iter!=m_queue.end();iter++)
		{
			if ((*iter)!=0)
				(*iter)->AddSocketToFdSet(&fdset, &maxfd);
		}

		if (select(maxfd, &fdset, NULL, NULL, NULL)<0)
		{
			perror("select");
			continue;
		}

		if (m_device->IsSet(&fdset))
		{
			key = m_device->HandleIncoming();
			printf("Read '%c' from keypad\n", key);
		}

		for(iter=m_queue.begin();iter!=m_queue.end();iter++)
		{
			if (((*iter)!=0)&&((*iter)->IsSet(&fdset)))
			{
				if ((*iter)->HandleConnection()==1)
				{
					delete (*iter);
					*iter = 0;
				}
			}
		}

		if (key != 0)
		{
			for(iter=m_queue.begin();iter!=m_queue.end();iter++)
			{
				if ((*iter)!=0)
					(*iter)->KeyNotification(key);
			}
			key = 0;
		}

		if (FD_ISSET(m_sock, &fdset))
		{
			int sock;

			sock = accept(m_sock, NULL, NULL);
			if (sock < 0)
				perror("accept");
			else {
				printf("Connection Accepted\n");
				conn = new connection(m_protocol, sock, m_device);
				m_queue.push_back(conn);
			}
		}
	}

	return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1