Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 122 lines (110 sloc) 3.132 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
#ifndef __LIBF2F_GREYNET_MESSAGE_H__
#define __LIBF2F_GREYNET_MESSAGE_H__

#include "playdar/resolver_query.hpp"
#include "playdar/playdar_plugin_include.h"
#include "libf2f/message.h"

#define PING 0
#define PONG 1
#define IDENT 2
#define QUERY 3
#define QUERYRESULT 4
#define QUERYSTOP 5
#define SIDREQUEST 6
#define SIDDATA 7
#define SIDFAIL 8
#define SIDHEADERS 9

/// TODO msg abstraction/oo-ness is half-assed atm.
/// maybe we should be passing a MsgFactory to the router, then just have one
/// polymorphic call to message_received(<msgtype> msgp).
/// either way, we need a big switch statement for msg types somewhere tho :|

class PongMessage : public libf2f::Message
{
public:
    PongMessage(const std::string& uuid)
    {
        libf2f::message_header h;
        memcpy( &h.guid, uuid.data(), 36 );
        h.type = PONG;
        h.ttl = 1;
        h.hops = 0;
        h.length = 0;
        m_header = h;
        m_payload = 0;
    }
};

class PingMessage : public libf2f::Message
{
public:
    PingMessage(const std::string& uuid)
    {
        libf2f::message_header h;
        memcpy( &h.guid, uuid.data(), 36 );
        h.type = PING;
        h.ttl = 1;
        h.hops = 0;
        h.length = 0;
        m_header = h;
        m_payload = 0;
    }
};

class IdentMessage : public libf2f::Message
{
public:
    IdentMessage(const std::string &body, const std::string& uuid)
    {
        libf2f::message_header h;
        memcpy( &h.guid, uuid.data(), 36 );
        h.type = IDENT;
        h.ttl = 1;
        h.hops = 0;
        h.length = htonl(body.length());
        m_header = h;
        malloc_payload();
        memcpy( m_payload, body.data(), body.length() );
    }
};

class QueryMessage : public libf2f::Message
{
public:
    QueryMessage(playdar::rq_ptr rq, const std::string& uuid)
    {
        libf2f::message_header h;
        memcpy( &h.guid, uuid.data(), 36 );
        h.type = QUERY;
        h.ttl = 1;
        h.hops = 0;
        using namespace json_spirit;
        Object jq = rq->get_json();
        std::ostringstream querystr;
        write( jq, querystr );
        std::string pl = querystr.str();
        h.length = htonl( pl.length() );
        m_header = h;
        malloc_payload();
        memcpy( m_payload, pl.c_str(), pl.length() );
    }

};

class QueryResultMessage : public libf2f::Message
{
public:
    QueryResultMessage(playdar::query_uid qid, playdar::ri_ptr rip, const std::string& uuid)
    {
        libf2f::message_header h;
        memcpy( &h.guid, uuid.data(), 36 );
        h.type = QUERYRESULT;
        h.ttl = 1;
        h.hops = 0;
        using namespace json_spirit;
        Object response;
        response.push_back( Pair("qid", qid) );
        response.push_back( Pair("result", rip->get_json()) );
        std::ostringstream ss;
        write( response, ss );
        std::string pl = ss.str();
        h.length = htonl( pl.length() );
        m_header = h;
        malloc_payload();
        memcpy( m_payload, pl.c_str(), pl.length() );
    }

};
#endif
Something went wrong with that request. Please try again.