pianod2
multisource multiuser scriptable networked music player
servicemanager.h
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include <config.h>
13 
14 #include <string>
15 #include <unordered_map>
16 
17 #include "fundamentals.h"
18 #include "interpreter.h"
19 #include "connection.h"
20 #include "response.h"
21 
22 namespace Media {
23  class Source;
24 }
25 
26 
29 public PianodInterpreter,
30 private std::unordered_map<std::string, PianodService *> {
31 public:
32 // using std::unordered_map<std::string, PianodService *>::size;
33  using std::unordered_map<std::string, PianodService *>::begin;
34  using std::unordered_map<std::string, PianodService *>::end;
35 
37  virtual const Parsnip::Parser::Definitions &getParserDefinitions () override;
39 
40  bool broadcast_user_actions = false;
41 
42 private:
46  bool shutdown_pending = false;
47  time_t startup_time = time (nullptr);
48 
49  virtual bool authorizedCommand (Parsnip::Parser::CommandId command, PianodConnection &conn) override;
50  virtual ResponseCollector handleCommand (Parsnip::Parser::CommandId command, const Parsnip::Data &options, PianodConnection &conn) override;
51 
52  void sendSourceReadyEvents (const Media::Source * const source,
53  RESPONSE_CODE result);
54  void sourceReady (const Media::Source * const src);
55  void sourceOffline (const Media::Source * const src);
56  void sourceRemoved (const Media::Source * const src);
57 
58 public:
59  ServiceManager ();
60  virtual ~ServiceManager ();
61  float periodic(void);
62 
63  void shutdown (bool immediate);
64  bool flush (void);
65 
66  // Events
67  void event (WaitEvent::Type type, const void *detail = nullptr, RESPONSE_CODE message = S_OK);
69  inline void event (WaitEvent::Type type, RESPONSE_CODE message = S_OK) {
70  event (type, nullptr, message);
71  };
72 
73  // Broadcast things
74 
77  inline void broadcastUserActions (bool v) {
79  }
82  inline bool broadcastingActions (void) {
84  }
85 
86  void broadcast (const ResponseGroup &messages);
87  void broadcast (const ResponseGroup &messages, User *target);
89  void broadcastEffectivePrivileges (User *target);
90 
91  // Room management
92  PianodService *createRoom (const std::string &name, const AudioSettings &audio,
93  FB_SERVICE_OPTIONS &options);
94  void removeRoom (PianodService *service);
95 
96  // User management
97  bool userIsOnline (const User *user);
98 };
99 
100 
Base class that wraps any audio source, such as Pandora, Spotify, or local music.
Definition: mediaunit.h:68
Generic data type.
Definition: parsnip.h:81
std::vector< Definition > Definitions
Definition: parsnip_command.h:117
int CommandId
Definition: parsnip_command.h:109
Connection to a pianod client, along with context and state of that connection.
Definition: connection.h:54
Interpreter class based on Parsnip template, customized base for pianod interpreters with the additio...
Definition: interpreter.h:101
Definition: interpreter.h:57
std::map< std::string, CommandId > CommandIds
Definition: interpreter.h:59
Pianod service, a customized FootballService for Pianod connections.
Definition: connection.h:115
A response collector/aggregator.
Definition: response.h:197
Container for multiple Responses.
Definition: response.h:155
Contains and manages all the services/rooms.
Definition: servicemanager.h:30
void removeRoom(PianodService *service)
Remove a room.
Definition: servicemanager.cpp:318
float periodic(void)
Perform periodic duties, and determine when next ones are due.
Definition: servicemanager.cpp:149
void sourceReady(const Media::Source *const src)
Prepare for a new source by alerting auto engines of it.
Definition: servicemanager.cpp:263
void broadcast(const ResponseGroup &messages)
Send a message to every user on all services/rooms.
Definition: servicemanager.cpp:200
PianodService * createRoom(const std::string &name, const AudioSettings &audio, FB_SERVICE_OPTIONS &options)
Add a room name/service pair to our list.
Definition: servicemanager.cpp:291
void broadcastEffectivePrivileges()
Broadcast privileges to all users.
Definition: servicemanager.cpp:225
void sendSourceReadyEvents(const Media::Source *const source, RESPONSE_CODE result)
Send events to those waiting on source ready, either a specific source, any source,...
Definition: servicemanager.cpp:249
void broadcastUserActions(bool v)
Enable or disable announcing user actions to all connections.
Definition: servicemanager.h:77
PianodService * master_service
The initial room/service.
Definition: servicemanager.h:45
void sourceOffline(const Media::Source *const src)
Handle a source going offline by removing it from use.
Definition: servicemanager.cpp:270
void sourceRemoved(const Media::Source *const src)
Handle a source being deleted.
Definition: servicemanager.cpp:282
virtual ResponseCollector handleCommand(Parsnip::Parser::CommandId command, const Parsnip::Data &options, PianodConnection &conn) override
Command handler.
Definition: servicemanager.cpp:418
void event(WaitEvent::Type type, const void *detail=nullptr, RESPONSE_CODE message=S_OK)
Distribute an event to connections waiting on it.
Definition: servicemanager.cpp:189
virtual const Parsnip::Parser::Definitions & getParserDefinitions() override
Retrieve the parser definitions handled by the interpreter.
Definition: servicemanager.cpp:394
bool broadcastingActions(void)
Check whether user actions are announced to all connections.
Definition: servicemanager.h:82
void event(WaitEvent::Type type, RESPONSE_CODE message=S_OK)
Definition: servicemanager.h:69
const Parsnip::ParserRef master_parser
A single parser shared by all rooms.
Definition: servicemanager.h:43
virtual ~ServiceManager()
Definition: servicemanager.cpp:696
bool broadcast_user_actions
Definition: servicemanager.h:40
ServiceManager()
Definition: servicemanager.cpp:679
bool shutdown_pending
Set when shutdown initiated.
Definition: servicemanager.h:46
virtual bool authorizedCommand(Parsnip::Parser::CommandId command, PianodConnection &conn) override
Check whether a command is authorized.
Definition: servicemanager.cpp:398
static const Parsnip::Parser::Definitions & parser_definitions()
Definition: servicemanager.cpp:342
bool flush(void)
Persist data before shutdown.
Definition: servicemanager.cpp:139
static const PianodSchema::CommandIds & json_request_names(PianodSchema &schema)
Retrieve names for our JSON requests, and make schema revisions.
Definition: servicemanager.cpp:371
time_t startup_time
Time the service manager was instantiated.
Definition: servicemanager.h:47
void shutdown(bool immediate)
Request all services (rooms) close so pianod can shutdown.
Definition: servicemanager.cpp:128
bool userIsOnline(const User *user)
Determine if a user is authenticated an connected in any room.
Definition: servicemanager.cpp:334
const PianodSchemaRef master_schema
A single schema shared by all rooms.
Definition: servicemanager.h:44
Data about each user.
Definition: user.h:53
Type
Kinds of events that can be waited for.
Definition: connection.h:31
Pianod specializations of Football connections & services.
Essential data structures and support.
enum server_status_t RESPONSE_CODE
@ S_OK
Definition: fundamentals.h:164
pianod command processing.
std::shared_ptr< PianodSchema > PianodSchemaRef
Definition: interpreter.h:75
const char * Source
Definition: pandoratypes.h:113
Media source, source parameters and player interfaces.
Definition: connection.h:22
std::shared_ptr< Parser > ParserRef
Definition: parsnip_command.h:138
Send messages of various kinds to clients.
ServiceManager * service_manager
Definition: servicemanager.cpp:700
Audio output device & driver parameters.
Definition: fundamentals.h:52
Service options are passed to a new service, defining its behavior.
Definition: fb_public.h:75