5 CORE gRPC API

COREgRPCAPI是一个用于与CORE仿真环境交互的客户端/服务器接口,支持通过Python客户端进行网络配置、事件监听、节点与链接管理等。文章提供了创建不同节点类型、配置接口、监听事件、设定链接参数以及配置服务和文件的例子,展示了如何使用gRPCAPI进行点对点、交换机/集线器、WLAN和EMANE模型的网络仿真。

CORE gRPC API

原文链接:gRPC API


gRPC 是一个用于与 CORE 交互的客户端/服务器 API,并被 Python GUI 用于驱动所有功能。它依赖于要利用的正在运行的 CORE 守护程序实例。

可以从包含在 CORE 中的原始生成的 grpc 文件创建一个 python 客户端,或者可以利用提供的 gRPC 客户端来帮助封装一些功能来尝试帮助使事情变得更容易。

1. Python 客户端

CoreGrpcClient 提供了一个 python 客户端包装器,以帮助在使用 API 时提供一些便利。

1.1. 客户端 HTTP 代理

由于 gRPC 基于 HTTP2,代理配置可能会导致问题。默认情况下,客户端禁用代理支持以避免出现代理时出现问题。可以在需要时启用并正确解决此问题。

2. 原型文件

原型文件用于定义用于与此 API 接口的 API 和 protobuf 消息。

可以在此处找到它们以查看正在发生的事情的细节以及将返回的响应消息值。

3. 例子

3.1. 节点模型

创建 NodeType.DEFAULT 类型的节点时,这些是默认模型和它们映射到的服务。

  • mdr
    • zebra, OSPFv3MDR, IPForward
  • PC
    • DefaultRoute
  • router
    • zebra, OSPFv2, OSPFv3, IPForward
  • host
    • DefaultRoute, SSH

3.2. 接口帮助

在为节点创建接口数据时,可以利用一个接口助手类来方便。 或者,可以使用适当的信息手动创建 core.api.grpc.core_pb2.Interface 类。

手动创建 gRPC 接口数据:

from core.api.grpc import core_pb2
# id is optional and will set to the next available id
# name is optional and will default to eth<id>
# mac is optional and will result in a randomly generated mac
iface_data = core_pb2.Interface(
    id=0,
    name="eth0",
    ip4="10.0.0.1",
    ip4_mask=24,
    ip6="2001::",
    ip6_mask=64,
)

利用接口前缀帮助器类:

from core.emulator.data import IpPrefixes

ip_prefixes = IpPrefixes(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")
# 2. node is used to get an ip4/ip6 address indexed from within the above prefixes
# 3. name is optional and would default to eth<id>
# 4. mac is optional and will result in a randomly generated mac
iface_data = ip_prefixes.create_iface(
    node=node, name="eth0", mac="00:00:00:00:aa:00"
)

3.3. 监听事件

可以监听会话中可能发生的各种事件。

事件类型:

  • session - 会话状态和移动性开始/停止/暂停变化的事件
  • node - 节点移动和图标更改的事件
  • link - 链接配置更改和无线链接添加/删除的事件
  • config - 旧版 gui 加入会话时的配置事件
  • exception - 警报/错误事件
  • file - 旧版 gui 加入会话时的文件事件
def event_listener(event):
    print(event)

# add an event listener to event type you want to listen to
# each handler will receive an object unique to that type
session.event_handlers.append(event_listener)
session.exception_handlers.append(event_listener)
session.node_handlers.append(event_listener)
session.link_handlers.append(event_listener)
session.file_handlers.append(event_listener)
session.config_handlers.append(event_listener)

3.4. 配置链接

链接可以在创建时或运行时进行配置。

当前支持的配置选项:

  • bandwidth (bps)
  • delay (us)
  • dup (%)
  • jitter (us)
  • loss (%)
from core.api.grpc import core_pb2

# 2. configuring when creating a link
options = core_pb2.LinkOptions(
    bandwidth=54_000_000,
    delay=5000,
    dup=5,
    loss=5.5,
    jitter=0,
)
core.add_link(session_id, n1_id, n2_id, iface1_data, iface2_data, options)

# 3. configuring during runtime
core.edit_link(session_id, n1_id, n2_id, iface1_id, iface2_id, options)

3.5. 点对点例子

# required imports
from core.api.grpc import client
from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState

# interface helper
iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")

# create grpc client and connect
core = client.CoreGrpcClient()
core.connect()

# create session and get id
response = core.create_session()
session_id = response.session_id

# change session state to configuration so that nodes get started when added
core.set_session_state(session_id, SessionState.CONFIGURATION)

# create node one
position = Position(x=100, y=100)
n1 = Node(type=NodeType.DEFAULT, position=position, model="PC")
response = core.add_node(session_id, n1)
n1_id = response.node_id

# create node two
position = Position(x=300, y=100)
n2 = Node(type=NodeType.DEFAULT, position=position, model="PC")
response = core.add_node(session_id, n2)
n2_id = response.node_id

# links nodes together
iface1 = iface_helper.create_iface(n1_id, 0)
iface2 = iface_helper.create_iface(n2_id, 0)
core.add_link(session_id, n1_id, n2_id, iface1, iface2)

# change session state
core.set_session_state(session_id, SessionState.INSTANTIATION)

3.6. Switch/Hub 例子

# required imports
from core.api.grpc import client
from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState

# interface helper
iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")

# create grpc client and connect
core = client.CoreGrpcClient()
core.connect()

# create session and get id
response = core.create_session()
session_id = response.session_id

# change session state to configuration so that nodes get started when added
core.set_session_state(session_id, SessionState.CONFIGURATION)

# create switch node
position = Position(x=200, y=200)
switch = Node(type=NodeType.SWITCH, position=position)
response = core.add_node(session_id, switch)
switch_id = response.node_id

# create node one
position = Position(x=100, y=100)
n1 = Node(type=NodeType.DEFAULT, position=position, model="PC")
response = core.add_node(session_id, n1)
n1_id = response.node_id

# create node two
position = Position(x=300, y=100)
n2 = Node(type=NodeType.DEFAULT, position=position, model="PC")
response = core.add_node(session_id, n2)
n2_id = response.node_id

# links nodes to switch
iface1 = iface_helper.create_iface(n1_id, 0)
core.add_link(session_id, n1_id, switch_id, iface1)
iface1 = iface_helper.create_iface(n2_id, 0)
core.add_link(session_id, n2_id, switch_id, iface1)

# change session state
core.set_session_state(session_id, SessionState.INSTANTIATION)

3.7. WLAN 例子

# required imports
from core.api.grpc import client
from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState

# interface helper
iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")

# create grpc client and connect
core = client.CoreGrpcClient()
core.connect()

# create session and get id
response = core.create_session()
session_id = response.session_id

# change session state to configuration so that nodes get started when added
core.set_session_state(session_id, SessionState.CONFIGURATION)

# create wlan node
position = Position(x=200, y=200)
wlan = Node(type=NodeType.WIRELESS_LAN, position=position)
response = core.add_node(session_id, wlan)
wlan_id = response.node_id

# create node one
position = Position(x=100, y=100)
n1 = Node(type=NodeType.DEFAULT, position=position, model="mdr")
response = core.add_node(session_id, n1)
n1_id = response.node_id

# create node two
position = Position(x=300, y=100)
n2 = Node(type=NodeType.DEFAULT, position=position, model="mdr")
response = core.add_node(session_id, n2)
n2_id = response.node_id

# configure wlan using a dict mapping currently
# support values as strings
core.set_wlan_config(session_id, wlan_id, {
    "range": "280",
    "bandwidth": "55000000",
    "delay": "6000",
    "jitter": "5",
    "error": "5",
})

# links nodes to wlan
iface1 = iface_helper.create_iface(n1_id, 0)
core.add_link(session_id, n1_id, wlan_id, iface1)
iface1 = iface_helper.create_iface(n2_id, 0)
core.add_link(session_id, n2_id, wlan_id, iface1)

# change session state
core.set_session_state(session_id, SessionState.INSTANTIATION)

3.8. EMANE 例子

对于 EMANE,您可以导入和使用现有模型之一,并使用其名称进行配置。

当前模型:

  • core.emane.ieee80211abg.EmaneIeee80211abgModel
  • core.emane.rfpipe.EmaneRfPipeModel
  • core.emane.tdma.EmaneTdmaModel
  • core.emane.bypass.EmaneBypassModel

它们的配置选项是从已安装的 EMANE 版本中解析的 EMANE 清单文件动态驱动的。

选项及其用途可以在 EMANE Wiki 中找到。

如果配置 EMANE 全局设置或模型 mac/phy 特定设置,任何未提供的值将使用默认值。当不使用配置时,使用默认值。

# required imports
from core.api.grpc import client
from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState
from core.emane.ieee80211abg import EmaneIeee80211abgModel

# interface helper
iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64")

# create grpc client and connect
core = client.CoreGrpcClient()
core.connect()

# create session and get id
response = core.create_session()
session_id = response.session_id

# change session state to configuration so that nodes get started when added
core.set_session_state(session_id, SessionState.CONFIGURATION)

# create emane node
position = Position(x=200, y=200)
emane = Node(type=NodeType.EMANE, position=position, emane=EmaneIeee80211abgModel.name)
response = core.add_node(session_id, emane)
emane_id = response.node_id

# create node one
position = Position(x=100, y=100)
n1 = Node(type=NodeType.DEFAULT, position=position, model="mdr")
response = core.add_node(session_id, n1)
n1_id = response.node_id

# create node two
position = Position(x=300, y=100)
n2 = Node(type=NodeType.DEFAULT, position=position, model="mdr")
response = core.add_node(session_id, n2)
n2_id = response.node_id

# configure general emane settings
core.set_emane_config(session_id, {
    "eventservicettl": "2"
})

# configure emane model settings
# using a dict mapping currently support values as strings
core.set_emane_model_config(session_id, emane_id, EmaneIeee80211abgModel.name, {
    "unicastrate": "3",
})

# links nodes to emane
iface1 = iface_helper.create_iface(n1_id, 0)
core.add_link(session_id, n1_id, emane_id, iface1)
iface1 = iface_helper.create_iface(n2_id, 0)
core.add_link(session_id, n2_id, emane_id, iface1)

# change session state
core.set_session_state(session_id, SessionState.INSTANTIATION)

EMANE 模型配置:

# emane network specific config
core.set_emane_model_config(session_id, emane_id, EmaneIeee80211abgModel.name, {
    "unicastrate": "3",
})

# node specific config
core.set_emane_model_config(session_id, node_id, EmaneIeee80211abgModel.name, {
    "unicastrate": "3",
})

# node interface specific config
core.set_emane_model_config(session_id, node_id, EmaneIeee80211abgModel.name, {
    "unicastrate": "3",
}, iface_id)

4. 配置一个服务

服务有助于在节点上生成和运行 bash 脚本以达到特定目的。

配置服务的文件会导致生成特定的硬编码脚本,而不是默认脚本,这可能会利用动态生成。

可以为服务配置以下功能:

  • configs - 将生成的文件
  • dirs - 将安装到节点唯一的目录
  • startup - 运行启动服务的命令
  • validate - 运行以验证服务的命令
  • shutdown - 运行以停止服务的命令

编辑服务属性:

# configure a service, for a node, for a given session
session.services.set_service(node_id, service_name)
service = session.services.get_service(node_id, service_name)
service.configs = ("file1.sh", "file2.sh")
service.dirs = ("/etc/node",)
service.startup = ("bash file1.sh",)
service.validate = ()
service.shutdown = ()

编辑服务文件时,它必须是服务将生成的配置文件的名称。

编辑服务文件:

# to edit the contents of a generated file you can specify
# the service, the file name, and its contents
session.services.set_service_file(
    node_id,
    service_name,
    file_name,
    "echo hello",
)

5. 文件例子

可以在此处找到网络示例的文件版本。这些示例将在核心守护程序运行时使用 gRPC API 创建会话。

然后,您可以使用任一 CORE GUI 切换到并附加到这些会话。

<think>好的,用户想查找与gRPC Core API相关的技术文档或使用指南。首先,我需要回忆一下gRPC的官方文档结构。gRPC是一个开源的高性能RPC框架,其文档通常托管在官方网站和GitHub上。根据用户提供的引用[^1],文档可能位于GitHub仓库,并且社区贡献者可以参与。另外,用户提到的引用[^2]可能不太相关,因为它涉及CoreDNS的日志配置,但可能需要确认是否有其他联系。 接下来,我应该考虑用户的具体需求。他们需要的是gRPC Core API的文档和使用指南。gRPC的核心API通常指的是底层的C++实现,而其他语言如Python、Java等可能提供更高层次的封装。因此,可能需要区分不同语言的文档。 首先,检查官方文档网站:grpc.io。这里通常有详细的入门指南、API参考和示例。核心API可能位于“Documentation”部分下的“Core Concepts”或类似章节。此外,GitHub仓库如grpc/grpc可能包含更详细的开发者文档,尤其是在源码中的说明或README文件。 然后,用户可能需要安装指南、API接口说明、配置选项和示例代码。例如,C++的Core API文档可能在grpc.io的C++部分,而其他语言可能有各自的文档页面。另外,引用[^1]提到项目托管在GitHub,并且有gRPC Ecosystem组织,可能需要引导用户到那里查找社区贡献的项目或扩展。 还需要考虑用户可能遇到的困难,比如文档结构复杂,难以找到具体内容。因此,提供明确的链接和步骤会有帮助。同时,提醒用户查阅官方文档和社区资源,如GitHub仓库的Wiki或Issues部分,以获取最新信息。 总结回答结构:首先指出官方文档的位置,然后分语言说明如何找到Core API文档,接着提供GitHub资源,最后建议查阅社区和生态系统项目。确保回答符合用户的中文需求,并正确引用提供的引用内容,尽管引用[^2]可能不相关,但引用[^1]提到GitHub托管和社区反馈,可以支持文档位置的说明。</think>关于gRPC Core API的技术文档和使用指南,以下是分步骤说明: 1. **官方文档入口** - 访问gRPC官方网站:https://grpc.io/docs/ - 核心API文档位于"Core Concepts"章节,包含$gRPC$架构、$Channel$、$Call$等基础组件说明 2. **按语言分类的API指南** - C++ Core API: ```markdown https://grpc.github.io/grpc/cpp/ 包含`grpc::Channel`、`grpc::ServerBuilder`等核心类详解 ``` - Python底层API: ```python # 使用grpc._cython模块示例 from grpc._cython import cygrpc channel = cygrpc.Channel(b"localhost:50051", None, None) ``` 3. **GitHub资源定位** - 核心代码库文档: ``` https://github.com/grpc/grpc/tree/master/doc 包含API设计规范、内存管理机制等核心说明 ``` - 关键API参考: $$ \text{Core API} \in \left\{ \text{src/core/lib/}, \text{include/grpc/} \right\} \text{目录结构} $$ 4. **实用工具链** - 通过`doxygen`生成的C++ API参考: ``` https://grpc.io/docs/guides/cpp_api_reference/ ``` - 性能调优指南中的核心参数: ```bash GRPC_ARG_MAX_CONCURRENT_STREAMS # 核心流控制参数 GRPC_ARG_HTTP2_BDP_PROBE # 带宽探测配置 ``` 5. **社区资源补充** - gRPC生态系统项目: ``` https://github.com/grpc-ecosystem 包含与Core API集成的观测性、负载均衡等扩展 ``` - 官方示例仓库: ```shell git clone https://github.com/grpc/grpc # 核心API使用示例位于examples/cpp/helloworld ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值