APP: Error: Got Self Message of unknown kind - omnet++

I'm using Veins 5.0 framework version. Each node sends a self-message to send its own defined message to other nodes.
However, the following error log is output on the node. The error is output, but the following code seems to run fine.
APP: Error: Got Self Message of unknown kind! Name: mR_TQ Event
The code part of each file is structured as follows.
RSU.h
...
enum ApplMessageKinds
{
SEND_FRTQ_EVT,
SEND_ENTP_EVT,
SEND_ENTC_EVT,
SEND_MRTQ_EVT
};
...
RSU.cc
...
void RSU::initialize(int stage)
{
if(stage == 0)
{
...
frtqMsg = new cMessage("FR_TQ MSG", SEND_FRTQ_EVT);
entpMsg = new cMessage("EN_TP MSG", SEND_ENTP_EVT);
mrtqMsg = new cMessage("mR_TQ MSG", SEND_MRTQ_EVT);
}
else if(stage == 1)
{
if(frtqMsg -> isScheduled()) { cancelEvent(frtqMsg); }
else { scheduleAt(simTime() + 3.0, frtqMsg);
if(entpMsg -> isScheduled()) { cancelEvent(entpMsg); }
else { scheduleAt(simTime() + 2.0, entpMsg);
if(mrtqMsg -> isScheduled()) { cancelEvent(mrtqMsg); }
else { scheduleAt(simTime(), mrtqMsg);
}
}
void RSU::handleSelfMsg(cMessage* msg)
{
DemoBaseApplLayer::handleSelfMsg(msg);
switch(msg -> getKind())
{
case SEND_FRTQ_EVT:
{
...
break;
}
case SEND_ENTP_EVT:
{
...
break;
}
case SEND_MRTQ_EVT:
{
...
break;
}
}
}
Many nodes send and receive messages at the same time, but is this relevant? I think there is no grammatical problem, but I don't understand why the problem occurs.

The mentioned error comes from DemoBaseApplLayer::handleSelfMsg(msg). Involving this method must be done only when in your switch the matched kind is not found, i.e.:
void RSU::handleSelfMsg(cMessage* msg)
{
switch(msg -> getKind())
{
case SEND_FRTQ_EVT:
{
...
break;
}
case SEND_ENTP_EVT:
{
...
break;
}
case SEND_MRTQ_EVT:
{
...
break;
}
default: {
DemoBaseApplLayer::handleSelfMsg(msg);
break;
}
}
}

Related

Updating a struct with reflect

I want to make my code shorter and I'm trying to understand how reflect works. The code below works (the real code has many more structs and are much longer). So shortly the function processInput gets string data from a TCP connection and it is used to populate the Hyperdeck struct.
type configuration struct {
videoInput string `cmd:"video input:"`
audioMapping int64 `cmd:"audio mapping:"`
genlockInputResync bool `cmd:"genlock input resync:"`
}
type Hyperdeck struct {
configuration configuration
}
func (hd *Hyperdeck) processInput(s string) {
switch hd.multilineCmd {
case "configuration":
if s != "\r\n" {
if strings.HasPrefix(s, "video input:") {
hd.configuration.videoInput = strings.TrimSpace(s[strings.Index(s, ":")+1 : len(s)-1])
} else if strings.HasPrefix(s, "audio mapping:") {
hd.configuration.audioMapping = stringToInt(strings.TrimSpace(s[strings.Index(s, ":")+1:len(s)-1]), hd)
} else if strings.HasPrefix(s, "genlock input resync:") {
hd.configuration.genlockInputResync = stringToBool(strings.TrimSpace(s[strings.Index(s, ":")+1:len(s)-1]), hd)
}
} else {
hd.multilineCmd = ""
}
}
switch {
case strings.Contains(strings.ToLower(s), "configuration"):
hd.multilineCmd = "configuration"
}
}
I was thinking of making something like this that loops the stuct and identifies the type but I can't figure out how to update the struct. I've read and tried things from multiple websites but I just can't get it right.
func (hd *Hyperdeck) processInput(s string) {
switch hd.multilineCmd {
case "configuration":
if s != "\r\n" {
v := reflect.ValueOf(hd.configuration)
t := v.Type()
for i := 0; i < v.NumField(); i++ {
if strings.HasPrefix(s, t.Field(i).Tag.Get("cmd")) {
if t.Field(i).Type.String() == "string" {
// UPDATE hd.configuration.videoInput
} else if t.Field(i).Type.String() == "int" {
//UPDATE hd.configuration.audioMapping
} else if t.Field(i).Type.String() == "bool" {
//UPDATE hd.configuration.genlockInputResync
}
}
}
} else {
hd.multilineCmd = ""
}
}
switch {
case strings.Contains(strings.ToLower(s), "configuration"):
hd.multilineCmd = "configuration"
}
}
I think this code is usable for strings but can't figure out how to address it...
...FieldByName(types.Field(i).Name).SetString(strings.TrimSpace(s[strings.Index(s, ":")+1 : len(s)-1]))
And maybe the most important question, should I do it with reflect or leave it as it is now?

Argument passed to call that takes no arguments.. Firebase

Not sure why i'm getting this.. any suggestions would be grateful!
I ran into issues with my original coding where I had Firebase pod and Firebase Package.. so I started from scratch since that wasnt fixing itself.. now I get this.. and I am at a loss for how to resolve it.
static func fetchUsers() -> AnyPublisher<[UserProfile], Error> {
Future< [UserProfile], Error > { promise in
self.db.collection("Users")
.getDocuments { (snapshot, error) in
if let error = error {
promise(.failure(error))
return
}
guard let snapshot = snapshot else {
promise(.failure(FirebaseError.badSnapshot))
return
}
var users = [UserProfile]()
snapshot.documents.forEach { document in
print(users.count)
if let user = try? document.data(as: UserProfile.self){
if users.contains(where: { $0.id == user.id}) {return}
users.append(user)
} else {
print("Not working")
}
}
promise(.success(users))
}
}
.eraseToAnyPublisher()
}
I believe this is the syntax you're after:
var users = [UserProfile]()
users = snapshot.documents.compactMap { (document) -> UserProfile? in
if users.contains(where: { $0.id == user.id}) {
return nil
} else {
return try? document.data(as: UserProfile.self)
}
}
Also be aware that when you iterate something in Swift and encounter a false condition on an iteration, return will return out of the greater scope, not just that iteration. Therefore, use continue.
for x in y {
guard x > 0 else {
continue // continues loop
}
// ...
}

How to write a dynamic declarative pipeline that contains sequential job inside parallel job

I'm trying to write a declarative pipeline code that accepts a map and create a pipeline. I can able to achieve sequential stages or parallel stages but facing problems while making a pipeline that contains sequential stages inside parallel stages.
The input data would be Map. Each list in the map should run parallel and the items inside the list corresponding to each key should run in sequentially.
example data : [1:[11,12], 2:[21,22], 3:[31,32]]
The output should be of image. Could someone give some idea?
Below is the code i have tried.
def stageData = [1:[11,12], 2:[21,22], 3:[31,32]];
def getDeployStages1(stageData){
Map deployStages = [:]
stageData.each{ key, stgValue ->
List stgs = []
stgValue.each{ value ->
deployStages.put("${value}", {
echo "${value}"
})
}
}
return deployStages;
}
def getDeployStages2(stageData){
Map deployStages = [:]
stageData.each{ key, stgValue ->
List stgs = []
stgValue.each{ value ->
stgs.add(stage("${value}"){
echo "${value}"
})
}
deployStages.put("${key}", stgs)
}
return deployStages;
}
pipeline {
agent any
stages {
stage ("deploy1") {
steps {
script {
parallel getDeployStages1(stageData)
}
}
}
stage ("deploy2") {
steps {
script {
parallel getDeployStages2(stageData)
}
}
}
}
}
According to this documentation you can nest the stages in this way
pipeline {
agent none
stages {
stage("build and deploy on Windows and Linux") {
parallel {
stage("windows") {
agent {
label "windows"
}
stages {
stage("build") {
steps {
bat "run-build.bat"
}
}
stage("deploy") {
when {
branch "master"
}
steps {
bat "run-deploy.bat"
}
}
}
}
stage("linux") {
agent {
label "linux"
}
stages {
stage("build") {
steps {
sh "./run-build.sh"
}
}
stage("deploy") {
when {
branch "master"
}
steps {
sh "./run-deploy.sh"
}
}
}
}
}
}
}
}
This should result in the following flow
To apply this in your case, you can simplify your functions to return just elements that need to be sequential (just the values).
pipeline {
agent any
stages {
stage ("parallel") {
parallel {
stage ("deploy1") {
stages {
def list = getDeployStages1(stageData)
for (int i=0; i < list.size(); i++) {
stage(i) {
echo("${list[i]}")
}
}
}
stage ("deploy2") {
stages {
//similar
}
}
}
}
}

How to populate Enum from the values retrieved from Database

Looking at the example here at Message Controller for Pizza Example, if I want to populate Size or Kind based on some user input and make a call to the database, how would I do that?
So far as I know, there is not an easy way to populate the Enum at runtime.
It looks like this hasn't been implemented yet. I took a look inside https://github.com/Microsoft/BotBuilder/blob/master/CSharp/Library/FormFlow/FormBuilder.cs and found this:
internal static void TypePaths(Type type, string path, List<string> paths)
{
if (type.IsClass)
{
if (type == typeof(string))
{
paths.Add(path);
}
else if (type.IsIEnumerable())
{
var elt = type.GetGenericElementType();
if (elt.IsEnum)
{
paths.Add(path);
}
else
{
// TODO: What to do about enumerations of things other than enums?
}
}
else
{
FieldPaths(type, path, paths);
}
}
else if (type.IsEnum)
{
paths.Add(path);
}
else if (type == typeof(bool))
{
paths.Add(path);
}
else if (type.IsIntegral())
{
paths.Add(path);
}
else if (type.IsDouble())
{
paths.Add(path);
}
else if (type.IsNullable() && type.IsValueType)
{
paths.Add(path);
}
else if (type == typeof(DateTime))
{
paths.Add(path);
}
}
Notice the TODO about enumerations other than enums.
Outside of the FormBuilder we can use PromptDialog.Choice which takes an IEnumerable<> of your options.
It is possible to chain dialogs together, so you may have to split your FormDialog into two with the PromptDialog in-between.
Alternatively take a fork of BotBuilder and implement the TODO!

How to unit test throwing functions in Swift?

How to test wether a function in Swift 2.0 throws or not? How to assert that the correct ErrorType is thrown?
EDIT: Updated the code for Swift 4.1 (still valid with Swift 5.2)
Here's the latest Swift version of Fyodor Volchyok's answer who used XCTAssertThrowsError:
enum MyError: Error {
case someExpectedError
case someUnexpectedError
}
func functionThatThrows() throws {
throw MyError.someExpectedError
}
func testFunctionThatThrows() {
XCTAssertThrowsError(try functionThatThrows()) { error in
XCTAssertEqual(error as! MyError, MyError.someExpectedError)
}
}
If your Error enum has associated values, you can either have your Error enum conform to Equatable, or use the if case statement:
enum MyError: Error, Equatable {
case someExpectedError
case someUnexpectedError
case associatedValueError(value: Int)
}
func functionThatThrows() throws {
throw MyError.associatedValueError(value: 10)
}
// Equatable pattern: simplest solution if you have a simple associated value that can be tested inside 1 XCTAssertEqual
func testFunctionThatThrows() {
XCTAssertThrowsError(try functionThatThrows()) { error in
XCTAssertEqual(error as! MyError, MyError.associatedValueError(value: 10))
}
}
// if case pattern: useful if you have one or more associated values more or less complex (struct, classes...)
func testFunctionThatThrows() {
XCTAssertThrowsError(try functionThatThrows()) { error in
guard case MyError.associatedValueError(let value) = error else {
return XCTFail()
}
XCTAssertEqual(value, 10)
// if you have several values or if they require more complex tests, you can do it here
}
}
At least of Xcode 7.3 (maybe earlier) you could use built-in XCTAssertThrowsError():
XCTAssertThrowsError(try methodThatThrows())
If nothing is thrown during test you'll see something like this:
If you want to check if thrown error is of some concrete type, you could use errorHandler parameter of XCTAssertThrowsError():
enum Error: ErrorType {
case SomeExpectedError
case SomeUnexpectedError
}
func functionThatThrows() throws {
throw Error.SomeExpectedError
}
XCTAssertThrowsError(try functionThatThrows(), "some message") { (error) in
XCTAssertEqual(error as? Error, Error.SomeExpectedError)
}
Given the following functions and declarations:
enum SomeError: ErrorType {
case FifthError
case FirstError
}
func throwingFunction(x: Int) throws {
switch x {
case 1:
throw SomeError.FirstError
case 5:
throw SomeError.FifthError
default:
return
}
}
This function will throw a FifthError if 5 is given to the function and FirstError if 1 is given.
To test, that a function successfully runs the unit test could look as follows:
func testNotError() {
guard let _ = try? throwingFunction(2) else {
XCTFail("Error thrown")
return
}
}
The let _ may also be replaced by any other name, so you can further test the output.
To assert that a function throws, no matter what ErrorType the unit test could look like this:
func testError() {
if let _ = try? throwingFunction(5) {
XCTFail("No error thrown")
return
}
}
If you want to test for a specific ErrorType it's done with a do-catch-statement. This is not the best way compared to other languages.
You have to make sure that you...
return in the catch for the correct ErrorType
XCTFail() and return for all other catch
XCTFail() if no catch is executed
Given this requirements a test case could look like this:
func testFifthError() {
do {
try throwingFunction(5)
} catch SomeError.FifthError {
return
} catch {
XCTFail("Wrong error thrown")
return
}
XCTFail("No error thrown")
}
Swift 4.1 Error throwing Test for associated values
enum ParseError: Error, Equatable {
case unexpectedArgument(String)
}
func testWithNoSchemaButWithOneArgument() {
XCTAssertThrowsError(try Args(withSchema: "", andArguments: ["-x"])) { error in
XCTAssertEqual(error as? ParseError, ParseError.unexpectedArgument("Argument(s) -x unexpected."))
}
}
You can use this function:
func XCTAssertThrowsError<T, E: Error & Equatable>(
_ expression: #autoclosure () throws -> T,
error: E,
in file: StaticString = #file,
line: UInt = #line
) {
var thrownError: Error?
XCTAssertThrowsError(
try expression(),
file: file,
line: line) {
thrownError = $0
}
XCTAssertTrue(
thrownError is E,
"Unexpected error type: \(type(of: thrownError))",
file: file,
line: line
)
XCTAssertEqual(
thrownError as? E,
error,
file: file,
line: line
)
}
Example:
XCTAssertThrowsError(try funcThatThrowsSpecificError(), error: SpecificErrorEnum.someError)

Resources