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

@ -1100,7 +1100,7 @@ class CoreGrpcClient:
session_id: int,
node_id: int,
model: str,
config: Dict[str, str],
config: Dict[str, str] = None,
interface_id: int = -1,
) -> SetEmaneModelConfigResponse:
"""

View file

@ -1,7 +1,8 @@
import argparse
import logging
from core.api.grpc import client, core_pb2
from core.api.grpc import client
from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState
def log_event(event):
@ -26,13 +27,11 @@ def main(args):
core.events(session_id, log_event)
# change session state
response = core.set_session_state(
session_id, core_pb2.SessionState.CONFIGURATION
)
response = core.set_session_state(session_id, SessionState.CONFIGURATION)
logging.info("set session state: %s", response)
# create switch node
switch = core_pb2.Node(type=core_pb2.NodeType.SWITCH)
switch = Node(type=NodeType.SWITCH)
response = core.add_node(session_id, switch)
logging.info("created switch: %s", response)
switch_id = response.node_id
@ -41,8 +40,8 @@ def main(args):
interface_helper = client.InterfaceHelper(ip4_prefix="10.83.0.0/16")
# create node one
position = core_pb2.Position(x=100, y=50)
node = core_pb2.Node(position=position)
position = Position(x=100, y=50)
node = Node(position=position)
response = core.add_node(session_id, node)
logging.info("created node one: %s", response)
node_one_id = response.node_id
@ -53,8 +52,8 @@ def main(args):
logging.info("created link from node one to switch: %s", response)
# create node two
position = core_pb2.Position(x=200, y=50)
node = core_pb2.Node(position=position, server=server_name)
position = Position(x=200, y=50)
node = Node(position=position, server=server_name)
response = core.add_node(session_id, node)
logging.info("created node two: %s", response)
node_two_id = response.node_id
@ -65,9 +64,7 @@ def main(args):
logging.info("created link from node two to switch: %s", response)
# change session state
response = core.set_session_state(
session_id, core_pb2.SessionState.INSTANTIATION
)
response = core.set_session_state(session_id, SessionState.INSTANTIATION)
logging.info("set session state: %s", response)

View file

@ -0,0 +1,74 @@
"""
Example using gRPC API to create a simple EMANE 80211 network.
"""
import logging
from core.api.grpc import client
from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState
from core.emane.ieee80211abg import EmaneIeee80211abgModel
def log_event(event):
logging.info("event: %s", event)
def main():
# helper to create interface addresses
interface_helper = client.InterfaceHelper(ip4_prefix="10.83.0.0/24")
# create grpc client and start connection context, which auto closes connection
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 to configuration so that nodes get started when added
response = core.set_session_state(session_id, SessionState.CONFIGURATION)
logging.info("set session state: %s", response)
# create emane node
position = Position(x=200, y=200)
emane = Node(type=NodeType.EMANE, position=position)
response = core.add_node(session_id, emane)
logging.info("created emane: %s", response)
emane_id = response.node_id
# an emane model must be configured for use, by the emane node
core.set_emane_model_config(session_id, emane_id, EmaneIeee80211abgModel.name)
# create node one
position = Position(x=100, y=100)
node1 = Node(type=NodeType.DEFAULT, position=position)
response = core.add_node(session_id, node1)
logging.info("created node: %s", response)
node1_id = response.node_id
# create node two
position = Position(x=300, y=100)
node2 = Node(type=NodeType.DEFAULT, position=position)
response = core.add_node(session_id, node2)
logging.info("created node: %s", response)
node2_id = response.node_id
# links nodes to switch
interface_one = interface_helper.create_interface(node1_id, 0)
response = core.add_link(session_id, node1_id, emane_id, interface_one)
logging.info("created link: %s", response)
interface_one = interface_helper.create_interface(node2_id, 0)
response = core.add_link(session_id, node2_id, emane_id, interface_one)
logging.info("created link: %s", response)
# change session state
response = core.set_session_state(session_id, SessionState.INSTANTIATION)
logging.info("set session state: %s", response)
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
main()

View file

@ -1,54 +0,0 @@
import logging
from core.api.grpc import client, core_pb2
def log_event(event):
logging.info("event: %s", event)
def main():
core = client.CoreGrpcClient()
with core.context_connect():
# create session
response = core.create_session()
session_id = response.session_id
logging.info("created session: %s", response)
# create nodes for session
nodes = []
position = core_pb2.Position(x=50, y=100)
switch = core_pb2.Node(id=1, type=core_pb2.NodeType.SWITCH, position=position)
nodes.append(switch)
for i in range(2, 50):
position = core_pb2.Position(x=50 + 50 * i, y=50)
node = core_pb2.Node(id=i, position=position, model="PC")
nodes.append(node)
# create links
interface_helper = client.InterfaceHelper(ip4_prefix="10.83.0.0/16")
links = []
for node in nodes:
interface_one = interface_helper.create_interface(node.id, 0)
link = core_pb2.Link(
type=core_pb2.LinkType.WIRED,
node_one_id=node.id,
node_two_id=switch.id,
interface_one=interface_one,
)
links.append(link)
# start session
response = core.start_session(session_id, nodes, links)
logging.info("started session: %s", response)
input("press enter to shutdown session")
response = core.stop_session(session_id)
logging.info("stop sessionL %s", response)
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
main()

View file

@ -1,6 +1,11 @@
"""
Example using gRPC API to create a simple switch network.
"""
import logging
from core.api.grpc import client, core_pb2
from core.api.grpc import client
from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState
def log_event(event):
@ -8,8 +13,11 @@ def log_event(event):
def main():
core = client.CoreGrpcClient()
# helper to create interface addresses
interface_helper = client.InterfaceHelper(ip4_prefix="10.83.0.0/24")
# create grpc client and start connection context, which auto closes connection
core = client.CoreGrpcClient()
with core.context_connect():
# create session
response = core.create_session()
@ -19,38 +27,41 @@ def main():
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
)
# change session state to configuration so that nodes get started when added
response = core.set_session_state(session_id, SessionState.CONFIGURATION)
logging.info("set session state: %s", response)
# create switch node
switch = core_pb2.Node(type=core_pb2.NodeType.SWITCH)
position = Position(x=200, y=200)
switch = Node(type=NodeType.SWITCH, position=position)
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")
# create node one
position = Position(x=100, y=100)
node1 = Node(type=NodeType.DEFAULT, position=position)
response = core.add_node(session_id, node1)
logging.info("created node: %s", response)
node1_id = response.node_id
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 node two
position = Position(x=300, y=100)
node2 = Node(type=NodeType.DEFAULT, position=position)
response = core.add_node(session_id, node2)
logging.info("created node: %s", response)
node2_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)
# links nodes to switch
interface_one = interface_helper.create_interface(node1_id, 0)
response = core.add_link(session_id, node1_id, switch_id, interface_one)
logging.info("created link: %s", response)
interface_one = interface_helper.create_interface(node2_id, 0)
response = core.add_link(session_id, node2_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
)
response = core.set_session_state(session_id, SessionState.INSTANTIATION)
logging.info("set session state: %s", response)

View file

@ -0,0 +1,82 @@
"""
Example using gRPC API to create a simple wlan network.
"""
import logging
from core.api.grpc import client
from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState
def log_event(event):
logging.info("event: %s", event)
def main():
# helper to create interface addresses
interface_helper = client.InterfaceHelper(ip4_prefix="10.83.0.0/24")
# create grpc client and start connection context, which auto closes connection
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 to configuration so that nodes get started when added
response = core.set_session_state(session_id, SessionState.CONFIGURATION)
logging.info("set session state: %s", response)
# create wlan node
position = Position(x=200, y=200)
wlan = Node(type=NodeType.WIRELESS_LAN, position=position)
response = core.add_node(session_id, wlan)
logging.info("created wlan: %s", response)
wlan_id = response.node_id
# change/configure wlan if desired
# NOTE: error = loss, and named this way for legacy purposes for now
config = {
"bandwidth": "54000000",
"range": "500",
"jitter": "0",
"delay": "5000",
"error": "0",
}
response = core.set_wlan_config(session_id, wlan_id, config)
logging.info("set wlan config: %s", response)
# create node one
position = Position(x=100, y=100)
node1 = Node(type=NodeType.DEFAULT, position=position)
response = core.add_node(session_id, node1)
logging.info("created node: %s", response)
node1_id = response.node_id
# create node two
position = Position(x=300, y=100)
node2 = Node(type=NodeType.DEFAULT, position=position)
response = core.add_node(session_id, node2)
logging.info("created node: %s", response)
node2_id = response.node_id
# links nodes to switch
interface_one = interface_helper.create_interface(node1_id, 0)
response = core.add_link(session_id, node1_id, wlan_id, interface_one)
logging.info("created link: %s", response)
interface_one = interface_helper.create_interface(node2_id, 0)
response = core.add_link(session_id, node2_id, wlan_id, interface_one)
logging.info("created link: %s", response)
# change session state
response = core.set_session_state(session_id, SessionState.INSTANTIATION)
logging.info("set session state: %s", response)
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
main()

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.