打印

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

[复制链接]
437|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
keer_zu|  楼主 | 2022-10-7 17:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

代码,头文件:

#ifndef __IPERF_COLLECTION_H__
#define __IPERF_COLLECTION_H__

#include <map>
#include <pthread.h>
#include "macros.h"

using namespace std;

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

template<typename T>
class Singleton{
public:
    static T& get_instance(){
        static T instance;
        return instance;
    }
    virtual ~Singleton(){
        std::cout<<"destructor called!"<<std::endl;
    }
    Singleton(const Singleton&)=delete;
    Singleton& operator =(const Singleton&)=delete;
protected:
    Singleton(){
        std::cout<<"constructor called!"<<std::endl;
    }

};



class IperfCollection:public       Singleton<IperfCollection> {
        friend class Singleton<IperfCollection>;
public:

       
        //static shared_ptr<IperfCollection> getInstance(){
        //        return _instance;
        //}

        int Init(port_performance_calback_t cb);
       
       
private:

        int createFifo();
        int stringParsing(string str,int port);
        static void *iperfDataCollection(void *data);
        IperfCollection():_callback(NULL){}
        ~IperfCollection(){}
       
        pthread_t        _threadId;
        port_performance_calback_t _callback;
        //static shared_ptr<IperfCollection> _instance;
        map<int,string> _portFifo;
        map<int,int> _portFd;
};






#endif


使用特权

评论回复
评论
keer_zu 2022-10-7 17:38 回复TA
采用模板类实现单例模式 

相关帖子

沙发
keer_zu|  楼主 | 2022-10-7 17:39 | 只看该作者
.cpp文件:
#include <fstream>
#include <fcntl.h>  
#include <iostream>
#include<regex>
#include "IperfCollection.h"


using namespace std;



int IperfCollection::createFifo()
{
        ifstream fin("ethernetTest.sh");
        string::const_iterator itStart;
        string::const_iterator itEnd;
        string scriptLine;

        int ret;
        regex filePath("/var/fifo_([0-9]\\d*)");
       
        while( getline(fin,scriptLine) ){       
                itStart = scriptLine.begin();
                itEnd = scriptLine.end();
                smatch result;
                if (regex_search(itStart, itEnd, result, filePath)) {
                        string str_port = result[1];
                        int port = atoi(str_port.c_str());
                        cout << "+++++ file path: " << result[0] << " num: " << port << endl;
                        string filepath = result[0];
                        ret = mkfifo(filepath.c_str(),S_IRUSR | S_IWUSR);
                        cout << "ret: " << ret << endl;
                        _portFifo[port] = filepath;
                } else {
                        cout << "--- no search ---" << endl;
                }
        }
       
        return 0;
}


struct rtl9072_port_status
{
  uint32_t port ;
  uint32_t tx_speed ;
  uint32_t tx_pack_drop_rate ;
  uint32_t rx_speed ;
  uint32_t rx_pack_drop_rate ;
} ;

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

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

        if (regex_search(iterStart, iterEnd, result, BandwidthMbits))
        {
                temp = result[0];
                cout << "==" << port << "== BandwidthMbits: " << result[1] << endl;
                //iterStart = result[0].second;       
        } else if (regex_search(iterStart, iterEnd, result, BandwidthKbits)) {
                        temp = result[0];
                        cout << "==" << port << "== bandwidthKbits: " << result[1] << endl;
                        //iterStart = result[0].second;       
        } else if (regex_search(iterStart, iterEnd, result, BandwidthGbits)) {
                        temp = result[0];
                        cout << "==" << port << "== BandwidthGbits: " << result[1] << endl;
                        //iterStart = result[0].second;       
        }
       
        if (regex_search(iterStart, iterEnd, result, Packageloss))
        {
                temp = result[0];
                cout << "==" << port << "== package loss: " << result[1] << endl;
                //iterStart = result[0].second;       
        }

        if(call != NULL)
                call(&status);

        return 0;
}



void * IperfCollection::iperfDataCollection(void *data)
{
        int ret,fd;
        IperfCollection *collection = (IperfCollection *)data;
        map<int,string>::iterator iter_pf;
        for(iter_pf = (collection->_portFifo).begin();iter_pf != (collection->_portFifo).end();iter_pf ++) {
                string file = iter_pf->second;
                int port = iter_pf->first;
                fd = open(file.c_str(),O_RDONLY | O_NONBLOCK,0);
                if(fd < 0){
                        cout << "open error!\n" ;
                        return NULL;
                }

                collection->_portFd[port] = fd;
        }
       
        char buf1[500];
        while(1) {
                for(iter_pf = (collection->_portFifo).begin();iter_pf != (collection->_portFifo).end();iter_pf ++) {
                        string file = iter_pf->second;
                        int port = iter_pf->first;
                        ssize_t s = read(collection->_portFd[port],buf1,sizeof(buf1)-1);
                        if(s > 0) {
                                buf1[s-1] = 0;
                                string row = buf1;
                                cout << row << endl;
                                //test1(row,port);
                                collection->stringParsing(row,port);
                        } else if (s == 0) {
                                cout << "client quit,exit ..." << endl;
                                continue;
                        }
                }

                sleep(1);
        }
        close(ret);
        return NULL;
}



int IperfCollection::Init(port_performance_calback_t cb)
{
        int ret;
       
        ret = this->createFifo();
        if (ret != 0) {
                cout << "createFifo error" << endl;
                return ret;
        }

        if(cb != NULL)
                _callback = cb;

        pthread_create(&_threadId, NULL, &iperfDataCollection ,this);
       
        return 0;
}





使用特权

评论回复
板凳
keer_zu|  楼主 | 2022-10-7 17:40 | 只看该作者
main.cpp

void callback(void *data)
{
        cout << "======== call back =====" << endl;
}


int main(void)
{
        IperfCollection* collection;
        collection = &IperfCollection::get_instance();
        collection->Init(callback);

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

       
       
/*
        string line;
        pthread_t        thread_id;
        ifstream fin("ethernetTest.sh");
        //test();
        string s;  
   

        int ret,fd;
        regex filePath("/var/fifo_([0-9]\\d*)");
       
        string::const_iterator itStart;// = fp.begin();
        string::const_iterator itEnd;// = fp.end();
       
        cout << "==============================" << endl;
       
        while( getline(fin,s) ){       
                itStart = s.begin();
                itEnd = s.end();
                smatch result;
                if (regex_search(itStart, itEnd, result, filePath)) {
                        string str_port = result[1];
                        int port = atoi(str_port.c_str());
                        cout << "+++++ file path: " << result[0] << " num: " << port << endl;
                        string filepath = result[0];
                        ret = mkfifo(filepath.c_str(),S_IRUSR | S_IWUSR);
                        cout << "ret: " << ret << endl;
                        fifo_port[port] = filepath;
                } else {
                        cout << "--- no search ---" << endl;
                }
        }
       
        pthread_create(&thread_id, NULL, &iperfDataCollection ,NULL);

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

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

                port_fd[port] = fd;
        }
       
        char buf1[500];
        while(1) {
                //cout << "please waiting...\n";
                for(iter_pf = fifo_port.begin();iter_pf != fifo_port.end();iter_pf ++) {
                        string file = iter_pf->second;
                        int port = iter_pf->first;
                        ssize_t s = read(port_fd[port],buf1,sizeof(buf1)-1);
                        if(s > 0) {
                                buf1[s-1] = 0;
                                string row = buf1;//"[  5] 38.0-39.0 sec  11.5 MBytes  96.4 Mbits/sec   0.294 ms   11/ 8208 (0.13%)";//buf1;
                                cout << row << endl;
                                test1(row,port);
                        } else if (s == 0) {
                                cout << "client quit,exit ..." << endl;
                                continue;
                        }
                }

                sleep(1);
        }
        close(ret);*/
        while(1) {
                sleep(10);
        }
}

使用特权

评论回复
地板
keer_zu|  楼主 | 2022-10-7 17:43 | 只看该作者
参考:

C++的单例模式

使用特权

评论回复
5
keer_zu|  楼主 | 2022-10-7 17:43 | 只看该作者
参考:
智能指针

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1314

主题

12271

帖子

53

粉丝