git-svn-id: svn://db.shs.com.ru/pip@843 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
314
main.cpp
314
main.cpp
@@ -1,232 +1,110 @@
|
|||||||
#include "pip.h"
|
#include "pip.h"
|
||||||
#include "piintrospection_server.h"
|
|
||||||
#include "piintrospection_containers_p.h"
|
|
||||||
//#include <winsock2.h>
|
|
||||||
|
|
||||||
class CL: public PIObject {
|
|
||||||
PIOBJECT(CL)
|
template <typename T>
|
||||||
|
class PIHash {
|
||||||
public:
|
public:
|
||||||
EVENT_HANDLER1(void, nc, PIEthernet * , client) {
|
inline PIHash() {
|
||||||
piCout << "client" << client;
|
shift = 0;
|
||||||
|
r_size = 0;
|
||||||
}
|
}
|
||||||
|
inline T & operator [](size_t index) {return content[index].second;}
|
||||||
|
inline const T & operator [](size_t index) const {return content[index].second;}
|
||||||
|
inline size_t size() const {return r_size;}
|
||||||
|
inline ssize_t size_s() const {return r_size;}
|
||||||
|
inline size_t capacity() const {return content.capacity();}
|
||||||
|
inline bool isEmpty() const {return content.isEmpty();}
|
||||||
|
|
||||||
|
inline PIHash<T> & push_back(const T & v) {
|
||||||
|
uint h = piHash(v);
|
||||||
|
add_hash(h);
|
||||||
|
int i = (h >> (32 - shift));
|
||||||
|
if (content[i].first == 0) r_size++;
|
||||||
|
content[i] = HashPair(h, v);
|
||||||
|
//for (int i=0; i<content.size_s(); ++i) piCout << i << content[i].first << content[i].second;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
inline PIHash<T> & append(const T & v) {return push_back(v);}
|
||||||
|
// inline PIVector<T> & append(const PIVector<T> & other) {
|
||||||
|
// assert(&other != this);
|
||||||
|
// size_t ps = piv_size;
|
||||||
|
// alloc(piv_size + other.piv_size);
|
||||||
|
// newT(piv_data + ps, other.piv_data, other.piv_size);
|
||||||
|
// return *this;
|
||||||
|
// }
|
||||||
|
inline PIHash<T> & operator <<(const T & v) {return push_back(v);}
|
||||||
|
// inline PIVector<T> & operator <<(const PIVector<T> & other) {return append(other);}
|
||||||
|
private:
|
||||||
|
typedef PIPair<uint, T> HashPair;
|
||||||
|
void add_hash(uint nh) {
|
||||||
|
piCout << "add" << nh;
|
||||||
|
//for (int i=0; i<content.size_s(); ++i) piCout << i << content[i].first << content[i].second;
|
||||||
|
if (nh == 0) {
|
||||||
|
piCout << "invalid hash";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (shift == 0) {
|
||||||
|
shift++;
|
||||||
|
content.resize(1 << shift, HashPair(0, T()));
|
||||||
|
}
|
||||||
|
int i = (nh >> (32 - shift));
|
||||||
|
while (content[i].first != nh && content[i].first != 0) {
|
||||||
|
shift++;
|
||||||
|
i = (nh >> (32 - shift));
|
||||||
|
content.resize(1 << shift, HashPair(0, T()));
|
||||||
|
rehash();
|
||||||
|
}
|
||||||
|
//for (int i=0; i<content.size_s(); ++i) piCout << i << content[i].first << content[i].second;
|
||||||
|
}
|
||||||
|
void rehash() {
|
||||||
|
piCout << "rehash";
|
||||||
|
PIVector<PIPair<uint, T> > tmpc(content.size(), HashPair(0, T()));
|
||||||
|
for (int i=0; i<content.size_s(); ++i) {
|
||||||
|
if (content[i].first == 0) continue;
|
||||||
|
int h = (content[i].first >> (32 - shift));
|
||||||
|
tmpc[h] = content[i];
|
||||||
|
piCout << i << "->" << h;
|
||||||
|
}
|
||||||
|
content.swap(tmpc);
|
||||||
|
}
|
||||||
|
|
||||||
|
PIVector<PIPair<uint, T> > content;
|
||||||
|
uint shift;
|
||||||
|
size_t r_size;
|
||||||
};
|
};
|
||||||
|
|
||||||
PIKbdListener kbd(0, 0, false);
|
|
||||||
#include <typeinfo>
|
|
||||||
#define PIIS_TYPENAME(t) typeid(t).name()
|
|
||||||
|
|
||||||
template<typename T> class Name
|
template<typename T>
|
||||||
{
|
inline PICout operator <<(PICout s, const PIHash<T> & v) {
|
||||||
public:
|
s.space();
|
||||||
static const char * name() {return PIIS_TYPENAME(T);}
|
s.setControl(0, true);
|
||||||
};
|
s << "{";
|
||||||
|
for (size_t i = 0; i < v.size(); ++i) {
|
||||||
PISystemMonitor sys_mon;
|
s << v[i];
|
||||||
|
if (i < v.size() - 1)
|
||||||
|
s << ", ";
|
||||||
|
|
||||||
class TestRec: public PIObject {
|
|
||||||
PIOBJECT(TestRec)
|
|
||||||
public:
|
|
||||||
EVENT_HANDLER(void, reinitSpec) {/*piCout << "reinitSpec";*/int a = 10; (void*)&a;}
|
|
||||||
|
|
||||||
EVENT_HANDLER(void, coeffsChanged) {piCout << "coeffsChanged";}
|
|
||||||
EVENT_HANDLER(void, guiAboutSend) {piCout << "guiAboutSend";}
|
|
||||||
EVENT_HANDLER(void, guiAfterSend) {piCout << "guiAfterSend";}
|
|
||||||
EVENT_HANDLER(void, guiReceived) {piCout << "guiReceived";}
|
|
||||||
|
|
||||||
EVENT_HANDLER(void, playerReceived) {/*piCout << "playerReceived";*/int a = 10; (void*)&a;}
|
|
||||||
|
|
||||||
EVENT_HANDLER(void, playerValuesReceived) {piCout << "playerValuesReceived";}
|
|
||||||
EVENT_HANDLER(void, calculate) {piCout << "calculate";}
|
|
||||||
EVENT_HANDLER(void, autostart) {piCout << "autostart";}
|
|
||||||
|
|
||||||
EVENT_HANDLER(void, syncOff) {/*piCout << "syncOff";*/int a = 10; (void*)&a;}
|
|
||||||
|
|
||||||
EVENT_HANDLER(void, del) {piCout << "DEL";}
|
|
||||||
};
|
|
||||||
|
|
||||||
class TestSnd: public PIObject {
|
|
||||||
PIOBJECT(TestSnd)
|
|
||||||
public:
|
|
||||||
EVENT(reinitSpec)
|
|
||||||
EVENT(coeffsChanged)
|
|
||||||
EVENT(guiAboutSend)
|
|
||||||
EVENT(guiAfterSend)
|
|
||||||
EVENT(guiReceived)
|
|
||||||
EVENT(playerReceived)
|
|
||||||
EVENT(playerValuesReceived)
|
|
||||||
EVENT(calculate)
|
|
||||||
EVENT(autostart)
|
|
||||||
EVENT(syncOff);
|
|
||||||
};
|
|
||||||
|
|
||||||
class CA: public PIObject {
|
|
||||||
PIOBJECT(CA)
|
|
||||||
EVENT(evA)
|
|
||||||
};
|
|
||||||
|
|
||||||
class CB: public CA {
|
|
||||||
PIOBJECT_SUBCLASS(CB, CA)
|
|
||||||
EVENT(evB)
|
|
||||||
};
|
|
||||||
|
|
||||||
class CC: public CB {
|
|
||||||
PIOBJECT_SUBCLASS(CC, CB)
|
|
||||||
EVENT(evC)
|
|
||||||
};
|
|
||||||
|
|
||||||
#define CONN(sn) CONNECTU(snd, sn, rec, sn)
|
|
||||||
uint HashLy(const void* d, int l) {
|
|
||||||
uint hash = 0u;
|
|
||||||
for(int i = 0; i < l; ++i)
|
|
||||||
hash = (hash * 1664525u) + ((uchar*)d)[i] + 1013904223u;
|
|
||||||
return hash;
|
|
||||||
}
|
}
|
||||||
int main(int argc, char * argv[]) {
|
s << "}";
|
||||||
//sys_mon.startOnSelf();
|
s.restoreControl();
|
||||||
|
return s;
|
||||||
PITimeMeasurer tm;
|
|
||||||
|
|
||||||
TestRec * rec = 0;
|
|
||||||
TestSnd * snd = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < 1; ++i) {
|
|
||||||
if (rec) delete rec;
|
|
||||||
if (snd) delete snd;
|
|
||||||
rec = new TestRec();
|
|
||||||
snd = new TestSnd();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CONNECTU(snd, deleted, rec, del)
|
|
||||||
CONN(reinitSpec)
|
|
||||||
CONN(coeffsChanged)
|
|
||||||
CONN(guiAboutSend)
|
|
||||||
CONN(guiAfterSend)
|
|
||||||
CONN(guiReceived)
|
|
||||||
CONN(playerReceived)
|
|
||||||
CONN(playerValuesReceived)
|
|
||||||
CONN(calculate)
|
|
||||||
CONN(autostart)
|
|
||||||
CONN(syncOff);
|
|
||||||
|
|
||||||
// 0.6 us on call
|
int main() {
|
||||||
for (int j = 0; j < 10; ++j) {
|
PIHash<PIString> v;
|
||||||
tm.reset();
|
v << "x";
|
||||||
for (int i = 0; i < 10000; ++i) {
|
v << "bbb";
|
||||||
snd->reinitSpec();
|
v << "bbc";
|
||||||
snd->playerReceived();
|
v << "aaa";
|
||||||
snd->syncOff();
|
v << "123";
|
||||||
|
v << "321";
|
||||||
|
v << "aaa" << "bbb" << "ccc";
|
||||||
|
piCout << v.size() << v.capacity();
|
||||||
|
//piCout << (1 << 2);
|
||||||
|
PIHash<PIString> v2;
|
||||||
|
for (int i=0; i<1000; ++i) {
|
||||||
|
v2 << PIString::fromNumber(randomi());
|
||||||
}
|
}
|
||||||
double el = tm.elapsed_m();
|
piCout << v2.size() << v2.capacity();
|
||||||
piCout << el;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*piCout << "****";
|
|
||||||
PIPeer().dump();
|
|
||||||
piCout << "----";*/
|
|
||||||
|
|
||||||
/* PIString _s = "012345678901234567890123456789";
|
|
||||||
CRC_32 crc = standardCRC_32();
|
|
||||||
PIByteArray _sd = _s.toByteArray();
|
|
||||||
double el;
|
|
||||||
uint ret;
|
|
||||||
|
|
||||||
tm.reset();
|
|
||||||
for (int i = 0; i < 1000000; ++i) {
|
|
||||||
ret = crc.calculate(_sd.data(), _sd.size_s());
|
|
||||||
}
|
|
||||||
el = tm.elapsed_m();
|
|
||||||
piCout << "";
|
|
||||||
piCout << ret;
|
|
||||||
piCout << "crc" << el;
|
|
||||||
|
|
||||||
tm.reset();
|
|
||||||
for (int i = 0; i < 1000000; ++i) {
|
|
||||||
ret = HashLy(_sd.data(), _sd.size_s());
|
|
||||||
}
|
|
||||||
el = tm.elapsed_m();
|
|
||||||
piCout << "";
|
|
||||||
piCout << ret;
|
|
||||||
piCout << " ly" << el;
|
|
||||||
|
|
||||||
tm.reset();
|
|
||||||
for (int i = 0; i < 1000000; ++i) {
|
|
||||||
ret = murmur3_32((const uchar*)_sd.data(), _sd.size_s());
|
|
||||||
}
|
|
||||||
el = tm.elapsed_m();
|
|
||||||
piCout << "";
|
|
||||||
piCout << ret;
|
|
||||||
piCout << "mur" << el;
|
|
||||||
*/
|
|
||||||
delete snd;
|
|
||||||
delete rec;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
PIString s;
|
|
||||||
s = "gafaffaf";
|
|
||||||
piCout << s;
|
|
||||||
piCout << s;
|
|
||||||
s = "dd";
|
|
||||||
piCout << s;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
PIINTROSPECTION_START;
|
|
||||||
//uint id = standardCRC_32().calculate("6PIChar");
|
|
||||||
//PIIntrospectionContainers * ci = PIINTROSPECTION_CONTAINERS->p;
|
|
||||||
FOREVER {
|
|
||||||
piMSleep(100);
|
|
||||||
//piCout << ci->data[id].count << ci->data[id].bytes_allocated << ci->data[id].bytes_used;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
PICLI cli(argc, argv);
|
|
||||||
cli.setDebug(false);
|
|
||||||
cli.addArgument("send");
|
|
||||||
//PISystemInfo::machineID();
|
|
||||||
kbd.enableExitCapture();
|
|
||||||
kbd.start();
|
|
||||||
|
|
||||||
if (cli.hasArgument("send")) {
|
|
||||||
/*piCout << "send mode";
|
|
||||||
PIEthernet eth;
|
|
||||||
eth.setSendAddress(cli.rawArguments().back() + ":15123");
|
|
||||||
eth.open();
|
|
||||||
while (!kbd.exiting) {
|
|
||||||
eth.send(PIByteArray("test string", 12));
|
|
||||||
piMSleep(500);
|
|
||||||
}*/
|
|
||||||
PIEthernet eth(PIEthernet::TCP_Server);
|
|
||||||
eth.listen(cli.rawArguments().back() + ":15123", true);
|
|
||||||
//eth.open();
|
|
||||||
CL cl;
|
|
||||||
CONNECTU(ð, newConnection, &cl, nc);
|
|
||||||
|
|
||||||
WAIT_FOR_EXIT;
|
|
||||||
piCout << "exiting ...";
|
|
||||||
|
|
||||||
} else {
|
|
||||||
piCout << "recv mode";
|
|
||||||
PIEthernet eth(PIEthernet::TCP_Client);
|
|
||||||
//eth.setReadAddress(cli.rawArguments().back() + ":15123");
|
|
||||||
eth.startThreadedRead();
|
|
||||||
int s = eth.socket();
|
|
||||||
piCout << s;
|
|
||||||
piCout << "connect ...";
|
|
||||||
eth.connect(cli.rawArguments().back() + /*":13361"*/":15123");
|
|
||||||
piCout << "connect done" << eth.isConnected();
|
|
||||||
//fcntl(fd, F_SETFL, O_NONBLOCK);
|
|
||||||
|
|
||||||
WAIT_FOR_EXIT;
|
|
||||||
piCout << "exiting ...";
|
|
||||||
|
|
||||||
}
|
|
||||||
/*PIThread t;
|
|
||||||
t.start(10);
|
|
||||||
//WAIT_FOR_EXIT;
|
|
||||||
piSleep(20.);
|
|
||||||
t.stop(true);*/
|
|
||||||
piCout << "exit main ...";
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -461,7 +461,7 @@ uint letobe_i(uint v) {return (v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// \brief Generic hash function, impements murmur3/32 algorithm
|
/// \brief Generic hash function, impements murmur3/32 algorithm
|
||||||
inline uint piHash(const uchar * data, uint len, uint seed = 0) {
|
inline uint piHashData(const uchar * data, uint len, uint seed = 0) {
|
||||||
if (!data || len <= 0) return 0u;
|
if (!data || len <= 0) return 0u;
|
||||||
uint h = seed;
|
uint h = seed;
|
||||||
if (len > 3) {
|
if (len > 3) {
|
||||||
|
|||||||
@@ -278,7 +278,7 @@ uint PIByteArray::checksumPlain32() const {
|
|||||||
|
|
||||||
|
|
||||||
uint PIByteArray::hash() const {
|
uint PIByteArray::hash() const {
|
||||||
return piHash(data(), size_s());
|
return piHashData(data(), size_s());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -315,4 +315,16 @@ __PIBYTEARRAY_SIMPLE_TYPE__(double)
|
|||||||
__PIBYTEARRAY_SIMPLE_TYPE__(ldouble)
|
__PIBYTEARRAY_SIMPLE_TYPE__(ldouble)
|
||||||
__PIBYTEARRAY_SIMPLE_TYPE__(PIChar)
|
__PIBYTEARRAY_SIMPLE_TYPE__(PIChar)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline uint piHash(const T & v) {
|
||||||
|
PIByteArray ba;
|
||||||
|
ba << v;
|
||||||
|
return ba.hash();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<> inline uint piHash(const PIByteArray & ba) {return ba.hash();}
|
||||||
|
|
||||||
|
|
||||||
#endif // PIBYTEARRAY_H
|
#endif // PIBYTEARRAY_H
|
||||||
|
|||||||
@@ -420,7 +420,7 @@ const char * PIString::dataAscii() const {
|
|||||||
|
|
||||||
uint PIString::hash() const {
|
uint PIString::hash() const {
|
||||||
buildData();
|
buildData();
|
||||||
return piHash(data_.data(), data_.size_s() - 1);
|
return piHashData(data_.data(), data_.size_s() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -863,4 +863,6 @@ inline PIByteArray & operator >>(PIByteArray & s, PIStringList & v) {int sz; s >
|
|||||||
//! \relatesalso PIStringList \relatesalso PICout \brief Output operator to PICout
|
//! \relatesalso PIStringList \relatesalso PICout \brief Output operator to PICout
|
||||||
inline PICout operator <<(PICout s, const PIStringList & v) {s.space(); s.setControl(0, true); s << "{"; for (uint i = 0; i < v.size(); ++i) {s << "\"" << v[i] << "\""; if (i < v.size() - 1) s << ", ";} s << "}"; s.restoreControl(); return s;}
|
inline PICout operator <<(PICout s, const PIStringList & v) {s.space(); s.setControl(0, true); s << "{"; for (uint i = 0; i < v.size(); ++i) {s << "\"" << v[i] << "\""; if (i < v.size() - 1) s << ", ";} s << "}"; s.restoreControl(); return s;}
|
||||||
|
|
||||||
|
template<> inline uint piHash(const PIString & s) {return s.hash();}
|
||||||
|
|
||||||
#endif // PISTRING_H
|
#endif // PISTRING_H
|
||||||
|
|||||||
Reference in New Issue
Block a user