是一種能力,可讓您探索一或多個個別命名的服務功能由Directory
通訊協定支援,目錄中的每個項目都會公開已命名的服務執行個體。
library fuchsia.examples;
const MAX_STRING_LENGTH uint64 = 32;
@discoverable
protocol Echo {
EchoString(struct {
value string:MAX_STRING_LENGTH;
}) -> (struct {
response string:MAX_STRING_LENGTH;
});
SendString(struct {
value string:MAX_STRING_LENGTH;
});
-> OnString(struct {
response string:MAX_STRING_LENGTH;
});
};
service EchoService {
regular_echo client_end:Echo;
reversed_echo client_end:Echo;
};
服務執行個體
單一元件可代管多個服務的命名執行個體。這些元件會顯示在元件架構會為每個服務執行個體名稱產生任意專屬 ID。
舉例來說,如果架構以 57dfe118a2a8
做為 fuchsia.examples.EchoService
服務的執行個體名稱,則使用者元件可透過下列命名空間路徑連線至該執行個體中的通訊協定:
/svc/fuchsia.examples.EchoService/57dfe118a2a8/regular_echo
/svc/fuchsia.examples.EchoService/57dfe118a2a8/reversed_echo
提供服務功能
如要提供服務能力,元件必須宣告該功能,並從 self
路由該功能。服務能力元件會在其
如要定義能力,請為其新增 capabilities
宣告:
{
capabilities: [
{
service: "fuchsia.example.ExampleService",
},
],
}
這會定義這個元件所代管的能力,其傳出目錄路徑為 /svc/fuchsia.example.ExampleService
。您也可以自訂路徑:
{
capabilities: [
{
service: "fuchsia.example.ExampleService",
path: "/my_svc/fuchsia.example.MyExampleService",
},
],
}
轉送服務功能
如要進一步瞭解架構如何轉送元件功能,請參閱「功能轉送」。
公開
公開服務能力可讓元件的父項存取該能力:
{
expose: [
{
service: "fuchsia.example.ExampleService",
from: "self",
},
],
}
from: "self"
指令表示服務能力是由此元件提供。
動態集合
服務能力可透過動態集合公開:
{
collections: [
{
name: "coll",
durability: "transient",
},
],
expose: [
{
service: "fuchsia.example.ExampleService",
from: "#coll",
},
],
}
提供
提供服務能力可讓子元件存取該能力:
{
offer: [
{
service: "fuchsia.example.ExampleService",
from: "self",
to: [ "#child-a", "#child_b" ],
},
],
}
使用服務功能
如要要求這項能力,請為其新增 use
宣告:
{
use: [
{
service: "fuchsia.example.ExampleService",
},
],
}
這會在元件命名空間的已知路徑 /svc/fuchsia.example.ExampleService
中填入服務。您也可以自訂路徑:
{
use: [
{
service: "fuchsia.example.ExampleService",
path: "/my_svc/fuchsia.example.MyExampleService",
},
],
}
如要進一步瞭解開啟要求,請參閱「通訊協定開啟的生命週期」。