Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | #ifdef HAVE_CONFIG_H |
| 2 | #include <config.h> |
| 3 | #endif |
| 4 | |
| 5 | #include "echo-server.h" |
| 6 | #include <unistd.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <signal.h> |
| 9 | #include <stdio.h> |
| 10 | #include <limits.h> |
| 11 | |
| 12 | static const char *ECHO_SERVER_NAME = "org.freedesktop.DBus.Examples.Echo"; |
| 13 | static const char *ECHO_SERVER_PATH = "/org/freedesktop/DBus/Examples/Echo"; |
| 14 | |
| 15 | EchoServer::EchoServer(DBus::Connection &connection) |
| 16 | : DBus::ObjectAdaptor(connection, ECHO_SERVER_PATH) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | int32_t EchoServer::Random() |
| 21 | { |
| 22 | return rand(); |
| 23 | } |
| 24 | |
| 25 | std::string EchoServer::Hello(const std::string &name) |
| 26 | { |
| 27 | return "Hello " + name + "!"; |
| 28 | } |
| 29 | |
| 30 | DBus::Variant EchoServer::Echo(const DBus::Variant &value) |
| 31 | { |
| 32 | this->Echoed(value); |
| 33 | |
| 34 | return value; |
| 35 | } |
| 36 | |
| 37 | std::vector< uint8_t > EchoServer::Cat(const std::string &file) |
| 38 | { |
| 39 | FILE *handle = fopen(file.c_str(), "rb"); |
| 40 | |
| 41 | if (!handle) throw DBus::Error("org.freedesktop.DBus.EchoDemo.ErrorFileNotFound", "file not found"); |
| 42 | |
| 43 | uint8_t buff[1024]; |
| 44 | |
| 45 | size_t nread = fread(buff, 1, sizeof(buff), handle); |
| 46 | |
| 47 | fclose(handle); |
| 48 | |
| 49 | return std::vector< uint8_t > (buff, buff + nread); |
| 50 | } |
| 51 | |
| 52 | int32_t EchoServer::Sum(const std::vector<int32_t>& ints) |
| 53 | { |
| 54 | int32_t sum = 0; |
| 55 | |
| 56 | for (size_t i = 0; i < ints.size(); ++i) sum += ints[i]; |
| 57 | |
| 58 | return sum; |
| 59 | } |
| 60 | |
| 61 | std::map< std::string, std::string > EchoServer::Info() |
| 62 | { |
| 63 | std::map< std::string, std::string > info; |
| 64 | char hostname[HOST_NAME_MAX]; |
| 65 | |
| 66 | gethostname(hostname, sizeof(hostname)); |
| 67 | info["hostname"] = hostname; |
| 68 | info["username"] = getlogin(); |
| 69 | |
| 70 | return info; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | DBus::BusDispatcher dispatcher; |
| 75 | |
| 76 | void niam(int sig) |
| 77 | { |
| 78 | dispatcher.leave(); |
| 79 | } |
| 80 | |
| 81 | int main() |
| 82 | { |
| 83 | signal(SIGTERM, niam); |
| 84 | signal(SIGINT, niam); |
| 85 | |
| 86 | DBus::default_dispatcher = &dispatcher; |
| 87 | |
| 88 | DBus::Connection conn = DBus::Connection::SessionBus(); |
| 89 | conn.request_name(ECHO_SERVER_NAME); |
| 90 | |
| 91 | EchoServer server(conn); |
| 92 | |
| 93 | dispatcher.enter(); |
| 94 | |
| 95 | return 0; |
| 96 | } |