- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 Ubuntu 中运行了以下代码。 DBUS 代码在进程中放置在守护程序中时会崩溃,但在没有守护程序肉鸡板代码结构的独立独立 main() 函数中运行良好。注意:我稍后会在解决问题后重构类结构中的代码。
以下代码崩溃。
//file3=giommdaemontest
//g++ -g -std=c++0x $(file3).cc -o $(file3) `pkg-config --libs --cflags giomm-2.4`
/* Copyright (C) 2011 The giomm Development Team
* Copyright (C) 2013 enthusiasticgeek (Demo for StackOverflow)
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* This is a basic server providing a clock like functionality. Clients can
* get the current time, set the alarm and get notified when the alarm time is
* reached. It is basic because there is only one global alarm which any
* client can set. Clients listening for the alarm signal will be notified by
* use of the global alarm signal. The server should be easily modifiable to
* allow per-client alarms, but that is left as an exercise.
*
* Along with the above it provides a method to get its stdout's file
* descriptor to test the Gio::DBus::Message API.
*/
#include <giomm.h>
#include <glibmm.h>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
namespace
{
static Glib::RefPtr<Gio::DBus::NodeInfo> introspection_data;
static Glib::ustring introspection_xml =
"<node>"
" <interface name='enthusiasticgeek.test.DBus'>"
" <method name='GetTime'>"
" <arg type='s' name='iso8601' direction='out'/>"
" </method>"
" <method name='ParamFunction'>"
" <arg type='s' name='param1' direction='in'/>"
" <arg type='s' name='param2' direction='in'/>"
" <arg type='s' name='param3' direction='out'/>"
" <arg type='s' name='param4' direction='out'/>"
" </method>"
// The time of the alarm as an iso8601 string.
// " <property type='s' name='myproperty' access='readwrite'/>"
" <signal name='EventOccurred'>"
" <arg type='s' name='some_string'/>"
" </signal> "
" </interface>"
"</node>";
guint registered_id = 0;
// Stores the current alarm.
static Glib::TimeVal curr_alarm;
Glib::RefPtr<Gio::DBus::Connection> m_connection;
const char *m_path;
const char *m_interface_name;
} // anonymous namespace
void emit_signal(const Glib::ustring & name, const Glib::VariantContainerBase & parameters)
{
GError *error;
error = NULL;
/* g_dbus_connection_emit_signal (connection,
NULL,
"/org/gtk/GDBus/PeerTestObject",
"org.gtk.GDBus.PeerTestInterface",
"PeerSignal",
NULL,
&error); */
//work-around glibmm bug 645072
if( (m_connection) && (m_path) && (m_interface_name)){
std::cout << "info is " << m_connection << " and " << m_path << " and " << m_interface_name << std::endl;
g_dbus_connection_emit_signal(m_connection->gobj(), NULL, m_path, m_interface_name, name.c_str(),
const_cast<GVariant*>(parameters.gobj()), &error);
}
}
void EventOccurred(const std::string & some_string)
{
emit_signal("EventOccurred", Glib::VariantContainerBase::create_tuple(Glib::Variant<Glib::ustring>::create(some_string)));
}
static void on_method_call(const Glib::RefPtr<Gio::DBus::Connection>& conn /* connection */,
const Glib::ustring& sender /* sender */, const Glib::ustring& path/* object_path */,
const Glib::ustring& interface/* interface_name */, const Glib::ustring& method_name,
const Glib::VariantContainerBase& parameters,
const Glib::RefPtr<Gio::DBus::MethodInvocation>& invocation)
{
m_connection = conn;
m_path = path.c_str();
m_interface_name = interface.c_str();
if(method_name == "GetTime")
{
std::cout << "GetTime function" << std::endl;
std::cout << "test this function" << std::endl;
Glib::TimeVal curr_time;
curr_time.assign_current_time();
const Glib::ustring time_str = curr_time.as_iso8601();
const Glib::Variant<Glib::ustring> time_var =
Glib::Variant<Glib::ustring>::create(time_str);
// Create the tuple.
Glib::VariantContainerBase response =
Glib::VariantContainerBase::create_tuple(time_var);
// Return the tuple with the included time.
invocation->return_value(response);
}
else if(method_name == "ParamFunction")
{
// Get the parameter tuple.
Glib::Variant<Glib::ustring> param1;
Glib::Variant<Glib::ustring> param2;
if(parameters.get_n_children() == 2) {
parameters.get_child(param1, 0);
parameters.get_child(param2, 1);
}
// Get the strings.
const Glib::ustring param1_str = param1.get();
std::cout << "received data param[0]" << param1_str << std::endl;
const Glib::ustring param2_str = param2.get();
std::cout << "received data param[1]" << param2_str << std::endl;
std::string string1 = " Ok I received the name.";
std::string string2 = " Ok I received the profession.";
std::vector<Glib::VariantBase> parameters_response;
parameters_response.reserve(2); ;
parameters_response.push_back(Glib::Variant<Glib::ustring>::create(string1));
parameters_response.push_back(Glib::Variant<Glib::ustring>::create(string2));
Glib::VariantContainerBase params_response = Glib::VariantContainerBase::create_tuple(parameters_response);
invocation->return_value(params_response);
/*
if(!curr_alarm.assign_from_iso8601(time_str))
{
// If setting alarm was not successful, return an error.
Gio::DBus::Error error(Gio::DBus::Error::INVALID_ARGS,
"Alarm string is not in ISO8601 format.");
invocation->return_error(error);
}
*/
}
else
{
// Non-existent method on the interface.
Gio::DBus::Error error(Gio::DBus::Error::UNKNOWN_METHOD,
"Method does not exist.");
invocation->return_error(error);
}
}
//This must be a global instance. See the InterfaceVTable documentation.
//TODO: Make that unnecessary.
const Gio::DBus::InterfaceVTable interface_vtable(sigc::ptr_fun(&on_method_call));
void on_bus_acquired(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& /* name */)
{
// Export an object to the bus:
// See https://bugzilla.gnome.org/show_bug.cgi?id=646417 about avoiding
// the repetition of the interface name:
try
{
registered_id = connection->register_object("/enthusiasticgeek/test/DBus",
introspection_data->lookup_interface(),
interface_vtable);
}
catch(const Glib::Error& ex)
{
std::cerr << "Registration of object failed." << std::endl;
}
return;
}
void on_name_acquired(const Glib::RefPtr<Gio::DBus::Connection>& /* connection */, const Glib::ustring& /* name */)
{
//TODO: What is this good for? See https://bugzilla.gnome.org/show_bug.cgi?id=646427
}
void on_name_lost(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& /* name */)
{
connection->unregister_object(registered_id);
}
void on_get_property(Glib::VariantBase& property,
const Glib::RefPtr<Gio::DBus::Connection>& /* connection */,
const Glib::ustring& /* sender */, const Glib::ustring& /* object_path */,
const Glib::ustring& /* interface_name */, const Glib::ustring& property_name)
{
if(property_name == "myproperty")
{
if(curr_alarm.valid())
{
std::cout << "property is called " << std::endl;
//Glib::ustring alarm_str = curr_alarm.as_iso8601();
//Glib::Variant<Glib::ustring> alarm_var =
// Glib::Variant<Glib::ustring>::create(alarm_str);
//property = alarm_var;
}
else
{
throw Gio::Error(Gio::Error::FAILED, "Property has not been set.");
}
}
else
{
throw Gio::DBus::Error(Gio::DBus::Error::FAILED, "Unknown property name.");
}
}
/** TODO: This code does not seem to be used. murrayc.
bool on_set_property(const Glib::RefPtr<Gio::DBus::Connection>& connection,
const Glib::ustring& sender, const Glib::ustring& object_path,
const Glib::ustring& interface_name, const Glib::ustring& property_name,
const Glib::VariantBase& value)
{
if(property_name == "myproperty")
{
}
else
{
}
}
*/
bool TimeoutCheck()
{
std::cout << "Checking for timeout ..." << std::endl;
EventOccurred(" <<< Timeout Event has occurred >>>");
return true;
}
int main(int, char**)
{
/* Our process ID and Session ID */
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0) {
exit(EXIT_SUCCESS);
}
/* Change the file mode mask */
umask(0);
/* Open any logs here */
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Change the current working directory */
if ((chdir("/")) < 0) {
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
/* Daemon-specific initialization goes here */
while(1){
std::locale::global(std::locale(""));
Gio::init();
try
{
introspection_data = Gio::DBus::NodeInfo::create_for_xml(introspection_xml);
}
catch(const Glib::Error& ex)
{
std::cerr << "Unable to create introspection data: " << ex.what() <<
"." << std::endl;
return 1;
}
const guint id = Gio::DBus::own_name(Gio::DBus::BUS_TYPE_SESSION,
"enthusiasticgeek.test.DBus",
sigc::ptr_fun(&on_bus_acquired),
sigc::ptr_fun(&on_name_acquired),
sigc::ptr_fun(&on_name_lost));
//create timeout signal
static const unsigned delayInMillis = 5000;
sigc::slot<bool> tslot = sigc::ptr_fun(&TimeoutCheck);
Glib::signal_timeout().connect(tslot, delayInMillis);
//Keep the service running until the process is killed:
Glib::RefPtr<Glib::MainLoop> loop = Glib::MainLoop::create();
loop->run();
Gio::DBus::unown_name(id);
}
return EXIT_SUCCESS;
}
但是以下工作正常
int main(int, char**)
{
//while(1){
std::locale::global(std::locale(""));
Gio::init();
try
{
introspection_data = Gio::DBus::NodeInfo::create_for_xml(introspection_xml);
}
catch(const Glib::Error& ex)
{
std::cerr << "Unable to create introspection data: " << ex.what() <<
"." << std::endl;
return 1;
}
const guint id = Gio::DBus::own_name(Gio::DBus::BUS_TYPE_SESSION,
"enthusiasticgeek.test.DBus",
sigc::ptr_fun(&on_bus_acquired),
sigc::ptr_fun(&on_name_acquired),
sigc::ptr_fun(&on_name_lost));
//create timeout signal
static const unsigned delayInMillis = 5000;
sigc::slot<bool> tslot = sigc::ptr_fun(&TimeoutCheck);
Glib::signal_timeout().connect(tslot, delayInMillis);
//Keep the service running until the process is killed:
Glib::RefPtr<Glib::MainLoop> loop = Glib::MainLoop::create();
loop->run();
Gio::DBus::unown_name(id);
//}
return EXIT_SUCCESS;
}
最佳答案
好的,这段代码中的一些调整有助于正确运行代码
...
pid_t pid, sid;
static int pipefd[2];
pid = fork();
if (pid < 0) { //error
close(pipefd[0]);
close(pipefd[1]);
exit(EXIT_FAILURE);
}
if (pid > 0) { //parent process
close(pipefd[1]); //close write/ output
printf("%d\n", pid);
char buf[32];
while (read(pipefd[0], buf, sizeof(buf)) > 0) {
printf("%s", buf);
}
printf("\n");
close(pipefd[0]); //close read/ input
exit(EXIT_SUCCESS);
}
// running as the child, continue daemonizing
close(pipefd[0]);
umask(0);
...
关于c++ - DBUS 代码在放置在守护进程中时崩溃,但在没有守护进程代码的独立独立 main() 函数中运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20842331/
我如何使用 CQLINQ 获取当前方法的输入参数集合?有像“参数”或“参数”这样的集合,只有“NbParamenter”不适合我的目的。 最佳答案 事实上,CQLinq 还没有这个功能。但是,在许多情
我想知道是否有人知道我的 makefile 中独立的 @ 符号和“dir”命令在这里(第二行和第三行)的作用: $(BUILD)/%.o: %.cpp @mkdir -p $(dir $@)
我想知道是否有人知道我的 makefile 中独立的 @ 符号和“dir”命令在这里(第二行和第三行)的作用: $(BUILD)/%.o: %.cpp @mkdir -p $(dir $@)
我的机器上有带有 4 个 cpu 的 Ubuntu 14.04(nproc 恢复了 4 个)。我安装并执行 Spark Standalone 后(本地),我可以自己定义不同数量的奴隶。例如我想要有4个
我看到所有这些 iPhone 应用程序都带有内置的独立 webDav 服务器。是否有可以集成到现有应用程序中的独立(如在其自己的 IIS 中)C# webDAV 项目。 最佳答案 至少有两个用于 .N
我如何在独立的 Django 应用程序上进行迁移(即不属于任何项目的应用程序)。 例如在以下之后:https://docs.djangoproject.com/en/1.8/intro/reusabl
我目前正在使用 tortoiseSVN 对本地编程文件进行版本控制。我不运行 SVN 服务器,因为可以直接使用 tortoiseSVN(例如 http://invalidlogic.com/2006/
我有一些 Bootstrap 代码,当用户查看它时,它可以很好地为进度条部分设置动画。 然而它动画 全部 页面中的进度条而不是动画仅限 该查看部分中的进度条。结果,当用户转到进度条的另一部分时,这些已
我认为我们在 iOS 13.2/13.3 中发现了关于在独立模式下运行的 PWA 的回归。 由于在 iOS PWA 上无法访问 getUserMedia() 我们依赖 capture HTML5 输入
我有一个每周从系统运行一次的报告,并将数据导出到 Excel 文档中。我已经设置了将数据导出到 Excel 的工具,以便在格式化方面做得很好,但是一旦数据进入 Excel,我还需要做更多的事情。 是否
//值数组的格式为 { "var1", "val1", "var2", "val2",.. } public static String replaceMethod(String template,
当我在 eclipse 中运行我的项目时,它工作正常,当我将它导出为独立 jar 时,它会滞后。我使用相同的 vmargs,在 Eclipse 中尝试了 3 种不同的导出设置,似乎没有任何帮助 最佳答
我了解到 Java EE 中我非常喜欢的注释基础配置(@Resource)功能。然后我注意到注释实际上是 Java SE 的一部分。 所以我想知道是否可以将它与 Java SE 一起使用。我当然可以在
我无法理解为什么这种关系没有被持久化,并且程序不会正常退出,但在 Eclipse 中继续运行。 下面是我的代码,排除了包名: 主要: import java.io.BufferedInputStrea
我有一个在 Linux + Java 6 上运行的独立 Java 应用程序,它似乎被卡住了(没有生成日志)我如何在不使用任何其他工具(例如 jstack)的情况下获取此线程转储 尝试了以下命令,但它们
我正在非节点环境中构建应用程序,但我想利用 Babel 的 ES6 转译,以便我可以编写更好的代码并且仍然支持 IE11。 所以我继续包含在这里找到的独立文件: https://github.com/
扩展我对 MySQL 的理解。 1) 是否需要 64 位帮助?我是安装还是单独使用? 2) 如果我打算在 MySQL Community Service 中使用 64 位,它会影响仅提供 32 位的
我有一个独立的 Java 应用程序,我必须为其集成一个规则引擎。我应该使用属性文件或 XML 文件定义规则。我需要规则引擎来读取属性或 XML 文件中定义的这些规则,并相应地在应用程序中实现代码。 任
我是wiremock新手,我正在尝试使用它来记录我负责集成测试的java应用程序的请求和响应。 我知道我的命令将类似于: java -jar wiremock-1.57-standalone.jar
我到处寻找我的问题的解决方案,但我的问题有点具体...我需要有关如何创建独立 radioGroup 列表的建议,例如图示: o item1 • item1' • item2 或 item2' o it
我是一名优秀的程序员,十分优秀!