Skip to content

Commit

Permalink
Made proper serialization for TransportSocketEndpoint's.
Browse files Browse the repository at this point in the history
  • Loading branch information
GamePad64 committed Mar 29, 2013
1 parent fe8bac4 commit 2c4ef23
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 82 deletions.
22 changes: 3 additions & 19 deletions src/daemon/net/TransportSocketEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,16 @@

#include "../protobuf/TransportSocketEndpoint_s.pb.h"
#include "TransportSocketEndpoint.h"
#include "UDPTransportSocketEndpoint.h"
//#include "UDPTransportSocketEndpoint.h"
#include <memory>

namespace p2pnet {
namespace net {

static TransportSocketEndpoint::pointer TransportSocketEndpoint::fromProtobuf(TransportSocketEndpoint_s tse_s){
TransportSocketEndpoint::pointer tse_ptr;

switch(tse_s.type()){
case TransportSocketEndpoint_type::UDP:
tse_ptr = std::make_shared<UDPTransportSocketEndpoint>();
tse_ptr->parseFromTypedProtobuf(tse_s);
break;
default:
tse_ptr = std::make_shared<UDPTransportSocketEndpoint>();
break;
}

return tse_ptr;
}

static TransportSocketEndpoint::pointer TransportSocketEndpoint::fromString(std::string endpoint_s){
void TransportSocketEndpoint::fromString(std::string endpoint_s){
TransportSocketEndpoint_s tse_s;
tse_s.ParseFromString(endpoint_s);
return fromProtobuf(tse_s);
fromProtobuf(tse_s);
}

std::string TransportSocketEndpoint::toString(){
Expand Down
17 changes: 7 additions & 10 deletions src/daemon/net/TransportSocketEndpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <string>
#include <memory>
#include "../protobuf/TransportSocketEndpoint_s.pb.h"

namespace p2pnet {
namespace net {
Expand All @@ -30,16 +31,12 @@ class TransportSocketEndpoint {

virtual TransportSocketEndpoint::pointer yieldCopyPtr() const = 0;

/*!
* Returns Protocol Buffers representation of this TransportSocketEndpoint.
* @return binary std::string
*/
virtual std::string toString() = 0;
/*!
* This function restores state of TransportSocketEndpoint from Protocol Buffers representation.
* @param endpoint_s std::string in binary form
*/
virtual void fromString(std::string endpoint_s) = 0;
virtual void fromProtobuf(TransportSocketEndpoint_s tse_s) = 0;
virtual TransportSocketEndpoint_s toProtobuf() = 0;

void fromString(std::string endpoint_s);
std::string toString();

virtual std::string toHRString() = 0;
};

Expand Down
27 changes: 15 additions & 12 deletions src/daemon/net/udp/UDPTransportSocketEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,25 @@ UDPTransportSocketEndpoint::UDPTransportSocketEndpoint(std::string ip, UDPTransp
this->setPort(port);
};

std::string UDPTransportSocketEndpoint::toString() {
UDPTransportSocketEndpoint_s endpoint_pb;
endpoint_pb.set_ip(this->getIP());
endpoint_pb.set_port(this->getPort());
return endpoint_pb.SerializeAsString();
void UDPTransportSocketEndpoint::fromProtobuf(TransportSocketEndpoint_s tse_s){
setIP(tse_s.ip());
setPort(tse_s.port());
}

void UDPTransportSocketEndpoint::fromString(std::string endpoint_s) {
UDPTransportSocketEndpoint_s endpoint_pb;
endpoint_pb.ParseFromString(endpoint_s);
this->setIP(endpoint_pb.ip());
this->setPort(endpoint_pb.port());
TransportSocketEndpoint_s UDPTransportSocketEndpoint::toProtobuf(){
TransportSocketEndpoint_s tse_s;
tse_s.set_type(TransportSocketEndpoint_type::UDP);
tse_s.set_ip(getIP());
tse_s.set_port(getPort());
return tse_s;
}

UDPTransportSocketEndpoint::UDPTransportSocketEndpoint(std::string endpoint_s) {
this->fromString(endpoint_s);
UDPTransportSocketEndpoint::UDPTransportSocketEndpoint(TransportSocketEndpoint_s tse_s) {
fromProtobuf(tse_s);
}

UDPTransportSocketEndpoint::UDPTransportSocketEndpoint(std::string tse_str){
fromString(tse_str);
}

std::string UDPTransportSocketEndpoint::toHRString(){
Expand Down
29 changes: 5 additions & 24 deletions src/daemon/net/udp/UDPTransportSocketEndpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,15 @@ class UDPTransportSocketEndpoint: public p2pnet::net::TransportSocketEndpoint {

typedef std::shared_ptr<UDPTransportSocketEndpoint> udp_pointer;

/*!
* This function returns true if IP address is an IPv4 address.
* @return bool
*/
bool isIPv4();
/*!
* This function returns true if IP address is an IPv6 address.
* @return bool
*/
bool isIPv6();

/*!
* This function returns IP address of the endpoint.
* @return std::string in "255.255.255.255" format (if IPv4) or in hexadecimal IPv6 representation.
*/
std::string getIP() const;
/*!
* This function sets IP address of the endpoint
* @param ip IP address in IPv4 or IPv6 format.
*/
void setIP(const std::string& ip);

typedef unsigned short int port_t;
port_t getPort() const;
void setPort(port_t port);
/*!
* This is a constructor of UDPTransportSocketEndpoint with setting IP and port.
* @param ip IP address in IPv4 or IPv6 format.
* @param port Port ranging from 1 to 65535.
*/
UDPTransportSocketEndpoint(std::string ip, port_t port);

// Inherited from TransportSocketEndpoint
Expand All @@ -75,9 +54,11 @@ class UDPTransportSocketEndpoint: public p2pnet::net::TransportSocketEndpoint {
return copy;
}

virtual std::string toString();
virtual void fromString(std::string endpoint_s);
UDPTransportSocketEndpoint(std::string endpoint_s);
virtual void fromProtobuf(TransportSocketEndpoint_s tse_s);
virtual TransportSocketEndpoint_s toProtobuf();
UDPTransportSocketEndpoint(TransportSocketEndpoint_s tse_s);

UDPTransportSocketEndpoint(std::string tse_str);

virtual std::string toHRString();
};
Expand Down
6 changes: 4 additions & 2 deletions src/daemon/protobuf/TransportSocketEndpoint_s.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ package p2pnet.net;

enum TransportSocketEndpoint_type {
UDP = 0;
RELAY = 1;
}

message UDPTransportSocketEndpoint_s {
message TransportSocketEndpoint_s {
required TransportSocketEndpoint_type type = 1 [default = UDP];
required string ip = 2;
optional string ip = 2;
optional uint32 port = 3;
optional bytes th = 4;
}

message PeerRoute_s {
Expand Down
32 changes: 21 additions & 11 deletions src/daemon/relay/RelayTransportSocketEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,45 @@ RelayTransportSocketEndpoint::RelayTransportSocketEndpoint() {}
RelayTransportSocketEndpoint::~RelayTransportSocketEndpoint() {}

const crypto::hash_t& RelayTransportSocketEndpoint::getRelayHash() const {
return relay_hash;
return m_relay_th;
}

void RelayTransportSocketEndpoint::setRelayHash(
const crypto::hash_t& relayHash) {
relay_hash = relayHash;
m_relay_th = relayHash;
}

RelayTransportSocketEndpoint::RelayTransportSocketEndpoint(
crypto::hash_t relay_hash) {
this->relay_hash = relay_hash;
this->m_relay_th = relay_hash;
}

std::string RelayTransportSocketEndpoint::toString() {
return std::string(relay_hash.begin(), relay_hash.end());
void RelayTransportSocketEndpoint::fromProtobuf(net::TransportSocketEndpoint_s tse_s){
std::string relay_th_str = tse_s.th();
crypto::hash_t relay_th(relay_th_str.begin(), relay_th_str.end());
setRelayHash(relay_th);
}

void RelayTransportSocketEndpoint::fromString(std::string endpoint_s) {
relay_hash = crypto::hash_t(endpoint_s.begin(), endpoint_s.end());
net::TransportSocketEndpoint_s RelayTransportSocketEndpoint::toProtobuf(){
net::TransportSocketEndpoint_s tse_s;
tse_s.set_type(net::TransportSocketEndpoint_type::RELAY);

std::string relay_th_str = std::string(getRelayHash().begin(), getRelayHash().end());
tse_s.set_th(relay_th_str);
return tse_s;
}

RelayTransportSocketEndpoint::RelayTransportSocketEndpoint(
std::string endpoint_s) {
this->fromString(endpoint_s);
RelayTransportSocketEndpoint::RelayTransportSocketEndpoint(net::TransportSocketEndpoint_s tse_s) {
fromProtobuf(tse_s);
}

RelayTransportSocketEndpoint::RelayTransportSocketEndpoint(std::string tse_str){
fromString(tse_str);
}

std::string RelayTransportSocketEndpoint::toHRString(){
std::ostringstream os;
os << "TH:" << crypto::hashToHex(relay_hash);
os << "TH:" << crypto::hashToHex(m_relay_th);
return os.str();
}

Expand Down
10 changes: 6 additions & 4 deletions src/daemon/relay/RelayTransportSocketEndpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace p2pnet {
namespace relay {

class RelayTransportSocketEndpoint: public p2pnet::net::TransportSocketEndpoint {
crypto::hash_t relay_hash;
crypto::hash_t m_relay_th;
public:
RelayTransportSocketEndpoint();
virtual ~RelayTransportSocketEndpoint();
Expand All @@ -37,9 +37,11 @@ class RelayTransportSocketEndpoint: public p2pnet::net::TransportSocketEndpoint
return copy;
}

virtual std::string toString();
virtual void fromString(std::string endpoint_s);
RelayTransportSocketEndpoint(std::string endpoint_s);
virtual void fromProtobuf(net::TransportSocketEndpoint_s tse_s);
virtual net::TransportSocketEndpoint_s toProtobuf();
RelayTransportSocketEndpoint(net::TransportSocketEndpoint_s tse_s);

RelayTransportSocketEndpoint(std::string tse_str);

virtual std::string toHRString();
};
Expand Down

0 comments on commit 2c4ef23

Please sign in to comment.