两个进程通过fifo进行通信和解析

[复制链接]
 楼主| keer_zu 发表于 2022-9-27 13:42 | 显示全部楼层 |阅读模式
#申请原创# #技术资源# 可以监控终端的输入和其他程序写在此FIFO的数据,并对特殊字段进行解析。
运行环境:qnx

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>  
  4. #include <iostream>
  5. #include<regex>



  6. //int mkfifo( const char* path,
  7. //            mode_t mode );

  8. using namespace std;

  9. #include <string>

  10. void test()
  11. {
  12.         std::regex reg("-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*))\\sMbits");
  13.         std::regex reg1("-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*))");

  14.         std::string str = "这是中文示例abc 1234.2  a 匹配浮点数值XX5678 2.6  5.789  -0.23411 +01.45";

  15.         string row = "[  5] 38.0-39.0 sec  11.5 MBytes  96.4 Mbits/sec   0.294 ms   11/ 8208 (0.13%)";

  16.         const std::sregex_iterator end;
  17.         for (std::sregex_iterator iter(std::cbegin(str), std::cend(str), reg1); iter != end;++iter)
  18.                 std::cout << iter->str() << std::endl;

  19.         for (std::sregex_iterator iter(std::cbegin(row), std::cend(row), reg); iter != end;++iter)
  20.                 std::cout << iter->str() << std::endl;

  21. }

  22. void test1(string str)
  23. {
  24.        
  25.         //string str = "Hello 2018, Bye 2017";
  26.         //string str = "[  5] 38.0-39.0 sec  11.5 MBytes  96.4 Mbits/sec   0.294 ms   11/ 8208 (0.13%)";
  27.         smatch result;
  28.         regex Bandwidth("-?(([0-9]\\d*\\.[1-9]\\d*)|(0)|(0\\.[1-9]\\d*)|([0-9]\\d*))\\sMbits");//(".+(\\d Mbits/sec).+(\\(\\d%\\))");        //
  29.         //regex  Packageloss("\\(([0-9]+%)\\)");
  30.         regex  Packageloss("\\(-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*)|([0-9]\\d*))\\%\\)");
  31.         //regex Value("-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*))");
  32.        
  33.        
  34.        
  35.         //迭代器声明
  36.         string::const_iterator iterStart = str.begin();
  37.         string::const_iterator iterEnd = str.end();
  38.         string temp,value;
  39.         cout << "==============================" << endl;
  40.         while (regex_search(iterStart, iterEnd, result, Bandwidth))
  41.         {
  42.                 temp = result[0];
  43.                 cout << "==== bandwidth: " << result[1] << endl;
  44.                 iterStart = result[0].second;        //更新搜索起始位置,搜索剩下的字符串
  45.         }
  46.         cout << "----------------------------------" << endl;
  47.         while (regex_search(iterStart, iterEnd, result, Packageloss))
  48.         {
  49.                 temp = result[0];
  50.                 cout << "==== package loss: " << result[1] << endl;
  51.                 iterStart = result[0].second;        //更新搜索起始位置,搜索剩下的字符串
  52.         }
  53.         cout << "-------+++------------------------" << endl;
  54. }


  55. int main(void)
  56. {
  57.         test();
  58.         int ret,fd,pid;
  59.         //regex Bandwidth("\\s\\s([1-9,\\\\.]+)\\sMbits");
  60.         regex Bandwidth(".+(\\d Mbits/sec).+(\\(\\d%\\))");
  61.         cout << "\\s\\s([1-9,\\\\.]+)\\sMbits" << endl;
  62.         //"MBytes  .* Mbits/sec"
  63.         regex Packageloss("\\(([0-9]+%)\\)");
  64.        
  65.        
  66.         ret = mkfifo("/var/fifo1",S_IRUSR | S_IWUSR);
  67.         cout << "ret: " << ret << endl;

  68.         pid = fork();
  69.         if (pid > 0) {
  70.                 cout << "- parent -" << endl;
  71.                 fd = open("/var/fifo1",O_RDONLY,0);
  72.                 if(fd < 0){
  73.                         cout << "open error!\n" ;
  74.                         return -1;
  75.                 }
  76.                 char buf1[500];
  77.                 while(1) {
  78.                         cout << "please waiting...\n";
  79.                         ssize_t s = read(fd,buf1,sizeof(buf1)-1);
  80.                         if(s > 0) {
  81.                                 buf1[s-1] = 0;
  82.                                 string row = buf1;//"[  5] 38.0-39.0 sec  11.5 MBytes  96.4 Mbits/sec   0.294 ms   11/ 8208 (0.13%)";//buf1;
  83.                                 cout << row << endl;
  84.                                 test1(row);
  85.                                

  86.                         } else if (s == 0) {
  87.                                 cout << "client quit,exit ..." << endl;
  88.                                 break;
  89.                         }
  90.                 }
  91.                
  92.         } else if (pid == 0){
  93.                 cout << "- child -" << endl;
  94.                 fd = open("/var/fifo1",O_WRONLY,0);
  95.                 if(fd < 0){
  96.                         cout << "open error!\n" ;
  97.                         return -1;
  98.                 }
  99.                 char buf1[500];
  100.                 while(1) {
  101.                         cout << "client\n";
  102.                         fflush(stdout);
  103.                 ssize_t s = read(0,buf1,sizeof(buf1)-1);
  104.                 if(s > 0) {
  105.                                 buf1[s] = 0;
  106.                                 write(fd,buf1,strlen(buf1));
  107.                 }
  108.                 }
  109.         } else {
  110.                 cout << "fork error!\n" ;
  111.         }
  112.        
  113.                 cout << "file is exit!\n" ;
  114.                 close(ret);
  115.        

  116.        
  117. }

 楼主| keer_zu 发表于 2022-9-27 13:44 | 显示全部楼层
部分运行结果日志:

  1. ==============================
  2. ----------------------------------
  3. -------+++------------------------
  4. please waiting...
  5. a123 Mbits
  6. a123 Mbits
  7. client
  8. ==============================
  9. ==== bandwidth: 123
  10. ----------------------------------
  11. -------+++------------------------
  12. please waiting...
  13. 456 Mbits
  14. 456 Mbits
  15. client
  16. ==============================
  17. ==== bandwidth: 456
  18. ----------------------------------
  19. -------+++------------------------
  20. please waiting...
  21. [  3] local 192.168.0.1 port 1200 connected with 192.168.0.2 port 58812 (peer 2.1.8)
  22. ==============================
  23. ----------------------------------
  24. -------+++------------------------
  25. please waiting...
  26. [  3]  0.0-10.0 sec   114 MBytes  95.7 Mbits/sec   1.044 ms    1/81440 (0.0012%)
  27. ==============================
  28. ==== bandwidth: 95.7
  29. ----------------------------------
  30. ==== package loss: 0.0012
  31. -------+++------------------------
  32. please waiting...

 楼主| keer_zu 发表于 2022-9-27 13:54 | 显示全部楼层
监控另外一个程序日志的实现:

这里是iperf,一个网络带宽丢包率等测试工具。

  1. # iperf2 -u -s -p 1200 > /var/fifo1
 楼主| keer_zu 发表于 2022-9-27 14:02 | 显示全部楼层
另一台机器上,iperf客户端启动:

  1. iperf-2.1.8-win.exe -c 192.168.0.1  -b1000M -t 100 -B 192.168.0.2 -p 1200 -u -i 1 -t 10
 楼主| keer_zu 发表于 2022-9-27 14:06 | 显示全部楼层
@21ic小管家 @21小跑堂
原创,请给个编辑推荐
 楼主| keer_zu 发表于 2022-9-27 18:18 | 显示全部楼层
增加读取文件每一行并且解析:

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>  
  4. #include <iostream>
  5. #include<regex>
  6. #include <fstream>



  7. //int mkfifo( const char* path,
  8. //            mode_t mode );

  9. using namespace std;

  10. #include <string>

  11. void test()
  12. {
  13.         std::regex reg("-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*))\\sMbits");
  14.         std::regex reg1("-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*))");

  15.         std::string str = "这是中文示例abc 1234.2  a 匹配浮点数值XX5678 2.6  5.789  -0.23411 +01.45";

  16.         string row = "[  5] 38.0-39.0 sec  11.5 MBytes  96.4 Mbits/sec   0.294 ms   11/ 8208 (0.13%)";

  17.         const std::sregex_iterator end;
  18.         for (std::sregex_iterator iter(std::cbegin(str), std::cend(str), reg1); iter != end;++iter)
  19.                 std::cout << iter->str() << std::endl;

  20.         for (std::sregex_iterator iter(std::cbegin(row), std::cend(row), reg); iter != end;++iter)
  21.                 std::cout << iter->str() << std::endl;

  22. }

  23. void test1(string str)
  24. {
  25.        
  26.         //string str = "Hello 2018, Bye 2017";
  27.         //string str = "[  5] 38.0-39.0 sec  11.5 MBytes  96.4 Mbits/sec   0.294 ms   11/ 8208 (0.13%)";
  28.         smatch result;
  29.         regex Bandwidth("-?(([0-9]\\d*\\.[1-9]\\d*)|(0)|(0\\.[1-9]\\d*)|([0-9]\\d*))\\sMbits");//(".+(\\d Mbits/sec).+(\\(\\d%\\))");        //
  30.         //regex  Packageloss("\\(([0-9]+%)\\)");
  31.         regex  Packageloss("\\(-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*)|([0-9]\\d*))\\%\\)");
  32.         //regex Value("-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*))");
  33.        
  34.        
  35.        
  36.         //迭代器声明
  37.         string::const_iterator iterStart = str.begin();
  38.         string::const_iterator iterEnd = str.end();
  39.         string temp,value;
  40.         cout << "==============================" << endl;
  41.         while (regex_search(iterStart, iterEnd, result, Bandwidth))
  42.         {
  43.                 temp = result[0];
  44.                 cout << "==== bandwidth: " << result[1] << endl;
  45.                 iterStart = result[0].second;        //更新搜索起始位置,搜索剩下的字符串
  46.         }
  47.         cout << "----------------------------------" << endl;
  48.         while (regex_search(iterStart, iterEnd, result, Packageloss))
  49.         {
  50.                 temp = result[0];
  51.                 cout << "==== package loss: " << result[1] << endl;
  52.                 iterStart = result[0].second;        //更新搜索起始位置,搜索剩下的字符串
  53.         }
  54.         cout << "-------+++------------------------" << endl;
  55. }


  56. int main(void)
  57. {
  58.         string line;
  59.           
  60.            //打开文件data.txt
  61.         ifstream fin("ethernetTest.sh");
  62.        
  63.         string s;  
  64.    

  65.         test();
  66.         int ret,fd,pid;
  67.         //regex Bandwidth("\\s\\s([1-9,\\\\.]+)\\sMbits");
  68.         regex Bandwidth(".+(\\d Mbits/sec).+(\\(\\d%\\))");
  69.         cout << "\\s\\s([1-9,\\\\.]+)\\sMbits" << endl;
  70.         //"MBytes  .* Mbits/sec"
  71.         regex Packageloss("\\(([0-9]+%)\\)");
  72.         regex filePath("/var/fifo\\d*");
  73.         string fp = "iperf2 -u -s -p 1200 > /var/fifo01 &";
  74.         string::const_iterator itStart;// = fp.begin();
  75.         string::const_iterator itEnd;// = fp.end();
  76.        
  77.         cout << "==============================" << endl;
  78.        
  79.         while( getline(fin,s) ){       
  80.                 itStart = s.begin();
  81.                 itEnd = s.end();
  82.                 smatch result;
  83.                 if (regex_search(itStart, itEnd, result, filePath)) {
  84.                         cout << "+++++ file path: " << result[0] << endl;
  85.                 } else {
  86.                         cout << "--- no search ---" << endl;
  87.                 }
  88.         }
  89.        
  90.         ret = mkfifo("/var/fifo1",S_IRUSR | S_IWUSR);
  91.         cout << "ret: " << ret << endl;

  92.         pid = fork();
  93.         if (pid > 0) {
  94.                 cout << "- parent -" << endl;
  95.                 fd = open("/var/fifo1",O_RDONLY,0);
  96.                 if(fd < 0){
  97.                         cout << "open error!\n" ;
  98.                         return -1;
  99.                 }
  100.                 char buf1[500];
  101.                 while(1) {
  102.                         cout << "please waiting...\n";
  103.                         ssize_t s = read(fd,buf1,sizeof(buf1)-1);
  104.                         if(s > 0) {
  105.                                 buf1[s-1] = 0;
  106.                                 string row = buf1;//"[  5] 38.0-39.0 sec  11.5 MBytes  96.4 Mbits/sec   0.294 ms   11/ 8208 (0.13%)";//buf1;
  107.                                 cout << row << endl;
  108.                                 test1(row);
  109.                                

  110.                         } else if (s == 0) {
  111.                                 cout << "client quit,exit ..." << endl;
  112.                                 break;
  113.                         }
  114.                 }
  115.                
  116.         } else if (pid == 0){
  117.                 cout << "- child -" << endl;
  118.                 fd = open("/var/fifo1",O_WRONLY,0);
  119.                 if(fd < 0){
  120.                         cout << "open error!\n" ;
  121.                         return -1;
  122.                 }
  123.                 char buf1[500];
  124.                 while(1) {
  125.                         cout << "client\n";
  126.                         fflush(stdout);
  127.                 ssize_t s = read(0,buf1,sizeof(buf1)-1);
  128.                 if(s > 0) {
  129.                                 buf1[s] = 0;
  130.                                 write(fd,buf1,strlen(buf1));
  131.                 }
  132.                 }
  133.         } else {
  134.                 cout << "fork error!\n" ;
  135.         }
  136.        
  137.                 cout << "file is exit!\n" ;
  138.                 close(ret);
  139.        

  140.        
  141. }

 楼主| keer_zu 发表于 2022-9-28 09:31 | 显示全部楼层
在正则表达式里面先找出fifo文件名和路径,然后找出端口号:

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>  
  4. #include <iostream>
  5. #include<regex>
  6. #include <fstream>



  7. //int mkfifo( const char* path,
  8. //            mode_t mode );

  9. using namespace std;

  10. #include <string>

  11. void test()
  12. {
  13.         std::regex reg("-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*))\\sMbits");
  14.         std::regex reg1("-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*))");

  15.         std::string str = "这是中文示例abc 1234.2  a 匹配浮点数值XX5678 2.6  5.789  -0.23411 +01.45";

  16.         string row = "[  5] 38.0-39.0 sec  11.5 MBytes  96.4 Mbits/sec   0.294 ms   11/ 8208 (0.13%)";

  17.         const std::sregex_iterator end;
  18.         for (std::sregex_iterator iter(std::cbegin(str), std::cend(str), reg1); iter != end;++iter)
  19.                 std::cout << iter->str() << std::endl;

  20.         for (std::sregex_iterator iter(std::cbegin(row), std::cend(row), reg); iter != end;++iter)
  21.                 std::cout << iter->str() << std::endl;

  22. }

  23. void test1(string str)
  24. {
  25.        
  26.         //string str = "Hello 2018, Bye 2017";
  27.         //string str = "[  5] 38.0-39.0 sec  11.5 MBytes  96.4 Mbits/sec   0.294 ms   11/ 8208 (0.13%)";
  28.         smatch result;
  29.         regex Bandwidth("-?(([0-9]\\d*\\.[1-9]\\d*)|(0)|(0\\.[1-9]\\d*)|([0-9]\\d*))\\sMbits");//(".+(\\d Mbits/sec).+(\\(\\d%\\))");        //
  30.         //regex  Packageloss("\\(([0-9]+%)\\)");
  31.         regex  Packageloss("\\(-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*)|([0-9]\\d*))\\%\\)");
  32.         //regex Value("-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*))");
  33.        
  34.        
  35.        
  36.         //迭代器声明
  37.         string::const_iterator iterStart = str.begin();
  38.         string::const_iterator iterEnd = str.end();
  39.         string temp,value;
  40.         cout << "==============================" << endl;
  41.         while (regex_search(iterStart, iterEnd, result, Bandwidth))
  42.         {
  43.                 temp = result[0];
  44.                 cout << "==== bandwidth: " << result[1] << endl;
  45.                 iterStart = result[0].second;        //更新搜索起始位置,搜索剩下的字符串
  46.         }
  47.         cout << "----------------------------------" << endl;
  48.         while (regex_search(iterStart, iterEnd, result, Packageloss))
  49.         {
  50.                 temp = result[0];
  51.                 cout << "==== package loss: " << result[1] << endl;
  52.                 iterStart = result[0].second;        //更新搜索起始位置,搜索剩下的字符串
  53.         }
  54.         cout << "-------+++------------------------" << endl;
  55. }


  56. int main(void)
  57. {
  58.         string line;
  59.           
  60.            //打开文件data.txt
  61.         ifstream fin("ethernetTest.sh");
  62.        
  63.         string s;  
  64.    

  65.         test();
  66.         int ret,fd,pid;
  67.         //regex Bandwidth("\\s\\s([1-9,\\\\.]+)\\sMbits");
  68.         regex Bandwidth(".+(\\d Mbits/sec).+(\\(\\d%\\))");
  69.         cout << "\\s\\s([1-9,\\\\.]+)\\sMbits" << endl;
  70.         //"MBytes  .* Mbits/sec"
  71.         regex Packageloss("\\(([0-9]+%)\\)");
  72.         regex filePath("/var/fifo_([0-9]\\d*)");
  73.        
  74.         string::const_iterator itStart;// = fp.begin();
  75.         string::const_iterator itEnd;// = fp.end();
  76.        
  77.         cout << "==============================" << endl;
  78.        
  79.         while( getline(fin,s) ){       
  80.                 itStart = s.begin();
  81.                 itEnd = s.end();
  82.                 smatch result;
  83.                 if (regex_search(itStart, itEnd, result, filePath)) {
  84.                         cout << "+++++ file path: " << result[0] << " num: " << result[1] << endl;
  85.                 } else {
  86.                         cout << "--- no search ---" << endl;
  87.                 }
  88.         }
  89.        
  90.         ret = mkfifo("/var/fifo1",S_IRUSR | S_IWUSR);
  91.         cout << "ret: " << ret << endl;

  92.         pid = fork();
  93.         if (pid > 0) {
  94.                 cout << "- parent -" << endl;
  95.                 fd = open("/var/fifo1",O_RDONLY,0);
  96.                 if(fd < 0){
  97.                         cout << "open error!\n" ;
  98.                         return -1;
  99.                 }
  100.                 char buf1[500];
  101.                 while(1) {
  102.                         cout << "please waiting...\n";
  103.                         ssize_t s = read(fd,buf1,sizeof(buf1)-1);
  104.                         if(s > 0) {
  105.                                 buf1[s-1] = 0;
  106.                                 string row = buf1;//"[  5] 38.0-39.0 sec  11.5 MBytes  96.4 Mbits/sec   0.294 ms   11/ 8208 (0.13%)";//buf1;
  107.                                 cout << row << endl;
  108.                                 test1(row);
  109.                                

  110.                         } else if (s == 0) {
  111.                                 cout << "client quit,exit ..." << endl;
  112.                                 break;
  113.                         }
  114.                 }
  115.                
  116.         } else if (pid == 0){
  117.                 cout << "- child -" << endl;
  118.                 fd = open("/var/fifo1",O_WRONLY,0);
  119.                 if(fd < 0){
  120.                         cout << "open error!\n" ;
  121.                         return -1;
  122.                 }
  123.                 char buf1[500];
  124.                 while(1) {
  125.                         cout << "client\n";
  126.                         fflush(stdout);
  127.                 ssize_t s = read(0,buf1,sizeof(buf1)-1);
  128.                 if(s > 0) {
  129.                                 buf1[s] = 0;
  130.                                 write(fd,buf1,strlen(buf1));
  131.                 }
  132.                 }
  133.         } else {
  134.                 cout << "fork error!\n" ;
  135.         }
  136.        
  137.                 cout << "file is exit!\n" ;
  138.                 close(ret);
  139.        

  140.        
  141. }



运行结果:

  1. # ./fifo_test
  2. 1234.2
  3. 2.6
  4. 5.789
  5. -0.23411
  6. 1.45
  7. 96.4 Mbits
  8. \s\s([1-9,\\.]+)\sMbits
  9. ==============================
  10. --- no search ---
  11. --- no search ---
  12. --- no search ---
  13. --- no search ---
  14. --- no search ---
  15. +++++ file path: /var/fifo_1200 num: 1200
  16. +++++ file path: /var/fifo_1201 num: 1201
  17. +++++ file path: /var/fifo_1202 num: 1202
  18. +++++ file path: /var/fifo_1203 num: 1203
  19. +++++ file path: /var/fifo_1205 num: 1205
  20. --- no search ---
  21. +++++ file path: /var/fifo_1206 num: 1206
  22. +++++ file path: /var/fifo_1207 num: 1207
  23. +++++ file path: /var/fifo_1209 num: 1209
  24. +++++ file path: /var/fifo_1210 num: 1210
  25. --- no search ---
  26. --- no search ---
  27. --- no search ---
  28. --- no search ---
  29. --- no search ---
  30. ret: -1
  31. - parent -
  32. - child -
  33. please waiting...
  34. client

 楼主| keer_zu 发表于 2022-10-9 09:40 | 显示全部楼层
封装。
头文件:
  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.        
  16.     virtual ~Singleton(){
  17.         std::cout<<"destructor called!"<<std::endl;
  18.     }
  19.        
  20.     Singleton(const Singleton&)=delete;
  21.     Singleton& operator =(const Singleton&)=delete;
  22.        
  23. protected:
  24.     Singleton(){
  25.         std::cout<<"constructor called!"<<std::endl;
  26.     }

  27. };



  28. class IperfCollection:public       Singleton<IperfCollection> {
  29.         friend class Singleton<IperfCollection>;
  30. public:

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

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






  46. #endif
 楼主| keer_zu 发表于 2022-10-9 09:41 | 显示全部楼层
.cpp文件:

  1. #include <fstream>
  2. #include <fcntl.h>  
  3. #include <iostream>
  4. #include <regex>
  5. #include <string>
  6. #include "common_log.h"
  7. #include "IperfCollection.h"


  8. using namespace std;



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

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


  38. struct rtl9072_port_status
  39. {
  40.   uint32_t port ;
  41.   uint32_t tx_speed ;
  42.   uint32_t tx_pack_drop_rate ;
  43.   uint32_t rx_speed ;
  44.   uint32_t rx_pack_drop_rate ;
  45. } ;

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

  58.         string::const_iterator iterStart = str.begin();
  59.         string::const_iterator iterEnd = str.end();
  60.         string temp,value;

  61.         if (regex_search(iterStart, iterEnd, result, BandwidthMbits))
  62.         {
  63.                 temp = result[0];

  64.                 value = result[1];
  65.                 bandwidth = stof(value) * 1000;
  66.                 status.rx_speed = (uint32_t)bandwidth;
  67.                 SLOG_I("BandwidthMbits:%s\n",value.c_str());
  68.         } else if (regex_search(iterStart, iterEnd, result, BandwidthKbits)) {
  69.                 temp = result[0];
  70.                 value = result[1];
  71.                 bandwidth = stof(value);
  72.                 status.rx_speed = (uint32_t)bandwidth;
  73.                 SLOG_I("bandwidthKbits:%s\n",value.c_str());
  74.         } else if (regex_search(iterStart, iterEnd, result, BandwidthGbits)) {
  75.                 temp = result[0];
  76.                 value = result[1];
  77.                 bandwidth = stof(value) * 1000000;
  78.                 status.rx_speed = (uint32_t)bandwidth;
  79.                 SLOG_I("BandwidthGbits:%s\n",value.c_str());
  80.         }
  81.        
  82.         if (regex_search(iterStart, iterEnd, result, Packageloss))
  83.         {
  84.                 temp = result[0];
  85.                 value = result[1];
  86.                 loss = stof(value) * 100000;
  87.                 status.rx_pack_drop_rate = (uint32_t)loss;
  88.                 SLOG_I("package loss:%s\n",value.c_str());       
  89.         }

  90.         if(call != NULL)
  91.                 call(&status);

  92.         return 0;
  93. }




  94. void * IperfCollection::iperfDataCollection(void *data)
  95. {
  96.         int ret,fd;
  97.         IperfCollection *collection = (IperfCollection *)data;
  98.         map<int,string>::iterator iter_pf;
  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.                 fd = open(file.c_str(),O_RDONLY | O_NONBLOCK,0);
  103.                 if(fd < 0){
  104.                         SLOG_E("open error!\n") ;
  105.                         return NULL;
  106.                 }

  107.                 collection->_portFd[port] = fd;
  108.         }
  109.        
  110.         char buf1[500];
  111.         while(1) {
  112.                 for(iter_pf = (collection->_portFifo).begin();iter_pf != (collection->_portFifo).end();iter_pf ++) {
  113.                         string file = iter_pf->second;
  114.                         int port = iter_pf->first;
  115.                         ssize_t s = read(collection->_portFd[port],buf1,sizeof(buf1)-1);
  116.                         if(s > 0) {
  117.                                 buf1[s-1] = 0;
  118.                                 string row = buf1;
  119.                                 cout << row << endl;
  120.                                 collection->stringParsing(row,port);
  121.                         } else if (s == 0) {
  122.                                 SLOG_I("client(%d) quit,exit ...\n", port);
  123.                                 continue;
  124.                         }
  125.                 }

  126.                 sleep(1);
  127.         }
  128.         close(ret);
  129.         return NULL;
  130. }



  131. int IperfCollection::Init(port_performance_calback_t cb)
  132. {
  133.         int ret;
  134.        
  135.         ret = this->createFifo();
  136.         if (ret != 0) {
  137.                 SLOG_E("createFifo error:%d\n", ret);
  138.                 return ret;
  139.         }

  140.         if(cb != NULL)
  141.                 _callback = cb;

  142.         pthread_create(&_threadId, NULL, &iperfDataCollection ,this);
  143.        
  144.         return 0;
  145. }





 楼主| keer_zu 发表于 2022-10-9 09:43 | 显示全部楼层
demo:

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


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

  10.         IperfCollection *coll = &IperfCollection::get_instance();
  11.         coll->Init(callback);

  12.         auto re = system("./ethernetTest.sh");
  13.     cout << "re = " << re << endl;

  14.         while(1) {
  15.                 sleep(10);
  16.                 //return 0;
  17.         }
  18. }
 楼主| keer_zu 发表于 2022-10-9 09:44 | 显示全部楼层
ethernetTest.sh:

  1. #!/bin/sh

  2. # FIFO files are named as follows: /var/fifo_XXXX,XXXX is the port number to be tested

  3. #100base
  4. iperf2 -u -s -p 1200 > /var/fifo_1200 &
  5. iperf2 -u -s -p 1201 > /var/fifo_1201 &
  6. iperf2 -u -s -p 1202 > /var/fifo_1202 &
  7. iperf2 -u -s -p 1203 > /var/fifo_1203 &
  8. iperf2 -u -s -p 1205 > /var/fifo_1205 &
  9. #1000base
  10. iperf2 -u -s -p 1206 > /var/fifo_1206 &
  11. iperf2 -u -s -p 1207 > /var/fifo_1207 &
  12. iperf2 -u -s -p 1209 > /var/fifo_1209 &
  13. iperf2 -u -s -p 1210 > /var/fifo_1210 &
  14. #iperf2 -u -c 192.168.1.9 -p 1208 -B 192.168.1.12 -b 100m -t 60 -i 1

  15. #SocketTest &
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1478

主题

12917

帖子

55

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