summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/stl/examples
diff options
context:
space:
mode:
authorYaroslav Brustinov <ybrustin@cisco.com>2016-05-28 05:08:16 +0300
committerYaroslav Brustinov <ybrustin@cisco.com>2016-05-28 05:08:16 +0300
commitb0b3908b1b3742b84b48298fb8f7524e422bb28d (patch)
treeab2074e8e8f443613ee1c12968ab163972418911 /scripts/automation/trex_control_plane/stl/examples
parent4607fb5588bbb7dc0a708ffd5f97fe99bee98dd2 (diff)
master_daemon & other daemons updates
Diffstat (limited to 'scripts/automation/trex_control_plane/stl/examples')
-rwxr-xr-xscripts/automation/trex_control_plane/stl/examples/rpc_proxy_server.py67
-rwxr-xr-xscripts/automation/trex_control_plane/stl/examples/using_rpc_proxy.py25
2 files changed, 67 insertions, 25 deletions
diff --git a/scripts/automation/trex_control_plane/stl/examples/rpc_proxy_server.py b/scripts/automation/trex_control_plane/stl/examples/rpc_proxy_server.py
index 39d642f4..ad2697d8 100755
--- a/scripts/automation/trex_control_plane/stl/examples/rpc_proxy_server.py
+++ b/scripts/automation/trex_control_plane/stl/examples/rpc_proxy_server.py
@@ -6,14 +6,18 @@ import logging
import sys
import os
import json
+import socket
+from functools import partial
logging.basicConfig(level = logging.FATAL) # keep quiet
import stl_path
from trex_stl_lib.api import *
-from trex_stl_lib.trex_stl_hltapi import CTRexHltApi, HLT_ERR
+from trex_stl_lib.trex_stl_hltapi import CTRexHltApi, HLT_OK, HLT_ERR
# ext libs
-ext_libs = os.path.join(os.pardir, os.pardir, os.pardir, os.pardir, 'external_libs')
+ext_libs = os.path.join(os.pardir, os.pardir, os.pardir, os.pardir, 'external_libs') # usual package path
+if not os.path.exists(ext_libs):
+ ext_libs = os.path.join(os.pardir, os.pardir, 'external_libs') # client package path
sys.path.append(os.path.join(ext_libs, 'jsonrpclib-pelix-0.2.5'))
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
import yaml
@@ -62,18 +66,17 @@ def native_proxy_del():
def hltapi_proxy_init(force = False, *args, **kwargs):
global hltapi_client
if hltapi_client and not force:
- return ERR('HLTAPI Client is already initiated')
+ return HLT_ERR('HLTAPI Client is already initiated')
try:
hltapi_client = CTRexHltApi(*args, **kwargs)
- return OK('HLTAPI Client initiated')
+ return HLT_OK()
except:
- return ERR(traceback.format_exc())
+ return HLT_ERR(traceback.format_exc())
def hltapi_proxy_del():
global hltapi_client
hltapi_client = None
- return OK()
-
+ return HLT_OK()
# any method not listed above can be called with passing its name here
def native_method(func_name, *args, **kwargs):
@@ -94,14 +97,7 @@ def hltapi_method(func_name, *args, **kwargs):
### /Server functions ###
-### Main ###
-
-
-if __name__ == '__main__':
- parser = argparse.ArgumentParser(description = 'Runs TRex Stateless proxy for usage with any language client.')
- parser.add_argument('-p', '--port', type=int, default = 8095, dest='port', action = 'store',
- help = 'Select port on which the stl proxy will run.\nDefault is 8095.')
- args = parser.parse_args()
+def run_server(port = 8095):
native_methods = [
'acquire',
'connect',
@@ -122,25 +118,50 @@ if __name__ == '__main__':
]
try:
- server = SimpleJSONRPCServer(('0.0.0.0', args.port))
+ register_socket('trex_stl_rpc_proxy')
+ server = SimpleJSONRPCServer(('0.0.0.0', port))
server.register_function(add)
server.register_function(check_connectivity)
server.register_function(native_proxy_init)
server.register_function(native_proxy_del)
server.register_function(hltapi_proxy_init)
server.register_function(hltapi_proxy_del)
+ server.register_function(native_method)
+ server.register_function(hltapi_method)
for method in native_methods:
- server.register_function(lambda method=method, *args, **kwargs: native_method(method, *args, **kwargs), method)
- server.register_function(native_method)
+ server.register_function(partial(native_method, method), method)
for method in hltapi_methods:
- if method == 'connect':
- server.register_function(lambda method=method, *args, **kwargs: hltapi_method(method, *args, **kwargs), 'hlt_connect')
+ if method in native_methods: # collision in names
+ method_hlt_name = 'hlt_%s' % method
else:
- server.register_function(lambda method=method, *args, **kwargs: hltapi_method(method, *args, **kwargs), method)
- server.register_function(hltapi_method)
- print('Started Stateless RPC proxy at port %s' % args.port)
+ method_hlt_name = method
+ server.register_function(partial(hltapi_method, method), method_hlt_name)
+ server.register_function(server.funcs.keys, 'get_methods') # should be last
+ print('Started Stateless RPC proxy at port %s' % port)
server.serve_forever()
except KeyboardInterrupt:
print('Done')
+# provides unique way to determine running process
+def register_socket(tag):
+ global foo_socket # Without this our lock gets garbage collected
+ foo_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
+ try:
+ foo_socket.bind('\0%s' % tag)
+ print('Got the socket lock for tag %s.' % tag)
+ except socket.error:
+ print('Error: process with tag %s is already running.' % tag)
+ sys.exit(-1)
+
+### Main ###
+
+
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser(description = 'Runs TRex Stateless RPC proxy for usage with any language client.')
+ parser.add_argument('-p', '--port', type=int, default = 8095, dest='port', action = 'store',
+ help = 'Select port on which the stl rpc proxy will run.\nDefault is 8095.')
+ kwargs = vars(parser.parse_args())
+ run_server(**kwargs)
+
diff --git a/scripts/automation/trex_control_plane/stl/examples/using_rpc_proxy.py b/scripts/automation/trex_control_plane/stl/examples/using_rpc_proxy.py
index 82bf0d0a..065f4284 100755
--- a/scripts/automation/trex_control_plane/stl/examples/using_rpc_proxy.py
+++ b/scripts/automation/trex_control_plane/stl/examples/using_rpc_proxy.py
@@ -32,9 +32,29 @@ if __name__ == '__main__':
help = 'Address of rpc proxy.')
parser.add_argument('-p', '--port', type=int, default = 8095, dest='port', action = 'store',
help = 'Port of rpc proxy.\nDefault is 8095.')
+ parser.add_argument('--master_port', type=int, default = 8091, dest='master_port', action = 'store',
+ help = 'Port of Master daemon.\nDefault is 8091.')
args = parser.parse_args()
server = jsonrpclib.Server('http://%s:%s' % (args.server, args.port))
+ master = jsonrpclib.Server('http://%s:%s' % (args.server, args.master_port))
+
+# Connecting
+
+ try:
+ print('Connecting to STL RPC proxy server')
+ server.check_connectivity()
+ print('Connected')
+ except Exception as e:
+ print('Could not connect to STL RPC proxy server: %s\nTrying to start it from Master daemon.' % e)
+ try:
+ master.check_connectivity()
+ master.start_stl_rpc_proxy()
+ print('Started')
+ except Exception as e:
+ print('Could not start it from Master daemon. Error: %s' % e)
+ sys.exit(-1)
+
# Native API
@@ -68,7 +88,8 @@ if __name__ == '__main__':
# HLTAPI
print('Initializing HLTAPI Client')
- verify(server.hltapi_proxy_init(force = True))
+ verify_hlt(server.hltapi_proxy_init(force = True))
+ print('HLTAPI Client initiated')
print('HLTAPI connect')
verify_hlt(server.hlt_connect(device = args.server, port_list = ports, reset = True, break_locks = True))
@@ -98,4 +119,4 @@ if __name__ == '__main__':
print(res)
print('Deleting HLTAPI Client instance')
- verify(server.hltapi_proxy_del())
+ verify_hlt(server.hltapi_proxy_del())