Commit e176343a6687756bf886b006ad098c986cc1edc5

Authored by David RENE
1 parent 5da3baef

--no commit message

Showing 1 changed file with 151 additions and 0 deletions   Show diff stats
calexium_lib/net_services_protocols/ftp.anubis 0 → 100644
  1 +/*
  2 + *
  3 + * User: David RENE
  4 + * Date: 11/05/2007
  5 + * Time: 22:34
  6 + * (c) Calexium
  7 + *
  8 + * To change this template use Tools | Options | Coding | Edit Standard Headers.
  9 + */
  10 +
  11 +read calexium_lib/CXM_message_constants.anubis
  12 +read calexium_lib/net_services/CXM_generic_protocol.anubis
  13 +read system/message_queue.anubis
  14 +read system/message_transceiver.anubis
  15 +read system/muscle.anubis
  16 +read tools/basis.anubis
  17 +
  18 +define One
  19 + receive_data
  20 + (
  21 + MessageQueue mQ,
  22 + WStream fd,
  23 + Int32 so_far,
  24 + Int32 left_read
  25 + )=
  26 + //println("receive_data ");
  27 + if mQ.get_next_received_Message(30) is
  28 + {
  29 + timeout then unique,
  30 + closed then unique,
  31 + msg(_msg) then
  32 + with last_block = if find_bool(_msg, "End") is {failure then false, success(r) then r},
  33 + if find_raw(_msg, "Data") is
  34 + {
  35 + failure then println("Can't find raw Data"); send_ACK_error(mQ, _CXM_FTP_DATA),
  36 + success(data) then
  37 + if write(fd, data) is
  38 + {
  39 + failure then println("Can't write into file"); send_ACK_error(mQ, _CXM_FTP_DATA),
  40 + success(len) then
  41 + send_ACK_ok(mQ, _CXM_FTP_DATA);
  42 + if last_block then
  43 + println("File received successfully");
  44 + unique
  45 + else
  46 + println("Bytes received : " + (so_far + len));
  47 + receive_data(mQ, fd, so_far + len, left_read - len)
  48 + }
  49 + }
  50 + }
  51 + .
  52 +
  53 +define One
  54 + start_get_file_transtert
  55 + (
  56 + MessageQueue mQ,
  57 + String local_file,
  58 + Int32 size
  59 + )=
  60 + if file(local_file, new) is
  61 + {
  62 + failure then println("Can't create \""+local_file+"\" file"),
  63 + success(fd) then //the local file is open
  64 + with start = message(_CXM_FTP_START_TRANSFERT),
  65 + mQ.add_Message_to_send(start);
  66 + receive_data(mQ, weaken(fd), 0, size)
  67 + }
  68 + .
  69 +
  70 +define One
  71 + get_file
  72 + (
  73 + MessageQueue mQ,
  74 + String remote_file,
  75 + String local_file
  76 + )=
  77 + with get_file_msg = message(_CXM_FTP_GET_FILE),
  78 + if add_string(get_file_msg, "FileName", remote_file) is
  79 + {
  80 + failure then unique,
  81 + success(_) then
  82 + mQ.add_Message_to_send(get_file_msg);
  83 + println("_CXM_FTP_GET_FILE sent");
  84 + if wait_for_reply(mQ, _CXM_FTP_GET_FILE, 30) is
  85 + {
  86 + failure then unique,
  87 + timeout then unique,
  88 + unknow_cmd then unique,
  89 + error then unique,
  90 + ok then unique,
  91 + ok_msg(msg)then
  92 + if find_int32(msg, "FileSize") is
  93 + {
  94 + failure then println("file size not found"); unique,
  95 + success(size) then
  96 + start_get_file_transtert(mQ, local_file, size)
  97 + }
  98 + }
  99 + }
  100 + .
  101 +
  102 +
  103 +define Bool
  104 + request_for_service
  105 + (
  106 + MessageQueue queue
  107 + )=
  108 + with test_msg = message(_CXM_REQUEST_FOR_SERVICE),
  109 + forget(add_int32(test_msg, "service", _CXM_FTP_SERVICE_ID));
  110 + forget(add_int32(test_msg, "version", 1));
  111 + queue.add_Message_to_send(test_msg);
  112 + if queue.get_next_received_Message(30) is
  113 + {
  114 + timeout then println("request_for_service receive timeout");false,
  115 + closed then println("request_for_service socket closed");false,
  116 + msg(msg) then
  117 + if find_int32(msg, "STATUS") is
  118 + {
  119 + failure then println("status not found");false,
  120 + success(v) then
  121 + if v = _CXM_OK then
  122 + true
  123 + else
  124 + false
  125 + }
  126 + }.
  127 +
  128 +public define One
  129 + ftp_get_file
  130 + (
  131 + String server,
  132 + Int32 ip_port,
  133 + String remote_file,
  134 + String local_file
  135 + )=
  136 + if dns(server) is ok(ip_adr) then
  137 + if connect( ip_adr, ip_port) is
  138 + {
  139 + error(_) then println("can't connect to ftp server"),
  140 + ok(conn) then
  141 + with queue = create_MessageQueue,
  142 + message_transceiver(conn, queue);
  143 + if request_for_service(queue) then
  144 + get_file(queue, remote_file, local_file)
  145 + else
  146 + println("Service not found")
  147 + }
  148 + else
  149 + println("server "+server+" DNS error")
  150 + .
  151 +