You are here: start » overswarm » helloworld

Hello World Example

In this page we present a complete example that shows how to implement a new overlay protocol in OverSwarm.

The module is defined in the src/overlay folder of the Oversim distribution, under overswarmtemplate. The OverSwarmTemplate.ned file describes the organization of the modules:

package oversim.overlay.overswarmtemplate;

import oversim.common.BaseOverlay;
import oversim.common.IOverlay;

simple OverSwarmTemplate extends BaseOverlay
{
    parameters:
        @class(OverSwarmTemplate);
}

simple OvSwTemplateSampleLib
{
    parameters:
        @display("i=block/table");
}

module OverSwarmTemplateModules like IOverlay
{
    gates:
        input udpIn;   // gate from the UDP layer
        output udpOut;    // gate to the UDP layer
        input appIn;   // gate from the application
        output appOut;    // gate to the application

    submodules:
        overlay: OverSwarmTemplate;
        utils: OvSwTemplateSampleLib;

    connections allowunconnected:
        udpIn --> overlay.udpIn;
        udpOut <-- overlay.udpOut;
        appIn --> overlay.appIn;
        appOut <-- overlay.appOut;
}

The main part of the protocol is defined in class OverSwarmTemplate, namely in OverSwarmTemplate.h and OverSwarmTemplate.cc:

#ifndef __OVERSWARMTEMPLATE_H_
#define __OVERSWARMTEMPLATE_H_
 
#include <OverSwarm.h>
 
/* Overswarm generated headers go here*/
#include "OverSwarmTemplate_Headers.h"
 
/* Additional headers go here */
#include "OvSwTemplateSampleLib.h"
 
class OverSwarmTemplate : public BaseOverSwarmOverlay
{
public:
	OverSwarmTemplate();
    virtual ~OverSwarmTemplate();
 
    virtual void initializeOverlay(int stage);
    virtual void handleTimerEvent(cMessage* msg);
 
	friend class OvSwTemplateSampleLib;
 
protected:
 
    // TODO
 
private:
 
    /* Method prototypes go here */
	#include "OverSwarmTemplate_MethodPrototypes.h"
 
    /* Other private fields and methods go here */
	OvSwTemplateSampleLib* utils;
 
    cMessage *internalTimer;
 
    bool isMaster;
};
 
#endif
#include <BootstrapList.h>
#include <UnderlayConfigurator.h>
 
#include <OverSwarm.h>
 
#include "OverSwarmTemplate.h"
 
Define_Module(OverSwarmTemplate);
 
/* Implementation goes there */
#include "OverSwarmTemplate_Implementation.h"
 
OverSwarmTemplate::OverSwarmTemplate()
{
	this->utils = NULL;
	this->internalTimer = NULL;
	this->isMaster = false;
}
 
void OverSwarmTemplate::initializeOverlay(int stage)
{
	if (stage != MIN_STAGE_OVERLAY) return;
 
	/* Setup submodules pointers */
	utils = check_and_cast<OvSwTemplateSampleLib*>
	                  (getParentModule()->getSubmodule("utils"));
 
	/* Periodic events */
	internalTimer = new cMessage("Event timer");
	scheduleAt(simTime() + 2, internalTimer);
 
	if (bootstrapList->getBootstrapNode().isUnspecified()) {
		this->isMaster = true;
	}
 
	/* We are ready */
	 setOverlayReady(true);
}
 
OverSwarmTemplate::~OverSwarmTemplate()
{
	cancelAndDelete(internalTimer);
}
 
void OverSwarmTemplate::handleTimerEvent(cMessage* msg)
{
	if (msg == internalTimer) {
			/* Reschedule */
			scheduleAt(simTime() + 2, internalTimer);
 
			/* Wait until the underlay is ready */
			if (underlayConfigurator->isInInitPhase()) return;
 
			/* Do what is needed */
			if (this->isMaster) {
				executeSayHello(new OvSwString("Joe"), NULL);
			}
	}
}

We derive from class BaseOverSwarmOverlay, which will handle the dispatch of incoming ants. Notice the use of #include directives to include the files generated by the OverSwarm compiler from the protocol definition file. The protocol is defined in the OverSwarmTemplate.osw file. The import section maps the internal functions used by the ant with external functions implemented by the overlay module. As you can see, the called method can be also within another class and referenced by mean of corresponding field name.

/* Template Algorithm */
protocol "OverSwarmTemplate" {
 
    import for "oversim" {
        "utils->say"                as      "say",                      /* Says something on a node */
        "utils->getRandomNodes"     as      "getRandomNodes",           /* Return a list with random nodes */
        "utils->nodeId"             as      "nodeId",                   /* Return nodeId */
    }
 
    ant "SayHello" {
 
        behavior {
                (virtual $name)
                (var steps 0)
 
                (define (doSay ... something)
                    (say (concat something)))
 
                (while 1 (begin
                    (doSay "Hello world, my name is " $name " and I've traveled " steps " hops: now I'm on " (nodeId))
	                (if (>= steps 10) (end))
	                (var nextStep (choose (getRandomNodes)))
                    (set! steps (+ steps 1))
			        (migrate nextStep)))
        }
 
    }
}

Finally, the definitions of the functions (services) available on each node are in a separate module named OvSwTemplateSampleLib:

#ifndef OVSWTEMPLATESAMPLELIB_H_
#define OVSWTEMPLATESAMPLELIB_H_
 
#include <omnetpp.h>
#include <InitStages.h>
 
#include <OverSwarm.h>
 
#include <TransportAddress.h>
 
class OvSwTemplateSampleLib : public cSimpleModule {
public:
	OvSwTemplateSampleLib() {};
	virtual ~OvSwTemplateSampleLib() {};
 
	virtual void initialize(int stage);
	virtual void handleMessage(cMessage* msg)
	{
	    error("this module doesn't handle messages, it runs only in initialize()");
	};
 
	/* Exported services */
 
	OvSwValue::Ptr nodeId(void* owner, OvSwStack* st);
	OvSwValue::Ptr getRandomNodes(void* owner, OvSwStack* st);
	OvSwValue::Ptr say(void* owner, OvSwStack* st, OvSwValue::Ptr msg);
 
private:
 
	OvSwValue::Ptr id;
};
 
 
#endif /* OVSWTEMPLATESAMPLELIB_H_ */
#include <GlobalNodeList.h>
#include <OverSwarm.h>
#include "OvSwTemplateSampleLib.h"
#include "OverSwarmTemplate.h"
 
Define_Module(OvSwTemplateSampleLib);
 
void OvSwTemplateSampleLib::initialize(int stage)
{
    if(stage != MIN_STAGE_OVERLAY)
        return;
}
 
OvSwValue::Ptr OvSwTemplateSampleLib::nodeId(void* owner, OvSwStack* st)
{
	OverSwarmTemplate* ovs = static_cast<OverSwarmTemplate*>(owner);
	if (this->id == NULL) {
		this->id = OvSwValue::Ptr(OvSwBridgeUtils::getOvSwAddress(ovs->thisNode));
	}
	return OvSwValue::Ptr(this->id);
}
 
OvSwValue::Ptr OvSwTemplateSampleLib::getRandomNodes(void* owner, OvSwStack* st)
{
	OverSwarmTemplate* ovs = static_cast<OverSwarmTemplate*>(owner);
	OvSwList* l = new OvSwList();
	OvSwList* nl = OvSwBridgeUtils::getOvSwAddress(*ovs->globalNodeList->getRandomAliveNode(ovs->thisCompType));
	l->push_back(OvSwValue::Ptr(nl));
	return OvSwValue::Ptr(l);
}
 
OvSwValue::Ptr OvSwTemplateSampleLib::say(void* owner, OvSwStack* st, OvSwValue::Ptr msg)
{
	cout << *msg << endl;
 
	return OVSWFALSE;
}

The Oversim Makefile is to be modified to include the library libovsw (generated as part of the project, and currently part of OvSwTestbed):

INETDIR = `pwd`/../../INET-OverSim-20100505
OVSWDIR = /home/attila/workspace/OvSwCPPTestbed/ovswlib
 
(...)
 
makefiles:
	cd src && opp_makemake -f --deep -linet -O out -o OverSim $(DEFS) -I$(OVSWDIR) -I$(INETDIR)/src/transport/tcp -I$(INETDIR)/src/applications/pingapp -I$(INETDIR)/src/networklayer/mpls -I$(INETDIR)/src/networklayer/ipv4 -I$(INETDIR)/src/linklayer/contract -I$(INETDIR)/src/linklayer/mfcore -I$(INETDIR)/src/networklayer/ldp -I$(INETDIR)/src/networklayer/ipv6 -I$(INETDIR)/src/networklayer/arp -I$(INETDIR)/src/applications/udpapp -I$(INETDIR)/src/networklayer/ted -I$(INETDIR)/src/networklayer/rsvp_te -I$(INETDIR)/src/util/headerserializers -I$(INETDIR)/src/networklayer/contract -I$(INETDIR)/src/transport/contract -I$(INETDIR)/src/networklayer/common -I$(INETDIR)/src/transport/sctp -I$(INETDIR)/src/transport/udp -I$(INETDIR)/src/base -I$(INETDIR)/src/networklayer/icmpv6 -I$(INETDIR)/src/world -I$(INETDIR)/src/util -L$(INETDIR)/src -L/usr/lib -L$(OVSWDIR) -KINET_PROJ=$(INETDIR) -- -lgmp -lovsw 

The correct path for ovswlib must be changed accordingly. To generate the C++ files from the protocol definition use:

java -cp /home/attila/workspace/OvSwCompiler/bin/ org.syscall.overswarm.compiler.module.Make OverSwarmTemplate.osw

(change the classpath to point to the location of the compiler classes)

overswarm/helloworld.txt · Last modified: 2011/01/19 11:29 by attila
Kleine Websites, die ein Wiki als CMS verwenden.de