服务功能

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 进行路由

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

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

定义了传出目录路径为 /svc/fuchsia.example.ExampleService 的此组件托管的功能。您还可以自定义路径:

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

路由服务功能

组件通过向父级公开服务功能并将其提供给子级,来路由服务功能。exposing

如需详细了解框架如何路由组件功能,请参阅功能路由

正在曝光

公开服务 capability 可让组件的父项使用该功能:

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

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