summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/stl
diff options
context:
space:
mode:
authoritraviv <itraviv@cisco.com>2016-08-04 17:45:55 +0300
committeritraviv <itraviv@cisco.com>2016-08-04 17:45:55 +0300
commit20691575dbdb9ba899c7d818caeca80902279554 (patch)
tree9a59aedc2341d233ed2dca619af8dfcc6614784d /scripts/automation/trex_control_plane/stl
parent28b09fb263445b1c73e3119d17feb5cc6f954ee6 (diff)
server was made as an object with execute function
Diffstat (limited to 'scripts/automation/trex_control_plane/stl')
-rwxr-xr-xscripts/automation/trex_control_plane/stl/services/scapy_server/zmq_for_scapy_server.py73
1 files changed, 39 insertions, 34 deletions
diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/zmq_for_scapy_server.py b/scripts/automation/trex_control_plane/stl/services/scapy_server/zmq_for_scapy_server.py
index 853db69e..4f5fab36 100755
--- a/scripts/automation/trex_control_plane/stl/services/scapy_server/zmq_for_scapy_server.py
+++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/zmq_for_scapy_server.py
@@ -12,18 +12,10 @@ import zmq
import inspect
from scapy_server import *
-server_file_name = 'zmq_server_hello_world_server'
-
-context = zmq.Context()
-socket = context.socket(zmq.REP)
-socket.bind("tcp://*:5555")
-
-
class ParseException(Exception): pass
class InvalidRequest(Exception): pass
class MethodNotFound(Exception): pass
class InvalidParams(Exception): pass
-#def error_response()
class Scapy_wrapper:
def __init__(self):
@@ -63,9 +55,18 @@ class Scapy_wrapper:
def create_success_response(self,result,req_id='null'):
return {"jsonrpc": "2.0", "result": result, "id": req_id }
- def getException(self):
+ def get_exception(self):
return sys.exc_info()
+
+ def execute(self,method,params):
+ if len(params)>0:
+ result = eval('self.scapy_master.'+method+'(*'+str(params)+')')
+ else:
+ result = eval('self.scapy_master.'+method+'()')
+ return result
+
+
def metaraise(self,exc_info):
raise exc_info[0], exc_info[1], exc_info[2]
@@ -83,35 +84,39 @@ class Scapy_wrapper:
response = self.create_error_response(-32603,'Invalid params',e.message)
except SyntaxError as e:
response = self.create_error_response(-32097,'SyntaxError')
- except Exception as e:
+ except BaseException as e:
response = self.create_error_response(-32098,'Scapy Server: '+str(e.message),req_id)
finally:
return response
+class Scapy_server():
+ def __init__(self,port):
+ self.scapy_wrapper = Scapy_wrapper()
+ self.port = port
+ self.context = zmq.Context()
+ self.socket = self.context.socket(zmq.REP)
+ self.socket.bind("tcp://*:"+str(port))
-scapy_wrapper = Scapy_wrapper()
-try:
- while True:
- # Wait for next request from client
- message = socket.recv()
-# print("Received request: %s" % message)
-# print ("message type is: %s" % type(message))
-# print("message is now: %s" % message)
+ def activate(self):
try:
- method,params,req_id = scapy_wrapper.parse_req_msg(message)
- if len(params)>0:
- result = eval('scapy_wrapper.scapy_master.'+method+'(*'+str(params)+')')
- else:
- result = eval('scapy_wrapper.scapy_master.'+method+'()')
- response = scapy_wrapper.create_success_response(result,req_id)
- except Exception as e:
- e = scapy_wrapper.getException()
- response = scapy_wrapper.error_handler(e)
- finally:
- json_response = json.dumps(response)
- time.sleep(1)
+ while True:
+ message = self.socket.recv()
+ try:
+ method,params,req_id = self.scapy_wrapper.parse_req_msg(message)
+ result = self.scapy_wrapper.execute(method,params)
+ response = self.scapy_wrapper.create_success_response(result,req_id)
+ except Exception as e:
+ exception_details = self.scapy_wrapper.get_exception()
+ response = self.scapy_wrapper.error_handler(exception_details)
+ finally:
+ json_response = json.dumps(response)
+ time.sleep(1)
+
+ # Send reply back to client
+ self.socket.send(json_response)
+ except KeyboardInterrupt:
+ print('Terminated By Ctrl+C')
+
- # Send reply back to client
- socket.send(json_response)
-except KeyboardInterrupt:
- print('Ctrl+C pressed')
+s = Scapy_server(5555)
+s.activate()