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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
--!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, Xmake Open Source Community.
--
-- @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_COMPILE = 8 -- compile the given file from client in server
message.CODE_PULL = 9 -- pull the given file from server
message.CODE_PUSH = 10 -- push the given file to server
message.CODE_FILEINFO = 11 -- get the given file info in server
message.CODE_EXISTINFO = 12 -- get exists info in server (use bloom filter)
message.CODE_END = 13 -- end
-- 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 compile message?
function message:is_compile()
return self:code() == message.CODE_COMPILE
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 pull message?
function message:is_pull()
return self:code() == message.CODE_PULL
end
-- is push message?
function message:is_push()
return self:code() == message.CODE_PUSH
end
-- is fileinfo message?
function message:is_fileinfo()
return self:code() == message.CODE_FILEINFO
end
-- is existinfo message?
function message:is_existinfo()
return self:code() == message.CODE_EXISTINFO
end
-- is end message?
function message:is_end()
return self:code() == message.CODE_END
end
-- get user authorization
function message:token()
return self:body().token
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, opt)
opt = opt or {}
return _new({
code = message.CODE_CONNECT,
session_id = session_id,
token = opt.token,
xmakever = xmake.version():shortstr()
})
end
-- new disconnect message
function new_disconnect(session_id, opt)
opt = opt or {}
return _new({
code = message.CODE_DISCONNECT,
session_id = session_id,
token = opt.token
})
end
-- new diff message, e.g manifest = {["src/main.c"] = {sha256 = "", mtime = ""}}
function new_diff(session_id, manifest, opt)
opt = opt or {}
return _new({
code = message.CODE_DIFF,
session_id = session_id,
token = opt.token,
manifest = manifest,
xmakesrc = opt.xmakesrc
})
end
-- new sync message, e.g. manifest = {modified = {"src/main.c"}, inserted = {}, removed = {}}
function new_sync(session_id, manifest, opt)
opt = opt or {}
return _new({
code = message.CODE_SYNC,
session_id = session_id,
token = opt.token,
manifest = manifest,
xmakesrc = opt.xmakesrc
})
end
-- new clean message
function new_clean(session_id, opt)
opt = opt or {}
return _new({
code = message.CODE_CLEAN,
session_id = session_id,
token = opt.token,
all = opt.all
})
end
-- new run command message
function new_runcmd(session_id, program, argv, opt)
opt = opt or {}
return _new({
code = message.CODE_RUNCMD,
session_id = session_id,
token = opt.token,
program = program,
argv = argv
})
end
-- new data message
function new_data(session_id, size, opt)
opt = opt or {}
return _new({
code = message.CODE_DATA,
size = size,
session_id = session_id,
token = opt.token
})
end
-- new compile command message
function new_compile(session_id, toolname, toolkind, plat, arch, toolchain, flags, sourcename, opt)
opt = opt or {}
return _new({
code = message.CODE_COMPILE,
session_id = session_id,
token = opt.token,
toolname = toolname,
toolkind = toolkind,
cachekey = opt.cachekey,
plat = plat,
arch = arch,
toolchain = toolchain,
flags = flags,
sourcename = sourcename
})
end
-- new pull message
function new_pull(session_id, filename, opt)
opt = opt or {}
return _new({
code = message.CODE_PULL,
filename = filename,
session_id = session_id,
token = opt.token
})
end
-- new push message
function new_push(session_id, filename, opt)
opt = opt or {}
return _new({
code = message.CODE_PUSH,
filename = filename,
session_id = session_id,
token = opt.token,
extrainfo = opt.extrainfo
})
end
-- new fileinfo message
function new_fileinfo(session_id, filename, opt)
opt = opt or {}
return _new({
code = message.CODE_FILEINFO,
filename = filename,
session_id = session_id,
token = opt.token
})
end
-- new existinfo message
function new_existinfo(session_id, name, opt)
opt = opt or {}
return _new({
code = message.CODE_EXISTINFO,
name = name,
session_id = session_id,
token = opt.token
})
end
-- new end message
function new_end(session_id, opt)
opt = opt or {}
return _new({
code = message.CODE_END,
session_id = session_id,
token = opt.token
})
end
function main(body)
return _new(body)
end
|