This commit is contained in:
WangQiang 2016-08-13 01:59:09 +00:00
parent 1cadb2c69e
commit 860422b225
4 changed files with 280 additions and 0 deletions

46
Device/Proxy.cpp Normal file
View File

@ -0,0 +1,46 @@

#include "Proxy.h"
Proxy::Proxy()
{
Cache = nullptr;
CacheSize = 0;
TimeStamp = 0;
Stamp = false;
}
bool Proxy::Open()
{
CacheSize = 256;
Cache = new MemoryStream(CacheSize);
OnOpen();
return true;
}
bool Proxy::Close()
{
delete Cache;
OnClose();
return true;
}
bool Proxy::Upload(Buffer& data)
{
if (!Cache || !CacheSize)
{
// 没有Cache则直接发送
MemoryStream ms;
BinaryPair bp(ms);
bp.Set("Port", name);
if (Stamp)bp.Set("Stamp",Sys.Ms());
bp.Set("Data", data);
Buffer data2(ms.GetBuffer(), ms.Position());
// auto * fac = ProxyFactory::Current;
// fac->Client->Invoke("Proxy\Upload",data2);
}
return true;
}

31
Device/Proxy.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef __Proxy_H__
#define __Proxy_H__
#include "Sys.h"
#include "SerialPort.h"
#include "Message/BinaryPair.h"
class Proxy
{
public:
String name; // 端口名
MemoryStream* Cache; // 缓存空间
int CacheSize; // 缓存大小
bool Stamp; // 时间戳开关
int TimeStamp; // 时间戳
Proxy();
bool Open();
bool Close();
virtual bool OnOpen() = 0;
virtual bool OnClose() = 0;
virtual bool SetConfig(Dictionary<cstring, cstring>& config, String& str) = 0;
virtual bool GetConfig(Dictionary<cstring, cstring>& config) = 0;
virtual int Write(Buffer& data) = 0;
virtual Buffer& Read(Buffer& data) = 0;
bool Upload(Buffer& data);
};
#endif

164
Message/ProxyFactory.cpp Normal file
View File

@ -0,0 +1,164 @@

#include "ProxyFactory.h"
ProxyFactory * ProxyFactory::Current = nullptr;
ProxyFactory::ProxyFactory()
{
Client = nullptr;
}
bool ProxyFactory::Open(TokenClient* client)
{
if (!client)return false;
Client = client;
Client->Register("Proxy/GetConfig", &ProxyFactory::GetConfig, this);
Client->Register("Proxy/SetConfig", &ProxyFactory::SetConfig, this);
return true;
}
bool ProxyFactory::Register(cstring& name, Proxy* dev)
{
Proxys.Add(name, dev);
return true;
}
bool ProxyFactory::GetConfig(const BinaryPair& args, Stream& result)
{
String Name;
args.Get("name", Name);
Proxy* port;
Proxys.TryGetValue(Name.GetBuffer(), port);
auto ms = (MemoryStream&)result;
if (!port)
{
BinaryPair bp(ms);
bp.Set("ErrorCode", (byte)0x01);
}
else
{
// String str;
// port->GetConfig(str); // 调用端口的函数处理内容
// ms.Write(str);
Dictionary<cstring, cstring> cfg;
port->GetConfig(cfg); // 调用端口的函数处理内容
// 数据先写进缓冲区ms2
MemoryStream ms2;
auto name = cfg.Keys();
auto value = cfg.Keys();
for (int i = 0; i < cfg.Count(); i++)
{
ms2.Write(String(name[i]));
ms2.Write('=');
ms2.Write(String(value[i]));
if (i < cfg.Count() - 1) ms2.Write('&');
}
// 然后组成名词对写进回复数据内去
BinaryPair bp(ms);
bp.Set("config", Buffer(ms2.GetBuffer(), ms2.Position()));
}
return true;
}
bool ProxyFactory::SetConfig(const BinaryPair& args, Stream& result)
{
String Name;
args.Get("name", Name);
Proxy* port;
Proxys.TryGetValue(Name.GetBuffer(), port);
auto ms = (MemoryStream&)result;
if (!port)
{
BinaryPair bp(ms);
bp.Set("ErrorCode", (byte)0x01);
}
else
{
Dictionary<cstring, char* > config; // Dictionary<cstring, cstring> 无法实例化 不知道哪出问题了
String str;
args.Get("config", str); // 需要确认字段名称
if (GetDic(str, config))
{
String str2;
port->SetConfig((Dictionary<cstring, cstring> &)config, str2); // 调用端口的函数处理内容
ms.Write(str2);
}
else
{
BinaryPair bp(ms);
bp.Set("ErrorCode", (byte)0x02);
}
}
return true;
}
bool ProxyFactory::QueryPorts(const BinaryPair& args, Stream& result)
{
MemoryStream ms;
auto portnames = Proxys.Keys();
for (int i = 0; i < Proxys.Count(); i++)
{
ms.Write(String(portnames[i]));
if (i < Proxys.Count() - 1)ms.Write(',');
}
BinaryPair rsms(result);
rsms.Set("ports", Buffer(ms.GetBuffer(), ms.Position()));
return true;
}
bool ProxyFactory::GetDic(String& str, Dictionary<cstring, char*>& dic)
{
int start = 0;
int end = 0;
while (1)
{
char* name = nullptr;
char* data = nullptr;
end = str.IndexOf('=', start); // 找到 = 表示一个参数名的结束
if (end > start && end < str.Length()) // 需要限制很多
{
cstring name = str.GetBuffer() + start; // 拿到 name
str[end] = '\0';
start = end + 1;
}
else
{
if (!end) // 没有 = 表示没有参数名了
return false;
else
return true;
}
end = str.IndexOf('&', start); // 找到 & 表示一个参数的结束
if (!end)
end = str.Length();
else
str[end] = '\0';
if (end > start)
{
cstring name = str.GetBuffer() + start; // 拿到 data
start = end + 1;
}
dic.Add(name, data); // Dictionary<cstring, cstring> 无法实例化 不知道哪出问题了
}
}
ProxyFactory* ProxyFactory::Create()
{
if (!Current)Current = new ProxyFactory();
return Current;
}

39
Message/ProxyFactory.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef __ProxyFactory_H__
#define __ProxyFactory_H__
#include "Sys.h"
#include "TokenNet\TokenMessage.h"
#include "Message\BinaryPair.h"
#include "TokenNet\TokenClient.h"
#include "Device\Proxy.h"
class ProxyFactory
{
public:
Dictionary<cstring, Proxy*> Proxys;
TokenClient* Client;
ProxyFactory();
bool Open(TokenClient* client);
// 配置
bool GetConfig(const BinaryPair& args, Stream& result);
bool SetConfig(const BinaryPair& args, Stream& result);
// 获取设备列表
bool QueryPorts(const BinaryPair& args, Stream& result);
// 设备注册
bool Register(cstring& name, Proxy* dev);
private:
bool GetDic(String& str, Dictionary<cstring, char*>& dic);
public:
static ProxyFactory* Current;
static ProxyFactory* Create();
};
#endif