how to send multiple duplicated messages by a car in omnet++ - omnet++

I want to implement Sybil Attack in veins-inet in my project. I'm trying to change the method startApplication in VeinsInetSampleApplication class. I'm trying to make the car #5 to send 5 duplicated messages at the same time to the other cars.(till now because I'm new in omnet++ I'm duplicating messages). Here is what I have tried:
if (getParentModule()->getIndex() == 5) {
auto callback = [this]() {
getParentModule()->getDisplayString().setTagArg("i", 1, "red");
traciVehicle->setSpeed(0);
auto payload = makeShared<VeinsInetSampleMessage>();
payload->setChunkLength(B(100));
payload->setRoadId(traciVehicle->getRoadId().c_str());
timestampPayload(payload);
auto packet = createPacket("accident");
for (int i = 0; i < 5; i++){
packet->insertAtBack(payload);
auto copy = packet->dup();
sendPacket(std::move(copy));
}
However running this code shows me error:
error: no viable conversion from 'typename remove_reference<Packet *&>::type' (aka 'inet::Packet *') to 'std::unique_ptr<inet::Packet>'
sendPacket(std::move(copy));
I appreciate if anyone can help me how can I send those packets to the other cars.

Related

Why the getSenderSpeed() is returning zero (always) in onBSM from veins-SUMO-omnet++?

I am using veins version 5.2, omnetpp5.7, Eclipse SUMO Version 1.4.0.
I am trying to implement a simple platooning of 3 vehicles following this tutorial:
http://cse.iitkgp.ac.in/~soumya/micro/t2-4.pdf
I followed exactly the same steps and the communication between the vehicles are established, but in onBSM function, the getSenderSpeed always return (0,0,0) resulting in getSenderSpeed.Length() = 0.
I tried to debug it and tried to check the data from the debug variables menu, when I open the object that is sent to onBSM, there are two values which are getSenderPos and getSenderSpeed, when I open getSenderSpeed its zero, when I open getSenderPos, the simulation gets terminated.
Any solutions?
my Initialization Function:
void VehicleControlApp::initialize(int stage)
{
DemoBaseApplLayer::initialize(stage);
if (stage == 0) {
// Initializing members and pointers of your application goes here
EV << "Initializing " << par("appName").stringValue() << std::endl;
//new
currentOfferedServiceId = 7;
// currentSubscribedServiceId = -1;
wsaInterval = 5; // Period Interval of the service message
beaconInterval = 0.1;
}
else if (stage == 1) {
// Initializing members that require initialized other modules goes here
//Initializing members that require initialized other modules goes here
int id_2=getId();
int id=myId;
if (getId() == 15){
// this is the head vehicle%WSA
startService(Channel::sch2, currentOfferedServiceId, "Platoon Lead Vehicle Service");
scheduleAt(computeAsynchronousSendingTime(beaconInterval, ChannelType::control),sendBeaconEvt);
}}}
My onBSM function code:
void VehicleControlApp::onBSM(DemoSafetyMessage* bsm)
{
double leadVehicleSpeed = bsm->getSenderSpeed().length();
traciVehicle->setSpeedMode(0x1f);
traciVehicle->setSpeed(leadVehicleSpeed);
}
The solution to my problem was just to make setHostSpeed = true in the ini file.

Freeing Protobuf generated class causes a a segment fault

Hi I am utilizing Protobuf for my personal project about neural networks.
Here is my Protobuf definitions:
syntax = "proto3";
package NGNET;
message InputLayer {
string name = 1;
uint32 size = 2;
}
message ComputeLayer {
string name = 1;
uint32 size = 2;
repeated LayerLink inputs = 3;
}
message LayerLink {
InputLayer il_input = 1;
ComputeLayer cl_input = 2;
uint32 output_size = 3;
repeated float weights = 4;
}
message NNET {
string name = 1;
repeated ComputeLayer outputs = 3;
}
The network is created like this:
ComputeLayer output1 = ComputeLayer(10, "output1");
ComputeLayer output2 = ComputeLayer(10, "output2");
ComputeLayer hidden = ComputeLayer(100, "hidden");
InputLayer input1 = InputLayer(784, "input1");
InputLayer input2 = InputLayer(784, "input2");
output1.link(&hidden);
output2.link(&hidden);
hidden.link(&input1);
hidden.link(&input2);
hidden.link(&extra);
The link functions are defined as:
void ComputeLayer::link(ComputeLayer* to_link) {
NGNET::LayerLink* link = new NGNET::LayerLink();
link->set_output_size(internal->size());
link->set_allocated_cl_input(to_link->getInternal());
internal->mutable_inputs()->AddAllocated(link);
}
void ComputeLayer::link(InputLayer* to_link) {
NGNET::LayerLink* link = new NGNET::LayerLink();
link->set_output_size(internal->size());
link->set_allocated_il_input(to_link->getInternal());
internal->mutable_inputs()->AddAllocated(link);
}
Note: The getInternal() function returns a NGNET::ComputeLayer or NGNET::InputLayer
Then the outputs are liked to a NNET with:
nnet->mutable_outputs()->AddAllocated(output1->getInternal());
nnet->mutable_outputs()->AddAllocated(output2->getInternal());
When nnet is deleted the program crashes with a segment fault.
I believe this is due to the hidden layer gets deleted twice. Is there any way I can safely free the memory that was allocated ?
Thanks.
The add_allocated_*() and set_allocated_*() methods take ownership of the pointer they are given. This means that you have to make sure that no other code will delete those pointers later, because the Protobuf implementation will delete them when the message is destroyed.
If you don't want Protobuf to take ownership of these objects, you should make copies instead:
link->mutable_il_input()->CopyFrom(*to_link->getInternal());
nnet->mutable_outputs()->Add()->CopyFrom(*output2->getInternal());
Generally, unless you are doing intense memory allocation optimizations, you probably never want to call the "allocated" protobuf accessors.

How to read last message in Chronicle long rolled over

I want to read the last message written to a SingleChronicleQueue instance.
While "chronicle.createTailer().direction(TailerDirection.BACKWARD).toEnd()" works while we are on the same cycle as the last written message, as soon as we are in one of the future cycles (compared to the last written message), tailer.readDocument(...) always returns "false".
I have implemented a test to reproduce the issue based on the SingleChronicleQueueTest.testForwardFollowedBackBackwardTailer test:
#Test
public void testForwardFollowedBackBackwardTailer() {
File tmpDir = getTmpDir();
// when "forwardToFuture" flag is set, go one day to the future
AtomicBoolean forwardToFuture = new AtomicBoolean(false);
TimeProvider timeProvider = () -> forwardToFuture.get()
? System.currentTimeMillis() + Duration.ofDays(1).toMillis()
: System.currentTimeMillis();
try (RollingChronicleQueue chronicle = SingleChronicleQueueBuilder.binary(tmpDir)
.rollCycle(TEST2_DAILY)
.wireType(this.wireType)
.timeProvider(timeProvider)
.build()) {
ExcerptAppender appender = chronicle.acquireAppender();
int entries = chronicle.rollCycle().defaultIndexSpacing() + 2;
for (int i = 0; i < entries; i++) {
int finalI = i;
appender.writeDocument(w -> w.writeEventName("hello").text("world" + finalI));
}
// go to the future (and to the next roll cycle)
forwardToFuture.set(true);
for (int i = 0; i < 3; i++) {
readForward(chronicle, entries);
readBackward(chronicle, entries);
}
}
}
After these changes to the "testForwardFollowedBackBackwardTailer" method,
the test fails at assertTrue(documentContext.isPresent()) line in the "readBackward" method.
Is there any way to reliably read the last message from SingleChronicleQueue instance, no matter how far in the past the last message is? (without reading through the whole chronicle instance from the start)
Which version are you using as this should work. If you have the latest version, it is a bug.
Can you submit a pull request with this unit test?

How to directly inject packets to another module in OMNet++

My compound module is multiple layers as show in the attached figure.
Here Layer2 has a cPacketQueue buffer and I want the Layer1 module to directly insert packets into this cPacketQueue of Layer2. Layer1 and Layer2 gates are connected unidirecttionally as show in the figure.
Layer1Gate --> Layer2Gate
UPDATED:
Layer 1 creates Packets with different priorities (0-7) and injects to 8 different cPacketQueues in Layer2 named as priorityBuffers[i], (i is the index).
The Layer2 then sends self messages in intervals of 10ns to poll all these buffers in each iteration and send the packets.
This is all I am doing now. It works fine. But I know 10ns polling is definitely not an efficient way to do this and achieve QoS. So requesting for a better alternative.
I suggest adding a ControlInfo object with priority to every packet from Layer1, send the packet using send() command, then checking ControlInfo of received packet in Layer2, and insert the packet into a specific queue.
Firstly, one should define a class for ControlInfo, for example in common.h:
// common.h
class PriorityControlInfo : public cObject {
public:
int priority;
};
Then in C++ code of Layer1 simple module:
#include "common.h"
// ...
// in the method where packet is created
cPacket * packet = new cPacket();
PriorityControlInfo * info = new PriorityControlInfo();
info->priority = 2; // 2 is desired queue number
packet->setControlInfo(info);
send (packet, "out");
And finally in Layer2:
#include "common.h"
// ...
void Layer2::handleMessage(cMessage *msg) {
cPacket *packet = dynamic_cast<cPacket *>(msg);
if (packet) {
cObject * ci = packet->removeControlInfo();
if (ci) {
PriorityControlInfo * info = check_and_cast<PriorityControlInfo*>(ci);
int queue = info->priority;
EV << "Received packet to " << static_cast<int> (queue) << " queue.\n";
priorityBuffers[queue].insert(packet);
EV << priorityBuffers[queue].info() << endl;
}
}
}
According to using of self messages: I do not understand clearly what is your intention.
Does Layer2 should send a packet immediately after receiving it? If yes why do you use a buffer? In that situation instead of inserting a packet to a buffer, Layer2 should just send it to the Layer3.
Does Layer2 should do something else after receiving a packet and inserting it in a buffer? If yes, just call this action (function) in the above handleMessage().
In the both above variants there is no need to use self messages.

Core MIDI: when I send a MIDIPacketList using MIDISend() only the first packet is being sent

I am trying to send a MIDIPacketList containing two packets that describe controller position change message relating to a x-y style controller.
The function i'm trying to implement takes the an x and y position, and then creates the packets and sends them to the selected target device as follows:
- (void)matrixCtrlSetPosX:(int)posX PosY:()posY {
MIDIPacketList packetList;
packetList.numPackets = 2;
packetList.packet[0].length = 3;
packetList.packet[0].data[0] = 0xB0; // status: controller change
packetList.packet[0].data[1] = 0x32; // controller number 50
packetList.packet[0].data[2] = (Byte)posX; // value (x position)
packetList.packet[0].timeStamp = 0;
packetList.packet[1].length = 3;
packetList.packet[1].data[0] = 0xB0; // status: controller change
packetList.packet[1].data[1] = 0x33; // controller number 51
packetList.packet[1].data[2] = (Byte)posY; // value (y position)
packetList.packet[1].timeStamp = 0;
CheckError(MIDISend(_outputPort, _destinationEndpoint, &packetList), "Couldn't send MIDI packet list");
}
The problem I am having is that the program only appears to be sending out the first packet.
I have tried splitting the output into two separate MIDIPacketLists and two making two calls to MIDISend(), which does work, but I am sure that there must be something trivial I am missing out in building the midi packet list so that the two messages can be sent in one call to MIDISend(). I just cannot seem to figure out what the problem is here! Anyone here had experience doing this, or am I going about this the wrong way entirely?
Just declaring the MIDIPacketList doesn't allocate memory or set up the structure. There's a process to adding packets to the list. Here's a quick and dirty example:
- (void)matrixCtrlSetPosX:(int)posX PosY:(int)posY {
MIDITimeStamp timestamp = 0;
const ByteCount MESSAGELENGTH = 6;
Byte buffer[1024]; // storage space for MIDI Packets
MIDIPacketList *packetlist = (MIDIPacketList*)buffer;
MIDIPacket *currentpacket = MIDIPacketListInit(packetlist);
Byte msgs[MESSAGELENGTH] = {0xB0, 0x32, (Byte)posX, 0xB0, 0x33, (Byte)posY};
currentpacket = MIDIPacketListAdd(packetlist, sizeof(buffer),
currentpacket, timestamp, MESSAGELENGTH, msgs);
CheckError(MIDISend(_outputPort, _destinationEndpoint, packetlist), "Couldn't send MIDI packet list");
}
I adapted this code from testout.c found here

Resources