1. 程式人生 > >Dbus-glib使用方法說明

Dbus-glib使用方法說明

server.c


#include <gio/gio.h>
#include <stdlib.h>

#ifdef G_OS_UNIX
//#include <gio/gunixfdlist.h>
/* For STDOUT_FILENO */
#include <unistd.h>
#endif

static GDBusNodeInfo *introspection_data = NULL;

/* Introspection data for the service we are exporting */
static const gchar introspection_xml[] =
	"<node>"
	"<interface name='org.myapp.JelariInterface'>"
	"<method name='HelloWorld'>"
	"<arg type='s' name='greeting' direction='in'/>" 
        "<arg type='s' name='response' direction='out'/>"
        "</method>"
        "</interface>" 
         "</node>";

static void
handle_method_call (GDBusConnection * connection,
	const gchar * sender, const gchar * object_path, const gchar * interface_name, const gchar * method_name, GVariant * parameters, GDBusMethodInvocation * invocation, gpointer user_data)
{
	g_printf ("Inside Handle Method Call \n");

	if (g_strcmp0 (method_name, "HelloWorld") == 0)
	{
		const gchar *greeting;

		gchar *response;
		g_variant_get (parameters, "(&s)", &greeting);
		response = g_strdup_printf ("You typed '%s', !!! ", greeting);

		g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", response));
//g_print ("Sending %s  -> to the client ", response);

	}
}

static const GDBusInterfaceVTable interface_vtable = {
	handle_method_call,
	NULL,
	NULL
};

static void on_bus_acquired (GDBusConnection * connection, const gchar * name, gpointer user_data)
{
	g_print ("\n Inside on_bus_acquired \n");

	guint registration_id;

	registration_id = g_dbus_connection_register_object (connection, "/org/myapp/JelariObject", introspection_data->interfaces[0], &interface_vtable, NULL,	/* user_data */
		NULL,					/* user_data_free_func */
		NULL);					/* GError** */
	g_assert (registration_id > 0);

}

static void on_name_acquired (GDBusConnection * connection, const gchar * name, gpointer user_data)
{
}

static void on_name_lost (GDBusConnection * connection, const gchar * name, gpointer user_data)
{
	exit (1);
}

int main (int argc, char **argv)
{
	guint owner_id;
	GMainLoop *loop;
	g_type_init ();
	introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
	g_assert (introspection_data != NULL);

	owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, "org.myapp.JelariServer", G_BUS_NAME_OWNER_FLAGS_NONE, on_bus_acquired, on_name_acquired, on_name_lost, NULL, NULL);

	g_print ("\n Owner id is %d ", owner_id);
	loop = g_main_loop_new (NULL, FALSE);
	g_main_loop_run (loop);

	g_bus_unown_name (owner_id);

	g_dbus_node_info_unref (introspection_data);

}

Client.c

#include <gio/gio.h>
#include <stdlib.h>

#ifdef G_OS_UNIX
//#include <gio/gunixfdlist.h>
/* For STDOUT_FILENO */
#include <unistd.h>
#endif

GDBusProxy *bproxy;
GMainLoop * g_main_loop;

void mycallback (GObject * gobj, GAsyncResult * res, gpointer user_data)
{
	g_printf ("\n Inside mycallback\n ");

	GError *error;
	GVariant *result;
	gchar *str;

	error = NULL;
	result = g_dbus_proxy_call_finish (bproxy, res, &error);
	g_variant_get (result, "(s)", &str);
	g_print ("String from Server is: %s \n", str);
	g_main_loop_quit(g_main_loop);
	exit(0);
}

int main (int argc, char **argv)
{
	GDBusConnection *bcon;
	GMainLoop *loop;

	g_type_init ();
	bcon = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
	bproxy = g_dbus_proxy_new_sync (bcon, G_DBUS_PROXY_FLAGS_NONE, NULL, "org.myapp.JelariServer", "/org/myapp/JelariObject", "org.myapp.JelariInterface", NULL, NULL);
	g_dbus_proxy_call (bproxy, "HelloWorld", g_variant_new ("(s)", "Wow"), G_DBUS_CALL_FLAGS_NONE, -1, NULL, (GAsyncReadyCallback) mycallback, NULL);

	g_main_loop = g_main_loop_new(NULL, FALSE);
	g_main_loop_run(g_main_loop);
	return 0;
}


相關推薦

Dbus-glib使用方法說明

server.c #include <gio/gio.h> #include <stdlib.h> #ifdef G_OS_UNIX //#include <gio/gunixfdlist.h> /* For STDOUT_FILEN

dbus例項講解(四下):使用dbus-glib

4、複雜的資料型別 在dbus中怎樣處理複雜的資料型別?第一個建議是儘量不要使用複雜的資料型別。但如果確實需要呢? 有的網友建議用GArray作為容器, 不管什麼引數,在客戶端都手工放入GArray,在伺服器端再自己取出來。 這確實是個思路,比較適合伺服器和客戶端都是自己開

DBUS-GLIB:從DBUS文字訊息到函式呼叫背後的機制

之前曾經詳細看過在DBUS GLIB BINDING中本地訊息(Signal)如何對映到DBUS訊息(Signal),最近再次研究DBUS 的GLIB,發現尚遺漏了DBus訊息如何對映成本地方法呼叫的重要一環。此處補上。為了比較通透了解文字訊息到函式呼叫的動態型別繫結實現過

dbus-glib 和 GDBus 的區別

Conceptual differences(概念上的區別) The central concepts of D-Bus are modelled in a very similar way in dbus-glib and GDBus. Both have a objec

dbusdbus-glib

應用程式A和訊息匯流排連線,這個連接獲取了一個眾所周知的公共名(記作連線A)。應用程式A中有物件A1提供了介面I1,介面I1有方法M1。應用程式B和訊息匯流排連線,要求呼叫連線A上物件A1的介面I1的方法M1。 在上一講的加法例子中,上面這段話可以例項化為:應用程式example-service和會話

OC 方法使用

class turn -c obj sel ack bject urn 死循環 Person.h #import <Foundation/Foundation.h> @interface Person : NSObject { int _age; }

方法的聲和調用——java

sta 靜態 參數 靜態方法 裏的 div 調用 方法 沒有 方法只有聲明和調用,方法裏只能調用其它方法;方法外,只能聲明方法。 調用方法時參數順序不能顛倒。 同一個類裏的方法可以直接調用或訪問其他方法或屬性; 但靜態方法例外,靜態方法只能調用或訪問其他的靜態方法或

Spring的Service調用本類方法式事務無效的解決方案

{} 解決 ostc oid color wire ont spa 出了 示例: class Test{ public void a(){ b(); } @Transactional pubic void b(){} }

5. 方法的聲

clas style urn 有一個 有意義 console ole tel 機制 1、什麽是方法?   方法即函數,每個方法都有一個名稱和主體。給方法命名時應該給方法命名一個有意義的標識符,能描述出方法的用途。方法是一種基本的、功能強大的編程機制。 格式:    修飾符

Java中什麽是匿名對象,空參構造方法輸出創建了幾個匿名對象,屬性聲成static

es2017 ava cit 得到 定義 屬性 自增 alt spa package com.swift; //使用無參構造方法自動生成對象,序號不斷自增 public class Person { private static int count; //如果在定

Spring 實戰-第四章-4.4 使用xml中聲切面及引入新方法

cati sys epp proc oca cover tor ring tex 當不能直接接觸源碼時,同樣的不能給源碼增加註解,這時可以使用xml的方式聲明切面並引入新方法 CompactDisc接口 package main.java.soundsystem;

十二、事件,委托,泛型委托,集合(泛型和非泛型),Lambda表達式(聲委托,使用委托,簡單的委托示例,action<T>和func<t>委托,多播委托,匿名方法,Lambda表達式,參數,事件)

multicast new 調用方法 多播 ted 被調用 輸入參數 pac cas 事件(Event) 事件是一種對象或類能夠提供通知的成員,客戶端可以通過提供事件處理程序為相應的事件可添加可執行代碼,事件可以理解為一種特殊的委托。 委托(Delegate) 委托是存有對

PHP 頁面編碼聲方法詳解(header或meta)

指定編碼 httpd art 註釋 編碼 query 實現 文件夾 文本編輯 php的header來定義一個php頁面為utf編碼或GBK編碼 php頁面為utf編碼 header("Content-type: text/html; charset=utf-8"); php

c# 第21節 方法和調用

分享圖片 out .com bubuko 輸出 com img 數組 gpo 本節內容: 1:為什麽要有方法 2:方法的聲明及使用 3:方法params 傳入接收數組 4:值傳遞和引用傳遞 5:輸出參數out 1:為什麽要有方法 2:方法的聲明及使用

ES6中6種聲變量的方法

defined 嚴格 .cn def fin 默認 建議 默認值 語言 ES5 只有兩種聲明變量的方法:var命令和function命令。 ES6 除了添加let和const命令,還有兩種聲明變量的方法:import命令和class命令。 所以,ES6 一共有 6 種聲

佳媽媽談營銷方略五大方法

產品 小夥伴 效率 獲取 媽媽 最終 兩個 發生 營銷策略 明佳媽媽方案網小編與大家分享的是營銷策略有哪些方法,不知道的小夥伴都來看看小編為大家整理的營銷方略五大方法說明吧。   策略一:知己知彼、百戰不殆   要與競爭對手過招,知己知彼是關鍵,以便制定進攻策略,不打無準備

Srping MVC中Controller的void方法誤區

springmvc 不定 pin 如果 maven detail rpi mapping 內容 轉載自:http://blog.csdn.net/yh_zeng2 https://blog.csdn.net/yh_zeng2/article/details/75136614

SimHash+漢距離的C#實現方法

以下為SimHash+漢明距離的C#實現: using System; using System.Collections.Generic; using System.Linq; using System.Numerics; using System.T

Vue.js隨筆四(方法的聲和使用)

font 裏的 多說 就會 點擊 col eth ima 是的 1.首先你需要新建路由,這個就不多說了 2.然後在你的新的.vue裏面需要如下所示的添加methods:{方法},然後按鈕的裏面你會看到v-on:click,這就是點擊這個按鈕會觸發的動作,這個就是觸發met

es6 - 一共有 6 種聲變量的方法(var, function, let, const, class, import)

bject style ron func token 重復 常量 tor 代碼 let聲明的變量只在它所在的代碼塊有效。 不存在變量提升 let不允許在相同作用域內,重復聲明同一個變量 暫時性死區,只要塊級作用域內存在let命令,它所聲明的變量就“綁定