15.12.2011 - version 0.1.1

This commit is contained in:
peri4
2011-12-15 22:39:40 +03:00
parent 74b4173c4c
commit f141bab1c8
18 changed files with 361 additions and 111 deletions

View File

@@ -98,11 +98,6 @@ public:
};
int main(int argc, char * argv[]) {
PIFile f;
f.open("_test", PIFile::ReadWrite || PIFile::New);
if (f.isEmpty()) f.remove();
return 0;
bool r_string = true, r_thread = true, r_mutex = true, r_timer = true, r_file = true, r_eval = true, r_event = true;
bool succ = true;
cout << "== PIP test program ==" << endl;

View File

@@ -138,3 +138,159 @@ PIByteArray & PIByteArray::decompressRLE(uchar threshold) {
*this = t;
return *this;
}
uchar PIByteArray::checksumPlain8() {
uchar c = 0;
int sz = size_s();
for (int i = 0; i < sz; ++i)
c += at(i);
c = ~(c + 1);
return c;
}
uint PIByteArray::checksumPlain32() {
uint c = 0;
int sz = size_s();
for (int i = 0; i < sz; ++i)
c += at(i) * (i + 1);
c = ~(c + 1);
return c;
}
uchar PIByteArray::checksumCRC8() {
const uchar CRC8Table[256] = {
0x00, 0x31, 0x62, 0x53, 0xC4, 0xF5, 0xA6, 0x97,
0xB9, 0x88, 0xDB, 0xEA, 0x7D, 0x4C, 0x1F, 0x2E,
0x43, 0x72, 0x21, 0x10, 0x87, 0xB6, 0xE5, 0xD4,
0xFA, 0xCB, 0x98, 0xA9, 0x3E, 0x0F, 0x5C, 0x6D,
0x86, 0xB7, 0xE4, 0xD5, 0x42, 0x73, 0x20, 0x11,
0x3F, 0x0E, 0x5D, 0x6C, 0xFB, 0xCA, 0x99, 0xA8,
0xC5, 0xF4, 0xA7, 0x96, 0x01, 0x30, 0x63, 0x52,
0x7C, 0x4D, 0x1E, 0x2F, 0xB8, 0x89, 0xDA, 0xEB,
0x3D, 0x0C, 0x5F, 0x6E, 0xF9, 0xC8, 0x9B, 0xAA,
0x84, 0xB5, 0xE6, 0xD7, 0x40, 0x71, 0x22, 0x13,
0x7E, 0x4F, 0x1C, 0x2D, 0xBA, 0x8B, 0xD8, 0xE9,
0xC7, 0xF6, 0xA5, 0x94, 0x03, 0x32, 0x61, 0x50,
0xBB, 0x8A, 0xD9, 0xE8, 0x7F, 0x4E, 0x1D, 0x2C,
0x02, 0x33, 0x60, 0x51, 0xC6, 0xF7, 0xA4, 0x95,
0xF8, 0xC9, 0x9A, 0xAB, 0x3C, 0x0D, 0x5E, 0x6F,
0x41, 0x70, 0x23, 0x12, 0x85, 0xB4, 0xE7, 0xD6,
0x7A, 0x4B, 0x18, 0x29, 0xBE, 0x8F, 0xDC, 0xED,
0xC3, 0xF2, 0xA1, 0x90, 0x07, 0x36, 0x65, 0x54,
0x39, 0x08, 0x5B, 0x6A, 0xFD, 0xCC, 0x9F, 0xAE,
0x80, 0xB1, 0xE2, 0xD3, 0x44, 0x75, 0x26, 0x17,
0xFC, 0xCD, 0x9E, 0xAF, 0x38, 0x09, 0x5A, 0x6B,
0x45, 0x74, 0x27, 0x16, 0x81, 0xB0, 0xE3, 0xD2,
0xBF, 0x8E, 0xDD, 0xEC, 0x7B, 0x4A, 0x19, 0x28,
0x06, 0x37, 0x64, 0x55, 0xC2, 0xF3, 0xA0, 0x91,
0x47, 0x76, 0x25, 0x14, 0x83, 0xB2, 0xE1, 0xD0,
0xFE, 0xCF, 0x9C, 0xAD, 0x3A, 0x0B, 0x58, 0x69,
0x04, 0x35, 0x66, 0x57, 0xC0, 0xF1, 0xA2, 0x93,
0xBD, 0x8C, 0xDF, 0xEE, 0x79, 0x48, 0x1B, 0x2A,
0xC1, 0xF0, 0xA3, 0x92, 0x05, 0x34, 0x67, 0x56,
0x78, 0x49, 0x1A, 0x2B, 0xBC, 0x8D, 0xDE, 0xEF,
0x82, 0xB3, 0xE0, 0xD1, 0x46, 0x77, 0x24, 0x15,
0x3B, 0x0A, 0x59, 0x68, 0xFF, 0xCE, 0x9D, 0xAC};
int sz = size_s();
ushort ret = 0xFF;
for (int i = 0; i < sz; ++i)
ret = CRC8Table[at(i) ^ ret];
return ret;
}
ushort PIByteArray::checksumCRC16() {
const ushort CRC16Table[256] = {
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040};
int sz = size_s();
uchar nTemp;
ushort ret = 0xFFFF;
for (int i = 0; i < sz; ++i) {
nTemp = at(i) ^ ret;
ret >>= 8;
ret ^= CRC16Table[nTemp];
}
return ret;
}
uint PIByteArray::checksumCRC32() {
const uint CRC32Table[256] = {
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D};
int sz = size_s();
uchar nTemp;
uint ret = 0xFFFFFFFF;
for (int i = 0; i < sz; ++i) {
nTemp = at(i) ^ ret;
ret >>= 8;
ret ^= CRC32Table[nTemp];
}
return ret;
}

View File

@@ -80,15 +80,22 @@ public:
PIByteArray & convertToBase64();
PIByteArray & convertFromBase64();
PIByteArray & compressRLE(uchar threshold = 192);
PIByteArray & decompressRLE(uchar threshold = 192);
PIByteArray & compressHuffman() {*this = huffman.compress(*this); return *this;}
PIByteArray toBase64() {PIByteArray ba(*this); ba.convertToBase64(); return ba;}
PIByteArray fromBase64() {PIByteArray ba(*this); ba.convertFromBase64(); return ba;}
PIByteArray & compressRLE(uchar threshold = 192);
PIByteArray & decompressRLE(uchar threshold = 192);
PIByteArray compressedRLE(uchar threshold = 192) {PIByteArray ba(*this); ba.compressedRLE(threshold); return ba;}
PIByteArray decompressedRLE(uchar threshold = 192) {PIByteArray ba(*this); ba.decompressedRLE(threshold); return ba;}
PIByteArray & compressHuffman() {*this = huffman.compress(*this); return *this;}
uchar checksumPlain8();
uint checksumPlain32();
uchar checksumCRC8();
ushort checksumCRC16();
uint checksumCRC32();
void operator =(const PIVector<uchar> & d) {resize(d.size()); for (uint i = 0; i < size(); ++i) (*this)[i] = d[i];}
private:

View File

@@ -253,6 +253,7 @@ void PIConsole::begin() {
void PIConsole::run() {
uint cx, clen = 0;
int j;
#ifdef WINDOWS
GetConsoleScreenBufferInfo(hOut, &sbi);
width = sbi.srWindow.Right - sbi.srWindow.Left;
@@ -275,7 +276,10 @@ void PIConsole::run() {
cx = col_wid * i;
toUpperLeft();
if (my < vars()[i].size()) my = vars()[i].size();
j = 0;
piForeachC (Variable & tv, vars()[i]) {
if (j > height - 3) continue;
j++;
moveRight(cx);
if (tv.type == 0 && tv.s == 0) {
newLine();
@@ -333,6 +337,7 @@ void PIConsole::fillLabels() {
cy = 1;
toUpperLeft();
for (uint j = 0; j < vars()[i].size(); ++j) {
if (int(j) > height - 3) continue;
if (my < j) my = j;
moveRight(cx);
tv = vars()[i][j];
@@ -446,6 +451,8 @@ void PIConsole::addVariable(const PIString & name, PIProtocol * ptr, int column,
addVariable("Immediate Frequency, Hz", ptr->immediateFrequency_ptr(), column, format);
addVariable("Integral Frequency, Hz", ptr->integralFrequency_ptr(), column, format);
addVariable("Disconnect Timeout, s", ptr->disconnectTimeout_ptr(), column, format);
addVariable("Receiver history size", ptr->receiverHistorySize_ptr(), column, format);
addVariable("Sender history size", ptr->senderHistorySize_ptr(), column, format);
addVariable("Quality", ptr->quality_ptr(), column, format);
}
void PIConsole::addVariable(const PIString & name, PISystemMonitor * ptr, int column, PIFlags<PIConsole::Format> format) {

View File

@@ -279,14 +279,14 @@ inline bool operator <(const PIPair<Type0, Type1> & value0, const PIPair<Type0,
template<typename Type, typename Key = int>
class PIHash: public PISet<PIPair<Key, Type> > {
typedef PIHash<Type, Key> _CHash;
class PIMap: public PISet<PIPair<Key, Type> > {
typedef PIMap<Type, Key> _CMap;
typedef PISet<PIPair<Key, Type> > _CSet;
public:
PIHash() {;}
PIHash(const Type & value, const Key & key) {insert(value, key);}
_CHash & insert(const Type & value, const Key & key) {_CSet::insert(PIPair<Key, Type>(key, value)); return *this;}
Type value(Key key) const {for (typename _CHash::iterator i = _CHash::begin(); i != _CHash::end(); i++) if ((*i).first == key) return (*i).second; return Type();}
PIMap() {;}
PIMap(const Type & value, const Key & key) {insert(value, key);}
_CMap & insert(const Type & value, const Key & key) {_CSet::insert(PIPair<Key, Type>(key, value)); return *this;}
Type value(Key key) const {for (typename _CMap::iterator i = _CMap::begin(); i != _CMap::end(); i++) if ((*i).first == key) return (*i).second; return Type();}
Type operator[](Key key) const {return value(key);}
};

View File

@@ -81,8 +81,8 @@ void PIFile::resize(llong new_size, char fill_) {
llong PIFile::pos() {
if (cmode[Read]) return stream.tellg();
if (cmode[Write]) return stream.tellp();
if (cmode[Read]) return stream.tellg();
return -1;
}

View File

@@ -15,7 +15,7 @@ public:
PIFile(const PIString & path, PIFlags<Mode> mode = ReadWrite): PIObject(path) {open(path, mode);}
PIFile(const PIFile & file): PIObject(file.cpath) {cpath = file.cpath; cmode = file.cmode;}
~PIFile() {if (isOpened()) close();}
~PIFile() {close();}
PIFile & operator =(const PIFile & f) {cpath = f.cpath; cmode = f.cmode; return *this;}
@@ -23,7 +23,7 @@ public:
EVENT_HANDLER1(PIFile, bool, open, PIFlags<Mode>, mode) {return open(cpath, mode);}
EVENT_HANDLER1(PIFile, bool, open, const PIString & , path) {return open(path, cmode);}
EVENT_HANDLER0(PIFile, bool, open) {return open(cpath, cmode);}
EVENT_HANDLER0(PIFile, void, close) {stream.close();}
EVENT_HANDLER0(PIFile, void, close) {stream.clear(); stream.close();}
EVENT_HANDLER0(PIFile, void, clear) {string st = cpath.stdString(); close(); stream.open(st.c_str(), fstream::trunc | fstream::binary | (fstream::openmode)(int)cmode);}
void seek(llong position) {stream.clear(); stream.seekg(position); stream.seekp(position);}
void seekToBegin() {stream.clear(); stream.seekg(0, fstream::beg); stream.seekp(0, fstream::beg);}
@@ -36,7 +36,7 @@ public:
PIString readLine();
llong readAll(void * data);
PIByteArray readAll(bool forceRead = false);
EVENT_HANDLER0(PIFile, void, remove) {if (isOpened()) close(); std::remove(cpath.data());}
EVENT_HANDLER0(PIFile, void, remove) {close(); std::remove(cpath.data());}
PIString path() const {return cpath;}
void setPath(const PIString & path) {cpath = path;}

View File

@@ -1,7 +1,7 @@
#ifndef PIINCLUDES_H
#define PIINCLUDES_H
#define PIP_VERSION 0x000100
#define PIP_VERSION 0x000101
#define PIP_VERSION_MAJOR (PIP_VERSION & 0xFF0000) >> 16
#define PIP_VERSION_MINOR (PIP_VERSION & 0xFF00) >> 8
#define PIP_VERSION_REVISION PIP_VERSION & 0xFF

View File

@@ -5,7 +5,7 @@ PIVector<PIObject * > PIObject::objects;
/*
PIStringList PIObject::events() {
PIStringList l;
for (PIHash<NamedFunction, PIString>::const_iterator i = signals_.begin(); i != signals_.end(); i++)
for (PIMap<NamedFunction, PIString>::const_iterator i = signals_.begin(); i != signals_.end(); i++)
l << (*i).first;
return l;
}
@@ -13,7 +13,7 @@ PIStringList PIObject::events() {
PIStringList PIObject::eventHandlers() {
PIStringList l;
for (PIHash<NamedFunction, PIString>::const_iterator i = slots_.begin(); i != slots_.end(); i++)
for (PIMap<NamedFunction, PIString>::const_iterator i = slots_.begin(); i != slots_.end(); i++)
l << (*i).first;
return l;
}

View File

@@ -12,12 +12,12 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
return;
}
int ps;
bool ok, has_dev = false;
bool ok, gok, has_dev = false;
PIFlags<PISerial::Parameters> pp;
PIConfig::Entry & b(conf.getValue(name)),
& rb(b.getValue("receiver")),
& sb(b.getValue("sender"));
PIString dev;
PIString dev, gdev;
/// receiver section
if (rb.isEntryExists("ip") && rb.isEntryExists("device")) {
@@ -26,7 +26,14 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
return;
}
dev = rb.getValue("ip", "", &ok);
if (ok) {
gdev = b.getValue("ip", "", &gok);
if (ok || gok) {
if (gok && !ok) dev = gdev;
if (gok && ok && (dev != gdev)) {
cout << "[PIProtocol \"" << name << "\"] Ambiguous receiver type in \"" << config << "\"!" << endl;
devReceiverState = "Config error";
return;
}
ps = rb.getValue("port", 0, &ok);
if (!ok) {
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".receiver.port\" in \"" << config << "\"!" << endl;
@@ -43,7 +50,14 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
cout << "[PIProtocol \"" << name << "\"] Warning: null receive data size!" << endl;
}
dev = rb.getValue("device", "", &ok);
if (ok) {
gdev = b.getValue("device", "", &gok);
if (ok || gok) {
if (gok && !ok) dev = gdev;
if (gok && ok && (dev != gdev)) {
cout << "[PIProtocol \"" << name << "\"] Ambiguous receiver type in \"" << config << "\"!" << endl;
devReceiverState = "Config error";
return;
}
ps = rb.getValue("speed", 0, &ok);
if (!ok) {
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".receiver.speed\" in \"" << config << "\"!" << endl;
@@ -66,15 +80,41 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
if (recDataSize == 0)
cout << "[PIProtocol \"" << name << "\"] Warning: null receive data size!" << endl;
}
float freq = rb.getValue("frequency", -1.f);
if (freq > 0. && !has_dev)
history_write_rec = rb.getValue("writeHistory", false, &ok);
bool ghist = b.getValue("writeHistory", false, &gok);
if (ok || gok) {
if (gok && !ok) history_write_rec = ghist;
if (gok && ok && (history_write_rec != ghist)) {
cout << "[PIProtocol \"" << name << "\"] Ambiguous receiver history in \"" << config << "\"!" << endl;
devReceiverState = "Config error";
return;
}
if (history_write_rec) {
history_path_rec = rb.getValue("historyFile", "./history_" + protName + "_rec_" +
date2string(currentDate(), "__dd_mm_yyyy_") +
time2string(currentTime(), "_hh_mm_ss_")).value();
history_id_rec = b.getValue("historyID", 0, &ok);
if (!ok) {
history_id_rec = protName.toByteArray().checksumCRC16();
cout << "[PIProtocol \"" << name << "\"] Warning: no receiver history ID defined, write with ID = " << history_id_rec << endl;
}
history_file_rec.open(history_path_rec, PIFile::Write | PIFile::New);
}
}
float freq = rb.getValue("frequency", -1.f, &ok), gfreq = b.getValue("frequency", -1.f, &gok);
if (gok && !ok) freq = gfreq;
if (gok && ok && (freq != gfreq)) {
cout << "[PIProtocol \"" << name << "\"] Ambiguous expected frequency in \"" << config << "\"!" << endl;
devReceiverState = "Config error";
return;
}
if (freq > 0.f && !has_dev)
cout << "[PIProtocol \"" << name << "\"] Warning: no receiver device and not null expected frequency!" << endl;
float tm = b.getValue("disconnectTimeout", 3.f);
if (tm <= 0.)
if (tm <= 0.f)
cout << "[PIProtocol \"" << name << "\"] Warning: diconnect timeout <= 0 s!" << endl;
timeout_ = (tm < 0.) ? 0. : tm;
timeout_ = (tm < 0.f) ? 0.f : tm;
setExpectedFrequency(freq);
changeDisconnectTimeout();
/// sender section
if (sb.isEntryExists("ip") && sb.isEntryExists("device")) {
@@ -83,8 +123,15 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
return;
}
dev = sb.getValue("ip", "", &ok);
gdev = b.getValue("ip", "", &gok);
has_dev = false;
if (ok) {
if (ok || gok) {
if (gok && !ok) dev = gdev;
if (gok && ok && (dev != gdev)) {
cout << "[PIProtocol \"" << name << "\"] Ambiguous sender type in \"" << config << "\"!" << endl;
devSenderState = "Config error";
return;
}
ps = sb.getValue("port", 0, &ok);
if (!ok) {
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".sender.port\" in \"" << config << "\"!" << endl;
@@ -101,7 +148,14 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
cout << "[PIProtocol \"" << name << "\"] Warning: null send data size!" << endl;
}
dev = sb.getValue("device", "", &ok);
if (ok) {
gdev = b.getValue("device", "", &gok);
if (ok || gok) {
if (gok && !ok) dev = gdev;
if (gok && ok && (dev != gdev)) {
cout << "[PIProtocol \"" << name << "\"] Ambiguous sender type in \"" << config << "\"!" << endl;
devSenderState = "Config error";
return;
}
ps = sb.getValue("speed", 0, &ok);
if (!ok) {
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".sender.speed\" in \"" << config << "\"!" << endl;
@@ -122,8 +176,36 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
if (sendDataSize_ == 0)
cout << "[PIProtocol \"" << name << "\"] Warning: null send data size!" << endl;
}
freq = sb.getValue("frequency", -1.f);
if (freq > 0. && !has_dev)
history_write_send = sb.getValue("writeHistory", false, &ok);
ghist = b.getValue("writeHistory", false, &gok);
if (ok || gok) {
if (gok && !ok) history_write_send = ghist;
if (gok && ok && (history_write_send != ghist)) {
cout << "[PIProtocol \"" << name << "\"] Ambiguous sender history in \"" << config << "\"!" << endl;
devSenderState = "Config error";
return;
}
if (history_write_send) {
history_path_send = sb.getValue("historyFile", "./history_" + protName + "_send_" +
date2string(currentDate(), "__dd_mm_yyyy_") +
time2string(currentTime(), "_hh_mm_ss_")).value();
history_id_send = b.getValue("historyID", 0, &ok);
if (!ok) {
history_id_send = protName.toByteArray().checksumCRC16() + 1;
cout << "[PIProtocol \"" << name << "\"] Warning: no sender history ID defined, write with ID = " << history_id_send << endl;
}
history_file_send.open(history_path_send, PIFile::Write | PIFile::New);
}
}
freq = sb.getValue("frequency", -1.f, &ok);
gfreq = b.getValue("frequency", -1.f, &gok);
if (gok && !ok) freq = gfreq;
if (gok && ok && (freq != gfreq)) {
cout << "[PIProtocol \"" << name << "\"] Ambiguous sender frequency in \"" << config << "\"!" << endl;
devSenderState = "Config error";
return;
}
if (freq > 0.f && !has_dev)
cout << "[PIProtocol \"" << name << "\"] Warning: no sender device and not null send frequency!" << endl;
setSenderFrequency(freq);
@@ -145,6 +227,20 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
PIProtocol::~PIProtocol() {
//cout << "prot " << protName << " delete\n";
if (history_write_rec) {
if (history_file_rec.isEmpty()) {
history_file_rec.close();
history_file_rec.remove();
}
history_file_rec.close();
}
if (history_write_send) {
if (history_file_send.isEmpty()) {
history_file_send.close();
history_file_send.remove();
}
history_file_send.close();
}
delete diagTimer;
delete sendTimer;
if (packet != 0) delete packet;
@@ -154,7 +250,7 @@ PIProtocol::~PIProtocol() {
void PIProtocol::init() {
work = new_mp_prot = false;
work = new_mp_prot = history_write_rec = history_write_send = false;
eth = 0;
ser = 0;
ret_func = 0;
@@ -173,6 +269,7 @@ void PIProtocol::init() {
type_rec = type_send = PIProtocol::None;
devSenderState = devReceiverState = "Unknown";
devSenderName = devReceiverName = "no device";
history_rsize_rec = history_rsize_send = "no file";
/*addEvent("receiver started");
addEvent("receiver stopped");
addEvent("sender started");
@@ -287,6 +384,10 @@ bool PIProtocol::receiveEvent(void * t, char * data, int size) {
p->work = true;
//p->lock();
if (p->validate()) {
if (p->history_write_rec) {
p->history_file_rec.writeToBinLog(p->history_id_rec, data, size);
p->history_rsize_rec.setReadableSize(p->history_file_rec.pos());
}
raiseEvent<bool>(p, "received", true);
//p->unlock();
p->receive_count++;
@@ -389,6 +490,10 @@ void PIProtocol::check_state() {
void PIProtocol::send(const void * data, int size) {
if (data == 0 || size == 0) return;
if (!aboutSend()) return;
if (history_write_send) {
history_file_send.writeToBinLog(history_id_send, data, size);
history_rsize_send.setReadableSize(history_file_send.pos());
}
if (type_send == PIProtocol::Serial)
if (ser->send((char * )data, size))
send_count++;
@@ -404,6 +509,10 @@ void PIProtocol::send() {
//unlock();
if (sendDataPtr == 0 || sendDataSize == 0) return;
if (!aboutSend()) return;
if (history_write_send) {
history_file_send.writeToBinLog(history_id_send, sendDataPtr, sendDataSize);
history_rsize_send.setReadableSize(history_file_send.pos());
}
if (type_send == PIProtocol::Serial)
if (ser->send((char * )sendDataPtr, sendDataSize))
send_count++;

View File

@@ -46,23 +46,23 @@ public:
EVENT_HANDLER0(PIProtocol, void, startReceive) {startReceive(-1.f);}
EVENT_HANDLER1(PIProtocol, void, startReceive, float, exp_frequency); // if "frequency = -1" used last passed value
EVENT_HANDLER0(PIProtocol, void, stopReceive);
float expectedFrequency() const {return exp_freq;}
void setExpectedFrequency(float frequency); // for connection quality diagnostic
void setReceiverDevice(const PIString & device, PISerial::Speed speed, bool force = false); // for Serial
void setReceiverData(void * dataPtr, int dataSize) {this->dataPtr = (uchar * )dataPtr; this->dataSize = dataSize;}
void setReceiverAddress(const PIString & ip, int port, bool force = false); // for Ethernet
void setReceiverParameters(PIFlags<PISerial::Parameters> parameters) {if (type_send == PIProtocol::Serial) ser->setParameters(parameters);} // for Serial
void setReceiveSlot(ReceiveFunc slot) {ret_func = slot;}
float expectedFrequency() const {return exp_freq;}
EVENT_HANDLER0(PIProtocol, void, startSend) {startSend(-1.f);} // if "frequency = -1" used last passed value
EVENT_HANDLER1(PIProtocol, void, startSend, float, frequency); // if "frequency = -1" used last passed value
EVENT_HANDLER0(PIProtocol, void, stopSend) {sendTimer->stop(); raiseEvent(this, "sender stopped");}
float senderFrequency() const {return send_freq;}
void setSenderFrequency(float frequency) {send_freq = frequency;}
void setSenderDevice(const PIString & device, PISerial::Speed speed, bool force = false); // for Serial
void setSenderData(void * dataPtr, int dataSize) {sendDataPtr = (uchar * )dataPtr; sendDataSize = dataSize;}
void setSenderAddress(const PIString & ip, int port, bool force = false); // for Ethernet
void setSenderParameters(PIFlags<PISerial::Parameters> parameters) {if (type_send == PIProtocol::Serial) ser->setParameters(parameters);} // for Serial
float senderFrequency() const {return send_freq;}
EVENT_HANDLER0(PIProtocol, void, start) {startReceive(); startSend();}
EVENT_HANDLER0(PIProtocol, void, stop) {stopReceive(); stopSend();}
@@ -92,6 +92,14 @@ public:
PIString * receiverDeviceState_ptr() {return &devReceiverState;}
PIString senderDeviceState() const {return devSenderState;}
PIString * senderDeviceState_ptr() {return &devSenderState;}
PIString receiverHistorySize() const {return history_rsize_rec;}
PIString * receiverHistorySize_ptr() {return &history_rsize_rec;}
PIString senderHistorySize() const {return history_rsize_send;}
PIString * senderHistorySize_ptr() {return &history_rsize_send;}
bool writeReceiverHistory() const {return history_write_rec;}
bool * writeReceiverHistory_ptr() {return &history_write_rec;}
bool writeSenderHistory() const {return history_write_send;}
bool * writeSenderHistory_ptr() {return &history_write_send;}
void * receiveData() {return dataPtr;}
void * sendData() {return sendDataPtr;}
@@ -145,7 +153,10 @@ private:
PIDeque<float> last_freq;
PIDeque<char> last_packets;
PIString protName, devReceiverName, devReceiverState, devSenderName, devSenderState;
bool work, new_mp_prot;
PIString history_path_rec, history_path_send, history_rsize_rec, history_rsize_send;
PIFile history_file_rec, history_file_send;
ushort history_id_rec, history_id_send;
bool work, new_mp_prot, history_write_rec, history_write_send;
float exp_freq, send_freq, immediate_freq, integral_freq, timeout_;
int packets[2], pckt_cnt, pckt_cnt_max;
char * packet, cur_pckt;

View File

@@ -215,6 +215,7 @@ PIString & PIString::replace(const PIString & what, const PIString & with, bool
PIString & PIString::replaceAll(const PIString & what, const PIString & with) {
if (what.isEmpty()) return *this;
bool ok = true;
while (ok) replace(what, with, &ok);
return *this;
@@ -357,6 +358,29 @@ llong PIString::toLLong() const {
}
PIString & PIString::setReadableSize(long bytes) {
clear();
if (bytes < 1024) {*this += (PIString::fromNumber(bytes) + " B"); return *this;}
double fres = bytes / 1024.;
long res = bytes / 1024;
fres -= res;
if (res < 1024) {*this += (PIString::fromNumber(res) + "." + PIString::fromNumber(int(fres * 10)).left(1) + " kB"); return *this;}
fres = res / 1024.;
res /= 1024;
fres -= res;
if (res < 1024) {*this += (PIString::fromNumber(res) + "." + PIString::fromNumber(int(fres * 10)).left(1) + " MB"); return *this;}
fres = res / 1024.;
res /= 1024;
fres -= res;
if (res < 1024) {*this += (PIString::fromNumber(res) + "." + PIString::fromNumber(int(fres * 10)).left(1) + " GB"); return *this;}
fres = res / 1024.;
res /= 1024;
fres -= res;
*this += (PIString::fromNumber(res) + "." + PIString::fromNumber(int(fres * 10)).left(1) + " PB");
return *this;
}
char chrUpr(char c) {
if (c >= 'a' && c <= 'z') return c + 'A' - 'a';
//if (c >= 'а' && c <= 'я') return c + 'А' - 'а';

View File

@@ -3,6 +3,7 @@
#include "pibytearray.h"
#include "pichar.h"
#include "math.h"
class PIStringList;
@@ -115,7 +116,11 @@ public:
PIString toUpperCase() const;
PIString toLowerCase() const;
#ifdef QNX
PIString toNativeDecimalPoints() const {PIString s(*this); return s;}
#else
PIString toNativeDecimalPoints() const {PIString s(*this); if (currentLocale == 0) return s; return s.replaceAll(".", currentLocale->decimal_point);}
#endif
int find(const char str, const int start = 0) const;
int find(const PIString str, const int start = 0) const;
@@ -146,6 +151,7 @@ public:
PIString & setNumber(const float value) {clear(); *this += ftos(value); return *this;}
PIString & setNumber(const double value) {clear(); *this += dtos(value); return *this;}
PIString & setNumber(const ldouble value) {clear(); *this += dtos(value); return *this;}
PIString & setReadableSize(long bytes);
//inline static PIString fromNumber(const char value) {return PIString(itos(value));}
static PIString fromNumber(const int value) {return PIString(itos(value));}
@@ -156,6 +162,7 @@ public:
static PIString fromNumber(const double value) {return PIString(dtos(value));}
static PIString fromNumber(const ldouble value) {return PIString(dtos(value));}
static PIString fromBool(const bool value) {return PIString(value ? "true" : "false");}
static PIString readableSize(long bytes) {PIString s; s.setReadableSize(bytes); return s;}
private:
void appendFromChars(const char * c, int s);

View File

@@ -83,23 +83,10 @@ void PISystemMonitor::run() {
stat.data_memsize = sl[5].toLong() * page_size;
stat.physical_memsize = stat.resident_memsize - stat.share_memsize;
stat.physical_memsize_readable = readableSize(stat.physical_memsize);
stat.resident_memsize_readable = readableSize(stat.resident_memsize);
stat.share_memsize_readable = readableSize(stat.share_memsize);
stat.virtual_memsize_readable = readableSize(stat.virtual_memsize);
stat.data_memsize_readable = readableSize(stat.data_memsize);
stat.physical_memsize_readable = PIString::readableSize(stat.physical_memsize);
stat.resident_memsize_readable = PIString::readableSize(stat.resident_memsize);
stat.share_memsize_readable = PIString::readableSize(stat.share_memsize);
stat.virtual_memsize_readable = PIString::readableSize(stat.virtual_memsize);
stat.data_memsize_readable = PIString::readableSize(stat.data_memsize);
#endif
}
PIString PISystemMonitor::readableSize(long bytes) {
if (bytes < 999) return PIString::fromNumber(bytes) + " B";
long res = bytes / 1024;
if (res < 999) return PIString::fromNumber(res) + " kB";
res = res / 1024;
if (res < 999) return PIString::fromNumber(res) + " MB";
res = res / 1024;
if (res < 999) return PIString::fromNumber(res) + " GB";
res = res / 1024;
return PIString::fromNumber(res) + " PB";
}

View File

@@ -38,7 +38,6 @@ public:
private:
void run();
PIString readableSize(long bytes);
PIFile file, filem;
ProcessStats stat;

View File

@@ -1,52 +0,0 @@
gas.receiver.ip = 127.0.0.1
gas.receiver.port = 0x2401
gas.receiver.frequency = 105
gas.sender.ip = 127.0.0.1
gas.sender.port = 0x1001
gas.sender.frequency = 10
gas.writeHistory = false
mcp1.receiver.ip = 127.0.0.1
mcp1.receiver.port = 4012
mcp1.receiver.frequency = 20
mcp1.sender.ip = 192.168.0.190
mcp1.sender.port = 4013
mcp1.sender.frequency = 20
mcp1.writeHistory = false
#slk.receiver.device = /dev/ttyS0
slk.receiver.speed = 19200
slk.receiver.parity = false
slk.receiver.twoStopBits = false
slk.receiver.frequency = 10
slk.sender.frequency = 10
slk.writeHistory = false
#ts.receiver.device = /dev/ttyS0
#ts.receiver.speed = 57600
#ts.receiver.parity = false
#ts.receiver.twoStopBits = false
ts.receiver.ip = 192.168.0.190
ts.receiver.port = 4023
ts.receiver.frequency = 23
ts.sender.ip = 192.168.0.175
ts.sender.port = 4023
#ts.sender.frequency = 23
ts.writeHistory = false
ts_mcp1.receiver.ip = 192.168.0.190
ts_mcp1.receiver.port = 4022
ts_mcp1.sender.ip = 192.168.0.175
ts_mcp1.sender.port = 4022
r.mv2.receiver.ip = 127.0.0.2
r.mv2.receiver.port = 3003
r.mv2.receiver.frequency = 20
r.mv2.sender.ip = 127.0.0.1
r.mv2.sender.port = 3003
r.mv2.sender.frequency = 20
r.mv1.receiver.ip = 127.0.0.4
r.mv1.receiver.port = 3003
r.mv1.receiver.frequency = 20
r.mv1.sender.ip = 127.0.0.3
r.mv1.sender.port = 3003
r.mv1.sender.frequency = 20

Binary file not shown.

Binary file not shown.