Guitarix
jsonrpc.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, 2010 Hermann Meyer, James Warden, Andreas Degert
3  * Copyright (C) 2011 Pete Shorthose
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #pragma once
21 
22 #ifndef SRC_HEADERS_JSONRPC_H_
23 #define SRC_HEADERS_JSONRPC_H_
24 
25 #include "engine.h"
26 #include <bitset>
27 #include <queue>
28 #include <giomm/init.h> // NOLINT
29 #include <giomm/socketservice.h>
30 #include "jsonrpc_methods.h"
31 
32 class GxService;
33 
34 class JsonValue {
35 protected:
36  JsonValue() {}
37  virtual ~JsonValue() {}
38  friend class JsonArray;
39 public:
40  virtual double getFloat() const;
41  virtual int getInt() const;
42  virtual const Glib::ustring& getString() const;
43  virtual gx_system::JsonSubParser getSubParser() const;
44 };
45 
46 class JsonArray: public std::vector<JsonValue*> {
47 public:
48  JsonArray():std::vector<JsonValue*>() {}
49  ~JsonArray();
50  JsonValue *operator[](unsigned int i);
51  void append(gx_system::JsonParser& jp);
52 };
53 
54 class CmdConnection: public sigc::trackable {
55 public:
56  struct methodnames {
57  const char *name;
58  jsonrpc_method m_id;
59  };
60  enum msg_type {
76  };
77 private:
79  Glib::RefPtr<Gio::SocketConnection> connection;
80  std::list<std::string> outgoing;
81  unsigned int current_offset;
84  std::bitset<END_OF_FLAGS> flags;
85  std::map<string,float> maxlevel;
86 private:
87  bool find_token(const Glib::ustring& token, msg_type *start, msg_type *end);
88  void activate(int n, bool v) { flags.set(n, v); }
89  void exec(Glib::ustring cmd);
90  void call(gx_system::JsonWriter& jw, const methodnames *mn, JsonArray& params);
91  void notify(gx_system::JsonStringWriter& jw, const methodnames *mn, JsonArray& params);
93  void write_error(gx_system::JsonWriter& jw, int code, const char *message);
94  void write_error(gx_system::JsonWriter& jw, int code, Glib::ustring& message) { write_error(jw, code, message.c_str()); }
95  void error_response(gx_system::JsonWriter& jw, int code, const char *message);
96  void error_response(gx_system::JsonWriter& jw, int code, const Glib::ustring& message) { error_response(jw, code, message.c_str()); }
97  void send_notify_begin(gx_system::JsonStringWriter& jw, const char *method) { jw.send_notify_begin(method); }
98  void send_notify_end(gx_system::JsonStringWriter& jw, bool send_out=true);
99  void listen(const Glib::ustring& tp);
100  void unlisten(const Glib::ustring& tp);
102 
103 public:
104  CmdConnection(GxService& serv, const Glib::RefPtr<Gio::SocketConnection>& connection_);
105  ~CmdConnection();
106  bool on_data_in(Glib::IOCondition cond);
107  bool on_data_out(Glib::IOCondition cond);
109  bool is_activated(msg_type n) { return flags[n]; }
110  void update_maxlevel(const std::string& id, float v) { float& m = maxlevel[id]; m = max(m, v); }
111  friend class UiBuilderVirt;
112 };
113 
118 };
119 
120 class GxService: public Gio::SocketService {
121 private:
122  struct ChangedPlugin {
123  std::string id;
125  ChangedPlugin(const std::string& id_, gx_engine::PluginChange::pc status_): id(id_), status(status_) {}
126  };
127 private:
131  sigc::slot<void> quit_mainloop;
133  time_t last_change;
134  sigc::connection save_conn;
135  std::list<CmdConnection*> connection_list;
136  std::queue<broadcast_data> broadcast_list;
138  std::map<std::string,bool> *preg_map;
139  std::map<std::string,float> maxlevel;
140 private:
141  virtual bool on_incoming(const Glib::RefPtr<Gio::SocketConnection>& connection,
142  const Glib::RefPtr<Glib::Object>& source_object);
143  void save_state();
147  bool idle_broadcast_handler();
149 
150  // message formatting functions
152  void ladspaloader_write_changes(gx_system::JsonWriter& jw, std::vector<ChangedPlugin>& changed_plugins);
153 
154  // signal handler
155  void on_param_insert_remove(gx_engine::Parameter *p, bool insert);
157  void preset_changed();
159  void on_tuner_freq_changed();
160  void display(const Glib::ustring& bank, const Glib::ustring& preset);
162  void on_selection_done(bool v);
163  void on_presetlist_changed();
164  void on_log_message(const string& msg, GxLogger::MsgType tp, bool plugged);
165  void on_midi_changed();
166  void on_midi_value_changed(int ctl, int value);
167  void on_rack_unit_changed(bool stereo);
169  std::vector<ChangedPlugin>& vec);
170  void create_bluetooth_sockets(const Glib::ustring& host);
171 
172  friend class CmdConnection;
173 public:
175  TunerSwitcher& tunerswitcher, sigc::slot<void> quit_mainloop_,
176  const Glib::ustring& host, int *port);
177  ~GxService();
178  void send_rack_changed(bool stereo, CmdConnection *cmd);
180  float update_maxlevel(const std::string& id, bool reset=false);
181 };
182 
185 
186 #endif // SRC_HEADERS_JSONRPC_H_
GxService::save_conn
sigc::connection save_conn
Definition: jsonrpc.h:134
GxService::update_maxlevel
float update_maxlevel(const std::string &id, bool reset=false)
CmdConnection::call
void call(gx_system::JsonWriter &jw, const methodnames *mn, JsonArray &params)
CmdConnection::f_state_changed
@ f_state_changed
Definition: jsonrpc.h:62
CmdConnection::f_midi_value_changed
@ f_midi_value_changed
Definition: jsonrpc.h:70
GxService::jack
gx_jack::GxJack & jack
Definition: jsonrpc.h:129
broadcast_data::jw
gx_system::JsonStringWriter * jw
Definition: jsonrpc.h:115
GxService::on_tuner_freq_changed
void on_tuner_freq_changed()
gx_system::JsonStringParser
Definition: gx_json.h:202
CmdConnection::CmdConnection
CmdConnection(GxService &serv, const Glib::RefPtr< Gio::SocketConnection > &connection_)
CmdConnection::activate
void activate(int n, bool v)
Definition: jsonrpc.h:88
CmdConnection::msg_type
msg_type
Definition: jsonrpc.h:60
GxService::on_log_message
void on_log_message(const string &msg, GxLogger::MsgType tp, bool plugged)
string_to_engine_state
gx_engine::GxEngineState string_to_engine_state(const std::string &s)
CmdConnection::write_error
void write_error(gx_system::JsonWriter &jw, int code, const char *message)
gx_engine::PluginChange::pc
pc
Definition: gx_engine.h:67
CmdConnection::f_preset_changed
@ f_preset_changed
Definition: jsonrpc.h:61
CmdConnection::UiBuilderVirt
friend class UiBuilderVirt
Definition: jsonrpc.h:111
CmdConnection::serv
GxService & serv
Definition: jsonrpc.h:78
CmdConnection
Definition: jsonrpc.h:54
CmdConnection::request
bool request(gx_system::JsonStringParser &jp, gx_system::JsonStringWriter &jw, bool batch_start)
GxService::create_bluetooth_sockets
void create_bluetooth_sockets(const Glib::ustring &host)
CmdConnection::unlisten
void unlisten(const Glib::ustring &tp)
CmdConnection::error_response
void error_response(gx_system::JsonWriter &jw, int code, const Glib::ustring &message)
Definition: jsonrpc.h:96
CmdConnection::error_response
void error_response(gx_system::JsonWriter &jw, int code, const char *message)
CmdConnection::process
void process(gx_system::JsonStringParser &jp)
gx_system::JsonStringWriter
Definition: gx_json.h:99
GxService::ChangedPlugin::id
std::string id
Definition: jsonrpc.h:123
CmdConnection::jp
gx_system::JsonStringParser jp
Definition: jsonrpc.h:82
CmdConnection::methodnames::m_id
jsonrpc_method m_id
Definition: jsonrpc.h:58
JsonValue::getSubParser
virtual gx_system::JsonSubParser getSubParser() const
gx_system::JsonSubParser
Definition: gx_json.h:194
CmdConnection::f_display
@ f_display
Definition: jsonrpc.h:64
CmdConnection::f_display_state
@ f_display_state
Definition: jsonrpc.h:65
gx_engine::GxEngineState
GxEngineState
Definition: gx_modulesequencer.h:289
JsonValue::getInt
virtual int getInt() const
CmdConnection::midi_config_mode
bool midi_config_mode
Definition: jsonrpc.h:83
CmdConnection::f_presetlist_changed
@ f_presetlist_changed
Definition: jsonrpc.h:67
JsonArray
Definition: jsonrpc.h:46
JsonValue
Definition: jsonrpc.h:34
GxService::ChangedPlugin::ChangedPlugin
ChangedPlugin(const std::string &id_, gx_engine::PluginChange::pc status_)
Definition: jsonrpc.h:125
CmdConnection::flags
std::bitset< END_OF_FLAGS > flags
Definition: jsonrpc.h:84
max
#define max(x, y)
Definition: gx_faust_support.h:7
GxService::on_midi_changed
void on_midi_changed()
GxService::on_param_value_changed
void on_param_value_changed(gx_engine::Parameter *p)
CmdConnection::current_offset
unsigned int current_offset
Definition: jsonrpc.h:81
broadcast_data::sender
CmdConnection * sender
Definition: jsonrpc.h:117
GxService::settings
gx_preset::GxSettings & settings
Definition: jsonrpc.h:128
GxService::ladspaloader_update_plugins
void ladspaloader_update_plugins(gx_system::JsonWriter *jw, CmdConnection *cmd)
CmdConnection::methodnames::name
const char * name
Definition: jsonrpc.h:57
GxService::preset_changed
void preset_changed()
CmdConnection::outgoing
std::list< std::string > outgoing
Definition: jsonrpc.h:80
GxService::GxService
GxService(gx_preset::GxSettings &settings_, gx_jack::GxJack &jack_, TunerSwitcher &tunerswitcher, sigc::slot< void > quit_mainloop_, const Glib::ustring &host, int *port)
gx_jack::GxJack
Definition: gx_jack.h:145
GxService::idle_broadcast_handler
bool idle_broadcast_handler()
GxService::send_rack_changed
void send_rack_changed(bool stereo, CmdConnection *cmd)
CmdConnection::write_error
void write_error(gx_system::JsonWriter &jw, int code, Glib::ustring &message)
Definition: jsonrpc.h:94
GxService::on_engine_state_change
void on_engine_state_change(gx_engine::GxEngineState state)
GxService::on_incoming
virtual bool on_incoming(const Glib::RefPtr< Gio::SocketConnection > &connection, const Glib::RefPtr< Glib::Object > &source_object)
CmdConnection::f_freq_changed
@ f_freq_changed
Definition: jsonrpc.h:63
JsonValue::getString
virtual const Glib::ustring & getString() const
CmdConnection::find_token
bool find_token(const Glib::ustring &token, msg_type *start, msg_type *end)
CmdConnection::exec
void exec(Glib::ustring cmd)
GxService::quit_mainloop
sigc::slot< void > quit_mainloop
Definition: jsonrpc.h:131
CmdConnection::f_parameter_change_notify
@ f_parameter_change_notify
Definition: jsonrpc.h:71
gx_system::JsonWriter
Definition: gx_json.h:55
CmdConnection::~CmdConnection
~CmdConnection()
JsonValue::getFloat
virtual double getFloat() const
GxService::connect_value_changed_signal
void connect_value_changed_signal(gx_engine::Parameter *p)
GxService::jwc
gx_system::JsonStringWriter * jwc
Definition: jsonrpc.h:137
gx_system::JsonParser
Definition: gx_json.h:112
GxService::on_rack_unit_changed
void on_rack_unit_changed(bool stereo)
CmdConnection::on_data_in
bool on_data_in(Glib::IOCondition cond)
CmdConnection::send_notify_begin
void send_notify_begin(gx_system::JsonStringWriter &jw, const char *method)
Definition: jsonrpc.h:97
CmdConnection::is_activated
bool is_activated(msg_type n)
Definition: jsonrpc.h:109
JsonArray::append
void append(gx_system::JsonParser &jp)
JsonValue::~JsonValue
virtual ~JsonValue()
Definition: jsonrpc.h:37
GxService::add_changed_plugin
static void add_changed_plugin(gx_engine::Plugin *pl, gx_engine::PluginChange::pc v, std::vector< ChangedPlugin > &vec)
CmdConnection::methodnames
Definition: jsonrpc.h:56
GxService::ChangedPlugin
Definition: jsonrpc.h:122
GxService::preg_map
std::map< std::string, bool > * preg_map
Definition: jsonrpc.h:138
GxService::ladspaloader_write_changes
void ladspaloader_write_changes(gx_system::JsonWriter &jw, std::vector< ChangedPlugin > &changed_plugins)
gx_preset::GxSettings
Definition: gx_preset.h:140
GxService::on_selection_done
void on_selection_done(bool v)
GxService::last_change
time_t last_change
Definition: jsonrpc.h:133
GxService::ChangedPlugin::status
gx_engine::PluginChange::pc status
Definition: jsonrpc.h:124
CmdConnection::maxlevel
std::map< string, float > maxlevel
Definition: jsonrpc.h:85
GxService::on_param_insert_remove
void on_param_insert_remove(gx_engine::Parameter *p, bool insert)
GxService::set_display_state
void set_display_state(TunerSwitcher::SwitcherState newstate)
GxService::tuner_switcher
TunerSwitcher & tuner_switcher
Definition: jsonrpc.h:130
CmdConnection::connection
Glib::RefPtr< Gio::SocketConnection > connection
Definition: jsonrpc.h:79
JsonValue::JsonValue
JsonValue()
Definition: jsonrpc.h:36
CmdConnection::f_midi_changed
@ f_midi_changed
Definition: jsonrpc.h:69
GxService
Definition: jsonrpc.h:120
CmdConnection::f_units_changed
@ f_units_changed
Definition: jsonrpc.h:74
JsonArray::JsonArray
JsonArray()
Definition: jsonrpc.h:48
CmdConnection::send_notify_end
void send_notify_end(gx_system::JsonStringWriter &jw, bool send_out=true)
CmdConnection::notify
void notify(gx_system::JsonStringWriter &jw, const methodnames *mn, JsonArray &params)
GxService::broadcast
void broadcast(gx_system::JsonStringWriter &jw, CmdConnection::msg_type n, CmdConnection *sender=0)
CmdConnection::f_misc_msg
@ f_misc_msg
Definition: jsonrpc.h:73
broadcast_data
Definition: jsonrpc.h:114
CmdConnection::update_maxlevel
void update_maxlevel(const std::string &id, float v)
Definition: jsonrpc.h:110
broadcast_data::n
CmdConnection::msg_type n
Definition: jsonrpc.h:116
CmdConnection::f_selection_done
@ f_selection_done
Definition: jsonrpc.h:66
engine_state_to_string
const char * engine_state_to_string(gx_engine::GxEngineState s)
engine.h
TunerSwitcher::SwitcherState
SwitcherState
Definition: tunerswitcher.h:33
gx_system::JsonStringWriter::send_notify_begin
void send_notify_begin(const char *method)
TunerSwitcher
Definition: tunerswitcher.h:29
JsonArray::operator[]
JsonValue * operator[](unsigned int i)
gx_engine::Plugin
Definition: gx_pluginloader.h:45
CmdConnection::END_OF_FLAGS
@ END_OF_FLAGS
Definition: jsonrpc.h:75
CmdConnection::listen
void listen(const Glib::ustring &tp)
GxService::oldest_unsaved
time_t oldest_unsaved
Definition: jsonrpc.h:132
JsonArray::~JsonArray
~JsonArray()
GxService::maxlevel
std::map< std::string, float > maxlevel
Definition: jsonrpc.h:139
GxService::serialize_parameter_change
void serialize_parameter_change(gx_system::JsonWriter &jw)
GxService::remove_connection
void remove_connection(CmdConnection *p)
GxService::broadcast_listeners
bool broadcast_listeners(CmdConnection::msg_type n, CmdConnection *sender=0)
CmdConnection::f_plugins_changed
@ f_plugins_changed
Definition: jsonrpc.h:72
CmdConnection::f_log_message
@ f_log_message
Definition: jsonrpc.h:68
gx_engine::Parameter
Definition: gx_parameter.h:106
GxService::broadcast_list
std::queue< broadcast_data > broadcast_list
Definition: jsonrpc.h:136
CmdConnection::send
void send(gx_system::JsonStringWriter &jw)
GxLogger::MsgType
MsgType
Definition: gx_logging.h:39
GxService::save_state
void save_state()
GxService::~GxService
~GxService()
GxService::display
void display(const Glib::ustring &bank, const Glib::ustring &preset)
GxService::on_midi_value_changed
void on_midi_value_changed(int ctl, int value)
GxService::connection_list
std::list< CmdConnection * > connection_list
Definition: jsonrpc.h:135
GxService::on_presetlist_changed
void on_presetlist_changed()
CmdConnection::on_data_out
bool on_data_out(Glib::IOCondition cond)