Unable to deserialize flatbuf messages received through TCP - c++

I am trying to serialize, then deserialize a FlatBuf object.
I am using FlatBuffer 1.10.0. I want to send the serialized data through TCP to another process, but at the moment I am not able to deserialize it even in the same function.
The fbs file:
table StringPacket
{
type:int;
logLevel:int;
myAge:int;
stringdata:string;
}
root_type StringPacket;
The code in C++:
...
uint8_t * packet_data;
int data_size;
using namespace flatbuffers;
FlatBufferBuilder fbb;
//serialize the string data
auto thisStringData = fbb.CreateString(m_stringdata);
//create the packet (I tried thisway too)
//auto packet = CreateStringPacket(fbb, 2, 3, 27, thisStringData);
StringPacketBuilder strbuilder(fbb);
strbuilder.add_logLevel(3);
strbuilder.add_myAge(4);
strbuilder.add_type(1);
strbuilder.add_stringdata(fbb.CreateString("somehing"));
auto thisPacket = strbuilder.Finish();
packet_data = fbb.GetBufferPointer();
data_size = fbb.GetSize();
auto get = GetStringPacket(packet_data);
std::cout << "Deserialized Log level: " << get->logLevel()<<std::endl;
std::cout << "Des Age: " << get->myAge() << std::endl;
...
I get an unhandled exception of 0xC0000005, Access violation reading location...
at the line:
std::cout << "Deserialized Log level: " << get->logLevel()<<std::endl;
Or anytime when I call a function of the 'get' object.
What am I doing wrong? How can I correctly deserialize it?

Please make sure you test with asserts on (in "debug" mode), as it would have answered your questions already. In particular:
You are calling GetBufferPointer on an unfinished buffer (which normally asserts). You need to call fbb.Finish(thisPacket) or similar.
You are calling CreateString inside a table builder (which normally asserts), and you create a string outside of it which you don't use. Presumably you had intended to call strbuilder.add_stringdata(thisStringData) instead.

Related

Retrieve file sent through post request in c++

I'm setting up a micro-service for a cpp application, and i need to send a post request with a file as a parameter from a js project to the cpp one through this micro-service, i've been able to send the post request but i can't retrieve the file.
I've tried to use "http_response.extract-string/json" but it doesn't work, the result is empty
void MicroserviceController::handlePost(http_request message) {
std::cout<< "handlePost(http_request message) " << std::endl;
std::cout<< message.to_string() << std::endl;
std::cout<< message.extract_json().get() << std::endl;
std::ofstream o("hello.json");
o << message.extract_json().get()<< std::endl;
after running that, i got an empty hello file.
I'm sure there is a simple solution to this, i just have a lack in http_request methods .
Thanks in advance for your help.

AMQP-CPP RabbitMQ async event based consumer not consuming anything

I'm using the AMQ-CPP library (https://github.com/CopernicaMarketingSoftware/AMQP-CPP) to connect to an existing queue I've created but I'm unable to read anything. I've tested that the queue works using another library (https://github.com/alanxz/SimpleAmqpClient, it works and I consume messages), but it uses a polling approach and I need an event based one.
My code looks like (based on the provided example):
int main()
{
auto *poll = EV_DEFAULT;
// handler for libev (so we don't have to implement AMQP::TcpHandler!)
AMQP::LibEvHandler handler(poll);
// make a connection
AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));
// we need a channel too
AMQP::TcpChannel channel(&connection);
// Define callbacks and start
auto messageCb = [&channel](
const AMQP::Message &message, uint64_t deliveryTag,
bool redelivered)
{
std::cout << "message received" << std::endl;
// acknowledge the message
channel.ack(deliveryTag);
processMessage(message.routingKey(), message.body());
};
// callback function that is called when the consume operation starts
auto startCb = [](const std::string &consumertag) {
std::cout << "consume operation started: " << consumertag << std::endl;
};
// callback function that is called when the consume operation failed
auto errorCb = [](const char *message) {
std::cout << "consume operation failed" << std::endl;
};
channel.consume("domoqueue")
.onReceived(messageCb)
.onSuccess(startCb)
.onError(errorCb);
// run the poll
ev_run(poll, 0);
// done
return 0;
}
I'm running the code in a Raspberry Pi having :
Linux raspberrypi 4.4.26-v7+ #915 SMP Thu Oct 20 17:08:44 BST 2016 armv7l GNU/Linux
What can be the problem? Probably I'm missing some configuration parameters for the queue... I've placed some debug traces and the channel creation does not take place. It blocks in the connection statement:
AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));
cout << "I never show up" << endl;
// we need a channel too
AMQP::TcpChannel channel(&connection)
I've found my problem: I wasn't using the declareQueue() method! In fact, I had to use it but specifying the following parameters (the same as I did when I created the queue manually):
AMQP::Table arguments;
arguments["x-message-ttl"] = 120 * 1000;
// declare the queue
channel.declareQueue("domoqueue", AMQP::durable + AMQP::passive, arguments).onSuccess(callback);

C++ Function Completing Before Other Function Finishes

I am coding a C++ program to interact with the internet using the C++ REST SDK. I have a main function and a webCommunication function. The code is similar to below:
void webCommunication(data, url)
{
//Communicate with the internet using the http_client
//Print output
}
int main()
{
//Obtain information from user
webCommunication(ans1, ans2);
system("PAUSE");
}
However, it seems that the main function is progressing before the webCommunication function is finished. If I make webCommunication a function type of string and have
cout << webCommunication(ans1, ans2) << endl;
But that still pauses and then prints the data retrieved. Normally, this would be fine, expect I am referring to the returned answer later on in the code. If the webCommunication isn't completed, the application crashes. Is there some kind of wait_until function I can use?
UPDATE: I have tried using a mutex suggested with no success. I also tried starting the function as a thread and then using the .join() with still no success.
If you declare your webCommunications() function as a
pplx::task<void> webCommunications()
{
}
Then you can use ".wait()" when calling the function. It will then wait until the function executes to continue. Looks like this:
pplx::task<void> webCommunications()
{
}
int main()
{
webCommunications().wait();
//Do other stuff
}
I think you are missing a keyword in the descriptions. ASYNCHRONOUS. This is indicating that it returns before finishing. If you need it to be synchronous, you should put a semaphore acquire right after the call and put a release into the callback code.
https://msdn.microsoft.com/en-us/library/jj950081.aspx
Modified code snippet from link above( added lock to callback ) :
// Creates an HTTP request and prints the length of the response stream.
pplx::task<void> HTTPStreamingAsync()
{
http_client client(L"http://www.fourthcoffee.com");
// Make the request and asynchronously process the response.
return client.request(methods::GET).then([](http_response response)
{
// Print the status code.
std::wostringstream ss;
ss << L"Server returned returned status code " << response.status_code() << L'.' << std::endl;
std::wcout << ss.str();
// TODO: Perform actions here reading from the response stream.
auto bodyStream = response.body();
// In this example, we print the length of the response to the console.
ss.str(std::wstring());
ss << L"Content length is " << response.headers().content_length() << L" bytes." << std::endl;
std::wcout << ss.str();
// RELEASE lock/semaphore/etc here.
mutex.unlock()
});
/* Sample output:
Server returned returned status code 200.
Content length is 63803 bytes.
*/
}
Note : Acquire the mutex after the function call to start web processing. Add to the callback code to release the mutex. In this way the main thread locks until the function actually finishes and then continues to 'pause'.
int main()
{
HttpStreamingAsync();
// Acquire lock to wait for complete
mutex.lock();
system("PAUSE");
}

how to catch boost message_queue returns false

I am using boost::interprocess::message_queue and as per the definition given on
http://www.boost.org/doc/libs/1_35_0/doc/html/boost/interprocess/message_queue.html
message_queue(open_only_t open_only, const char * name);
Opens a previously created process shared message queue with name "name". If the was not previously created or there are no free resources, the function returns false.
now what i can't understand is that how a constructor is returning a value? though it states " the function returns false" but afaik message_queue is supposed to be a constructor.
and also if it do return false can i catch that in a Boolean variable?
A boost::interprocess::interprocess_exception will be thrown instead, as the current documentation suggests.
So,
using namespace boost::interprocess;
try {
//Create a message_queue. If the queue
//exists throws an exception
message_queue mq
(create_only //only create
,"message_queue" //name
,100 //max message number
,100 //max message size
);
} catch (interprocess_exception const& ipe)
{
std::cerr << "Error: #" << ipe.get_error_code() << ", " << ipe.what() << "\n";
}
When run twice, will print
Error: #9, File exists

Flush queued GPIB responses

Architecture ->GBIP from external interface is connected to target ( linux) system via gpib bus.
Inside Linux box , there is ethernet cable from GPIB to motherboard.
The PIC_GPIB card on external interface is IEEE 488.2
I am sending a query from external interface to linux box.
Few scenarios
1) If I send a query which does not expect a response back , then next query send will work.
2) If I send a query which expect response back , and when I have received the response and read it and then fire next query it works fine.
3) BUT if I send a query from external interface and got response back and I ignore to read the response , then Next query fails.
I am requesting help for scenario 3.
The coding is done on linux side and its a socket programming , which uses linux inbuilt function from unistd.h for read and write.
My investigation : I have found there is a internal memory on gbib card on external interface which stores the value of previous response until we have the read. Generally I use IEEE string utility software to write commands that goes to linux box and read reposne via read button .
Could someone please direct me how to clean input buffer or memory which stores value so that write from external command contiunues without bothering to read it.
My code on linux side has been developed in C++ and socket programming. I have used in bulit write and read function to write and read to the gpib and to json server.
Sample code is shown below
bool GpibClass::ReadWriteFromGPIB()
{
bool check = true;
int n = 0;
char buffer[BUFFER_SIZE];
fd_set read_set;
struct timeval lTimeOut;
// Reset the read mask for the select
FD_ZERO(&read_set);
FD_SET(mGpibFd, &read_set);
FD_SET(mdiffFd, &read_set);
// Set Timeout to check the status of the connection
// when no data is being received
lTimeOut.tv_sec = CONNECTION_STATUS_CHECK_TIMEOUT_SECONDS;
lTimeOut.tv_usec = 0;
cout << "Entered into this function" << endl;
// Look for sockets with available data
if (-1 == select(FD_SETSIZE, &read_set, NULL, NULL, &lTimeOut))
{
cout << "Select failed" << endl;
// We don't know the cause of select's failure.
// Close everything and start from scratch:
CloseConnection(mGpibFd);
CloseConnection(mdifferntServer); // this is different server
check = false;
}
// Check if data is available from GPIB server,
// and if any read and push it to gpib
if(true == check)
{
cout << "Check data from GPIB after select" << endl;
if (FD_ISSET(mGpibFd, &read_set))
{
n = read(mGpibFd, buffer, BUFFER_SIZE);
cout << "Read from GPIB" << n << " bytes" << endl;
if(0 < n)
{
// write it to different server and check if we get response from it
}
else
{
// Something failed on socket read - most likely
// connection dropped. Close socket and retry later
CloseConnection(mGpibFd);
check = false;
}
}
}
// Check if data is available from different server,
// and if any read and push it to gpib
if(true == check)
{
cout << "Check data from diff server after select" << endl;
if (FD_ISSET(mdiffFd, &read_set))
{
n = read(mdiffFd, buffer, BUFFER_SIZE);
cout << "Read from diff servewr " << n << " bytes" << endl;
if (0 < n)
{
// Append, just in case - makes sure data is sent.
// Extra cr/lf shouldn't cause any problem if the json
// server has already added them
strcpy(buffer + n, "\r\n");
write(mGpibFd, buffer, n + 2);
std::cout <<" the buffer sixze = " << buffer << std::endl;
}
else
{
// Something failed on socket read - most likely
// connection dropped. Close socket and retry later
CloseConnection(mdiffFd);
check = false;
}
}
}
return check;
}
You should ordinarily be reading responses after any operation which could generate them.
If you fail to do that, an easy solution would be to read responses in a loop until you have drained the queue to empty.
You can reset the instrument (probably *RST), but you would probably loose other state as well. You will have to check it's documentation to see if there is a command to reset only the response queue. Checking the documentation is always a good idea, because the number of instruments which precisely comply with the spec is dwarfed by the number which augment or omit parts in unique ways.