Permalink
Browse files

cleanup

  • Loading branch information...
1 parent ac35fc8 commit 45835cbdc061cca7372a74a92a7e96a218221286 @vshymanskyy committed Sep 26, 2012
Showing with 19 additions and 333 deletions.
  1. +16 −0 src/KadNodeId.h
  2. +1 −3 src/KadOpMgr.h
  3. +0 −122 src/operations/KadFind.h
  4. +0 −188 src/operations/KadMsg.h
  5. +0 −15 src/operations/KadPing.h
  6. +1 −4 src/test/MsgPackTS.h
  7. +1 −1 xplat
View
@@ -5,6 +5,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <msgpack.hpp>
#include "KadConfig.h"
#include "XHelpers.h"
@@ -318,7 +319,22 @@ class TKadId
return result;
}
+ /*****************************************************************
+ * MsgPack
+ *****************************************************************/
+ void msgpack_pack(msgpack::packer<msgpack::sbuffer>& pk) const
+ {
+ pk.pack_raw(SIZE);
+ pk.pack_raw_body((const char*)&mData, SIZE);
+ }
+
+ void msgpack_unpack(msgpack::object o)
+ {
+ if(o.type != msgpack::type::RAW) { throw msgpack::type_error(); }
+ if(o.via.raw.size != SIZE) { return; }
+ memcpy(&mData, o.via.raw.ptr, SIZE);
+ }
/*****************************************************************
* Private data
View
@@ -14,9 +14,7 @@
#include "KadConfig.h"
#include "KadRtNode.h"
-#include "operations/KadPing.h"
-#include "operations/KadFind.h"
-
+#include "KadMsg.h"
#include "KadOverIP.h"
#include <iostream>
View
@@ -1,122 +0,0 @@
-#include "KadMsg.h"
-/*
-class KadMsgAddr
-{
- enum Family {
- UNSPEC, IPv4, IPv6
- };
-public:
- KadMsgAddr() : family(UNSPEC) {}
- KadMsgAddr(const XSockAddr& a) {
- switch (a.SA()->sa_family) {
- case AF_INET:
- family = IPv4;
- port = a.SA4()->sin_port;
- memcpy(&addr, &a.SA4()->sin_addr, sizeof(a.SA4()->sin_addr));
- break;
- case AF_INET6:
- family = IPv6;
- port = a.SA6()->sin6_port;
- memcpy(&addr, &a.SA6()->sin6_addr, sizeof(a.SA6()->sin6_addr));
- break;
- default:
- family = UNSPEC;
- break;
- }
- }
- operator XSockAddr() const {
- XSockAddr result;
- switch (family) {
- case IPv4:
- result.SA4()->sin_family = AF_INET;
- result.SA4()->sin_port = port;
- memcpy(&result.SA4()->sin_addr, &addr, sizeof(result.SA4()->sin_addr));
- break;
- case IPv6:
- result.SA6()->sin6_family = AF_INET6;
- result.SA6()->sin6_port = port;
- memcpy(&result.SA6()->sin6_addr, &addr, sizeof(result.SA6()->sin6_addr));
- break;
- default:
- result.SA()->sa_family = AF_UNSPEC;
- break;
- }
- return result;
- }
-private:
- uint16_t family;
- uint16_t port;
- uint8_t addr[16];
-} GCC_SPECIFIC(__attribute__((packed)));
-
-struct KadMsgContact
-{
- KadId id;
- KadMsgAddr addr;
-
- KadMsgContact() {}
-
- KadMsgContact(const KadContact& c)
- : id(c.mId)
- , addr(c.mAddrExt)
- { }
-
-};
-
-class KadMsgFindReq : public KadMsg
-{
-
-public:
- KadMsgFindReq(const KadId& findId)
- : KadMsg(KadMsg::KAD_MSG_FIND_REQ)
- , mFindId (findId)
- {
- }
-
- const KadId& FindId() const { return mFindId; }
-
-private:
- KadId mFindId;
-
-};
-
-class KadMsgFindRsp : public KadMsgRsp
-{
-
-public:
- KadMsgFindRsp(KadMsgStatus status, KadContactList &lst)
- : KadMsgRsp(KadMsg::KAD_MSG_FIND_RSP, status)
- {
- X_ASSERT_LE(lst.Count(), KADEMLIA_BUCKET_SIZE, "%d");
- memset(mContacts, 0, sizeof(mContacts));
-
- unsigned i=0;
- for (KadContactList::It it=lst.First(); it!=lst.End(); ++it) {
- mContacts[i++]= KadMsgContact(*lst[it]);
- }
-
- }
-
-//private:
- KadMsgContact mContacts[KADEMLIA_BUCKET_SIZE];
-
-};
-
-inline
-const XLog::Stream& operator <<(const XLog::Stream& str, const KadMsgFindReq& v)
-{
- return str << "KAD_MSG_FIND_REQ: " << v.FindId();
-}
-
-inline
-const XLog::Stream& operator <<(const XLog::Stream& str, const KadMsgFindRsp& v)
-{
- int qty = 0;
- for (int i=0; i<KADEMLIA_BUCKET_SIZE; i++) {
- if (!v.mContacts[i].id.IsZero()) {
- qty++;
- }
- }
- return str << "KAD_MSG_FIND_RSP { " << qty << " }";
-}
-*/
View
@@ -1,188 +0,0 @@
-#ifndef KAD_MSG_H_
-#define KAD_MSG_H_
-
-#include "KadConfig.h"
-#include "KadContact.h"
-#include "KadNodeId.h"
-
-#include <msgpack.hpp>
-#include <vector>
-#include <string>
-
-template<unsigned SIZE>
-struct MsgPackRaw
-{
- uint8_t mData[SIZE];
-
- void msgpack_pack(msgpack::packer<msgpack::sbuffer>& pk) const
- {
- pk.pack_raw(SIZE);
- pk.pack_raw_body((const char*)&mData, SIZE);
- }
-
- void msgpack_unpack(msgpack::object o)
- {
- if(o.type != msgpack::type::RAW) { throw msgpack::type_error(); }
- if(o.via.raw.size != SIZE) { return; }
- memcpy(&mData, o.via.raw.ptr, SIZE);
- }
-};
-
-
-struct KadMsgAddr
-{
- enum Family {
- UNSPEC, IPv4, IPv6
- };
-
- KadMsgAddr() : mFamily(UNSPEC) {}
- KadMsgAddr(const XSockAddr& a) {
- switch (a.SA()->sa_family) {
- case AF_INET:
- mFamily = IPv4;
- mPort = a.SA4()->sin_port;
- memcpy(&mAddr, &a.SA4()->sin_addr, sizeof(a.SA4()->sin_addr));
- break;
- case AF_INET6:
- mFamily = IPv6;
- mPort = a.SA6()->sin6_port;
- memcpy(&mAddr, &a.SA6()->sin6_addr, sizeof(a.SA6()->sin6_addr));
- break;
- default:
- mFamily = UNSPEC;
- break;
- }
- }
- operator XSockAddr() const {
- XSockAddr result;
- switch (mFamily) {
- case IPv4:
- result.SA4()->sin_family = AF_INET;
- result.SA4()->sin_port = mPort;
- memcpy(&result.SA4()->sin_addr, &mAddr, sizeof(result.SA4()->sin_addr));
- break;
- case IPv6:
- result.SA6()->sin6_family = AF_INET6;
- result.SA6()->sin6_port = mPort;
- memcpy(&result.SA6()->sin6_addr, &mAddr, sizeof(result.SA6()->sin6_addr));
- break;
- default:
- result.SA()->sa_family = AF_UNSPEC;
- break;
- }
- return result;
- }
-
- uint8_t mFamily;
- uint16_t mPort;
- MsgPackRaw<16> mAddr;
-
- MSGPACK_DEFINE(mFamily, mPort, mAddr);
-};
-
-struct KadMsgId
-{
- KadMsgId() {}
- KadMsgId(const KadId& id) { memcpy(mData, id.Data(), KADEMLIA_ID_SIZE); }
- operator KadId() const { KadId id; memcpy(id.Data(), mData, KADEMLIA_ID_SIZE); return id; }
-
- uint8_t mData[KADEMLIA_ID_SIZE];
-
- void msgpack_pack(msgpack::packer<msgpack::sbuffer>& pk) const
- {
- pk.pack_raw(KADEMLIA_ID_SIZE);
- pk.pack_raw_body((const char*)&mData, KADEMLIA_ID_SIZE);
- }
-
- void msgpack_unpack(msgpack::object o)
- {
- if(o.type != msgpack::type::RAW) { throw msgpack::type_error(); }
- if(o.via.raw.size != KADEMLIA_ID_SIZE) { return; }
- memcpy(&mData, o.via.raw.ptr, KADEMLIA_ID_SIZE);
- }
-};
-
-struct KadMsgContact
-{
- KadMsgContact() {}
- KadMsgContact(const KadContact& a) : mId (a.mId), mAddr (a.mAddrExt) { }
- operator KadContact() const { return KadContact(mId, mAddr); }
-
- KadMsgId mId;
- KadMsgAddr mAddr;
-
- MSGPACK_DEFINE(mId, mAddr);
-};
-
-struct KadMsg
-{
- enum Type
- {
- KAD_MSG_PING,
- KAD_MSG_PONG,
- KAD_MSG_JOIN_REQ,
- KAD_MSG_JOIN_RSP,
- KAD_MSG_FIND_REQ,
- KAD_MSG_FIND_RSP,
- KAD_MSG_STORE_REQ,
- KAD_MSG_STORE_RSP,
- KAD_MSG_REMOVE_REQ,
- KAD_MSG_REMOVE_RSP,
- KAD_MSG_LEAVE_REQ,
- KAD_MSG_LEAVE_RSP,
-
- KAD_MSG_RFIND_REQ,
- KAD_MSG_RFIND_RSP,
-
- KAD_MSG_QTY
- };
-
- KadMsg() {}
- KadMsg(Type t) : mMsgType (t) {}
-
- uint16_t mMsgType;
- uint64_t mMsgId;
- KadMsgId mSrcId;
-
- MSGPACK_DEFINE(mMsgType, mMsgId, mSrcId);
-};
-
-struct KadMsgPing: public KadMsg
-{
- KadMsgPing() : KadMsg(KadMsg::KAD_MSG_PING) {}
-
- MSGPACK_DEFINE((KadMsg&)(*this));
-};
-
-struct KadMsgPong: public KadMsg
-{
- KadMsgPong() : KadMsg(KadMsg::KAD_MSG_PONG) {}
-
- MSGPACK_DEFINE((KadMsg&)(*this));
-};
-
-struct KadMsgFindReq: public KadMsg
-{
- KadMsgFindReq() {}
- KadMsgFindReq(const KadMsgId& id) : KadMsg(KadMsg::KAD_MSG_FIND_REQ), mTargetId(id) {}
-
- KadMsgId mTargetId;
-
- MSGPACK_DEFINE((KadMsg&)(*this), mTargetId);
-};
-
-struct KadMsgFindRsp: public KadMsg
-{
- KadMsgFindRsp() {}
- KadMsgFindRsp(const KadContactList& lst) : KadMsg(KadMsg::KAD_MSG_FIND_RSP) {
- for (KadContactList::It it = lst.First(); it != lst.End(); ++it) {
- mContacts.push_back(*lst[it]);
- }
- }
-
- std::vector<KadMsgContact> mContacts;
-
- MSGPACK_DEFINE((KadMsg&)(*this), mContacts);
-};
-
-#endif /* KAD_MSG_H_ */
View
@@ -1,15 +0,0 @@
-#include "KadMsg.h"
-
-/*
-inline
-const XLog::Stream& operator <<(const XLog::Stream& str, const KadMsgPing& v)
-{
- return str << "KAD_MSG_PING";
-}
-
-inline
-const XLog::Stream& operator <<(const XLog::Stream& str, const KadMsgPong& v)
-{
- return str << "KAD_MSG_PONG";
-}
-*/
View
@@ -1,7 +1,4 @@
-
-
-
-#include "operations/KadMsg.h"
+#include "KadMsg.h"
#include <cxxtest/TestSuite.h>
class MsgPackTS : public CxxTest::TestSuite
2 xplat
Submodule xplat updated 1 files
+14 −0 inc/XHelpers.h

0 comments on commit 45835cb

Please sign in to comment.