libpsynth 0.2.1
/home/raskolnikov/dev/psynth/trunk/src/psynth/net/osc_client.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   PSYCHOSYNTH                                                           *
00004  *   ===========                                                           *
00005  *                                                                         *
00006  *   Copyright (C) Juan Pedro Bolivar Puente 2007                          *
00007  *                                                                         *
00008  *   This program is free software: you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation, either version 3 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 #ifndef PSYNTH_OSCCLIENT_H
00024 #define PSYNTH_OSCCLIENT_H
00025 
00026 #include <psynth/net/osc_controller.hpp>
00027 
00028 #define PSYNTH_DEFAULT_CLIENT_PORT      8192
00029 #define PSYNTH_DEFAULT_CLIENT_PORT_STR "8192"
00030 
00031 namespace psynth
00032 {
00033 
00034 class osc_client;
00035 
00036 enum osc_client_error {
00037     CE_NONE = 0,
00038     CE_PORT_BINDING,
00039     CE_SERVER_TIMEOUT,
00040     CE_SERVER_DROP
00041 };
00042 
00043 class osc_client_listener
00044 {
00045 public:
00046     virtual ~osc_client_listener() {}
00047     
00048     virtual bool handle_client_connect(osc_client* client) = 0;
00049     virtual bool handle_client_disconnect(osc_client* client, osc_client_error err) = 0;
00050     virtual bool handle_client_accept(osc_client* client) = 0;
00051 };
00052 
00053 class osc_client_subject
00054 {
00055     std::list<osc_client_listener*> m_list;
00056 
00057 protected:
00058     void notify_client_connect(osc_client* param);
00059     void notify_client_disconnect(osc_client* param, osc_client_error err);
00060     void notify_client_accept(osc_client* param);
00061     
00062 public:
00063     void add_listener(osc_client_listener* l) {
00064         m_list.push_back(l);
00065     };
00066     
00067     void delete_listener(osc_client_listener* l) {
00068         m_list.remove(l);
00069     };
00070 };
00071 
00072 class osc_client : public osc_controller,
00073                   public osc_client_subject
00074 {
00075 public:
00076     enum state {
00077         IDLE,
00078         PENDING,
00079         CONNECTED,
00080         CLOSING
00081     };
00082 
00083 private:
00084     lo_address m_target;
00085     lo_server m_server;
00086     int m_id;
00087     state m_state;
00088     int m_last_alive_recv;
00089     int m_last_alive_sent;
00090     int m_count_next;
00091     
00092     LO_HANDLER(osc_client, alive);
00093     LO_HANDLER(osc_client, drop);
00094     LO_HANDLER(osc_client, accept);
00095 
00096     void add_methods();
00097     void close();
00098     
00099 public:
00100     
00101     osc_client();
00102     ~osc_client();
00103 
00104     state get_state() {
00105         return m_state;
00106     }
00107     
00108     void connect(lo_address target, const char* src_port);
00109     void disconnect();
00110 
00111     /* Timeout < 0 for blocking operation. */
00112     int receive(int time_out = 0);
00113     int update(int msec);
00114 };
00115 
00116 } /* namespace psynth */
00117 
00118 #endif /* PSYNTH_OSCCLIENT_H */