start develop picloud

This commit is contained in:
2020-08-24 13:23:31 +03:00
parent f033119a8b
commit 3965e54e38
10 changed files with 67 additions and 277 deletions

View File

@@ -498,7 +498,7 @@ if (NOT CROSSTOOLS)
# Test program # Test program
if(PIP_UTILS) if(PIP_UTILS)
add_executable(pip_test "main.cpp") add_executable(pip_test "main.cpp")
target_link_libraries(pip_test pip) target_link_libraries(pip_test pip pip_cloud)
if (LUA_FOUND) if (LUA_FOUND)
target_link_libraries(pip_test pip_lua ${LUA_LIBRARIES}) target_link_libraries(pip_test pip_lua ${LUA_LIBRARIES})
endif() endif()

View File

@@ -17,7 +17,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "piccloudclient.h" #include "picloudclient.h"
PICloudClient::PICloudClient() { PICloudClient::PICloudClient() {

View File

@@ -17,9 +17,34 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "piccloudserver.h" #include "picloudserver.h"
#include "piethernet.h"
PICloudServer::PICloudServer() { PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode) {
eth = new PIEthernet(PIEthernet::TCP_Client);
}
PICloudServer::~PICloudServer() {
}
bool PICloudServer::openDevice() {
piCout << "PICloudServer open device" << path();
bool op = eth->connect(path(), false);
if (op) {
CONNECTL(eth, disconnected, [this](bool){eth->connect(path());});
eth->startThreadedRead();
return true;
}
return false;
}
bool PICloudServer::closeDevice() {
eth->stopThreadedRead();
return eth->close();
} }

View File

@@ -17,7 +17,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "piccloudtcp.h" #include "picloudtcp.h"
#include "picrypt.h" #include "picrypt.h"
const char hash_def_key[] = "_picrypt_"; const char hash_def_key[] = "_picrypt_";

View File

@@ -1,4 +1,4 @@
/*! \file piccloudclient.h /*! \file picloudclient.h
* \brief PICloud Client * \brief PICloud Client
*/ */
/* /*
@@ -20,8 +20,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PICCLOUDCLIENT_H #ifndef PICLOUDCLIENT_H
#define PICCLOUDCLIENT_H #define PICLOUDCLIENT_H
#include "pip_cloud_export.h" #include "pip_cloud_export.h"
#include "piiodevice.h" #include "piiodevice.h"
@@ -45,4 +45,4 @@ private:
PIEthernet * eth; PIEthernet * eth;
}; };
#endif // PICCLOUDCLIENT_H #endif // PICLOUDCLIENT_H

View File

@@ -20,8 +20,8 @@
#ifndef PICLOUDMODULE_H #ifndef PICLOUDMODULE_H
#define PICLOUDMODULE_H #define PICLOUDMODULE_H
#include "piccloudtcp.h" #include "picloudtcp.h"
#include "piccloudclient.h" #include "picloudclient.h"
#include "piccloudserver.h" #include "picloudserver.h"
#endif // PICLOUDMODULE_H #endif // PICLOUDMODULE_H

View File

@@ -1,4 +1,4 @@
/*! \file piccloudserver.h /*! \file picloudserver.h
* \brief PICloud Server * \brief PICloud Server
*/ */
/* /*
@@ -20,21 +20,29 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PICCLOUDSERVER_H #ifndef PICLOUDSERVER_H
#define PICCLOUDSERVER_H #define PICLOUDSERVER_H
#include "pip_cloud_export.h" #include "pip_cloud_export.h"
#include "piiodevice.h" #include "piiodevice.h"
class PIEthernet;
class PIP_CLOUD_EXPORT PICloudServer {
class PIP_CLOUD_EXPORT PICloudServer : public PIIODevice
{
PIIODEVICE(PICloudServer)
public: public:
//! //!
explicit PICloudServer(); explicit PICloudServer(const PIString & path = PIString(), PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
virtual ~PICloudServer();
protected:
bool openDevice();
bool closeDevice();
private: private:
PIEthernet * eth;
}; };
#endif // PICCLOUDSERVER_H #endif // PICLOUDSERVER_H

View File

@@ -1,4 +1,4 @@
/*! \file piccloudtcp.h /*! \file picloudtcp.h
* \brief PICloud TCP transport * \brief PICloud TCP transport
*/ */
/* /*
@@ -20,12 +20,13 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PICCLOUDTCP_H #ifndef PICLOUDTCP_H
#define PICCLOUDTCP_H #define PICLOUDTCP_H
#include "pip_cloud_export.h" #include "pip_cloud_export.h"
#include "pistring.h" #include "pistring.h"
class PIP_CLOUD_EXPORT PICloudTCP { class PIP_CLOUD_EXPORT PICloudTCP {
public: public:
//! //!
@@ -36,4 +37,4 @@ private:
}; };
#endif // PICCLOUDTCP_H #endif // PICLOUDTCP_H

View File

@@ -30,5 +30,6 @@
#include "pisystemmodule.h" #include "pisystemmodule.h"
#include "pigeomodule.h" #include "pigeomodule.h"
#include "picryptmodule.h" #include "picryptmodule.h"
#include "picloudmodule.h"
#endif // PIP_H #endif // PIP_H

263
main.cpp
View File

@@ -1,259 +1,14 @@
#include "pip.h" #include "pip.h"
PIMap<PIString, ullong> results;
template<typename T>
struct __VariantTypeInfo__ {
typedef T PureType;
typedef const T ConstPureType;
typedef T * PointerType;
typedef const T * ConstPointerType;
typedef T & ReferenceType;
typedef const T & ConstReferenceType;
};
#define __TYPEINFO_SINGLE(PT, T) \
template<> struct __PIVariantTypeInfo__<T> { \
typedef PT PureType; \
typedef const PT ConstPureType; \
typedef PT * PointerType; \
typedef const PT * ConstPointerType; \
typedef PT & ReferenceType; \
typedef const PT & ConstReferenceType; \
};
#define REGISTER_VARIANT_TYPEINFO(T) \
__TYPEINFO_SINGLE(T, T &) \
__TYPEINFO_SINGLE(T, const T) \
__TYPEINFO_SINGLE(T, const T &)
class __VariantFunctionsBase__ {
public:
virtual __VariantFunctionsBase__ * instance() {return 0;}
virtual PIString typeName() const {return PIString();}
virtual uint hash() const {return 0;}
virtual void newT(void *& ptr, const void * value) {;}
virtual void newNullT(void *& ptr) {;}
virtual void equalT(void *& ptr, const void * value) {;}
virtual void deleteT(void *& ptr) {;}
virtual PIByteArray toData(const void * ptr) const {return PIByteArray();}
virtual void fromData(void *& ptr, PIByteArray ba) {;}
//template<typename C> static PIByteArray castHelper(PIByteArray ba) {return PIByteArray();}
//template<typename C> static C castVariant(const T & v) {return C();}
static PIMap<uint, __VariantFunctionsBase__*> & registered() {static PIMap<uint, __VariantFunctionsBase__*> ret; return ret;}
};
template<typename T>
class __VariantFunctions__: public __VariantFunctionsBase__ {
public:
__VariantFunctionsBase__ * instance() override {static __VariantFunctions__<T> ret; return &ret;}
PIString typeName() const override {return PIString();}
uint hash() const override {static uint ret = typeName().hash(); return ret;}
void newT(void *& ptr, const void * value) override {ptr = (void*)(new T(*(const T*)value)); printf(" * new\n");}
void newNullT(void *& ptr) override {ptr = (void*)(new T()); printf(" * new null\n");}
void equalT(void *& ptr, const void * value) override {*(T*)ptr = *(const T*)value; printf(" * =\n");}
void deleteT(void *& ptr) override {delete (T*)(ptr); printf(" * del\n");}
PIByteArray toData(const void * ptr) const override {PIByteArray ret; ret << (*(const T* &)ptr); return ret;}
void fromData(void *& ptr, PIByteArray ba) override {ba >> *(T*)ptr;}
template<typename C> static PIByteArray castHelper(PIByteArray ba) {return PIByteArray();}
template<typename C> static C castVariant(const T & v) {return C();}
};
class Variant {
public:
Variant() {ptr = 0; f = 0;}
Variant(const Variant & v) {
ptr = 0;
f = v.f;
if (f && v.ptr)
f->newT(ptr, v.ptr);
}
~Variant() {destroy();}
Variant & operator=(const Variant & v) {
destroy();
f = v.f;
if (f && v.ptr)
f->newT(ptr, v.ptr);
return *this;
}
template <typename T>
void setValue(const T & v) {
if (f) {
if (isMyType<T>()) {
f->equalT(ptr, (const void *)&v);
return;
}
}
destroy();
f = __VariantFunctions__<T>().instance();
f->newT(ptr, (const void *)&v);
}
template <typename T>
T value() const {
if (!f) return T();
if (!isMyType<T>())
return T();
return *(T*)(ptr);
}
PIByteArray save() const {
if (!ptr || !f) return PIByteArray();
PIByteArray ret;
ret << f->hash();
ret.append(f->toData(ptr));
return ret;
}
bool load(PIByteArray ba) {
if (ba.size_s() < 4) return false;
uint h(0); ba >> h;
destroy();
f = __VariantFunctionsBase__::registered().value(h, 0);
if (!f) return false;
f->newNullT(ptr);
f->fromData(ptr, ba);
return true;
}
//private:
template <typename T>
bool isMyType() const {return f->hash() == __VariantFunctions__<T>().instance()->hash();}
void destroy() {
if (ptr && f)
f->deleteT(ptr);
ptr = 0;
f = 0;
}
void * ptr;
__VariantFunctionsBase__ * f;
};
int Acnt = 0;
class A {
public:
A() {i = "constructor"; /*piCout << "A()";*/ ++Acnt;}
A(const A & a): i(a.i) {/*piCout << "copy A()";*/ ++Acnt;}
~A() {/*piCout << "~A()";*/ --Acnt;}
A & operator =(const A & a) {i = a.i; /*piCout << "= A()";*/ return *this;}
PIString i;
};
PIByteArray & operator <<(PIByteArray & ba, const A & v) {ba << v.i; return ba;}
PIByteArray & operator >>(PIByteArray & ba, A & v) {ba >> v.i; return ba;}
template <>
PIString __VariantFunctions__<int>::typeName() const {static PIString ret("int"); return ret;}
class _VariantRegistrator_int__ {
public:
_VariantRegistrator_int__() {
__VariantFunctionsBase__ * f = __VariantFunctions__<int>().instance();
__VariantFunctionsBase__::registered()[f->hash()] = f;
}
};
_VariantRegistrator_int__ __reg_int__;
template <>
PIString __VariantFunctions__<A>::typeName() const {static PIString ret("A"); return ret;}
class _VariantRegistrator_A__ {
public:
_VariantRegistrator_A__() {
__VariantFunctionsBase__ * f = __VariantFunctions__<A>().instance();
__VariantFunctionsBase__::registered()[f->hash()] = f;
}
};
_VariantRegistrator_A__ __reg_A__;
int main() { int main() {
{ PICloudServer s("127.0.0.1:10101");
for (int i=0; i<3; ++i) {
Variant v, v2; s.open();
piCout() << "opened";
/*{ piSleep(1);
A a, ra; s.close();
piCout() << "closed";
a.i = "main"; piSleep(1);
v.setValue(a);
ra = v.value<A>();
piCout << " 1 A.i =" << ra.i;
a.i = "main second";
v.setValue(a);
ra = v.value<A>();
piCout << " 2 A.i =" << ra.i;
piCout << "Acnt" << Acnt;
} }
piCout << "Acnt" << Acnt; return 0;
v2 = v;
PIString ra = v2.value<PIString>();
piCout << " 3 A.i =" << ra;*/
A a, ra;
a.i = "main";
v.setValue(a);
PIByteArray ba = v.save();
piCout << v2.load(ba);
piCout << v2.value<A>().i;
}
piCout << "Acnt" << Acnt;
//__VariantFunctionsBase__ * f = __VariantFunctions__<void*>().instance();
//piCout << f->typeName();
//f = __VariantFunctions__<int>().instance();
//piCout << f->typeName();
} }
/*
template <typename T>
void test_var(const char * Tname) {
piCout << Tname << "...";
ullong cnt = 0;
PIVariant _v;
PITimeMeasurer tm1s;
while (tm1s.elapsed_m() < 500.) {
++cnt;
for (int j = 0; j < 1000; ++j) {
PIVariant v;
v = T();
v.setValue(T(j));
_v = v;
}
}
PIVariant v;
results[Tname] = cnt;
}
#define TEST_VAR(T) test_var<T>(#T)
int main() {
TEST_VAR(int);
TEST_VAR(float);
TEST_VAR(ullong);
TEST_VAR(PIString);
int sz = 0;
auto i = results.makeIterator();
while (i.next()) sz = piMaxi(sz, i.key().length());
i.reset();
while (i.next())
piCout << i.key().expandedLeftTo(sz, ' ') << ":" << i.value();
}
*/