/* -*- c++ -*- */
/* 
 * Copyright 2021 Satlab A/S.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * This software 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifndef INCLUDED_SATLAB_BROADCAST_SERVER_H
#define INCLUDED_SATLAB_BROADCAST_SERVER_H

#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/common/thread.hpp>
#include <websocketpp/server.hpp>

#include <iostream>
#include <set>

typedef websocketpp::server<websocketpp::config::asio> server;

using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::bind;

using websocketpp::lib::thread;
using websocketpp::lib::lock_guard;

namespace gr {
  namespace satlab {

    class broadcast_server {
      public:
        broadcast_server();

        void run(uint16_t port);

        void stop();

        void on_open(connection_hdl hdl);

        void on_close(connection_hdl hdl);

        void broadcast(std::string msg);

        void set_greeting(std::string msg);

     private:
        typedef std::set<connection_hdl,std::owner_less<connection_hdl> > con_list;

        websocketpp::lib::thread *m_thread_server;
        std::string m_greeting = "{}";

        server m_server;
        con_list m_connections;
        std::mutex m_mutex;
    };

  } // namespace satlab
} // namespace gr

#endif /* INCLUDED_SATLAB_BROADCAST_SERVER_H */
