服務功能

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",
        },
    ],
}

轉送服務功能

元件轉送服務功能,方法是將服務功能「公開」exposing給父項,並提供給子項

如要進一步瞭解架構路徑元件功能,請參閱「功能轉送」。

公開

公開服務能力可讓元件的父項存取該能力:

{
    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",
        },
    ],
}

如要進一步瞭解開放式要求,請參閱通訊協定開放的生命週期