get_file.anubis
5.29 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
/*
* Created by PyramIDE.
* User: フランスのトトロ aka (David RENÉ)
* Date: 26/05/2019
* Time: 15:23
* © David RENÉ
*/
read xlib/message_constants.anubis
read xlib/net_services/generic_protocol.anubis
read xlib/net_services/generic_client.anubis //for get_ip
read xlib/types/generated/file_ref.anubis
read system/message_queue.anubis
read system/message_transceiver.anubis
read system/muscle.anubis
read system/files.anubis
read system/logger.anubis
read tools/basis.anubis
read network/dns.anubis
define Bool
receive_data
(
MessageQueue mQ,
WStream fd,
Int so_far,
Int left_read
)=
//println("receive_data ");
if mQ.get_next_received_Message(30) is
{
timeout then false,
closed then false,
msg(_msg) then
with last_block = if find_bool(_msg, "End") is {failure then false, success(r) then r},
if find_raw(_msg, "Data") is
{
failure then println("Can't find raw Data"); send_ACK_error(mQ, _CXM_FTP_DATA); false,
success(data) then
if write(fd, data) is
{
failure then println("Can't write into file"); send_ACK_error(mQ, _CXM_FTP_DATA);false,
success(len) then
send_ACK_ok(mQ, _CXM_FTP_DATA);
if last_block then
println("File received successfully");
//TODO this is a big hack, we must check if all data are sent from the mQ
sleep(10000);
true
else
// println("Bytes received : " + (so_far + len));
receive_data(mQ, fd, so_far + len, left_read - len)
}
}
}
.
define Bool
start_get_file_transtert
(
MessageQueue mQ,
String local_file,
Int size,
(LogLevel, String) -> One logger
)=
make_directories(local_file);
if file(local_file, new) is
{
failure then logger(logError, "start_get_file_transtert Can't create \""+local_file+"\" file"); false,
success(fd) then //the local file is open
with start = message(_CXM_FTP_START_TRANSFERT),
mQ.add_Message_to_send(start);
receive_data(mQ, weaken(fd), 0, size)
}
.
public define Bool
get_file_ref
(
MessageQueue mQ,
File_ref f_ref,
String tmp_path,
(LogLevel, String) -> One logger
)=
with target_file = tmp_path + "/" + f_ref.name,
with get_file_msg = message(_CXM_GET_FILE_REF),
forget(add_message(get_file_msg, "GET_FILE_REF", to_Message(f_ref)));
mQ.add_Message_to_send(get_file_msg);
if wait_for_reply(mQ, _CXM_GET_FILE_REF, 30) is
{
failure then println("get_file failure");false,
timeout then logger(logError, "get_file timeout");false,
unknow_cmd then logger(logWarning,"get_file unknow_cmd");false,
error then println("get_file remote error msg");false,
error(c,m) then println("get_file remote error [" + c + "] msg '" + m + "'");false,
ok then println("get_file ok");false,
ok_msg(msg)then
with size = if find_string(msg, "SIZE") is {failure then f_ref.size, success(size_str) then if decimal_scan(size_str) is { failure then should_not_happen(0), success(_size_) then _size_}},
start_get_file_transtert(mQ, target_file, size, logger)
}
.
define Bool
request_for_service
(
MessageQueue queue
)=
with test_msg = message(_CXM_REQUEST_FOR_SERVICE),
forget(add_int32(test_msg, "service", _CXM_FTP_SERVICE_ID));
forget(add_int32(test_msg, "version", 1));
queue.add_Message_to_send(test_msg);
if queue.get_next_received_Message(30) is
{
timeout then println("request_for_service receive timeout");false,
closed then println("request_for_service socket closed");false,
msg(msg) then
if find_int32(msg, "STATUS") is
{
failure then println("status not found");false,
success(v) then
if v = _CXM_OK then
true
else
false
}
}.
public define Maybe(One)
ftp_get_file
(
String server,
Word32 ip_port,
String remote_file,
String local_file,
Bool ftp,
) =
if mb_get_ip(server) is
{
failure then println("server "+server+" DNS error");failure,
success(ip_adr) then
if connect( ip_adr, ip_port) is
{
error(_) then println("can't connect to ftp server"); failure,
ok(conn) then
with queue = create_MessageQueue("ftp_get_file", tcp(conn)),
message_transceiver(/*conn,*/ queue);
println("request for ftp service");
if request_for_service(queue) then
with result = get_file(queue, remote_file, local_file, ftp),
queue.quit(unique);result
else
queue.quit(unique);
println("Service not found");failure
}
}.
/* Exists for backward compatibility.
* The last argument set to false (no ftp directory on the remote, pickup on collection dir)
*/
public define Maybe(One)
ftp_get_file
(
String server,
Word32 ip_port,
String remote_file,
String local_file,
) =
ftp_get_file(server, ip_port, remote_file, local_file, false).