服務功能

是一種能力,可讓您探索一或多個個別命名的 服務功能由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;
};

並從另一個元件中的 namespace 中取用。

服務執行個體

單一元件可代管多個服務的命名例項。這些元件會顯示在 元件架構會為每個服務執行個體名稱產生任意專屬 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",
        },
    ],
}

如要進一步瞭解開啟要求,請參閱「通訊協定開啟的生命週期」。