//------------------------------------------------------------------------------
// <copyright file="LqrSMACSocket.h" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
// </summary>
//
// <owner></owner>
//------------------------------------------------------------------------------

#ifndef __LqrSMACSocket_h__
#define __LqrSMACSocket_h__

#include "LqrSMACDeclareException.h"
#include "LqrSMACSharedLib.h"
#include <string>
#include <boost/shared_ptr.hpp>

namespace LiquidRobotics {
namespace SMACFramework {
namespace Socket {

struct SHARED_LIB_PUBLIC SocketBindInfo
{
	SocketBindInfo(int _port);
	SocketBindInfo(const string& _name, int _port);
	SocketBindInfo(const string& _name);

	const bool hasPort;
	const int port;
	const bool hasName;
	const string name;
};

class SHARED_LIB_PUBLIC ISocket
{
public:

	class SHARED_LIB_PUBLIC ErrnoException : public std::runtime_error
	{
	public:

		ErrnoException(int _errNum, const string& msg) throw()
		: std::runtime_error(msg)
		, errNum(_errNum)
		{
		}

		static void Throw(const string& operation)
		{
			Throw(errno, operation);
		}

		static void Throw(int _errNum, const string& operation)
		{
			using namespace std;
			stringstream msg;
			msg << operation << " error #" << _errNum << " '" << GetErrorDescription(_errNum) << "'";
			string msgStr = msg.str();
			throw ErrnoException(_errNum, msgStr);
		}

		int errNum;
	};


	typedef boost::shared_ptr<ISocket> ISocketPtr;
	typedef std::string string;

	virtual void ~ISocket() { }

	virtual bool GetUseAbstractName() const = 0;
	virtual void SetUseAbstractName(bool newValue) = 0;

	virtual ISocketPtr Accept() = 0;
	virtual void BindAndListen(const SocketBindInfo& sbi) = 0;
	virtual void Connect(const SocketBindInfo& sbi) = 0;
	virtual void Read(int length, string& data) = 0;
	virtual void Recv(string& data) = 0;
	virtual void Send(const string& data) = 0;
	virtual void SetNonBlocking(bool set = true) = 0;
	virtual bool Valid() const = 0;
	virtual int Write(const string& data) = 0;
};

class SHARED_LIB_PUBLIC SocketBase : public ISocket
{
public:

	SocketBase(int sockDomain, int sockType, int sockProtocol, bool abstractName = false);

	virtual bool GetUseAbstractName() const;
	virtual void SetUseAbstractName(bool newValue);

	virtual ISocketPtr Accept();
	virtual void BindAndListen(const SocketBindInfo& sbi);
	virtual void Connect(const SocketBindInfo& sbi);
	virtual void Create();
	virtual void Recv(const string& data);
	virtual void Send(const string& data);
	virtual void SetNonBlocking(bool set = true);
	virtual bool Valid() const;

private:

	int sockfd;
	bool abstractName;
	union
	{
		sockaddr sa;
		sockaddr_in sa_in;
		sockaddr_un sa_un;
	};
};

}
}
}

#endif
