first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / octopi / library / tentacles / file_transfer_infoton.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : file_transfer_infoton                                             *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 2005-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include "file_transfer_infoton.h"
16
17 #include <basis/mutex.h>
18 #include <structures/string_array.h>
19 #include <structures/static_memory_gremlin.h>
20
21 using namespace basis;
22 using namespace filesystem;
23 using namespace structures;
24
25 namespace octopi {
26
27 file_transfer_infoton::file_transfer_infoton()
28 : infoton(file_transfer_classifier()),
29   _success(common::OKAY),
30   _request(false),
31   _command(TREE_COMPARISON),
32   _src_root(),
33   _dest_root(),
34   _packed_data()
35 {}
36
37 file_transfer_infoton::file_transfer_infoton(const outcome &success,
38     bool request, commands command,
39     const astring &source, const astring &destination,
40     const byte_array &packed_data)
41 : infoton(file_transfer_classifier()),
42   _success(success),
43   _request(request),
44   _command(abyte(command)),
45   _src_root(source),
46   _dest_root(destination),
47   _packed_data(packed_data)
48 {}
49
50 file_transfer_infoton::~file_transfer_infoton()
51 {
52 }
53
54 void file_transfer_infoton::text_form(basis::base_string &fill) const
55 {
56   fill.assign(astring(class_name()) + ": unimplemented text_form.");
57 }
58
59 const char *file_transfer_constant = "#ftran";
60
61 SAFE_STATIC_CONST(string_array,
62     file_transfer_infoton::file_transfer_classifier,
63     (1, &file_transfer_constant))
64
65 int file_transfer_infoton::packed_size() const
66 {
67   return sizeof(int)
68       + sizeof(abyte) * 2
69       + _src_root.length() + 1
70       + _dest_root.length() + 1
71       + _packed_data.length() + sizeof(int);
72 }
73
74 void file_transfer_infoton::package_tree_info(const directory_tree &tree,
75     const string_array &includes)
76 {
77   _packed_data.reset();
78   tree.pack(_packed_data);
79   includes.pack(_packed_data);
80 }
81
82 void file_transfer_infoton::pack(byte_array &packed_form) const
83 {
84   attach(packed_form, _success.value());
85   attach(packed_form, abyte(_request));
86   attach(packed_form, _command);
87   _src_root.pack(packed_form);
88   _dest_root.pack(packed_form);
89   attach(packed_form, _packed_data);
90 }
91
92 bool file_transfer_infoton::unpack(byte_array &packed_form)
93 {
94   int temp_o;
95   if (!detach(packed_form, temp_o)) return false;
96   _success = temp_o;
97   abyte temp;
98   if (!detach(packed_form, temp)) return false;
99   _request = temp;
100   if (!detach(packed_form, _command)) return false;
101   if (!_src_root.unpack(packed_form)) return false;
102   if (!_dest_root.unpack(packed_form)) return false;
103   if (!detach(packed_form, _packed_data)) return false;
104   return true;
105 }
106
107 } //namespace.
108