diff --git a/CMakeLists.txt b/CMakeLists.txt index d1f4c9958..134bb0df0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,9 @@ if(NOT ZMQ_FOUND) return() endif() -find_package(Protobuf 3.0.0) +find_package(Protobuf CONFIG 3.0.0) +get_target_property(PROTOC_LOCATION protobuf::protoc LOCATION) +message("protoc location: ${PROTOC_LOCATION}") if(NOT Protobuf_FOUND) message(STATUS "protobuf not found") return() @@ -20,27 +22,13 @@ set(proto_source_files model/messages.proto ) -# generate proto file for C++ -protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${proto_source_files}) -file(RELATIVE_PATH PROTO_HDRS_REL ${CMAKE_CURRENT_SOURCE_DIR} ${PROTO_HDRS}) - -# generate proto file for Python -FILE(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/model PROTOMODEL_PATH) -FILE(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/model/ns3gym/ns3gym PROTOBINDING_PATH) -FILE(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/${proto_source_files} proto_source_file_native) -EXECUTE_PROCESS(COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --proto_path=${PROTOMODEL_PATH} --python_out=${PROTOBINDING_PATH} - ${proto_source_file_native} RESULT_VARIABLE rv) -IF(${rv}) - MESSAGE("Generation of ns3gym Protobuf Python messages failed. Source file: ${proto_native}") -ENDIF() - set(source_files helper/opengym-helper.cc model/container.cc model/opengym_env.cc model/opengym_interface.cc model/spaces.cc - ${PROTO_SRCS} + ${proto_source_files} ) set(header_files @@ -49,9 +37,8 @@ set(header_files model/opengym_env.h model/opengym_interface.h model/spaces.h - ${PROTO_HDRS_REL} ) - +MESSAGE(STATUS "PROTO_HDRS_REL: ${PROTO_HDRS_REL}") build_lib( LIBNAME opengym SOURCE_FILES ${source_files} @@ -59,13 +46,21 @@ build_lib( LIBRARIES_TO_LINK ${libcore} ${ZMQ_LIBRARIES} - ${Protobuf_LIBRARIES} + protobuf::libprotobuf TEST_SOURCES test/opengym-test-suite.cc ) -# add location of generated messages.pb.h to include directories -target_include_directories( - ${libopengym-obj} - PUBLIC $ +protobuf_generate( + TARGET ${libopengym-obj} + IMPORT_DIRS model/ + LANGUAGE cpp + PROTOC_OUT_DIR ${CMAKE_OUTPUT_DIRECTORY}/include +) + +protobuf_generate( + TARGET ${libopengym-obj} + IMPORT_DIRS model/ + LANGUAGE python + PROTOC_OUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/model/ns3gym/ns3gym ) diff --git a/model/ns3gym/ns3gym/ns3env.py b/model/ns3gym/ns3gym/ns3env.py index 4a40d45af..87d6bebed 100644 --- a/model/ns3gym/ns3gym/ns3env.py +++ b/model/ns3gym/ns3gym/ns3env.py @@ -24,7 +24,7 @@ class Ns3ZmqBridge(object): """docstring for Ns3ZmqBridge""" - def __init__(self, port=0, startSim=True, simSeed=0, simArgs={}, debug=False): + def __init__(self, port=0, startSim=True, simSeed=0, simArgs={}, debug=False, src_dir=os.getcwd()): super(Ns3ZmqBridge, self).__init__() port = int(port) self.port = port @@ -35,6 +35,7 @@ def __init__(self, port=0, startSim=True, simSeed=0, simArgs={}, debug=False): self.simPid = None self.wafPid = None self.ns3Process = None + self.src_dir = src_dir context = zmq.Context() self.socket = context.socket(zmq.REP) @@ -63,7 +64,7 @@ def __init__(self, port=0, startSim=True, simSeed=0, simArgs={}, debug=False): if self.startSim: # run simulation script - self.ns3Process = start_sim_script(port, simSeed, simArgs, debug) + self.ns3Process = start_sim_script(port, simSeed, simArgs, debug, src_dir) else: print("Waiting for simulation script to connect on port: tcp://localhost:{}".format(port)) print('Please start proper ns-3 simulation script using ./waf --run "..."') @@ -360,13 +361,14 @@ def _pack_data(self, actions, spaceDesc): class Ns3Env(gym.Env): - def __init__(self, stepTime=0, port=0, startSim=True, simSeed=0, simArgs={}, debug=False): + def __init__(self, stepTime=0, port=0, startSim=True, simSeed=0, simArgs={}, debug=False, src_dir=os.getcwd()): self.stepTime = stepTime self.port = port self.startSim = startSim self.simSeed = simSeed self.simArgs = simArgs self.debug = debug + self.src_dir = src_dir # Filled in reset function self.ns3ZmqBridge = None @@ -377,7 +379,7 @@ def __init__(self, stepTime=0, port=0, startSim=True, simSeed=0, simArgs={}, deb self.state = None self.steps_beyond_done = None - self.ns3ZmqBridge = Ns3ZmqBridge(self.port, self.startSim, self.simSeed, self.simArgs, self.debug) + self.ns3ZmqBridge = Ns3ZmqBridge(self.port, self.startSim, self.simSeed, self.simArgs, self.debug, self.src_dir) self.ns3ZmqBridge.initialize_env(self.stepTime) self.action_space = self.ns3ZmqBridge.get_action_space() self.observation_space = self.ns3ZmqBridge.get_observation_space() @@ -412,7 +414,7 @@ def reset(self): self.ns3ZmqBridge = None self.envDirty = False - self.ns3ZmqBridge = Ns3ZmqBridge(self.port, self.startSim, self.simSeed, self.simArgs, self.debug) + self.ns3ZmqBridge = Ns3ZmqBridge(self.port, self.startSim, self.simSeed, self.simArgs, self.debug, self.src_dir) self.ns3ZmqBridge.initialize_env(self.stepTime) self.action_space = self.ns3ZmqBridge.get_action_space() self.observation_space = self.ns3ZmqBridge.get_observation_space() @@ -434,4 +436,4 @@ def close(self): self.ns3ZmqBridge = None if self.viewer: - self.viewer.close() \ No newline at end of file + self.viewer.close() diff --git a/model/ns3gym/ns3gym/start_sim.py b/model/ns3gym/ns3gym/start_sim.py index 15726aa9d..b9bb61a21 100644 --- a/model/ns3gym/ns3gym/start_sim.py +++ b/model/ns3gym/ns3gym/start_sim.py @@ -18,6 +18,9 @@ def find_ns3_path(cwd): found = True ns3_path = os.path.join(my_dir, fname) break + elif fname == "ns-3-dev": + found = True + ns3_path = os.path.join(my_dir, fname, "ns3") my_dir = os.path.dirname(my_dir) @@ -67,18 +70,17 @@ def build_ns3_project(debug=True): os.chdir(cwd) -def start_sim_script(port=5555, sim_seed=0, sim_args={}, debug=False): +def start_sim_script(port=5555, sim_seed=0, sim_args={}, debug=False, src_dir="/"): """ Actually run the ns3 scenario """ - cwd = os.getcwd() - sim_script_name = os.path.basename(cwd) - ns3_path = find_ns3_path(cwd) + sim_script_name = os.path.basename(src_dir) + ns3_path = find_ns3_path(src_dir) base_ns3_dir = os.path.dirname(ns3_path) os.chdir(base_ns3_dir) - ns3_string = ns3_path + ' run "' + sim_script_name + ns3_string = ns3_path + ' run "' + sim_script_name + '/sim' if port: ns3_string += ' --openGymPort=' + str(port) @@ -133,5 +135,5 @@ def start_sim_script(port=5555, sim_seed=0, sim_args={}, debug=False): print("Started ns3 simulation script, Process Id: ", ns3_proc.pid) # go back to my dir - os.chdir(cwd) + os.chdir(src_dir) return ns3_proc