summaryrefslogtreecommitdiff
path: root/proto_lib
diff options
context:
space:
mode:
authorGluzskiy Alexandr <sss@sss.chaoslab.ru>2013-02-13 03:20:52 +0200
committerGluzskiy Alexandr <sss@sss.chaoslab.ru>2013-02-13 03:20:52 +0200
commite255c37eca94d4d9c23f689b473b106575b9e2fa (patch)
tree0d7916bcd872d3da2367acbec6d0594c837193fd /proto_lib
parent85a13d70cc5249e2f583bb16f0914646aefe4fe6 (diff)
some testing code
Diffstat (limited to 'proto_lib')
-rw-r--r--proto_lib/api_protocol.h8
-rw-r--r--proto_lib/lib.project2
-rw-r--r--proto_lib/packet.cpp42
-rw-r--r--proto_lib/utilities.cpp26
4 files changed, 67 insertions, 11 deletions
diff --git a/proto_lib/api_protocol.h b/proto_lib/api_protocol.h
index b18d2e7..bebd401 100644
--- a/proto_lib/api_protocol.h
+++ b/proto_lib/api_protocol.h
@@ -24,9 +24,16 @@ struct service_s
struct cmd
{
std::string command, description;
+#ifdef DEBUG
+ bool operator==(const cmd&);
+#endif
};
std::string service;
std::list<cmd> cmds;
+#ifdef DEBUG
+ bool operator==(service_s&);
+ bool operator!=(service_s&);
+#endif
};
struct svc_cmd
@@ -55,6 +62,7 @@ public:
//client functions
static packet *cli_make_init_packet(); //should be first packet to server
static packet *cli_make_command_packet(std::string &service, std::string &command);
+ static packet *cli_make_command_packet(const char* service, const char* command);
static packet *cli_make_request_services_packet();
static std::list<service_s> *cli_extract_services(packet&);
diff --git a/proto_lib/lib.project b/proto_lib/lib.project
index 65be5d8..d686c48 100644
--- a/proto_lib/lib.project
+++ b/proto_lib/lib.project
@@ -29,6 +29,7 @@
<Configuration Name="Debug" CompilerType="gnu g++" DebuggerType="GNU gdb debugger" Type="Static Library" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append">
<Compiler Options="-g;-O0;-Wall" C_Options="-g;-O0;-Wall" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" UseDifferentPCHFlags="no" PCHFlags="">
<IncludePath Value="."/>
+ <Preprocessor Value="DEBUG"/>
</Compiler>
<Linker Options="" Required="yes"/>
<ResourceCompiler Options="" Required="no"/>
@@ -74,6 +75,7 @@
<![CDATA[
+
]]>
</Environment>
<Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath="">
diff --git a/proto_lib/packet.cpp b/proto_lib/packet.cpp
index 92272a8..92d8508 100644
--- a/proto_lib/packet.cpp
+++ b/proto_lib/packet.cpp
@@ -18,6 +18,7 @@
#include <vector>
#include <algorithm>
#include <string.h>
+#include <stdio.h>
#include "api_protocol.h"
#include "packet.h"
#include "utilities.h"
@@ -135,6 +136,12 @@ packet *packet::cli_make_command_packet(std::string &service, std::string &comma
return new packet(v);
}
+packet *packet::cli_make_command_packet(const char* service, const char* command)
+{
+ std::string s = service, c = command;
+ return cli_make_command_packet(s, c);
+}
+
packet *packet::cli_make_request_services_packet()
{
std::vector<unsigned char> v;
@@ -165,17 +172,20 @@ std::list<service_s> *packet::cli_extract_services(packet& p)
i2++;
i2 = std::find(i2, i3, delimiter);
bool bdesc = false;
+ std::string cmd, desc;
while(i2 < i3)
{
- std::string cmd, desc;
+ cmd.clear(); desc.clear();
if(!bdesc)
{
+ i++; //wtf ?
for(; i < i2; ++i)
cmd.push_back((char)*i);
bdesc = true;
}
else
{
+ i++; //wtf ?
for(; i < i2; ++i)
desc.push_back((char)*i);
bdesc = false;
@@ -187,6 +197,14 @@ std::list<service_s> *packet::cli_extract_services(packet& p)
i2++;
i2 = std::find(i2, i3, delimiter);
}
+ i++; //wtf ?
+ desc.clear();
+ for(; i < i2; ++i)
+ desc.push_back((char)*i);
+ service_s::cmd c;
+ c.command = cmd;
+ c.description = desc;
+ cmds.push_back(c);
}
else //found service without commands
{
@@ -194,8 +212,8 @@ std::list<service_s> *packet::cli_extract_services(packet& p)
service.push_back((char)*i);
}
i = i3;
- i2 = std::find(i, p.raw().end(), delimiter);
- i3 = std::find(i, p.raw().end(), block_end);
+ i2 = std::find(i+1, p.raw().end(), delimiter);
+ i3 = std::find(i+1, p.raw().end(), block_end);
}
service_s s;
s.cmds = cmds;
@@ -223,17 +241,19 @@ std::string packet::cli_parse_command_reply(packet& p)
bool packet::serv_validate_client_proto(packet &p)
{
- const std::vector<unsigned char> &data = p.raw();
- if(data.empty())
- return false;
- if(std::search(data.begin(), data.end(), proto_header, proto_header + sizeof(proto_header)) == data.end())
+ if(!p.is_good())
return false;
- if(std::search(data.begin(), data.end(), proto_footer, proto_footer + sizeof(proto_footer)) == data.end())
+ if(!p.is_client_packet())
return false;
- std::vector<unsigned char>::const_iterator i = std::search(data.begin(), data.end(), cli_packet, cli_packet + sizeof(cli_packet));
- if(i == data.end())
+ std::vector<unsigned char>::const_iterator i = std::search(p.raw().begin(), p.raw().end(), type_auth, type_auth + sizeof(type_auth));
+ if(i == p.raw().end())
return false;
- i += sizeof(cli_packet);
+ i += sizeof(type_auth);
+ i++; //wtf ?
+#ifdef DEBUG
+ printf("version found %d\n", *i);
+ printf("actual version %d\n", proto_version);
+#endif
if(*i < proto_version)
return false;
return true;
diff --git a/proto_lib/utilities.cpp b/proto_lib/utilities.cpp
index 64f69bb..b54e45a 100644
--- a/proto_lib/utilities.cpp
+++ b/proto_lib/utilities.cpp
@@ -81,4 +81,30 @@ const unsigned char *to_internal_type(packet_type t)
return type;
}
+#ifdef DEBUG
+bool service_s::cmd::operator==(const cmd& c)
+{
+ if(c.command != command)
+ return false;
+ if(c.description != description)
+ return false;
+ return true;
+}
+
+bool service_s::operator==(service_s& s)
+{
+ if(s.service != service)
+ return false;
+ for(std::list<service_s::cmd>::iterator i = s.cmds.begin(), end = s.cmds.end(); i != end; ++i)
+ {
+ if(std::find(cmds.begin(), cmds.end(), *i) == cmds.end())
+ return false;
+ }
+ return true;
+}
+bool service_s::operator!=(service_s& s)
+{
+ return !(*this == s);
+}
+#endif
};