grpc doc improvements, grpc examples additions, small tweak to grpc client for setting emane models not requiring a config when using default configuration

This commit is contained in:
Blake Harnden 2020-06-05 11:20:23 -07:00
parent 9a5fc94ba2
commit 75d5bced9c
7 changed files with 220 additions and 148 deletions

View file

@ -1,72 +1,34 @@
# Using the gRPC API
gRPC is the main API for interfacing with CORE.
[gRPC](https://grpc.io/) is the main API for interfacing with CORE and used by
the python GUI for driving all functionality.
Currently we are providing a python client that wraps the generated files for
leveraging the API, but proto files noted below can also be leveraged to generate
bindings for other languages as well.
## HTTP Proxy
Since gRPC is HTTP2 based, proxy configurations can cause issue. Clear out your
proxy when running if needed.
Since gRPC is HTTP2 based, proxy configurations can cause issue. You can either
properly account for this issue or clear out your proxy when running if needed.
## Python Client
A python client wrapper is provided at **core.api.grpc.client.CoreGrpcClient**.
A python client wrapper is provided at
[CoreGrpcClient](../daemon/core/api/grpc/client.py) to help provide some
conveniences when using the API.
Below is a small example using it.
## Proto Files
```python
import logging
Proto files are used to define the API and protobuf messages that are used for
interfaces with this API.
from core.api.grpc import client, core_pb2
They can be found [here](../daemon/proto/core/api/grpc) to see the specifics of
what is going on and response message values that would be returned.
## Examples
def log_event(event):
logging.info("event: %s", event)
Example usage of this API can be found [here](../daemon/examples/grpc). These
examples will create a session using the gRPC API when the core-daemon is running.
def main():
core = client.CoreGrpcClient()
with core.context_connect():
# create session
response = core.create_session()
logging.info("created session: %s", response)
# handle events session may broadcast
session_id = response.session_id
core.events(session_id, log_event)
# change session state
response = core.set_session_state(session_id, core_pb2.SessionState.CONFIGURATION)
logging.info("set session state: %s", response)
# create switch node
switch = core_pb2.Node(type=core_pb2.NodeType.SWITCH)
response = core.add_node(session_id, switch)
logging.info("created switch: %s", response)
switch_id = response.node_id
# helper to create interfaces
interface_helper = client.InterfaceHelper(ip4_prefix="10.83.0.0/16")
for i in range(2):
# create node
position = core_pb2.Position(x=50 + 50 * i, y=50)
node = core_pb2.Node(position=position)
response = core.add_node(session_id, node)
logging.info("created node: %s", response)
node_id = response.node_id
# create link
interface_one = interface_helper.create_interface(node_id, 0)
response = core.add_link(session_id, node_id, switch_id, interface_one)
logging.info("created link: %s", response)
# change session state
response = core.set_session_state(session_id, core_pb2.SessionState.INSTANTIATION)
logging.info("set session state: %s", response)
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
main()
```
You can then switch to and attach to these sessions using either of the CORE GUIs.