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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file message.lua
--
-- imports
import("core.base.object")
-- define module
local message = message or object()
-- the common message code
message.CODE_CONNECT = 1 -- connect server
message.CODE_DISCONNECT = 2 -- disconnect server
message.CODE_CLEAN = 3 -- clean all cached files in server
message.CODE_DATA = 4 -- send data
message.CODE_RUNCMD = 5 -- run the given command in server
message.CODE_DIFF = 6 -- diff files between server and client
message.CODE_SYNC = 7 -- sync files between server and client
message.CODE_PULL = 8 -- pull the given file from server
-- init message
function message:init(body)
self._BODY = body
end
-- get message code
function message:code()
return self:body().code
end
-- get session id
function message:session_id()
return self:body().session_id
end
-- is connect message?
function message:is_connect()
return self:code() == message.CODE_CONNECT
end
-- is disconnect message?
function message:is_disconnect()
return self:code() == message.CODE_DISCONNECT
end
-- is diff message?
function message:is_diff()
return self:code() == message.CODE_DIFF
end
-- is sync message?
function message:is_sync()
return self:code() == message.CODE_SYNC
end
-- is pull message?
function message:is_pull()
return self:code() == message.CODE_PULL
end
-- is clean message?
function message:is_clean()
return self:code() == message.CODE_CLEAN
end
-- is run command message?
function message:is_runcmd()
return self:code() == message.CODE_RUNCMD
end
-- is data message?
function message:is_data()
return self:code() == message.CODE_DATA
end
-- is success?
function message:success()
return self:body().status == true
end
-- set status, ok or failed
function message:status_set(ok)
self:body().status = ok
end
-- get message body
function message:body()
return self._BODY
end
-- get message errors
function message:errors()
return self:body().errors
end
-- set message errors
function message:errors_set(errors)
self:body().errors = errors
end
-- clone a message
function message:clone()
local body = table.copy(self:body())
return _new(body)
end
-- dump message
function message:dump()
print(self:body())
end
-- new message
function _new(body)
local instance = message()
instance:init(body)
return instance
end
-- new connect message
function new_connect(session_id)
return _new({
code = message.CODE_CONNECT,
session_id = session_id,
xmakever = xmake.version():shortstr()
})
end
-- new disconnect message
function new_disconnect(session_id)
return _new({
code = message.CODE_DISCONNECT,
session_id = session_id
})
end
-- new diff message, e.g manifest = {["src/main.c"] = {sha256 = "", mtime = ""}}
function new_diff(session_id, manifest)
return _new({
code = message.CODE_DIFF,
session_id = session_id,
manifest = manifest
})
end
-- new sync message, e.g. manifest = {modified = {"src/main.c"}, inserted = {}, removed = {}}
function new_sync(session_id, manifest)
return _new({
code = message.CODE_SYNC,
session_id = session_id,
manifest = manifest
})
end
-- new clean message
function new_clean(session_id)
return _new({
code = message.CODE_CLEAN,
session_id = session_id
})
end
-- new run command message
function new_runcmd(session_id, program, argv)
return _new({
code = message.CODE_RUNCMD,
session_id = session_id,
program = program,
argv = argv
})
end
-- new data message
function new_data(session_id, size)
return _new({
code = message.CODE_DATA,
size = size,
session_id = session_id
})
end
function main(body)
return _new(body)
end
|