/
protocol.cpp
129 lines (105 loc) · 3.25 KB
/
protocol.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/** @file protocol.cpp Network protocol for communicating with a server.
*
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/shell/Protocol"
#include <de/LogBuffer>
#include <de/Reader>
#include <de/Writer>
namespace de {
namespace shell {
static String const PT_COMMAND = "shell.command";
// LogEntryPacket ------------------------------------------------------------
static char const *LOG_ENTRY_PACKET_TYPE = "LOGE";
LogEntryPacket::LogEntryPacket() : Packet(LOG_ENTRY_PACKET_TYPE)
{}
LogEntryPacket::~LogEntryPacket()
{
clear();
}
void LogEntryPacket::clear()
{
foreach(LogEntry *e, _entries) delete e;
_entries.clear();
}
bool LogEntryPacket::isEmpty() const
{
return _entries.isEmpty();
}
void LogEntryPacket::add(LogEntry const &entry)
{
_entries.append(new LogEntry(entry));
}
void LogEntryPacket::execute() const
{
// Copies of all entries in the packet are added to the LogBuffer.
LogBuffer &buf = LogBuffer::appBuffer();
foreach(LogEntry *e, _entries)
{
buf.add(new LogEntry(*e, LogEntry::Remote));
}
}
void LogEntryPacket::operator >> (Writer &to) const
{
Packet::operator >> (to);
to.writeObjects(_entries);
}
void LogEntryPacket::operator << (Reader &from)
{
_entries.clear();
Packet::operator << (from);
from.readObjects<LogEntry>(_entries);
}
Packet *LogEntryPacket::fromBlock(Block const &block)
{
return constructFromBlock<LogEntryPacket>(block, LOG_ENTRY_PACKET_TYPE);
}
// Protocol ------------------------------------------------------------------
Protocol::Protocol()
{
define(LogEntryPacket::fromBlock);
}
Protocol::PacketType Protocol::recognize(Packet const *packet)
{
if(packet->type() == LOG_ENTRY_PACKET_TYPE)
{
DENG2_ASSERT(dynamic_cast<LogEntryPacket const *>(packet) != 0);
return LogEntries;
}
// One of the generic-format packets?
RecordPacket const *rec = dynamic_cast<RecordPacket const *>(packet);
if(rec)
{
if(rec->name() == PT_COMMAND)
return Command;
}
return Unknown;
}
RecordPacket *Protocol::newCommand(String const &command)
{
RecordPacket *cmd = new RecordPacket(PT_COMMAND);
cmd->record().addText("execute", command);
return cmd;
}
String Protocol::command(Packet const &commandPacket)
{
DENG2_ASSERT(recognize(&commandPacket) == Command);
RecordPacket const *rec = dynamic_cast<RecordPacket const *>(&commandPacket);
DENG2_ASSERT(rec != 0);
return (*rec)["execute"].value().asText();
}
} // namespace shell
} // namespace de