QNX环境使用iperf获取网络性能参数

[复制链接]
 楼主| keer_zu 发表于 2022-10-7 17:38 | 显示全部楼层 |阅读模式

代码,头文件:

  1. #ifndef __IPERF_COLLECTION_H__
  2. #define __IPERF_COLLECTION_H__

  3. #include <map>
  4. #include <pthread.h>
  5. #include "macros.h"

  6. using namespace std;

  7. typedef void ( * port_performance_calback_t ) ( void * data ) ;

  8. template<typename T>
  9. class Singleton{
  10. public:
  11.     static T& get_instance(){
  12.         static T instance;
  13.         return instance;
  14.     }
  15.     virtual ~Singleton(){
  16.         std::cout<<"destructor called!"<<std::endl;
  17.     }
  18.     Singleton(const Singleton&)=delete;
  19.     Singleton& operator =(const Singleton&)=delete;
  20. protected:
  21.     Singleton(){
  22.         std::cout<<"constructor called!"<<std::endl;
  23.     }

  24. };



  25. class IperfCollection:public       Singleton<IperfCollection> {
  26.         friend class Singleton<IperfCollection>;
  27. public:

  28.        
  29.         //static shared_ptr<IperfCollection> getInstance(){
  30.         //        return _instance;
  31.         //}

  32.         int Init(port_performance_calback_t cb);
  33.        
  34.        
  35. private:

  36.         int createFifo();
  37.         int stringParsing(string str,int port);
  38.         static void *iperfDataCollection(void *data);
  39.         IperfCollection():_callback(NULL){}
  40.         ~IperfCollection(){}
  41.        
  42.         pthread_t        _threadId;
  43.         port_performance_calback_t _callback;
  44.         //static shared_ptr<IperfCollection> _instance;
  45.         map<int,string> _portFifo;
  46.         map<int,int> _portFd;
  47. };






  48. #endif


评论

采用模板类实现单例模式  发表于 2022-10-7 17:38
 楼主| keer_zu 发表于 2022-10-7 17:39 | 显示全部楼层
.cpp文件:
  1. #include <fstream>
  2. #include <fcntl.h>  
  3. #include <iostream>
  4. #include<regex>
  5. #include "IperfCollection.h"


  6. using namespace std;



  7. int IperfCollection::createFifo()
  8. {
  9.         ifstream fin("ethernetTest.sh");
  10.         string::const_iterator itStart;
  11.         string::const_iterator itEnd;
  12.         string scriptLine;

  13.         int ret;
  14.         regex filePath("/var/fifo_([0-9]\\d*)");
  15.        
  16.         while( getline(fin,scriptLine) ){       
  17.                 itStart = scriptLine.begin();
  18.                 itEnd = scriptLine.end();
  19.                 smatch result;
  20.                 if (regex_search(itStart, itEnd, result, filePath)) {
  21.                         string str_port = result[1];
  22.                         int port = atoi(str_port.c_str());
  23.                         cout << "+++++ file path: " << result[0] << " num: " << port << endl;
  24.                         string filepath = result[0];
  25.                         ret = mkfifo(filepath.c_str(),S_IRUSR | S_IWUSR);
  26.                         cout << "ret: " << ret << endl;
  27.                         _portFifo[port] = filepath;
  28.                 } else {
  29.                         cout << "--- no search ---" << endl;
  30.                 }
  31.         }
  32.        
  33.         return 0;
  34. }


  35. struct rtl9072_port_status
  36. {
  37.   uint32_t port ;
  38.   uint32_t tx_speed ;
  39.   uint32_t tx_pack_drop_rate ;
  40.   uint32_t rx_speed ;
  41.   uint32_t rx_pack_drop_rate ;
  42. } ;

  43. int IperfCollection::stringParsing(string str, int port)
  44. {
  45.         struct rtl9072_port_status status;
  46.         smatch result;
  47.         port_performance_calback_t call = (port_performance_calback_t)_callback;
  48.         regex BandwidthMbits("-?(((([1-9]\\d*)|([0-9]\\d)|(0))\\.(([0-9]\\d*)|([0-9])\\d))|(0)|([0-9]\\d*))\\sMbits");
  49.         regex BandwidthKbits("-?(((([1-9]\\d*)|([0-9]\\d)|(0))\\.(([0-9]\\d*)|([0-9])\\d))|(0)|([0-9]\\d*))\\sKbits");       
  50.         regex BandwidthGbits("-?(((([1-9]\\d*)|([0-9]\\d)|(0))\\.(([0-9]\\d*)|([0-9])\\d))|(0)|([0-9]\\d*))\\sGbits");
  51.        
  52.         regex  Packageloss("\\(-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*)|([0-9]\\d*))\\%\\)");

  53.         string::const_iterator iterStart = str.begin();
  54.         string::const_iterator iterEnd = str.end();
  55.         string temp,value;

  56.         if (regex_search(iterStart, iterEnd, result, BandwidthMbits))
  57.         {
  58.                 temp = result[0];
  59.                 cout << "==" << port << "== BandwidthMbits: " << result[1] << endl;
  60.                 //iterStart = result[0].second;       
  61.         } else if (regex_search(iterStart, iterEnd, result, BandwidthKbits)) {
  62.                         temp = result[0];
  63.                         cout << "==" << port << "== bandwidthKbits: " << result[1] << endl;
  64.                         //iterStart = result[0].second;       
  65.         } else if (regex_search(iterStart, iterEnd, result, BandwidthGbits)) {
  66.                         temp = result[0];
  67.                         cout << "==" << port << "== BandwidthGbits: " << result[1] << endl;
  68.                         //iterStart = result[0].second;       
  69.         }
  70.        
  71.         if (regex_search(iterStart, iterEnd, result, Packageloss))
  72.         {
  73.                 temp = result[0];
  74.                 cout << "==" << port << "== package loss: " << result[1] << endl;
  75.                 //iterStart = result[0].second;       
  76.         }

  77.         if(call != NULL)
  78.                 call(&status);

  79.         return 0;
  80. }



  81. void * IperfCollection::iperfDataCollection(void *data)
  82. {
  83.         int ret,fd;
  84.         IperfCollection *collection = (IperfCollection *)data;
  85.         map<int,string>::iterator iter_pf;
  86.         for(iter_pf = (collection->_portFifo).begin();iter_pf != (collection->_portFifo).end();iter_pf ++) {
  87.                 string file = iter_pf->second;
  88.                 int port = iter_pf->first;
  89.                 fd = open(file.c_str(),O_RDONLY | O_NONBLOCK,0);
  90.                 if(fd < 0){
  91.                         cout << "open error!\n" ;
  92.                         return NULL;
  93.                 }

  94.                 collection->_portFd[port] = fd;
  95.         }
  96.        
  97.         char buf1[500];
  98.         while(1) {
  99.                 for(iter_pf = (collection->_portFifo).begin();iter_pf != (collection->_portFifo).end();iter_pf ++) {
  100.                         string file = iter_pf->second;
  101.                         int port = iter_pf->first;
  102.                         ssize_t s = read(collection->_portFd[port],buf1,sizeof(buf1)-1);
  103.                         if(s > 0) {
  104.                                 buf1[s-1] = 0;
  105.                                 string row = buf1;
  106.                                 cout << row << endl;
  107.                                 //test1(row,port);
  108.                                 collection->stringParsing(row,port);
  109.                         } else if (s == 0) {
  110.                                 cout << "client quit,exit ..." << endl;
  111.                                 continue;
  112.                         }
  113.                 }

  114.                 sleep(1);
  115.         }
  116.         close(ret);
  117.         return NULL;
  118. }



  119. int IperfCollection::Init(port_performance_calback_t cb)
  120. {
  121.         int ret;
  122.        
  123.         ret = this->createFifo();
  124.         if (ret != 0) {
  125.                 cout << "createFifo error" << endl;
  126.                 return ret;
  127.         }

  128.         if(cb != NULL)
  129.                 _callback = cb;

  130.         pthread_create(&_threadId, NULL, &iperfDataCollection ,this);
  131.        
  132.         return 0;
  133. }





 楼主| keer_zu 发表于 2022-10-7 17:40 | 显示全部楼层
main.cpp

  1. void callback(void *data)
  2. {
  3.         cout << "======== call back =====" << endl;
  4. }


  5. int main(void)
  6. {
  7.         IperfCollection* collection;
  8.         collection = &IperfCollection::get_instance();
  9.         collection->Init(callback);

  10.         auto re = system("./ethernetTest.sh");
  11.     cout << "re = " << re << endl;

  12.        
  13.        
  14. /*
  15.         string line;
  16.         pthread_t        thread_id;
  17.         ifstream fin("ethernetTest.sh");
  18.         //test();
  19.         string s;  
  20.    

  21.         int ret,fd;
  22.         regex filePath("/var/fifo_([0-9]\\d*)");
  23.        
  24.         string::const_iterator itStart;// = fp.begin();
  25.         string::const_iterator itEnd;// = fp.end();
  26.        
  27.         cout << "==============================" << endl;
  28.        
  29.         while( getline(fin,s) ){       
  30.                 itStart = s.begin();
  31.                 itEnd = s.end();
  32.                 smatch result;
  33.                 if (regex_search(itStart, itEnd, result, filePath)) {
  34.                         string str_port = result[1];
  35.                         int port = atoi(str_port.c_str());
  36.                         cout << "+++++ file path: " << result[0] << " num: " << port << endl;
  37.                         string filepath = result[0];
  38.                         ret = mkfifo(filepath.c_str(),S_IRUSR | S_IWUSR);
  39.                         cout << "ret: " << ret << endl;
  40.                         fifo_port[port] = filepath;
  41.                 } else {
  42.                         cout << "--- no search ---" << endl;
  43.                 }
  44.         }
  45.        
  46.         pthread_create(&thread_id, NULL, &iperfDataCollection ,NULL);

  47.         auto re = system("./ethernetTest.sh");
  48.     cout << "re = " << re << endl;

  49.         map<int,string>::iterator iter_pf;
  50.         for(iter_pf = fifo_port.begin();iter_pf != fifo_port.end();iter_pf ++) {
  51.                 string file = iter_pf->second;
  52.                 int port = iter_pf->first;
  53.                 fd = open(file.c_str(),O_RDONLY | O_NONBLOCK,0);
  54.                 if(fd < 0){
  55.                         cout << "open error!\n" ;
  56.                         return -1;
  57.                 }

  58.                 port_fd[port] = fd;
  59.         }
  60.        
  61.         char buf1[500];
  62.         while(1) {
  63.                 //cout << "please waiting...\n";
  64.                 for(iter_pf = fifo_port.begin();iter_pf != fifo_port.end();iter_pf ++) {
  65.                         string file = iter_pf->second;
  66.                         int port = iter_pf->first;
  67.                         ssize_t s = read(port_fd[port],buf1,sizeof(buf1)-1);
  68.                         if(s > 0) {
  69.                                 buf1[s-1] = 0;
  70.                                 string row = buf1;//"[  5] 38.0-39.0 sec  11.5 MBytes  96.4 Mbits/sec   0.294 ms   11/ 8208 (0.13%)";//buf1;
  71.                                 cout << row << endl;
  72.                                 test1(row,port);
  73.                         } else if (s == 0) {
  74.                                 cout << "client quit,exit ..." << endl;
  75.                                 continue;
  76.                         }
  77.                 }

  78.                 sleep(1);
  79.         }
  80.         close(ret);*/
  81.         while(1) {
  82.                 sleep(10);
  83.         }
  84. }
 楼主| keer_zu 发表于 2022-10-7 17:43 | 显示全部楼层
 楼主| keer_zu 发表于 2022-10-7 17:43 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1478

主题

12915

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部

1478

主题

12915

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部