Music Hub  ..
A session-wide music playback service
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
service_skeleton.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2013-2014 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  */
18 
19 #include "service_skeleton.h"
20 
21 #include "mpris/service.h"
22 #include "player_configuration.h"
23 #include "the_session_bus.h"
24 
25 #include <core/dbus/message.h>
26 #include <core/dbus/object.h>
27 #include <core/dbus/types/object_path.h>
28 
29 #include <map>
30 #include <sstream>
31 
32 namespace dbus = core::dbus;
33 namespace media = core::ubuntu::media;
34 
35 namespace
36 {
37 std::map<dbus::types::ObjectPath, std::shared_ptr<media::Player>> session_store;
38 }
39 
41 {
43  : impl(impl),
44  object(impl->access_service()->add_object_for_path(
45  dbus::traits::Service<media::Service>::object_path()))
46  {
47  object->install_method_handler<mpris::Service::CreateSession>(
48  std::bind(
50  this,
51  std::placeholders::_1));
52  object->install_method_handler<mpris::Service::PauseOtherSessions>(
53  std::bind(
55  this,
56  std::placeholders::_1));
57  }
58 
59  void handle_create_session(const core::dbus::Message::Ptr& msg)
60  {
61  static unsigned int session_counter = 0;
62 
63  std::stringstream ss;
64  ss << "/core/ubuntu/media/Service/sessions/" << session_counter++;
65 
66  dbus::types::ObjectPath op{ss.str()};
67  media::Player::Configuration config{op};
68 
69  try
70  {
71  auto session = impl->create_session(config);
72 
73  bool inserted = false;
74  std::tie(std::ignore, inserted)
75  = session_store.insert(std::make_pair(op, session));
76 
77  if (!inserted)
78  throw std::runtime_error("Problem persisting session in session store.");
79 
80  auto reply = dbus::Message::make_method_return(msg);
81  reply->writer() << op;
82 
83  impl->access_bus()->send(reply);
84  } catch(const std::runtime_error& e)
85  {
86  auto reply = dbus::Message::make_error(
87  msg,
89  e.what());
90  impl->access_bus()->send(reply);
91  }
92  }
93 
94  void handle_pause_other_sessions(const core::dbus::Message::Ptr& msg)
95  {
96  std::cout << __PRETTY_FUNCTION__ << std::endl;
98  msg->reader() >> key;
99  impl->pause_other_sessions(key);
100 
101  auto reply = dbus::Message::make_method_return(msg);
102  impl->access_bus()->send(reply);
103  }
104 
106  dbus::Object::Ptr object;
107 
108 };
109 
111  : dbus::Skeleton<media::Service>(the_session_bus()),
112  d(new Private(this))
113 {
114 }
115 
117 {
118 }
119 
121 {
122  access_bus()->run();
123 }
124 
126 {
127  access_bus()->stop();
128 }
media::ServiceSkeleton * impl
core::dbus::Bus::Ptr the_session_bus()
void handle_create_session(const core::dbus::Message::Ptr &msg)
Private(media::ServiceSkeleton *impl)
static const std::string & name()
Definition: service.h:41
void handle_pause_other_sessions(const core::dbus::Message::Ptr &msg)