📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
ページの作成:「== 概要 == D-Busは、オープンソースのプロセス間通信(IPC:Inter Process Communication)機構であり、freedesktop.orgプロジェクトの一部である。<br> IPCとは、1台のコンピュータ上で動作する複数のプログラムの間で情報を交換するシステムのことである。<br> <br> IPCには、パイプ、名前付きパイプ、シグナル、共有メモリ、Unixソケット、ループバックソケット等が…」 |
|||
| 66行目: | 66行目: | ||
make -j $(nproc) | make -j $(nproc) | ||
make install | make install | ||
<br> | |||
==== 使用例 : Systemdサービスユニットの開始 ==== | |||
以下の例では、sdbus-c++ライブラリを使用して、<u>sudo systemctl start smb</u>コマンドと同等の操作を行っている。<br> | |||
D-Bus経由でSystemdと直接通信するため、より柔軟で、プログラム内から制御できるようになっている。<br> | |||
<br> | |||
ただし、さんぷるコードを実行するには、適切な権限が必要となる。<br> | |||
<syntaxhighlight lang="c++"> | |||
#include <sdbus-c++/sdbus-c++.h> | |||
#include <iostream> | |||
int main() | |||
{ | |||
try { | |||
// D-Busへの接続を作成 | |||
auto connection = sdbus::createSystemBusConnection(); | |||
// Systemdのマネージャーインターフェースへのプロキシオブジェクトを作成 | |||
auto proxy = sdbus::createProxy(*connection, | |||
"org.freedesktop.systemd1", | |||
"/org/freedesktop/systemd1"); | |||
// StartUnitメソッドを呼び出してsmbサービスを開始 | |||
// "StartUnit"はSystemdサービスのメソッド名 | |||
// "org.freedesktop.systemd1.Manager"はD-Busインターフェース名 | |||
// "smb.service"は開始するSystemdサービス名 | |||
// "replace"はSystemdサービスの起動モード | |||
proxy->callMethod("StartUnit") | |||
.onInterface("org.freedesktop.systemd1.Manager") | |||
.withArguments("smb.service", "replace") | |||
.storeResultsTo(); | |||
std::cout << "smb service started successfully." << std::endl; | |||
} | |||
catch (const sdbus::Error &e) { | |||
// sdbus::Errorをキャッチしてエラーメッセージを表示 | |||
std::cerr << "D-Bus error: " << e.what() << std::endl; | |||
return -1; | |||
} | |||
catch (const std::exception &e) { | |||
// std::exceptionをキャッチして一般エラーを処理 | |||
std::cerr << "Error: " << e.what() << std::endl; | |||
return -1; | |||
} | |||
return 0; | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== 使用例 : ユーザ定義のD-Busサービスの実行 ==== | |||
以下に示すようなユーザ定義のD-Busサービスがあるとする。<br> | |||
* D-Busサービス名 | |||
*: org.example.mochiu | |||
* D-Busオブジェクト名 | |||
*: /org/example/mochiu | |||
* D-Busインターフェース名 | |||
: org.example.mochiu.method | |||
* D-Busインターフェースメソッド名 | |||
*: func1 | |||
** func1の引数 | |||
**: 第1引数 int型 | |||
**: 第2引数 std::stringクラスの参照 | |||
** func1の戻り値 | |||
**: int型 | |||
<br> | |||
上記のD-Busサービスにおいて、指定したD-Busサービス、オブジェクト、インターフェース、メソッドを使用して実行している。<br> | |||
<br> | |||
std::stringクラスの参照渡しは明示的に行っていないが、これは、sdbus-c++ライブラリが内部で適切に処理するためである。<br> | |||
<u>sdbus-c++ライブラリは効率的な引数の受け渡しを行うよう設計されているため、大きなオブジェクトは自動的に参照として扱われる。</u><br> | |||
<syntaxhighlight lang="c++"> | |||
#include <sdbus-c++/sdbus-c++.h> | |||
#include <iostream> | |||
#include <cstring> | |||
int main() | |||
{ | |||
try { | |||
// セッションバスへの接続を作成(ユーザー定義サービスの場合、通常はセッションバスを使用) | |||
auto connection = sdbus::createSessionBusConnection(); | |||
// 指定されたD-Busサービスとオブジェクトへのプロキシオブジェクトを作成 | |||
auto proxy = sdbus::createProxy(*connection, | |||
"org.example.mochiu", // D-Busサービス名 | |||
"/org/example/mochiu" // D-Busオブジェクト名 | |||
); | |||
// func1メソッドの引数 | |||
int arg1 = 42; | |||
std::string arg2 = "Hello, D-Bus!"; | |||
// D-Busインターフェースメソッド (func1) を呼び出して、戻り値を受け取る | |||
auto result = proxy->callMethod("func1") // D-Busインターフェースメソッド名 | |||
.onInterface("org.example.mochiu.method") // D-Busインターフェース名 | |||
.withArguments(arg1, arg2) // 引数を渡す | |||
.returnValue<int>(); // int型の戻り値を受け取る | |||
std::cout << "Method 'func1' called successfully." << std::endl; | |||
std::cout << "Result: " << result << std::endl; | |||
} | |||
catch (const sdbus::Error &e) { | |||
// sdbus::Errorをキャッチしてエラーメッセージを表示 | |||
std::cerr << "D-Bus error: " << e.what() << std::endl; | |||
return -1; | |||
} | |||
catch (const std::exception &e) { | |||
// std::exceptionをキャッチして一般エラーを処理 | |||
std::cerr << "Error: " << e.what() << std::endl; | |||
return -1; | |||
} | |||
return 0; | |||
} | |||
</syntaxhighlight> | |||
<br><br> | <br><br> | ||