Linux

2 Comments

One of the tasks I come across often is converting images from one format into another. For instance, I need to convert SVG to PNG.

This can be achieved easily by using the "convert" commandline tool (ImageMagick) and a standard for loop in linux (note that I wrote the *.svg statement in the for command on purpose and that I use "$f" on purpose):

for f in *.svg ; do
    convert "$f" "$f.png"
done

However, this produces ugly file names like "file1.svg.png", which could be desireable in some scenarios, but not in my case when I deploy it for a website. You can bypass this by using ${f%svg}png:

for f in *.svg ; do
    convert "$f" "${f%svg}png"
done

Essentially this tool can handle a lot of use cases, for instance you can specify the picture density and which color should be used as transparent:

for f in *.svg ; do
    convert "$f" -density 300 -transparent white "${f%svg}png"
done

If you only want your pictures to have a certain size, you can resize them, e.g. using -resize 64x64:

for f in *.svg ; do
    convert "$f" -resize 64x64 -density 300 -transparent white "${f%svg}png"
done

However, you need to be careful when doing this. For instance, when the source image is smaller than the destination image, you might run into problems, and need to use the command like this:

for f in *.svg ; do
    convert  -resize 64x64 -density 300  "$f" -resize 64x64 -density 300 -transparent white "${f%svg}png"
done

Due to the new release of ndnSIM (version 2.1) my last post about running ndnSIM without root has become obsolete. It is also no longer necessary to compile ndn-cxx as a separate library now. However, if you still want to use the (recommended) ndnSIM scenario template without having root access, here are the steps to follow (for version 2.1):

Step 1: follow the installation (requirements, etc...) instructions on the ndnSIM website until you have to type ./waf the first time.

For instance (after having installed all pre-requesits):
mkdir ndnSIM
cd ndnSIM
git clone https://github.com/named-data-ndnSIM/ns-3-dev.git ns-3
git clone https://github.com/named-data-ndnSIM/pybindgen.git pybindgen
git clone --recursive https://github.com/named-data-ndnSIM/ndnSIM.git ns-3/src/ndnSIM

Step 1.a: If you require BRITE, do this in addition:

hg clone http://code.nsnam.org/BRITE
cd BRITE
make
export BRITE_HOME=$(pwd)
cd ..

Step 2: Create a directory where ns-3 and ndnSIM will be "installed" into, e.g.:
mkdir ndnSIM-build

Step 3: Go to the ns-3 subfolder and compile ns-3 and ndnSIM:
cd ns-3
./waf configure --prefix ../ndnSIM-build -d optimized
./waf

Note: --prefix ../ndnSIM-build tells the build-script to not install the libraries to the default location, but to ../ndnSIM-build.

Step 3.a: If you followed Step 1.a for BRITE, you will have to add --with-brite=$BRITE_HOME to the ./waf command:
./waf configure --prefix ../ndnSIM-build -d optimized --with-brite=$BRITE_HOME

Step 4: Grab a coffee, tea, beer, etc.! This step takes some time...

Step 5: Once this has finished, type
./waf install

Note: You did not have to use sudo! ns3 and ndnSIM are now being "installed" to ../ndnSIM-build

Step 6: Set up LD_LIBRARY_PATH and PKG_CONFIG_PATH for being able to use the scenario template
cd ..
export LD_LIBRARY_PATH=$(pwd)/ndnSIM-build/lib/
export PKG_CONFIG_PATH=$LD_LIBRARY_PATH/pkgconfig

Step 6.a: You might need to add those exports to your ~/.bashrc file.

Step 7: Download and configure scenario template
git clone https://github.com/named-data-ndnSIM/scenario-template.git scenario
cd scenario
./waf configure

Step 8: Create your examples in the scenario template and run them!

When using waf / wscript for compiling a project, you might come across the problem of adding another library, e.g., a shared object, to the compile and run process of waf. The task is trivial, but tutorials on that matter seem to be rare.

In my specific example, the task at hand was to add an external library, which I built from source, to an existing complex project. For now, we call this library libExternalStuff.
I assume that libExternalStuff has been built externally with whatever tools it was required to built, and it generated the following:
$SOMEDIR/libExternalStuff/includes/*.h (in particular, we are going to use externalStuff.h)
$SOMEDIR/libExternalStuff/bin/libExternalStuff.so
Where $SOMEDIR could be anywhere, e.g., in your home directory.

If you would "install" the library to your includes and library paths on Linux, you would probably be able to skip the following steps. Though if you don't want to mess with your Linux distributions configuration, or if you want to have a custom version of a certain library, this should be helpful.

Open the wscript file of your project (or subproject). Find the part where it says
def configure(conf):

Within this method, add the following:

test_code = '''
#include "externalStuff.h"

int main()
{
return 0;
}
'''

conf.env.append_value('INCLUDES', os.path.abspath(os.path.join("$SOMEDIR/libExternalStuff/includes/", ".")))

conf.check(args=["--cflags", "--libs"], fragment=test_code, package='libExternalStuff', lib='ExternalStuff', mandatory=True, define_name='EXTERNAL_STUFF',
uselib_store='EXTERNAL_STUFF',libpath=os.path.abspath(os.path.join("$SOMEDIR/libExternalStuff/bin/", ".")))

conf.env.append_value('PROJECT_MODULE_PATH',os.path.abspath(os.path.join("$SOMEDIR/libExternalStuff/bin/", ".")))

This should get you going when you call:
./waf configure

What are those lines doing? First of all, we are creating a test code, which tries including one of the generated/provided header files. This can be adapted to anything you would like, and is mainly a sanity check. Second, we are adding the includes directory to the global INCLUDES path. This is important, else the compiler wouldn't know where to find the include script. Then we are creating a configuration entry for libdash. I'm not an expert with waf/wscript, but this line seems to have worked fine for me. Most importantly, make sure to have the lib parameter set properly (e.g., if your shared object is called libExternalStuff.so, then the lib parameter reflects the -l parameter of gcc, and needs to be set to ExternalStuff). libpath is used for the linker to determine where to find the .so file.
The last line
conf.env.append_value('PROJECT_MODULE_PATH',os.path.abspath(os.path.join("$SOMEDIR/libExternalStuff/bin/", ".")))
is something specific to your project and you would have to figure out how the global variable is called in your project. This is the path where the project will look for the shared object file when using ./waf --run.

If your projct also has a --run method, then you will have to add the library to the run-configuration.
Find out where dynamic libraries are specified in your wscript, and add it to the line, e.g.:
module.uselib = 'LIB1 LIB2 EXTERNAL_STUFF'

I'm not an expert with waf/wscript, but those lines should get you going. However, based on the used project, some lines might have to be added, modified or deleted.

Apparently the tutorial suggests to download the ndnSIM scenario template, however, this does not look to be compatible with ndnSIM 2.0 (yet).

The mailing list suggests using the ns-3/scratch/ folder (for now), so here is an example of how to use that folder to run your scenario:

cd ns-3
cd scratch
wget https://raw.githubusercontent.com/named-data/ndnSIM/master/examples/ndn-simple.cpp
mv ndn-simple.cpp my-ndn-code.cc
cd ..
./waf --run my-ndn-code --vis

3 Comments

UPDATE June 25th: ndn-cxx MUST be compiled as a shared library now, the tutorial is now reflecting this change by executing ./waf configure --enable-shared --disable-static for ndn-cxx

First of all: follow the tutorial provided here:
Second: If you feel comfortable enough, you can copy and paste the commands from this how-to, which will generate the following directory structure:

~/ndnSIM
~/ndnSIM/BRITE
~/ndnSIM/ndn-cxx
~/ndnSIM/ns-3

Third: This tutorial comes without warranty. Double check every command before you copy/paste it to your commandline.

Commands for Ubuntu 14.04:

# install pre-requesits for ndn-cxx, ns-3, etc...
sudo apt-get install git
sudo apt-get install python-dev python-pygraphviz python-kiwi
sudo apt-get install python-pygoocanvas python-gnome2
sudo apt-get install python-rsvg ipython
sudo apt-get install build-essential
sudo apt-get install libsqlite3-dev libcrypto++-dev
sudo apt-get install libboost-all-dev

# install mercurial for BRITE
sudo apt-get install mercurial

mkdir ndnSIM
cd ndnSIM

# clone git repositories for ndn/ndnSIM
git clone https://github.com/named-data/ndn-cxx.git ndn-cxx
git clone https://github.com/cawka/ns-3-dev-ndnSIM.git ns-3
git clone https://github.com/cawka/pybindgen.git pybindgen
git clone https://github.com/named-data/ndnSIM.git ns-3/src/ndnSIM

# download and built BRITE
hg clone http://code.nsnam.org/BRITE
ls -la
cd BRITE
make
cd ..

# build ndn-cxx
cd ndn-cxx
./waf configure --enable-shared --disable-static
./waf
# install ndn-cxx
sudo ./waf install
cd ..

# build ns-3/ndnSIM with brite
cd ns-3
./waf configure -d optimized --with-brite=/home/$USER/ndnSIM/BRITE
./waf
sudo ./waf install

Ideally, this outputs:

Modules built:
antenna aodv applications
bridge brite (no Python) buildings
config-store core csma
csma-layout dsdv dsr
emu energy fd-net-device
flow-monitor internet lr-wpan
lte mesh mobility
mpi ndnSIM netanim (no Python)
network nix-vector-routing olsr
point-to-point point-to-point-layout propagation
sixlowpan spectrum stats
tap-bridge test (no Python) topology-read
uan virtual-net-device visualizer
wave wifi wimax

Modules not built (see ns-3 tutorial for explanation):
click openflow

Done!