Author: lslezak
Date: Wed Jul 9 17:36:46 2008
New Revision: 48941
URL: http://svn.opensuse.org/viewcvs/yast?rev=48941&view=rev
Log:
- added native SCR DBus server (SCR_dbus_server)
Added:
branches/tmp/lslezak/workshop/core/scr/src/DBusConn.cc
branches/tmp/lslezak/workshop/core/scr/src/DBusConn.h
branches/tmp/lslezak/workshop/core/scr/src/DBusMsg.cc
branches/tmp/lslezak/workshop/core/scr/src/DBusMsg.h
branches/tmp/lslezak/workshop/core/scr/src/DBusServer.cc
branches/tmp/lslezak/workshop/core/scr/src/DBusServer.h
branches/tmp/lslezak/workshop/core/scr/src/SCR_dbus_server.cc
Modified:
branches/tmp/lslezak/workshop/core/scr/src/Makefile.am
Added: branches/tmp/lslezak/workshop/core/scr/src/DBusConn.cc
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/lslezak/workshop/core/scr/src/DBusConn.cc?rev=48941&view=auto
==============================================================================
--- branches/tmp/lslezak/workshop/core/scr/src/DBusConn.cc (added)
+++ branches/tmp/lslezak/workshop/core/scr/src/DBusConn.cc Wed Jul 9 17:36:46 2008
@@ -0,0 +1,106 @@
+
+/*
+ DBusConn.cc
+*/
+
+#include "DBusConn.h"
+
+#include
+
+DBusConn::DBusConn() : connection(NULL)
+{
+ dbus_error_init(&dbus_error);
+}
+
+DBusConn::~DBusConn()
+{
+ if (connection)
+ {
+ dbus_connection_unref(connection);
+ }
+
+ dbus_error_free(&dbus_error);
+
+ y2milestone("DBusConn finished");
+}
+
+bool DBusConn::connect(DBusBusType type, const std::string& service)
+{
+ if (connection != NULL)
+ {
+ // already connected
+ return true;
+ }
+
+ y2milestone("Connecting to DBus...");
+
+ // connect to the system bus
+ connection = dbus_bus_get(type, &dbus_error);
+
+ if (dbus_error_is_set(&dbus_error))
+ {
+ y2error("Cannot connect to the system bus: %s", dbus_error.message);
+ return false;
+ }
+
+ if (connection == NULL)
+ {
+ y2error("DBus connection is NULL");
+ return false;
+ }
+
+ y2milestone("Registering service %s", service.c_str());
+
+ // set the service name, replace the existing owner if it's allowed
+ int result = dbus_bus_request_name(connection, service.c_str(), DBUS_NAME_FLAG_REPLACE_EXISTING, &dbus_error);
+ if (dbus_error_is_set(&dbus_error))
+ {
+ y2error("Cannot register service %s: %s", service.c_str(), dbus_error.message);
+ return false;
+ }
+
+ // primary owner?
+ if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
+ {
+ y2error("Service %s is already registered", service.c_str());
+ return false;
+ }
+
+ y2milestone("Service %s successfuly registered", service.c_str());
+
+ return true;
+}
+
+void DBusConn::setTimeout(int miliseconds)
+{
+ if (connection != NULL)
+ {
+ dbus_connection_read_write(connection, miliseconds);
+ }
+}
+
+DBusMsg DBusConn::receive()
+{
+ DBusMsg msg;
+
+ DBusMessage *m = dbus_connection_pop_message(connection);
+
+ if (m != NULL)
+ {
+ msg.setMessage(m);
+ }
+
+ return msg;
+}
+
+bool DBusConn::send(const DBusMsg &msg)
+{
+ // dbus_uint32_t serial = 0;
+ // NULL = we are not interested in the serial number
+ return dbus_connection_send(connection, msg.getMessage(), NULL);
+}
+
+void DBusConn::flush()
+{
+ dbus_connection_flush(connection);
+}
Added: branches/tmp/lslezak/workshop/core/scr/src/DBusConn.h
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/lslezak/workshop/core/scr/src/DBusConn.h?rev=48941&view=auto
==============================================================================
--- branches/tmp/lslezak/workshop/core/scr/src/DBusConn.h (added)
+++ branches/tmp/lslezak/workshop/core/scr/src/DBusConn.h Wed Jul 9 17:36:46 2008
@@ -0,0 +1,39 @@
+
+/*
+ DBusConn
+*/
+
+#ifndef DBusConn_h
+#define DBusConn_h
+
+#include
+#include <string>
+
+#include "DBusMsg.h";
+
+class DBusConn
+{
+ private:
+
+ DBusConnection *connection;
+ DBusError dbus_error;
+
+ // disable copying
+ DBusConn(const DBusConn &);
+ DBusConn& operator=(const DBusConn&);
+
+ public:
+
+ DBusConn();
+ ~DBusConn();
+
+ bool connect(DBusBusType type, const std::string& service);
+ void setTimeout(int miliseconds);
+ bool send(const DBusMsg &msg);
+ void flush();
+ DBusMsg receive();
+};
+
+#endif
+
+
Added: branches/tmp/lslezak/workshop/core/scr/src/DBusMsg.cc
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/lslezak/workshop/core/scr/src/DBusMsg.cc?rev=48941&view=auto
==============================================================================
--- branches/tmp/lslezak/workshop/core/scr/src/DBusMsg.cc (added)
+++ branches/tmp/lslezak/workshop/core/scr/src/DBusMsg.cc Wed Jul 9 17:36:46 2008
@@ -0,0 +1,272 @@
+
+/*
+
+ DBusMsg
+
+*/
+
+#include "DBusMsg.h"
+#include "YCP.h"
+#include
+
+DBusMsg::DBusMsg() : msg(NULL)
+{
+}
+
+void DBusMsg::createCall(const std::string &service, const std::string &object,
+ const std::string &interface, const std::string &method)
+{
+ release();
+ msg = dbus_message_new_method_call(service.c_str(), object.c_str(),
+ interface.c_str(), method.c_str());
+}
+
+void DBusMsg::createReply(const DBusMsg &m)
+{
+ release();
+ msg = dbus_message_new_method_return(m.getMessage());
+}
+
+void DBusMsg::release()
+{
+ if (msg != NULL)
+ {
+ // release the message
+ dbus_message_unref(msg);
+ }
+}
+
+DBusMsg::~DBusMsg()
+{
+ release();
+}
+
+bool DBusMsg::addValue(int type, void* data)
+{
+ if (msg != NULL)
+ {
+ DBusMessageIter it;
+
+ dbus_message_iter_init_append(msg, &it);
+ if (dbus_message_iter_append_basic(&it, type, data))
+ {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool DBusMsg::addString(const std::string &val)
+{
+ const char *ptr = val.c_str();
+ return addValue(DBUS_TYPE_STRING, &ptr);
+}
+
+bool DBusMsg::addInt64(dbus_int64_t val)
+{
+ return addValue(DBUS_TYPE_INT64, &val);
+}
+
+bool DBusMsg::addInt32(dbus_int32_t val)
+{
+ return addValue(DBUS_TYPE_INT32, &val);
+}
+
+bool DBusMsg::addBoolean(bool val)
+{
+ return addValue(DBUS_TYPE_INT64, &val);
+}
+
+bool DBusMsg::addDouble(double val)
+{
+ return addValue(DBUS_TYPE_DOUBLE, &val);
+}
+
+DBusMessage* DBusMsg::getMessage() const
+{
+ return msg;
+}
+
+void DBusMsg::setMessage(DBusMessage *message)
+{
+ msg = message;
+}
+
+bool DBusMsg::isMethodCall(const std::string &interface, const std::string &method) const
+{
+ if (msg != NULL)
+ {
+ return dbus_message_is_method_call(msg, interface.c_str(), method.c_str());
+ }
+
+ return false;
+}
+
+bool DBusMsg::empty() const
+{
+ return msg == NULL;
+}
+
+std::string DBusMsg::interface() const
+{
+ if (msg == NULL)
+ {
+ return std::string();
+ }
+
+ return std::string(dbus_message_get_interface(msg));
+}
+
+std::string DBusMsg::method() const
+{
+ if (msg == NULL)
+ {
+ return std::string();
+ }
+
+ return std::string(dbus_message_get_member(msg));
+}
+
+bool DBusMsg::addYCPValue(const YCPValue &val)
+{
+ if (val.isNull())
+ {
+ y2warning("Ignoring NULL YCPValue");
+ }
+ else if (val->isInteger())
+ {
+ addInt64(val->asInteger()->value());
+ }
+ else if (val->isFloat())
+ {
+ addDouble(val->asFloat()->value());
+ }
+ else if (val->isString())
+ {
+ addString(val->asString()->value());
+ }
+ else if (val->isBoolean())
+ {
+ addBoolean(val->asBoolean()->value());
+ }
+ // add term, path and symbol as string
+ else if (val->isTerm() || val->isPath() || val->isSymbol())
+ {
+ // add path as a string
+ addString(val->toString());
+ }
+ else if (val->isList())
+ {
+ YCPList lst = val->asList();
+ int sz = lst->size();
+ int i = 0;
+
+ DBusMessageIter it;
+ dbus_message_iter_init_append(msg, &it);
+
+ DBusMessageIter sub;
+
+ // open container
+ dbus_message_iter_open_container(&it, &sub);
+ while(i < sz)
+ {
+ // add an item
+ // TODO FIXME: addYCPValue(lst->value(i), &sub);
+ i++;
+ }
+
+ // close container
+ dbus_message_iter_close_container(&it, &sub);
+ }
+ else
+ {
+ y2error("Unsupported type: %s", val->toString().c_str());
+ return false;
+ }
+
+ return true;
+}
+
+int DBusMsg::arguments() const
+{
+ int ret = 0;
+ DBusMessageIter it;
+
+ if (msg == NULL)
+ {
+ return ret;
+ }
+
+ // read the parameters
+ if (!dbus_message_iter_init(msg, &it))
+ {
+ return ret;
+ }
+
+ // there is at least one argument
+ ret = 1;
+
+ // iterate over the other arguments
+ while (dbus_message_iter_next(&it))
+ {
+ ret++;
+ }
+
+ y2milestone("Message has %d arguments", ret);
+
+ return ret;
+}
+
+YCPValue DBusMsg::getYCPValue(int index)
+{
+ YCPValue ret;
+
+ if (msg != NULL)
+ {
+ DBusMessageIter it;
+
+ // read the parameters
+ if (!dbus_message_iter_init(msg, &it))
+ {
+ return ret;
+ }
+
+ // there is at least one argument
+ int i = 0;
+
+ // iterate over the other arguments
+ while (i < index && dbus_message_iter_next(&it))
+ {
+ i++;
+ }
+
+ if (i == index)
+ {
+ int type = dbus_message_iter_get_arg_type(&it);
+
+ if (type == DBUS_TYPE_BOOLEAN)
+ {
+ bool b;
+ dbus_message_iter_get_basic(&it, &b);
+ ret = YCPBoolean(b);
+ }
+ else if (type == DBUS_TYPE_STRING)
+ {
+ const char *s;
+ dbus_message_iter_get_basic(&it, &s);
+ ret = YCPString(s);
+ }
+ else
+ {
+ y2error("Unsupported Dbus type: %d (%c)", type, (char)type);
+ }
+ }
+ else
+ {
+ y2error("No argument at index %d", i);
+ }
+ }
+
+ return ret;
+}
Added: branches/tmp/lslezak/workshop/core/scr/src/DBusMsg.h
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/lslezak/workshop/core/scr/src/DBusMsg.h?rev=48941&view=auto
==============================================================================
--- branches/tmp/lslezak/workshop/core/scr/src/DBusMsg.h (added)
+++ branches/tmp/lslezak/workshop/core/scr/src/DBusMsg.h Wed Jul 9 17:36:46 2008
@@ -0,0 +1,57 @@
+
+/*
+ DBusMsg
+*/
+
+#ifndef DBUSMSG_H
+#define DBUSMSG_H
+
+#include <string>
+#include
+
+class YCPValue;
+
+// DBusMessage wrapper
+class DBusMsg
+{
+
+ public:
+
+ DBusMsg();
+ ~DBusMsg();
+
+ // create a method call
+ void createCall(const std::string &service, const std::string &object,
+ const std::string &interface, const std::string &method);
+
+ // create a reply
+ void createReply(const DBusMsg &m);
+
+ bool addString(const std::string &val);
+ bool addInt64(dbus_int64_t val);
+ bool addInt32(dbus_int32_t val);
+ bool addBoolean(bool val);
+ bool addDouble(double val);
+
+ bool addYCPValue(const YCPValue &val);
+ YCPValue getYCPValue(int index);
+
+ bool isMethodCall(const std::string &interface, const std::string &method) const;
+ int arguments() const;
+ bool empty() const;
+
+ DBusMessage *getMessage() const;
+ void setMessage(DBusMessage *message);
+
+ std::string interface() const;
+ std::string method() const;
+
+ private:
+
+ bool addValue(int type, void* data);
+ void release();
+ DBusMessage *msg;
+};
+
+#endif
+
Added: branches/tmp/lslezak/workshop/core/scr/src/DBusServer.cc
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/lslezak/workshop/core/scr/src/DBusServer.cc?rev=48941&view=auto
==============================================================================
--- branches/tmp/lslezak/workshop/core/scr/src/DBusServer.cc (added)
+++ branches/tmp/lslezak/workshop/core/scr/src/DBusServer.cc Wed Jul 9 17:36:46 2008
@@ -0,0 +1,135 @@
+
+/*
+
+ DBus server
+
+ */
+
+#include "DBusServer.h"
+#include "DBusMsg.h"
+
+#include
+
+
+DBusServer::DBusServer()
+{
+ sa = new ScriptingAgent();
+}
+
+DBusServer::~DBusServer()
+{
+ if (sa != NULL)
+ {
+ delete sa;
+ }
+}
+
+
+bool DBusServer::connect()
+{
+ // connect to DBus, request a service name
+ return connection.connect(DBUS_BUS_SESSION, "org.opensuse.yast.SCR");
+}
+
+
+/**
+ * Server that exposes a method call and waits for it to be called
+ */
+void DBusServer::run()
+{
+ y2milestone("Listening for incoming DBus messages...");
+
+ // mainloop
+ while (true)
+ {
+ // set 100 milisecond timeout
+ connection.setTimeout(100);
+ // try reading a message from DBus
+ DBusMsg request(connection.receive());
+
+ // check if a message was received
+ if (request.empty())
+ {
+ sleep(1);
+ continue;
+ }
+
+ // check this is a method call for the right interface & method
+ if (request.isMethodCall("org.opensuse.yast.SCR", "Read"))
+ {
+ request.arguments();
+
+ YCPValue arg0 = request.getYCPValue(0);
+
+ if (!arg0.isNull() && arg0->isString())
+ {
+ // DBus doesn't support YCPPath type, string is used as a workaround
+ std::string path_str(arg0->asString()->value());
+ YCPPath pth(path_str);
+ y2milestone("Reading path %s", path_str.c_str());
+
+ YCPValue ret = sa->Read(pth);
+
+ DBusMsg reply;
+ reply.createReply(request);
+
+ reply.addYCPValue(ret);
+
+ // send the result
+ if (!connection.send(reply))
+ {
+ y2error("Cannot send the result");
+ }
+ else
+ {
+ y2milestone("Sending result %s", ret->toString().c_str());
+ }
+
+ connection.flush();
+ }
+ }
+ else if (request.isMethodCall("org.opensuse.yast.SCR", "Write"))
+ {
+ }
+ else if (request.isMethodCall("org.opensuse.yast.SCR", "Execute"))
+ {
+ }
+ else if (request.isMethodCall("org.freedesktop.DBus.Introspectable", "Introspect"))
+ {
+ // handle Introspection request from "org.freedesktop.DBus.Introspectable" "Introspect"
+ // define all exported methods here
+ const char *introspect = "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\
+ \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\
+ \
+ \
+ \
+ \
+ \
+ </interface>\
+ </node>\
+";
+ // create a reply to the request
+ DBusMsg reply;
+ reply.createReply(request);
+
+ reply.addString(introspect);
+
+ if (!connection.send(reply))
+ {
+ y2error("Cannot send the introspect reply");
+ }
+ else
+ {
+ y2milestone("Introspection data sent");
+ }
+
+ connection.flush();
+ }
+ else
+ {
+ y2warning("Ignoring unknown interface or method call: interface: %s, method: %s",
+ request.interface().c_str(), request.method().c_str());
+ }
+ }
+}
+
Added: branches/tmp/lslezak/workshop/core/scr/src/DBusServer.h
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/lslezak/workshop/core/scr/src/DBusServer.h?rev=48941&view=auto
==============================================================================
--- branches/tmp/lslezak/workshop/core/scr/src/DBusServer.h (added)
+++ branches/tmp/lslezak/workshop/core/scr/src/DBusServer.h Wed Jul 9 17:36:46 2008
@@ -0,0 +1,43 @@
+
+/*
+ DBusServer.h
+*/
+
+#ifndef DBUSSERVER_H
+#define DBUSSERVER_H
+
+#include
+
+#include "DBusConn.h"
+
+#include "ScriptingAgent.h"
+
+class DBusServer
+{
+ public:
+
+ DBusServer();
+ ~DBusServer();
+
+ bool connect();
+ void run();
+
+ private:
+
+ const char *_service_name;
+ const char *_interface_name;
+
+ DBusConn connection;
+// DBusError dbus_error;
+
+ // SCR access
+ ScriptingAgent *sa;
+
+ // disable copying
+ DBusServer(const DBusServer&);
+ DBusServer& operator=(const DBusServer&);
+};
+
+
+#endif
+
Modified: branches/tmp/lslezak/workshop/core/scr/src/Makefile.am
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/lslezak/workshop/core/scr/src/Makefile.am?rev=48941&r1=48940&r2=48941&view=diff
==============================================================================
--- branches/tmp/lslezak/workshop/core/scr/src/Makefile.am (original)
+++ branches/tmp/lslezak/workshop/core/scr/src/Makefile.am Wed Jul 9 17:36:46 2008
@@ -40,3 +40,14 @@
libpy2scr_la_LDFLAGS = -version-info 2:0
INCLUDES = ${AGENT_INCLUDES}
+
+
+bin_PROGRAMS = SCR_dbus_server
+
+SCR_dbus_server_SOURCES = SCR_dbus_server.cc DBusServer.cc DBusServer.h \
+ DBusMsg.cc DBusMsg.h DBusConn.cc DBusConn.h
+
+SCR_dbus_server_CPPFLAGS = $(AM_CPPFLAGS) $(DBUS_CFLAGS)
+
+SCR_dbus_server_LDADD = $(DBUS_LIBS) ${AGENT_LIBADD} libpy2scr.la
+
Added: branches/tmp/lslezak/workshop/core/scr/src/SCR_dbus_server.cc
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/lslezak/workshop/core/scr/src/SCR_dbus_server.cc?rev=48941&view=auto
==============================================================================
--- branches/tmp/lslezak/workshop/core/scr/src/SCR_dbus_server.cc (added)
+++ branches/tmp/lslezak/workshop/core/scr/src/SCR_dbus_server.cc Wed Jul 9 17:36:46 2008
@@ -0,0 +1,23 @@
+
+/*
+
+*/
+
+#include "DBusServer.h"
+
+int main(int argc, char **argv)
+{
+
+ DBusServer server;
+
+ bool connected = server.connect();
+
+ if (connected)
+ {
+ server.run();
+ return 0;
+ }
+
+ // indicate an error
+ return 1;
+}
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org