.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;
- }
|