Skip to content

Instantly share code, notes, and snippets.

@matutter
Created December 24, 2014 18:34
Show Gist options
  • Select an option

  • Save matutter/7f335d871ce4c26ad954 to your computer and use it in GitHub Desktop.

Select an option

Save matutter/7f335d871ce4c26ad954 to your computer and use it in GitHub Desktop.
C++ UDP Json tryParse object
/*
from: https://github.com/matutter/AirRocks-FlightController
JCommand -
SHOULD be the main JSON parsing type
WILL be exception safe
boost causes hangs and weird assemlber errors, v8 embedding is worse...
i NEED c++11 support... next time its going to be an Intel Edison
*/
#ifndef JSON_COMMAND_T
#define JSON_COMMAND_T
//#include "../Includes.hpp"
#include <sstream>
#include <exception>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
class JCommand {
public:
boost::property_tree::ptree obj;
JCommand() {}
std::string Action() {
try {
std::string s;
s = this->obj.get<std::string>("action");
return s;
}catch(std::exception const& e) {}
return "err";
}
std::string Name() {
try {
return this->obj.get<std::string>("name");
}catch(std::exception const& e) {}
return "err";
}
char Val_t() {
try {
return this->obj.get<char>("val_t");
}catch(std::exception const& e) {}
return 0;
}
float Value() {
try {
switch( this->Val_t() ) {
case 'f':
return this->obj.get<float>("value");
case 0:
return (float)-1.0f;
}
}catch(std::exception const& e) {}
return (float)-1.0f;
}
bool tryParse(std::string s) {
std::stringstream ss;
ss << s;
try {
boost::property_tree::read_json(ss,this->obj);
return true;
}
catch( std::exception const& e ) {}
return false;
}
};
#endif
var PORT = 5000;
var HOST = '0.0.0.0';
var dgram = require('dgram');
var obj = {
action: 'setABC' /* MUST HAVE a string for the command map */
, val_t : 'f' /* MUST HAVE a SINGLE, leave null( or (int)0) for no value */
, value : '0.022' /* the data described by the val_t */
}
var message = new Buffer(JSON.stringify( obj ));
var client = dgram.createSocket('udp4');
client.send(message, 0, message.length, PORT, HOST, function(err, bytes) {
if (err) throw err;
console.log('UDP message sent to ' + HOST +':'+ PORT);
client.close();
});
/*
compile with g++ 4.6 or newer
NOTE: ignore swap{b} Assembler error
*/
#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdint.h>
#include "JCommand.hpp"
#define NOENT_SOCKET_ERR 0
#define BIND_SOCKET_ERR 0
#define UDP_BUF_SIZE 2048
#define PORT 5000
using namespace std;
int main(int argc, char const *argv[])
{
int soc, ret;
struct sockaddr_in address;
if ((soc = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
throw NOENT_SOCKET_ERR;
}
/*on stack zero out*/
memset((char *)&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY); // htn long
address.sin_port = htons(PORT);// htn short endianess is the same?
if (bind(soc, (struct sockaddr *)&address, sizeof(address)) < 0) {
throw( BIND_SOCKET_ERR );
}
cout << inet_ntoa( address.sin_addr ) << ":" << htons(address.sin_port)<<" "<< address.sin_port << endl;
for(;;) {
cout << "waiting on data..." << endl;
char data[UDP_BUF_SIZE];
struct sockaddr_in client;
socklen_t addrlen = sizeof(client);
int length = recvfrom(soc, data, UDP_BUF_SIZE, 0, (struct sockaddr *)&client, &addrlen);
JCommand jcom;
if( jcom.tryParse( data ) ) {
cout << jcom.Action() << endl;
cout << jcom.Value() << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment