keer_zu 发表于 2022-10-7 17:38

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


代码,头文件:

#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: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_(\\d*)");
       
        while( getline(fin,scriptLine) ){       
                itStart = scriptLine.begin();
                itEnd = scriptLine.end();
                smatch result;
                if (regex_search(itStart, itEnd, result, filePath)) {
                        string str_port = result;
                        int port = atoi(str_port.c_str());
                        cout << "+++++ file path: " << result << " num: " << port << endl;
                        string filepath = result;
                        ret = mkfifo(filepath.c_str(),S_IRUSR | S_IWUSR);
                        cout << "ret: " << ret << endl;
                        _portFifo = 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("-?((((\\d*)|(\\d)|(0))\\.((\\d*)|()\\d))|(0)|(\\d*))\\sMbits");
        regex BandwidthKbits("-?((((\\d*)|(\\d)|(0))\\.((\\d*)|()\\d))|(0)|(\\d*))\\sKbits");       
        regex BandwidthGbits("-?((((\\d*)|(\\d)|(0))\\.((\\d*)|()\\d))|(0)|(\\d*))\\sGbits");
       
        regexPackageloss("\\(-?((\\d*\\.\\d*)|(0\\.\\d*\\d*)|(\\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;
                cout << "==" << port << "== BandwidthMbits: " << result << endl;
                //iterStart = result.second;       
        } else if (regex_search(iterStart, iterEnd, result, BandwidthKbits)) {
                        temp = result;
                        cout << "==" << port << "== bandwidthKbits: " << result << endl;
                        //iterStart = result.second;       
        } else if (regex_search(iterStart, iterEnd, result, BandwidthGbits)) {
                        temp = result;
                        cout << "==" << port << "== BandwidthGbits: " << result << endl;
                        //iterStart = result.second;       
        }
       
        if (regex_search(iterStart, iterEnd, result, Packageloss))
        {
                temp = result;
                cout << "==" << port << "== package loss: " << result << endl;
                //iterStart = result.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 = fd;
        }
       
        char buf1;
        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,buf1,sizeof(buf1)-1);
                        if(s > 0) {
                                buf1 = 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_(\\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;
                        int port = atoi(str_port.c_str());
                        cout << "+++++ file path: " << result << " num: " << port << endl;
                        string filepath = result;
                        ret = mkfifo(filepath.c_str(),S_IRUSR | S_IWUSR);
                        cout << "ret: " << ret << endl;
                        fifo_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 = fd;
        }
       
        char buf1;
        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,buf1,sizeof(buf1)-1);
                        if(s > 0) {
                                buf1 = 0;
                                string row = buf1;//" 38.0-39.0 sec11.5 MBytes96.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++的单例模式

keer_zu 发表于 2022-10-7 17:43

参考:
智能指针
页: [1]
查看完整版本: QNX环境使用iperf获取网络性能参数