/*
 * Copyright (C) 2015 by Multi-Tech Systems
 *
 * This file is part of libmts-io.
 *
 * libmts-io 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 of the License, or
 * (at your option) any later version.
 *
 * libmts-io 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 libmts-io.  If not, see .
 *
 */
/*! 
 \file MTS_IO_Connection.cpp
 \brief A brief description 
 \date Jan 15, 2013
 \author sgodinez
 A more elaborate description than necessary.
*/
#include 
#include 
#include 
using namespace MTS;
using namespace MTS::IO;
int32_t Connection::BLOCKING = -1;
Connection::Connection(const std::string& name)
: m_sName(name)
, m_bClosed(true)
, m_bCloseable(true) {
    m_apLock.reset(new Lock());
    m_apReadLock.reset(new Lock());
    m_apWriteLock.reset(new Lock());
}
Connection::~Connection() {
    m_apWriteLock.reset();
    m_apReadLock.reset();
    m_apLock.reset();
}
const std::string& Connection::getName() const {
    return m_sName;
}
bool Connection::open(const int32_t& timeoutMillis) {
    bool bOpened = true;
    m_apLock->lock();
    if (m_bClosed) {
        bOpened = doOpen(timeoutMillis);
        if (bOpened) {
            m_bClosed = false;
        }
    }
    m_apLock->unlock();
    return bOpened;
}
void Connection::close() {
    m_apLock->lock();
    if (m_bCloseable) {
        doClose();
        m_bClosed = true;
    }
    m_apLock->unlock();
}
bool Connection::isClosed() const {
    m_apLock->lock();
    bool bResult = m_bClosed;
    m_apLock->unlock();
    return bResult;
}
void Connection::setCloseable(bool bCloseable) {
    m_apLock->lock();
    m_bCloseable = bCloseable;
    m_apLock->unlock();
}
bool Connection::isCloseable() const {
    m_apLock->lock();
    bool bResult = m_bCloseable;
    m_apLock->unlock();
    return bResult;
}
int Connection::read(std::string& sBuffer, const uint32_t& iSize, int32_t& timeoutMillis) {
    int result = -1;
    if (isClosed()) {
        return result;
    } else {
        char buffer[iSize];
        m_apReadLock->lock();
        result = doRead(buffer, iSize, timeoutMillis);
        m_apReadLock->unlock();
        if(result > 0) {
            sBuffer = std::string(buffer, result);
        }
    }
    return result;
}
int Connection::read(Buffer& oBuffer, const uint32_t& iSize, int32_t& timeoutMillis) {
    int result = -1;
    if (isClosed()) {
        return result;
    } else {
        oBuffer.setCapacity(iSize);
        m_apReadLock->lock();
        result = doRead((char*)oBuffer.getBuffer(), iSize, timeoutMillis);
        m_apReadLock->unlock();
    }
    return result;
}
int Connection::read(char* pBuffer, const uint32_t& iSize, int32_t& timeoutMillis) {
    int result = -1;
    if (isClosed()) {
        return result;
    } else {
        m_apReadLock->lock();
        result = doRead(pBuffer, iSize, timeoutMillis);
        m_apReadLock->unlock();
    }
    return result;
}
int Connection::write(const char* pBuffer, const uint32_t& iSize, int32_t& timeoutMillis) {
    int result = -1;
    if (isClosed()) {
        return result;
    } else {
        m_apWriteLock->lock();
        result = doWrite(pBuffer, iSize, timeoutMillis);
        m_apWriteLock->unlock();
    }
    return result;
}