Chinaunix首页 | 论坛 | 博客
  • 博客访问: 579711
  • 博文数量: 104
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1559
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-21 00:58
个人简介

锻炼精神,首先要锻炼肉体

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: C/C++

2015-04-19 22:58:51

在端对端文件传输中,没有服务器和客户端之分,每个端点都可以作为服务器为其余请求下载资源文件的请求者们提供下载的数据。
也可以作为客户端来向多个具有资源文件的其他对等端发送下载请求并接收对等端发来的资源文件数据。
所以端对端文件传输中,参与数据传输的各个节点之间是平等的,它们统一称呼彼此为“对等端”。
对于传输中的一个端点来说,它会维护两个队列:
1. 存放来自于其余对等端的请求下载资源的请求队列
 2. 存放自己将要发往占有自己感兴趣的资源的对等端的请求队列
通常情况下,我们将第一种队列叫做被请求队列,也就是 "我" 这个端点被其余对等端请求的信息所存放的队列
第二种队列叫做请求队列,也就是“我”这个端点想起与的对等端发送的请求信息所存放的队列

// 更详细的信息 ,明天再继续整理,先把代码贴上

// Makefile 

点击(此处)折叠或打开

  1. CPPFLAGS = -O3
  2. LDFLAGS = -lglog
  3. all: Main
  4. clean :
  5. rm -f *.o Main
  6. Main: Main.o peer.o peer_queue.o
  7. g++ -o $@ $^ $(LDFLAGS)

// peer.h

点击(此处)折叠或打开

  1. #ifndef PEER_H
  2. #define PEER_H

  3. #include <string>
  4. #include <vector>
  5. #include <time.h>
  6. #include <stdint.h>
  7. #include <cstring>


  8. #include "../bit_map/bit_map.h"

  9. enum state
  10. {
  11.   INITIAL = 0 ,
  12.   HALF_SHAKED,
  13.   HAND_SHAKED,
  14.   SEND_BIT_FIELD,
  15.   RECV_BIT_FIELD,
  16.   EXCHAIGING_DATA,
  17.   CLOSING
  18. } ;

  19. const long MSG_LEN = (1024*2 + 1024*16) ;

  20. typedef struct _request_piece
  21. {
  22.   int index ;
  23.   int begin ;
  24.   int length ;
  25. } request_piece_t ;

  26. typedef struct _peer_node
  27. {
  28.   int socket ;
  29.   char ip [16] ;
  30.   unsigned short port ;
  31.   char id[21] ;
  32.   
  33.   int state ;
  34.   
  35.   int am_chocking ;
  36.   int am_interested ;
  37.   int peer_chocking ;
  38.   int peer_interested ;
  39.     
  40.   Bitmap *pBitmap ;
  41.   
  42.   char *buff_in ;
  43.   int buff_in_len ;
  44.  
  45.   char *msg_out ;
  46.   int msg_out_len ;
  47.   
  48.   char *msg_buff_out ;
  49.   int msg_buf_out_len ;
  50.  
  51.  // download reqeustes received from others
  52.   std::vector<request_piece_t> recv_download_request_queue ;
  53. // uploaded requests send to others
  54.   std::vector<request_piece_t> send_upload_request_queue ;

  55.   unsigned int down_total ;
  56.   unsigned int up_total ;

  57.   time_t start_timestamp ;
  58.   time_t recet_timestamp ;
  59.  
  60.   time_t last_down_timestamp ;
  61.   time_t last_up_timestamp ;
  62.  
  63.   uint64_t down_count ;
  64.   uint64_t up_count ;
  65.  
  66.   double down_rate ;
  67.   double up_rate ;

  68.   _peer_node ()
  69.   {

  70.     socket = -1 ;
  71.     memset(ip , 0 , 16 ) ;
  72.     port = 0 ;
  73.     memset(id , 0 , 21 ) ;
  74.     state = INITIAL ;
  75.     
  76.     buff_in = NULL ;
  77.     buff_in_len = 0 ;    

  78.     msg_out = NULL ;
  79.     msg_out_len = 0 ;
  80.     
  81.     msg_buff_out = NULL ;
  82.     msg_buf_out_len = 0 ;
  83.     
  84.     
  85.     am_chocking = 1 ;
  86.     am_interested = 0 ;
  87.     peer_chocking = 1 ;
  88.     peer_interested = 0 ;
  89.     
  90.     pBitmap = NULL ;
  91.     
  92.     down_total = 0 ;
  93.     up_total = 0 ;
  94.     
  95.     last_down_timestamp = 0 ;
  96.     last_up_timestamp = 0 ;
  97.     
  98.     down_count = 0;
  99.     up_count = 0 ;
  100.     
  101.     down_rate = 0.0 ;
  102.     up_rate = 0.0 ;    

  103.   } // peer_node_t default value initialization


  104. } peer_node_t ;

  105. /// here we begin define the Peer class

  106. class Peer
  107. {
  108. public :
  109.     peer_node_t peer_node ;
  110.  
  111.  public :
  112.     Peer () ;
  113.     ~Peer () ;
  114.       
  115.        int cancel_recv_request_queue () ;
  116.        int cancel_send_request_queue () ;
  117.        void release_peer_node () ;
  118.        void print () ;    
  119. } ;


  120. #endif // peer.h

// peer.cpp

点击(此处)折叠或打开

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <vector>
  4. #include <cstdlib>
  5. #include <glog/logging.h>

  6. #include "peer.h"

  7. using namespace std ;

  8. Peer::Peer ()
  9. {
  10.   peer_node.buff_in = (char*)malloc(MSG_LEN) ;
  11.   
  12.   if (peer_node.buff_in == NULL )
  13.   {
  14.     LOG(ERROR)<<"[error] failed to allocate space to peer_node.buff_in";
  15.   }
  16.  
  17.   peer_node.msg_out = (char*)malloc(MSG_LEN) ;
  18.   
  19.   if (peer_node.msg_out == NULL )
  20.   {
  21.     if ( peer_node.buff_in != NULL )
  22.      free ( peer_node.buff_in ) ;
  23.     LOG(ERROR)<<"[error] failed to allocate space to peer_node.msg_out";
  24.   }
  25.     
  26.   peer_node.msg_buff_out = (char*)malloc (MSG_LEN) ;
  27.  
  28.   if ( peer_node.msg_buff_out == NULL )
  29.   {
  30.     if ( peer_node.buff_in != NULL )
  31.      free( peer_node.buff_in ) ;
  32.     if ( peer_node.msg_out != NULL )
  33.      free ( peer_node.msg_out ) ;
  34.     
  35.     LOG(ERROR)<<"[error] failed to allocate space to peer_node.msg_buff_out" ;
  36.   }

  37.  
  38. }

  39. Peer::~Peer ()
  40. {
  41.     cancel_recv_request_queue () ;
  42.     cancel_send_request_queue () ;
  43.      
  44.     release_peer_node () ;

  45.         cout << "Peer::~Peer() is called , peer node id = "<<peer_node.id <<
  46.         " recv and send requests are canceled " << endl ;
  47. }

  48. void Peer::release_peer_node ( )
  49. {
  50.   free ( peer_node.buff_in ) ;
  51.   free ( peer_node.msg_out ) ;
  52.   free ( peer_node.msg_buff_out ) ;

  53.   cout << "Peer::release_peer_node is called , every dynamic allocated space is free now " << endl ;
  54. }

  55. void Peer::print ( )
  56. {

  57.     cout << "my peer id ----> "<< peer_node.id << endl ;
  58. /*
  59.     cout << "am_chocking "<< peer_node.am_chocking << endl ;
  60.     cout << "am_interested " << peer_node.am_interested << endl;
  61.   
  62.     cout << "peer_chocking" << peer_node.peer_chocking << endl ;
  63.     cout << "peer_interested" << peer_node.peer_interested << endl ;
  64.     
  65.     cout << "down_rate " << peer_node.down_rate << endl ;
  66.     cout << "up_rate " << peer_node.up_rate << endl ;
  67. */

  68.     cout << "peer port " << peer_node.port << endl ;
  69. }


  70. int Peer::cancel_recv_request_queue ( )
  71. {
  72.   peer_node.recv_download_request_queue.clear() ;
  73. }

  74. int Peer::cancel_send_request_queue ()
  75. {
  76.   peer_node.send_upload_request_queue.clear () ;
  77. }

// peer_queue.h

点击(此处)折叠或打开

  1. #include <time.h>
  2. #include <vector>
  3. #include <stdint.h>

  4. #include "peer.h"

  5. class Peer_Queue
  6. {
  7.   public :
  8.     std::vector < Peer* > peer_queue ;
  9.   public :
  10.     Peer_Queue () ;
  11.     ~Peer_Queue () ;
  12.         
  13.        int add_peer_node ( Peer *peer_node ) ;
  14.        int del_peer_node ( Peer *peer_node ) ;
  15.        void release_peer_queue_nodes () ;
  16.        void print() ;
  17. } ;

// peer_queue.cpp

点击(此处)折叠或打开

  1. #include <time.h>
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <vector>
  5. #include <cstring>

  6. #include "peer.h"
  7. #include "peer_queue.h"

  8. using namespace std ;

  9. Peer_Queue::Peer_Queue ()
  10. {}

  11. Peer_Queue::~Peer_Queue ()
  12. {
  13. }


  14. int Peer_Queue::add_peer_node ( Peer *peer_node )
  15. {
  16.   peer_queue.push_back ( peer_node ) ;
  17. }


  18. // delete the node which has the same id
  19. // as the input peer's
  20. // do not forget release the space of peer's buff_in , msg_out , msg_buff_out

  21. int Peer_Queue::del_peer_node ( Peer *peer )
  22. {
  23.    int ret = -1 ;

  24.    for ( vector<Peer*>::iterator it = peer_queue.begin () ;
  25.             it != peer_queue.end () ; it++ )
  26.    {
  27.     if (!strcmp((*it)->peer_node.id , peer->peer_node.id))
  28.     {
  29.         cout << "success remove peer id = "<< (*it)->peer_node.port << " node " << endl ;
  30.         delete *it ;
  31.     
  32.      it = peer_queue.erase(it) ;
  33.     
  34.         ret = 0 ;
  35.         break ;        
  36.      }
  37.    }

  38.   if ( ret == -1 )
  39.     cout << "not find target peer id = " << peer->peer_node.port << "node "<< endl ;
  40.  
  41.   return ret ;
  42.     
  43. }

  44. void Peer_Queue::release_peer_queue_nodes ()
  45. {
  46.    for ( vector<Peer*>::iterator it = peer_queue.begin () ;
  47.         it != peer_queue.end() ; it++ )
  48.    {
  49.     // *it type: Peer*
  50.     delete *it ; // release the Peer object
  51.                 // take it easy ,
  52.     // here delete will call the destructor method of Peer
  53.     // and we have already write the release_peer_node inside ~Peer
  54.    }

  55.    // then clear the queue
  56.   peer_queue.clear() ;
  57. }


  58. void Peer_Queue::print ()
  59. {
  60.    // add by Aimer 2015/4/19

  61.    if ( peer_queue.empty () )
  62.    {
  63.     cout << "empty queue , without any peer node in it "<<endl ;
  64.     return ;
  65.    }

  66.    for ( vector<Peer*>::iterator it = peer_queue.begin () ;
  67.         it != peer_queue.end () ; it++ )
  68.    {
  69.     (*it)->print () ;
  70.    }
  71. }

// Main.cpp

点击(此处)折叠或打开

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <iostream>

  4. #include "peer.h"
  5. #include "peer_queue.h"

  6. using namespace std ;

  7. int main ( int argc , char **argv )
  8. {
  9.   // testify : here we are going to test
  10.   // 1. Peer::Peer ()
  11.   // 2. Peer_Queue::add_peer_node ( Peer *peer_node)
  12.   // 3. Peer_Queue::print()
  13.   // 4. Peer::print () // in queue print method should test whether it is empty
  14.   

  15.   Peer *pPeer ;
  16.   Peer_Queue peer_queue ;

  17.   for ( int i = 0 ; i < 20 ; i++ )
  18.   {
  19.     pPeer = new Peer () ;    
  20.     pPeer->peer_node.port = (i+1) ;
  21.         peer_queue.add_peer_node (pPeer) ;
  22.   }

  23.  //peer_queue.print () ;

  24.   // tesitfy Peer_Queue::del_peer_node( Peer *peer_node )

  25. /*
  26.   int len = peer_queue.peer_queue.size() ;
  27.   for ( int i = 0 ; i < len ; i++ )
  28.   {
  29.       peer_queue.del_peer_node ( peer_queue.peer_queue[i] ) ;
  30.   }
  31. */
  32.  
  33. // test Peer_Queue::release_peer_queue_nodes()
  34.          peer_queue.release_peer_queue_nodes () ;


  35. // peer_queue.print () ;


  36. // here we test delete one special in the peer_queue
  37.    for ( int i = 0 ; i < 15 ; i++ )
  38.    {
  39.     pPeer = new Peer () ;
  40.     pPeer->peer_node.port = i*10 ;
  41.         
  42.         if (i == 7 )
  43.            strcpy (pPeer->peer_node.id , "delete_this_node") ;
  44.     else
  45.      strcpy (pPeer->peer_node.id , "no id") ;

  46.     // after this , this id will be the "delete_this_node"
  47.          peer_queue.add_peer_node (pPeer) ;

  48.   }
  49.     
  50.   peer_queue.print () ;
  51.  
  52. // here we are going to test : delete the i = 7 by its id value

  53. // and in case of leaking of the memory
  54. // we write somthing in the ~Peer and release_peer_node methods to testfy
  55. // whether these method will be executed by calling del_peer_node's delete command


  56.   pPeer = peer_queue.peer_queue[7] ;
  57.   
  58.   peer_queue.del_peer_node( pPeer ) ;

  59. peer_queue.print () ;


  60.   return 0 ;
  61. }
end
阅读(1761) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~