send_file.anubis
8.79 KB
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
/*
* Created by PyramIDE.
* User: フランスのトトロ aka (David RENÉ)
* Date: 26/05/2019
* Time: 15:21
* © David RENÉ
*/
read tools/basis.anubis
read tools/findstring.anubis
read system/muscle.anubis
read system/data_io.anubis
read system/convert.anubis
read system/string.anubis
read tools/basis.anubis
read system/message_queue.anubis
read system/files.anubis
read system/logger.anubis
read calexium_lib/net_services_protocols/logger_service.anubis
read calexium_lib/CXM_message_constants.anubis
read calexium_lib/net_services/CXM_net_services.anubis
read calexium_lib/net_services/CXM_generic_protocol.anubis
read calexium_lib/types/generated/file_ref.anubis
read app_constants.anubis
read types/app_types.anubis
read tools/app_loggers.anubis
//TODO add return error handling in every functions
public define Word32 updater_srv_version = 1.
define Maybe(One)
send_file_state
(
MessageQueue mQ,
RStream src_file,
Int left_read,
(LogLevel, String) -> One logger
)=
with read_size = min(65536, left_read),
if read(src_file, read_size, 10) is
{
error then logger(logError, "send_file_state read error");failure,
timeout then logger(logError, "send_file_state timeout");failure,
ok(buffer)then
with data_msg = message(_CXM_FTP_DATA),
if add_raw(data_msg, "Data", buffer) is
{
failure then logger(logError, "send_file_state add_raw failure ");failure,
success(_) then
with end_value = if left_read - read_size = 0 then
true
else
false,
if add_bool(data_msg, "End", end_value) is
{
failure then failure,
success(_) then
//logDebug(main_log, "send_file_state send message");
mQ.add_Message_to_send(data_msg);
if mQ.get_next_received_Message(30) is
{
timeout then logger(logError, "send_file_state timeout"); failure,
closed then logger(logWarning, "send_file_state closed"); success(unique),
msg(_msg) then
if *_msg.what = _CXM_ACK then
if find_int32(_msg, "CMD") is
{
failure then logger(logError, "send_file_state CMD not found");failure,
success(cmd) then
if find_int32(_msg, "STATUS") is
{
failure then logger(logError, "send_file_state STATUS not found");failure,
success(status) then
if cmd = _CXM_FTP_DATA & status = _CXM_OK then
if end_value then //it was the last block, then quit here
logger(logDebug, "FTP Server: File sent successfully");
success(unique)
else
send_file_state(mQ, src_file, left_read - read_size, logger)
else
logger(logError, "send_file_state wrong command or status");failure
}
}
else
logger(logError, "send_file_state Not ACK message");failure
}
}
}
}.
define String
extract_hash
(
String full_path
) =
with path = (List(String))extract_dir(full_path),
get_last = (List(String) dirs, String last) |-get_last->
if dirs is
{
[] then last,
[h . t] then
if length(h) > 0 then get_last(t, h)
else get_last(t, last)
},
get_last(path, "").
define Bool
send_file_start_transfert
(
MessageQueue queue,
String file_name,
(LogLevel, String) -> One logger
)=
if queue.get_next_received_Message(30) is
{
timeout then logger(logWarning, "send_file_start_transfert timeout");false,
closed then logger(logWarning, "send_file_start_transfert connection closed");false,
msg(_msg) then
//check if the message is start the transfer
if *_msg.what = _CXM_FTP_START_TRANSFERT then
logger(logDebug, "_CXM_FTP_START_TRANSFERT received");
with start_offset = if find_int32(_msg, "ResumeOffset") is
{
failure then 0,
success(value) then to_Int(value)
},
if file(file_name, read) is
{
failure then logger(logError, "send_file_start_transfert can't open file '" + file_name + "'");false,
success(source) then
if send_file_state(queue, source, file_size(file_name) - start_offset, logger) is
{
failure then logger(logError, "send_file_start_transfert send_file_state failed"); false,
success(_) then logger(logInfo, "send_file_start_transfert file sent successfully"); true
}
}
else
false
}.
public define Bool
send_file_ref
(
MessageQueue queue,
Message msg,
(LogLevel, String) -> One logger
)=
if *msg.what = _CXM_GET_FILE_REF then
if find_message(msg, "GET_FILE_REF") is {
failure then logger(logError, "NET_SERVICE send_file_ref: can't find GET_FILE_REF message"); false,
success(file_ref_obj_msg) then
if (Maybe(File_ref))from_Message(file_ref_obj_msg) is
{
failure then logger(logError, "NET_SERVICE send_file_ref: can't extract GET_FILE_REF message");false,
success(f_ref) then
with source_file = f_ref.path + "/" + f_ref.name,
if file_exists(source_file) then
with result_msg = message(_CXM_ACK_RESULT_MSG),
with size = file_size(source_file),
forget(add_string(result_msg, "SIZE", to_String(size)));
//forget(add_int32(result_msg, "FileSize", truncate_to_Word32(file_size(file_name))));
logger(logDebug, "send _CXM_FTP_GET_FILE with filesize "+file_size(source_file));
send_result(queue, _CXM_FTP_GET_FILE, success(result_msg));
send_file_start_transfert(queue, source_file, logger)
else
logger(logError, "File '"+source_file+"' not found");
send_ACK_error(queue, _CXM_FTP_GET_FILE);
false
}
}
else
false
.
public define Bool
send_file_ref
(
MessageQueue mQ,
Message msg,
(LogLevel, String) -> One logger
)=
//println("NET_SERVICE send_file_ref received *");
if *msg.what = _CXM_GET_FILE_REF then
if find_message(msg, "GET_FILE_REF") is {
failure then println("NET_SERVICE send_file_ref: can't find GET_FILE_REF message"); false,
success(file_ref_obj_msg) then
if (Maybe(File_ref))from_Message(file_ref_obj_msg) is
{
failure then println("NET_SERVICE send_file_ref: can't extract GET_FILE_REF message");false,
success(f_ref) then
//println("NET_SERVICE send_file_ref: GET_FILE_REF message File_ref extracted\n"+dump(file_ref_obj_msg));
//create the target
with source_file = f_ref.path + "/" + f_ref.name,
with msg = message(0),
with size = file_size(source_file),
println("source_file = "+source_file);
println("size = "+size);
forget(add_string(msg, "MODE", "NEW"));
forget(add_string(msg, "SIZE", to_String(size)));
//println("NET_SERVICE send_file_ref: reply message \n"+dump(msg));
if (Maybe(RStream))file(source_file, read) is
{
failure then println("NET_SERVICE send_file_ref: can't open source file"+source_file);false, //nothing to write
success(source) then
//println("NET_SERVICE send_file_ref send ack file size "+size);
send_ACK_ok(mQ, _CXM_GET_FILE_REF, msg);
//println("call flush_wait");
mQ.flush_wait(unique);
if seek(source, 0) then
if copy_file_to_Connection(file(source), mQ.get_connection(unique), size) is
{
failure then println("NET_SERVICE send_file_ref copy_file_to_Connection error");false,
success(_) then println("NET_SERVICE send_file_ref copy_file_to_Connection ok");true
}
else
println("NET_SERVICE send_file_ref seek error");false,
}
}
}
else //not _CXM_GET_FILE_REF
println("[send_file_ref] it's not a _CXM_GET_FILE_REF");
false
.