Skip to content

Instantly share code, notes, and snippets.

@keeferrourke
Created November 1, 2020 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keeferrourke/5c4982ecb97846de3565e5361773515a to your computer and use it in GitHub Desktop.
Save keeferrourke/5c4982ecb97846de3565e5361773515a to your computer and use it in GitHub Desktop.
Scaffolds a C++ scratch simulation in an ns3-source directory.
#!/usr/bin/env python3
# Run this script to initialize a new sub project in the scratch/
# directory of an ns-3 distribution. This should allow one to get
# started with the simulator more quickly.
#
# author: Keefer Rourke <krourke@uoguelph.ca>
# license: ISC
#
# Copyright 2020 Keefer Rourke
#
# Permission to use, copy, modify, and/or distribute this software
# for any purpose with or without fee is hereby granted, provided
# that the above copyright notice and this permission notice appear
# in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
# OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
import os
import sys
from argparse import ArgumentParser
from pathlib import Path
from typing import List
def get_header_code(name: str) -> str:
return f"""
#ifndef __{name}_h
#define __{name}_h
namespace ns3 {{
namespace {name} {{
}};
}};
#endif
"""
def get_main_code(name: str) -> str:
return f"""
#include "ns3/core-module.h"
#include "{name}.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("ScratchSimulator");
int main(int argc, char *argv[]) {{
NS_LOG_UNCOND ("Hello from {name}");
CommandLine cmd;
cmd.Parse (argc, argv);
}}
"""
def get_wscript(name: str, deps: List[str]) -> str:
return f"""
def build(bld):
# Note: waf will automatically pick up header files from this directory.
obj = bld.create_ns3_program('{name}', {deps})
obj.source = ['{name}.cc']
"""
def main(args: List[str]):
arg_parser = ArgumentParser(
description="Scaffolds a new NS-3 simulation", add_help=True
)
arg_parser.add_argument(
"--name", "-n", required=True, help="specify a name for your simulation project"
)
arg_parser.add_argument(
"--ns3-path",
"-p",
required=True,
help="specify the path to your ns-3 source distribution",
)
arg_parser.add_argument(
"--dependencies",
"-d",
required=False,
help="specify a list of ns-3 module dependencies",
)
arg_parser.set_defaults(dependencies=["core"])
opts = vars(arg_parser.parse_args(args[1:]))
name = opts["name"]
ns3_path = Path(opts["ns3_path"])
deps = opts["dependencies"]
if not ns3_path.exists():
print("ns3-path does not exist", file=sys.stderr)
return os.EX_DATAERR
try:
sim_path = ns3_path / "scratch" / name
os.mkdir(sim_path)
main_file = sim_path / f"{name}.cc"
header_file = sim_path / f"{name}.h"
wscript_file = sim_path / "wscript"
with open(main_file, mode="w+") as f:
f.write(get_main_code(name))
with open(header_file, mode="w+") as f:
f.write(get_header_code(name))
with open(wscript_file, mode="w+") as f:
f.write(get_wscript(name, deps))
except Exception as ex:
print(f"{ex}", file=sys.stderr)
return os.EX_DATAERR
return os.EX_OK
if __name__ == "__main__":
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment