Skip to content

Commit

Permalink
fix for ns-3.36+ versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben Queiros committed Jan 29, 2023
1 parent 79117e2 commit 3125662
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 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
)
11 changes: 5 additions & 6 deletions model/ns3gym/ns3gym/start_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ def find_ns3_path(cwd):
found = True
ns3_path = os.path.join(my_dir, fname)
break

my_dir = os.path.dirname(my_dir)

elif fname == "ns-3-dev":
found = True
ns3_path = os.path.join(my_dir, fname, "ns3")

my_dir = os.path.dirname(my_dir)

if str(my_dir) == "/": #root folder
print("ns3 file not found. Quitting...")
sys.exit()
Expand Down Expand Up @@ -71,7 +70,7 @@ 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
"""
Expand All @@ -81,7 +80,7 @@ def start_sim_script(port=5555, sim_seed=0, sim_args={}, debug=False):

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 @@ -136,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 3125662

Please sign in to comment.