feisty meow concerns codebase  2.140
file_transfer_infoton.cpp
Go to the documentation of this file.
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>
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 
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 
51 {
52 }
53 
55 {
56  fill.assign(astring(class_name()) + ": unimplemented text_form.");
57 }
58 
59 const char *file_transfer_constant = "#ftran";
60 
64 
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 
75  const string_array &includes)
76 {
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 
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 
void reset(int number=0, const contents *initial_contents=NULL_POINTER)
Resizes this array and sets the contents from an array of contents.
Definition: array.h:349
int length() const
Returns the current reported length of the allocated C array.
Definition: array.h:115
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
void pack(byte_array &target) const
stores this string in the "target". it can later be unpacked again.
Definition: astring.cpp:961
int length() const
Returns the current length of the string.
Definition: astring.cpp:132
bool unpack(byte_array &source)
retrieves a string (packed with pack()) from "source" into this string.
Definition: astring.cpp:964
Defines the base class for all string processing objects in hoople.
Definition: base_string.h:28
virtual base_string & assign(const base_string &s)=0
Sets the contents of this string to "s".
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
the "common" class defines our common_outcomes.
Outcomes describe the state of completion for an operation.
Definition: outcome.h:31
int value() const
Definition: outcome.h:51
virtual const char * class_name() const =0
Returns the bare name of this class as a constant character pointer.
An object that traverses directory trees and provides a view of all files.
virtual void pack(basis::byte_array &packed_form) const
packs the directory_tree into a byte_array.
commands
the commands specify what this package is intended to do.
basis::abyte _command
one of the commands above.
basis::outcome _success
reports what kind of result occurred.
static const structures::string_array & file_transfer_classifier()
returns the classifier for this type of infoton.
basis::astring _src_root
the top-level directory of the source.
bool _request
if it's not a request, then it's a response.
basis::astring _dest_root
the top-level directory of the destination.
virtual int packed_size() const
reports how large the infoton will be when packed.
void package_tree_info(const filesystem::directory_tree &tree, const structures::string_array &includes)
prepares the packed data from the "tree" and "includes" list.
basis::byte_array _packed_data
the packed headers and file chunks.
virtual bool unpack(basis::byte_array &packed_form)
restores an infoton from a packed form.
virtual void pack(basis::byte_array &packed_form) const
stuffs the data in the infoton into the "packed_form".
An infoton is an individual request parcel with accompanying information.
Definition: infoton.h:32
virtual basis::astring text_form() const
local version just makes text_form() more functional.
Definition: infoton.h:108
An array of strings with some additional helpful methods.
Definition: string_array.h:32
virtual void pack(basis::byte_array &packed_form) const
Packs this string array into the "packed_form" byte array.
Definition: string_array.h:107
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
unsigned char abyte
A fairly important unit which is seldom defined...
Definition: definitions.h:51
void attach(byte_array &packed_form, const char *to_attach)
Packs a character string "to_attach" into "packed_form".
Definition: astring.cpp:1015
bool detach(byte_array &packed_form, astring &to_detach)
Unpacks a character string "to_attach" from "packed_form".
Definition: astring.cpp:1023
A platform independent way to obtain the timestamp of a file.
Definition: byte_filer.cpp:37
const char * file_transfer_constant
SAFE_STATIC_CONST(string_array, identity_infoton::identity_classifier,(1, identity_classifier_strings)) int identity_infoton
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55