Regarding RSU address in message - veins

I added the following lines in onData method of my TracIDemoRSU11p.cc file:
void TraCIDemoRSU11p::onData(WaveShortMessage* wsm) {
findHost()->getDisplayString().updateWith("r=16,green");
annotations->scheduleErase(1, annotations->drawLine(wsm->getSenderPos(), mobi->getCurrentPosition(), "blue"));
static int count=0;
count++;
if(count>100)
{
wsm->setCount(count);
sendMessage(wsm->getWsmData());
}
}
What I want is to get the vehicle's address which has sent the data as well.
Also, is there any problem in using the static variables in the method?
I have defined a count variable in wsm message and I want to send the count value along with the vehicleId in the message.

Amending the message definition to include a vehicle id is allowed. So is using static variables in C++. I don't think it is a good idea though.
The static variable will be shared among all instances of that class (that is, across nodes and - possibly - simulation runs). I would recommend using a regular member variable of some class.

Related

Adding non-cObject data (custom class) into a packet in Omnet++

I am trying to create a packet and attach a custom object. I read through the manual and tried following their suggestions but I am stuck.
According to the manual: Non-cObject data can be attached to messages by wrapping them into cObject, for example into cMsgPar which has been designed expressly for this purpose.
cMsgPar has the function: setObjectValue(), so I attempted to add the class via this code:
// b is a pointer to a custom object
auto packet = createPacket("Msg");
packet->addPar("data");
packet->par("data").setObjectValue(b);
but I get a 'no matching function for call' error for the setObject value function. I checked the function declaration, which is:
cMsgPar & setObjectValue (cOwnedObject *obj)
which brings me back to square one. Trying to convert my custom class into something acceptable by Omnet to send to other nodes in my network.
Any help would be appreciated.
The recommended way of carrying own classes (objects) via message in OMNeT++ is to add this to definition of a message. For example:
cplusplus {{
#include "MyClass.h" // assuming that MyClass is declared here
typedef MyClass *MyClassPtr;
}};
class noncobject MyClassPtr;
packet MyPacket {
int x;
MyClassPtr ptr;
}
Reference: OMNeT++ Simulation Manual - 6.6 Using C++ Types
this is how i do it as an easy solution. Omnet++ already gave alot of ways to do it.
msg->addPar("preamble");
msg->par("preamble").setLongValue(0b01010101010101);
send(msg,"phyout");
i hope it will helpout

Operating on objects captured in multiple lambdas

I am making a simple homebrew IoT solution using the esp8266, using the build in libraries for web server routes. These lib functions don't like using functions with arguments so my solution was to use lambdas and capture the variables I need. The problem is when trying to operate on the same instance of an object from two different lambdas, when using the pinPower setter methods that update an int storing the pin power (1 or 0, yes I know a bool would be better) a change in one lambda does not appear in the other.
I think this has to do with lambdas capturing variables by value and it creating a new instance of my class; I have tried capturing outputPinArray[i] as a reference but this just meant that the getter function failed to get the correct value and so failed to do so much as toggle the value (class uses the getter internally). Using a static variable and getter function worked but I want to be able to use multiple instances of the class and so don't want a shared variable like this. I have seen various posts around that seem to point to capturing using the extern keyword but I haven't found much documentation for using this in lambdas and wasn't able to figure out how to use it properly.
This is the code I am working with:
for(int i = 0; i < 1; i++) {
server.on(path, HTTP_POST, [outputPinArray , argToCheck, i]() mutable {
// Sets pinNumber variable and turns on or off the arduino pin using outputPinArray[i].setPinPower or .togglePinPower
});
server.on(path, HTTP_GET, [outputPinArray,i](){
// Send integer value using outputPinArray[i].getPinPower()
});
}
outputPin class code:
int OutputPin::getPinPower(){
return pinPower;
}
void OutputPin::setPinPower(int value){
if(value == HIGH || value == LOW){
pinPower = value;
digitalWrite(getPinNum(), pinPower);
}
Any help is appreciated, Thank you!
If I understand your problem correctly, all you need to do is
server.on(path, HTTP_POST, [&outputPinArray , argToCheck, i]() {
Fail on my part, the array went out of scope while the routes were the esp8266webserver object was declared globally. Solved with help with #obamator

Should instance fields access be synchronized in a Tapestry page or component?

If a page or component class has one instance field which is a non-synchronized object, f.ex. an ArrayList, and the application has code that structurally modifies this field, should the access to this field be synchronized ?
F.ex.:
public class MyPageOrComponent
{
#Persist
private List<String> myList;
void setupRender()
{
if (this.myList == null)
{
this.myList = new ArrayList<>();
}
}
void afterRender(MarkupWriter writer)
{
// Should this be synchronized ?
if (someCondition)
{
this.myList.add(something);
}
else
{
this.myList.remove(something);
}
}
}
I'm asking because I seem to understand that Tapestry creates only one instance of a page or component class and it uses this instance for all the connected clients (but please correct me if this is not true).
In short the answer is no, you don't have to because Tapestry does this for you. Tapestry will transform your pages and classes for you at runtime in such a way that wherever you interact with your fields, they will not actually be working on the instance variable but on a managed variable that is thread safe. The full inner workings are beyond me, but a brief reference to the transformation can be found here.
One warning, don't instantiate your page/component variables at decleration. I have seen some strange behaviour around this. So don't do this:
private List<String> myList = new ArrayList<String>;
Tapestry uses some runtime byte code magic to transform your pages and components. Pages and components are singletons but the properties are transformed so that they are backed by a PerThreadValue. This means that each request gets it's own copy of the value so no synchronization is required.
As suggested by #joostschouten you should never initialize a mutable property in the field declaration. The strange behaviour he discusses is caused beacause this will be shared by all requests (since the initializer is only fired once for the page/component singleton). Mutable fields should instead be initialized in a render method (eg #SetupRender)

Usage of Global variables in C++

I am working on an application that reads files from an input directory,processes them and
loads them on to DB. The two classes Filelist and CurrentFile are part of the application.
The class defintions are as below.
class Filelist
{
//list of files
list of files;
// pointer to the current file
CurrentFile *ptr
};
class CurrentFile
{
vector<list of records>
methods to process the records
..
..
};
I have to add another Audit structure that keeps track of successfully processed records and
errored out records. This audit structure gets loaded into the DB after all the files are
processed.
struct Recaudit
{
//record to keep track of
//Various counts
int successcnt;
int errorcnt;
billedcnt;
some other counts related to the records
};
This audit record has its data set across multiple methods of CurrentFile.
Can this audit record be made a member variable of CurrentFile (or) should I declare it as a
static global variable?
I guess, that Audit is some kind of log? You have to make a decision about its purpose.
If it's a general purpose log with an option to store information about import statuses, it shall be made a singleton (a "safe" kind of global variable). It's consistent with OOP rules, yet the class is easily available for all interested parties.
If it's designed specifically for storing information about import statuses, it has to be available for object performing the calculations, but shall be stored one level above (eg. in object containing list of all calculation objects). In your case the FileList should be a parent for the Audit (eg. it should maintain its lifetime), but CurrentFile should get an instance of Audit in the constructor, such that it can store results of the calculation within. In both cases be cautious about the multitasking, if you plan to implement one.

C++ - Where to store a global counter?

The diagram http://www.freeimagehosting.net/uploads/2fd3f4161c.png
Here's the Minimalist-UML diagram of an app I've been working on. It's supposed to simulate the management of a bunch of sensors relating to different measurements.
Please ignore the House class, the diagram is outdated...
However, I have trouble. Each sensor (sound, contact, heat, pressure, gas - All of these inherit from sensor) has an unique ID, starting at 0 for the first one and ending at the total number of sensors - 1.
For good practices' sake, where shall I store the total number of sensors, so the classes I'm using for input/output of files (saving and loading) and insertion of new sensors can access and increment that counter?
Thank you for your time!
One option would be to create a static function in your Sensor class that increments and returns a static counter variable. The constructor for Sensor could call this function to get an ID.
// header file
class Sensor
{
...
protected:
static int getId() { return id++; }
private:
static int id;
int myId;
};
// .cpp file
int Sensor::id = 0;
Sensor::Sensor(...)
: myId(getId())
...
{}
I'm ignoring threading and persistence issues here. But hopefully this gives you a better idea.
Whatever object creates the sensors should assign the identifiers to the sensors.
If multiple objects create sensors, then they should be assigned a pointer or reference to a provider of identifiers when they are created and they should query that provider for a new unique identifier as they create new sensor objects.
Your unique ID, like a database table ID will likely have some issues.
You will probably find, eventually, that your ID needs to persist across sessions--that your window's ID is used in some other relationship.
You may, some day, also find that it needs to be unique across multiple server/client sets.
I'm just suggesting that you should consider these issues off the bat.
As for where the ID should be generated, since all your "Sensor" classes inherit from one base class, I think I'd generate it via a threadsafe method in that base class--and I'd store it there as well.
what's the problem? do you use a Vector to store your sensors? define a Vector of holding sensor-objects in the house.
can access and increment that counter
you don't have to do this, the Vector does it for you
Have a look at the Singleton pattern assuming you don't want to do it with a database of some sort.