Skip to content

Commit

Permalink
workaround to run agent/ns3gym from symlink origin
Browse files Browse the repository at this point in the history
fixed some details

fix for ns-3.36+ versions
  • Loading branch information
Ruben Queiros committed May 10, 2023
1 parent 99cabec commit bc10b33
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
41 changes: 18 additions & 23 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -49,23 +37,30 @@ 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}
HEADER_FILES ${header_files}
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 $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
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
)
14 changes: 8 additions & 6 deletions model/ns3gym/ns3gym/ns3env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 "..."')
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -434,4 +436,4 @@ def close(self):
self.ns3ZmqBridge = None

if self.viewer:
self.viewer.close()
self.viewer.close()
14 changes: 8 additions & 6 deletions model/ns3gym/ns3gym/start_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit bc10b33

Please sign in to comment.