服务功能

是一种功能,可用于发现一个或多个单独命名的实例。FIDL 服务 服务功能由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;
};

并从其他组件的命名空间中使用这些数据。

服务实例

单个组件可以托管服务的多个命名实例。这些文件位于使用方组件的命名空间中,并作为服务的子目录。 组件框架会为每个服务实例名称生成一个任意唯一标识符。

例如,如果框架生成 57dfe118a2a8 作为 fuchsia.examples.EchoService 服务的实例名称,则使用方组件可以使用以下命名空间路径连接到该实例中的协议:

  • /svc/fuchsia.examples.EchoService/57dfe118a2a8/regular_echo
  • /svc/fuchsia.examples.EchoService/57dfe118a2a8/reversed_echo

提供服务功能

如需提供服务功能,组件必须声明该功能并从 self 路由该功能。该组件在其出站目录中托管服务 capability。

如需定义该 capability,请为其添加 capabilities 声明:

{
    capabilities: [
        {
            service: "fuchsia.example.ExampleService",
        },
    ],
}

这定义了此组件托管的 capability,其传出目录路径为 /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" ],
        },
    ],
}

使用服务功能

如需使用服务 capability,组件必须请求该 capability,并在其

如需请求该 capability,请为其添加 use 声明:

{
    use: [
        {
            service: "fuchsia.example.ExampleService",
        },
    ],
}

这会在组件的命名空间中以众所周知的路径 /svc/fuchsia.example.ExampleService 填充服务。您还可以自定义路径:

{
    use: [
        {
            service: "fuchsia.example.ExampleService",
            path: "/my_svc/fuchsia.example.MyExampleService",
        },
    ],
}

如需详细了解打开请求,请参阅协议打开的生命周期