/* Blinker
   Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org>
   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
   a blinkenarea.org project */

#include <string>

#include "OpConn.h"
#include "OpConnIf.h"

namespace Blinker {

/**
 * @brief constructor
 * @param[in] pConnIf connection interface of this side
 */
OpConn::OpConn(OpConnIf *pConnIf):
  m_pConnIf(pConnIf),
  m_pRemote(NULL)
{
}

/// destructor
OpConn::~OpConn()
{
  // think twice before putting something here
  // might be explicitly deconstructed by remote side
}

/**
 * @brief send key command
 * @param[in] key key that was pressed
 */
void OpConn::sendKey(char key)
{
  if (m_pRemote)
    m_pRemote->m_pConnIf->opConnRecvKey(m_pRemote, key);
}

/**
 * @brief send play command
 * @param[in] sound name of sound to play
 */
void OpConn::sendPlay(const std::string &sound)
{
  if (m_pRemote)
    m_pRemote->m_pConnIf->opConnRecvPlay(m_pRemote, sound);
}

/// close connection
void OpConn::close()
{
  // inform remote side
  if (m_pRemote)
    m_pRemote->m_pConnIf->opConnClose(m_pRemote);

  // destroy local and remote connection objects
  if (m_pRemote)
    delete m_pRemote;
  delete this;
}

/**
 * @brief set remote side of connection
 * @param[in] pRemote remote side connection object
 */
void OpConn::setRemote(OpConn *pRemote)
{
  m_pRemote = pRemote;
}

} // namespace Blinker

