在端对端文件传输中,没有服务器和客户端之分,每个端点都可以作为服务器为其余请求下载资源文件的请求者们提供下载的数据。
也可以作为客户端来向多个具有资源文件的其他对等端发送下载请求并接收对等端发来的资源文件数据。
所以端对端文件传输中,参与数据传输的各个节点之间是平等的,它们统一称呼彼此为“对等端”。
对于传输中的一个端点来说,它会维护两个队列:
1. 存放来自于其余对等端的请求下载资源的请求队列
2. 存放自己将要发往占有自己感兴趣的资源的对等端的请求队列
通常情况下,我们将第一种队列叫做被请求队列,也就是 "我" 这个端点被其余对等端请求的信息所存放的队列
第二种队列叫做请求队列,也就是“我”这个端点想起与的对等端发送的请求信息所存放的队列
// 更详细的信息 ,明天再继续整理,先把代码贴上
// Makefile
-
CPPFLAGS = -O3
-
LDFLAGS = -lglog
-
-
all: Main
-
-
clean :
-
rm -f *.o Main
-
-
Main: Main.o peer.o peer_queue.o
-
g++ -o $@ $^ $(LDFLAGS)
// peer.h
-
#ifndef PEER_H
-
#define PEER_H
-
-
#include <string>
-
#include <vector>
-
#include <time.h>
-
#include <stdint.h>
-
#include <cstring>
-
-
-
#include "../bit_map/bit_map.h"
-
-
enum state
-
{
-
INITIAL = 0 ,
-
HALF_SHAKED,
-
HAND_SHAKED,
-
SEND_BIT_FIELD,
-
RECV_BIT_FIELD,
-
EXCHAIGING_DATA,
-
CLOSING
-
} ;
-
-
const long MSG_LEN = (1024*2 + 1024*16) ;
-
-
typedef struct _request_piece
-
{
-
int index ;
-
int begin ;
-
int length ;
-
} request_piece_t ;
-
-
typedef struct _peer_node
-
{
-
int socket ;
-
char ip [16] ;
-
unsigned short port ;
-
char id[21] ;
-
-
int state ;
-
-
int am_chocking ;
-
int am_interested ;
-
int peer_chocking ;
-
int peer_interested ;
-
-
Bitmap *pBitmap ;
-
-
char *buff_in ;
-
int buff_in_len ;
-
-
char *msg_out ;
-
int msg_out_len ;
-
-
char *msg_buff_out ;
-
int msg_buf_out_len ;
-
-
// download reqeustes received from others
-
std::vector<request_piece_t> recv_download_request_queue ;
-
// uploaded requests send to others
-
std::vector<request_piece_t> send_upload_request_queue ;
-
-
unsigned int down_total ;
-
unsigned int up_total ;
-
-
time_t start_timestamp ;
-
time_t recet_timestamp ;
-
-
time_t last_down_timestamp ;
-
time_t last_up_timestamp ;
-
-
uint64_t down_count ;
-
uint64_t up_count ;
-
-
double down_rate ;
-
double up_rate ;
-
-
_peer_node ()
-
{
-
-
socket = -1 ;
-
memset(ip , 0 , 16 ) ;
-
port = 0 ;
-
memset(id , 0 , 21 ) ;
-
state = INITIAL ;
-
-
buff_in = NULL ;
-
buff_in_len = 0 ;
-
-
msg_out = NULL ;
-
msg_out_len = 0 ;
-
-
msg_buff_out = NULL ;
-
msg_buf_out_len = 0 ;
-
-
-
am_chocking = 1 ;
-
am_interested = 0 ;
-
peer_chocking = 1 ;
-
peer_interested = 0 ;
-
-
pBitmap = NULL ;
-
-
down_total = 0 ;
-
up_total = 0 ;
-
-
last_down_timestamp = 0 ;
-
last_up_timestamp = 0 ;
-
-
down_count = 0;
-
up_count = 0 ;
-
-
down_rate = 0.0 ;
-
up_rate = 0.0 ;
-
-
} // peer_node_t default value initialization
-
-
-
} peer_node_t ;
-
-
/// here we begin define the Peer class
-
-
class Peer
-
{
-
public :
-
peer_node_t peer_node ;
-
-
public :
-
Peer () ;
-
~Peer () ;
-
-
int cancel_recv_request_queue () ;
-
int cancel_send_request_queue () ;
-
void release_peer_node () ;
-
void print () ;
-
} ;
-
-
-
#endif // peer.h
// peer.cpp
-
#include <cstdio>
-
#include <iostream>
-
#include <vector>
-
#include <cstdlib>
-
#include <glog/logging.h>
-
-
#include "peer.h"
-
-
using namespace std ;
-
-
Peer::Peer ()
-
{
-
peer_node.buff_in = (char*)malloc(MSG_LEN) ;
-
-
if (peer_node.buff_in == NULL )
-
{
-
LOG(ERROR)<<"[error] failed to allocate space to peer_node.buff_in";
-
}
-
-
peer_node.msg_out = (char*)malloc(MSG_LEN) ;
-
-
if (peer_node.msg_out == NULL )
-
{
-
if ( peer_node.buff_in != NULL )
-
free ( peer_node.buff_in ) ;
-
LOG(ERROR)<<"[error] failed to allocate space to peer_node.msg_out";
-
}
-
-
peer_node.msg_buff_out = (char*)malloc (MSG_LEN) ;
-
-
if ( peer_node.msg_buff_out == NULL )
-
{
-
if ( peer_node.buff_in != NULL )
-
free( peer_node.buff_in ) ;
-
if ( peer_node.msg_out != NULL )
-
free ( peer_node.msg_out ) ;
-
-
LOG(ERROR)<<"[error] failed to allocate space to peer_node.msg_buff_out" ;
-
}
-
-
-
}
-
-
Peer::~Peer ()
-
{
-
cancel_recv_request_queue () ;
-
cancel_send_request_queue () ;
-
-
release_peer_node () ;
-
-
cout << "Peer::~Peer() is called , peer node id = "<<peer_node.id <<
-
" recv and send requests are canceled " << endl ;
-
}
-
-
void Peer::release_peer_node ( )
-
{
-
free ( peer_node.buff_in ) ;
-
free ( peer_node.msg_out ) ;
-
free ( peer_node.msg_buff_out ) ;
-
-
cout << "Peer::release_peer_node is called , every dynamic allocated space is free now " << endl ;
-
}
-
-
void Peer::print ( )
-
{
-
-
cout << "my peer id ----> "<< peer_node.id << endl ;
-
/*
-
cout << "am_chocking "<< peer_node.am_chocking << endl ;
-
cout << "am_interested " << peer_node.am_interested << endl;
-
-
cout << "peer_chocking" << peer_node.peer_chocking << endl ;
-
cout << "peer_interested" << peer_node.peer_interested << endl ;
-
-
cout << "down_rate " << peer_node.down_rate << endl ;
-
cout << "up_rate " << peer_node.up_rate << endl ;
-
*/
-
-
cout << "peer port " << peer_node.port << endl ;
-
}
-
-
-
int Peer::cancel_recv_request_queue ( )
-
{
-
peer_node.recv_download_request_queue.clear() ;
-
}
-
-
int Peer::cancel_send_request_queue ()
-
{
-
peer_node.send_upload_request_queue.clear () ;
-
}
// peer_queue.h
-
#include <time.h>
-
#include <vector>
-
#include <stdint.h>
-
-
#include "peer.h"
-
-
class Peer_Queue
-
{
-
public :
-
std::vector < Peer* > peer_queue ;
-
public :
-
Peer_Queue () ;
-
~Peer_Queue () ;
-
-
int add_peer_node ( Peer *peer_node ) ;
-
int del_peer_node ( Peer *peer_node ) ;
-
void release_peer_queue_nodes () ;
-
void print() ;
-
} ;
// peer_queue.cpp
-
#include <time.h>
-
#include <cstdio>
-
#include <iostream>
-
#include <vector>
-
#include <cstring>
-
-
#include "peer.h"
-
#include "peer_queue.h"
-
-
using namespace std ;
-
-
Peer_Queue::Peer_Queue ()
-
{}
-
-
Peer_Queue::~Peer_Queue ()
-
{
-
}
-
-
-
int Peer_Queue::add_peer_node ( Peer *peer_node )
-
{
-
peer_queue.push_back ( peer_node ) ;
-
}
-
-
-
// delete the node which has the same id
-
// as the input peer's
-
// do not forget release the space of peer's buff_in , msg_out , msg_buff_out
-
-
int Peer_Queue::del_peer_node ( Peer *peer )
-
{
-
int ret = -1 ;
-
-
for ( vector<Peer*>::iterator it = peer_queue.begin () ;
-
it != peer_queue.end () ; it++ )
-
{
-
if (!strcmp((*it)->peer_node.id , peer->peer_node.id))
-
{
-
cout << "success remove peer id = "<< (*it)->peer_node.port << " node " << endl ;
-
delete *it ;
-
-
it = peer_queue.erase(it) ;
-
-
ret = 0 ;
-
break ;
-
}
-
}
-
-
if ( ret == -1 )
-
cout << "not find target peer id = " << peer->peer_node.port << "node "<< endl ;
-
-
return ret ;
-
-
}
-
-
void Peer_Queue::release_peer_queue_nodes ()
-
{
-
for ( vector<Peer*>::iterator it = peer_queue.begin () ;
-
it != peer_queue.end() ; it++ )
-
{
-
// *it type: Peer*
-
delete *it ; // release the Peer object
-
// take it easy ,
-
// here delete will call the destructor method of Peer
-
// and we have already write the release_peer_node inside ~Peer
-
}
-
-
// then clear the queue
-
peer_queue.clear() ;
-
}
-
-
-
void Peer_Queue::print ()
-
{
-
// add by Aimer 2015/4/19
-
-
if ( peer_queue.empty () )
-
{
-
cout << "empty queue , without any peer node in it "<<endl ;
-
return ;
-
}
-
-
for ( vector<Peer*>::iterator it = peer_queue.begin () ;
-
it != peer_queue.end () ; it++ )
-
{
-
(*it)->print () ;
-
}
-
}
// Main.cpp
-
#include <cstdio>
-
#include <cstdlib>
-
#include <iostream>
-
-
#include "peer.h"
-
#include "peer_queue.h"
-
-
using namespace std ;
-
-
int main ( int argc , char **argv )
-
{
-
// testify : here we are going to test
-
// 1. Peer::Peer ()
-
// 2. Peer_Queue::add_peer_node ( Peer *peer_node)
-
// 3. Peer_Queue::print()
-
// 4. Peer::print () // in queue print method should test whether it is empty
-
-
-
Peer *pPeer ;
-
Peer_Queue peer_queue ;
-
-
for ( int i = 0 ; i < 20 ; i++ )
-
{
-
pPeer = new Peer () ;
-
pPeer->peer_node.port = (i+1) ;
-
peer_queue.add_peer_node (pPeer) ;
-
}
-
-
//peer_queue.print () ;
-
-
// tesitfy Peer_Queue::del_peer_node( Peer *peer_node )
-
-
/*
-
int len = peer_queue.peer_queue.size() ;
-
for ( int i = 0 ; i < len ; i++ )
-
{
-
peer_queue.del_peer_node ( peer_queue.peer_queue[i] ) ;
-
}
-
*/
-
-
// test Peer_Queue::release_peer_queue_nodes()
-
peer_queue.release_peer_queue_nodes () ;
-
-
-
// peer_queue.print () ;
-
-
-
// here we test delete one special in the peer_queue
-
for ( int i = 0 ; i < 15 ; i++ )
-
{
-
pPeer = new Peer () ;
-
pPeer->peer_node.port = i*10 ;
-
-
if (i == 7 )
-
strcpy (pPeer->peer_node.id , "delete_this_node") ;
-
else
-
strcpy (pPeer->peer_node.id , "no id") ;
-
-
// after this , this id will be the "delete_this_node"
-
peer_queue.add_peer_node (pPeer) ;
-
-
}
-
-
peer_queue.print () ;
-
-
// here we are going to test : delete the i = 7 by its id value
-
-
// and in case of leaking of the memory
-
// we write somthing in the ~Peer and release_peer_node methods to testfy
-
// whether these method will be executed by calling del_peer_node's delete command
-
-
-
pPeer = peer_queue.peer_queue[7] ;
-
-
peer_queue.del_peer_node( pPeer ) ;
-
-
peer_queue.print () ;
-
-
-
return 0 ;
-
}
end
阅读(1761) | 评论(0) | 转发(0) |