feisty meow concerns codebase  2.140
entity_defs.cpp
Go to the documentation of this file.
1 /*****************************************************************************\
2 * *
3 * Name : entity definitions for octopus *
4 * Author : Chris Koeritz *
5 * *
6 *******************************************************************************
7 * Copyright (c) 2002-$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 "entity_defs.h"
16 
18 #include <mathematics/chaos.h>
19 #include <structures/amorph.h>
21 #include <textual/byte_formatter.h>
22 #include <textual/parser_bits.h>
24 
25 #include <stdio.h> // for sscanf.
26 
27 using namespace basis;
28 using namespace configuration;
29 using namespace mathematics;
30 using namespace textual;
31 using namespace structures;
32 
33 namespace octopi {
34 
35 octopus_entity::octopus_entity()
36 : _hostname(new astring),
37  _pid(0),
38  _sequencer(0),
39  _add_in(0)
40 {}
41 
43  int process_id, int sequencer, int add_in)
44 : _hostname(new astring(hostname)),
45  _pid(process_id),
46  _sequencer(sequencer),
47  _add_in(add_in)
48 {}
49 
51 : packable(),
52  _hostname(new astring)
53 { operator = (to_copy); }
54 
56 
58 {
59  astring host;
60  int process_id;
61  int sequencer;
62  int add_in;
63  breakout(to_convert, host, process_id, sequencer, add_in);
64 
65  byte_array temp_form;
66  byte_formatter::string_to_bytes(host, temp_form);
67  host = astring((char *)temp_form.observe());
68 
69  return octopus_entity(host, process_id, sequencer, add_in);
70 }
71 
73 {
74  if (this == &to_copy) return *this;
75  *_hostname = *to_copy._hostname;
76  _pid = to_copy._pid;
77  _sequencer = to_copy._sequencer;
78  _add_in = to_copy._add_in;
79  return *this;
80 }
81 
82 const astring &octopus_entity::hostname() const { return *_hostname; }
83 
84 int octopus_entity::process_id() const { return _pid; }
85 
86 int octopus_entity::sequencer() const { return _sequencer; }
87 
88 int octopus_entity::add_in() const { return _add_in; }
89 
90 void octopus_entity::process_id(int id) { _pid = id; }
91 
92 void octopus_entity::sequencer(int seq) { _sequencer = seq; }
93 
94 void octopus_entity::add_in(int add) { _add_in = add; }
95 
96 void octopus_entity::hostname(const astring &new_host)
97 { *_hostname = new_host; }
98 
100 { return !_sequencer && !_add_in && !_pid && _hostname->empty(); }
101 
103 { return sizeof(int) * 3 + _hostname->length() + 1; }
104 
105 #define REPLACEMENT_CHARACTER '#'
106  // used to replace unprintable characters in the entity text_form().
107 
109 {
110  astring chewed_host = *_hostname;
111  // make sure the name we're going to show is totally printable. some
112  // users of entities have odd notions of hostname strings. there are no
113  // rules requiring these to be readable normal strings.
114  for (int i = 0; i < chewed_host.length(); i++) {
115  if (!parser_bits::is_printable_ascii(chewed_host[i]))
116  chewed_host[i] = REPLACEMENT_CHARACTER;
117  }
118  return a_sprintf("%d.%d.%d.%s", _add_in, _sequencer, _pid, chewed_host.s());
119 }
120 
122 {
123  astring hostdump;
124  byte_array temp_form(_hostname->length() + 1, (abyte *)_hostname->observe());
125  byte_formatter::bytes_to_string(temp_form, hostdump, false);
126  return a_sprintf("%d.%d.%d.%s", _add_in, _sequencer, _pid, hostdump.s());
127 }
128 
129 void octopus_entity::breakout(const astring &mangled_form, astring &hostname,
130  int &process_id, int &sequencer, int &add_in)
131 {
132  // there is pretty much no error checking here.
133  astring dupe = mangled_form; // allows us to destroy the id.
134  sscanf(dupe.s(), "%d", &add_in);
135  dupe.zap(0, dupe.find('.'));
136  sscanf(dupe.s(), "%d", &sequencer);
137  dupe.zap(0, dupe.find('.'));
138  sscanf(dupe.s(), "%d", &process_id);
139  dupe.zap(0, dupe.find('.'));
140  hostname = dupe;
141 }
142 
144 {
145  return (_add_in == that._add_in)
146  && (_pid == that._pid)
147  && (_sequencer == that._sequencer)
148  && (*_hostname == *that._hostname);
149 }
150 
151 void octopus_entity::pack(byte_array &packed_form) const
152 {
153  _hostname->pack(packed_form);
154  attach(packed_form, _pid);
155  attach(packed_form, _sequencer);
156  attach(packed_form, _add_in);
157 }
158 
160 {
161  if (!_hostname->unpack(packed_form)) return false;
162  if (!detach(packed_form, _pid)) return false;
163  if (!detach(packed_form, _sequencer)) return false;
164  if (!detach(packed_form, _add_in)) return false;
165  return true;
166 }
167 
169 
171 { return _entity.packed_size() + sizeof(int); }
172 
174 { return _entity.mangled_form() + a_sprintf("/%d", _request_num); }
175 
177 { return _entity.text_form() + a_sprintf("/%d", _request_num); }
178 
180 { return _entity.blank() || !_request_num; }
181 
183 {
184  chaos randona;
185  return octopus_request_id(
186  octopus_entity(string_manipulation::make_random_name(),
187  application_configuration::process_id(), randona.inclusive(0, MAXINT32 / 2),
188  randona.inclusive(0, MAXINT32 / 2)),
189  randona.inclusive(0, MAXINT32 / 2));
190 }
191 
193 {
194  // find the slash, starting at the end.
195  int indy = to_convert.find('/', to_convert.end(), true);
196  if (negative(indy)) return octopus_request_id(); // bad format.
197  astring partial = to_convert.substring(0, indy - 1);
198  int req_id = to_convert.substring(indy + 1, to_convert.end()).convert(0);
199  return octopus_request_id(octopus_entity::from_text(partial), req_id);
200 }
201 
202 void octopus_request_id::pack(byte_array &packed_form) const
203 {
204  _entity.pack(packed_form);
205  attach(packed_form, _request_num);
206 }
207 
209 {
210  if (!_entity.unpack(packed_form)) return false;
211  if (!detach(packed_form, _request_num)) return false;
212  return true;
213 }
214 
215 } // namespace.
216 
a_sprintf is a specialization of astring that provides printf style support.
Definition: astring.h:440
const contents * observe() const
Returns a pointer to the underlying C array of data.
Definition: array.h:172
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
Definition: astring.h:113
void pack(byte_array &target) const
stores this string in the "target". it can later be unpacked again.
Definition: astring.cpp:961
virtual void zap(int start, int end)
Deletes the characters between "start" and "end" inclusively.
Definition: astring.cpp:521
bool substring(astring &target, int start, int end) const
a version that stores the substring in an existing "target" string.
Definition: astring.cpp:865
int end() const
returns the index of the last (non-null) character in the string.
Definition: astring.h:86
bool empty() const
empty() reports if the string is empty, that is, of zero length().
Definition: astring.h:90
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
int find(char to_find, int position=0, bool reverse=false) const
Locates "to_find" in "this".
Definition: astring.cpp:574
virtual const char * observe() const
observes the underlying pointer to the zero-terminated string.
Definition: astring.cpp:140
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
A base class for objects that can pack into an array of bytes.
Definition: byte_array.h:87
a platform-independent way to acquire random numbers in a specific range.
Definition: chaos.h:51
int inclusive(int low, int high) const
< Returns a pseudo-random number r, such that "low" <= r <= "high".
Definition: chaos.h:88
Provides a way of identifying users of an octopus object.
Definition: entity_defs.h:35
int process_id() const
returns the process number in the id.
Definition: entity_defs.cpp:84
int packed_size() const
reports how large the packed entity will be.
virtual bool unpack(basis::byte_array &packed_form)
Restores the packable from the "packed_form".
basis::astring text_form() const
returns a readable form of the identifier.
octopus_entity & operator=(const octopus_entity &to_copy)
Definition: entity_defs.cpp:72
const basis::astring & hostname() const
returns the hostname portion of the id.
Definition: entity_defs.cpp:82
bool operator==(const octopus_entity &that) const
octopus_entity()
blank constructor.
Definition: entity_defs.cpp:35
int add_in() const
returns the random add-in from the id.
Definition: entity_defs.cpp:88
int sequencer() const
returns the sequencing number from the id.
Definition: entity_defs.cpp:86
virtual void pack(basis::byte_array &packed_form) const
Creates a packed form of the packable object in "packed_form".
static void breakout(const basis::astring &mangled_form, basis::astring &hostname, int &process_id, int &sequencer, int &add_in)
takes a "mangled_form" of an entity id and retrieves the components.
static octopus_entity from_text(const basis::astring &to_convert)
conversion from text format, parsing parameters out.
Definition: entity_defs.cpp:57
bool blank() const
true if the entity is blank, as constructed by default constructor.
Definition: entity_defs.cpp:99
basis::astring mangled_form() const
returns the combined string form of the identifier.
Identifies requests made on an octopus by users.
Definition: entity_defs.h:114
int packed_size() const
reports how large the packed id will be.
static octopus_request_id from_text(const basis::astring &to_convert)
basis::astring mangled_form() const
similar to entity id.
virtual void pack(basis::byte_array &packed_form) const
Creates a packed form of the packable object in "packed_form".
basis::astring text_form() const
human readable form of the request.
int _request_num
the item number from the entity.
Definition: entity_defs.h:117
octopus_entity _entity
the entity.
Definition: entity_defs.h:116
bool blank() const
returns true if this is a blank id (as constructed by default ctor).
static octopus_request_id randomized_id()
provides a pre-randomized request id.
virtual bool unpack(basis::byte_array &packed_form)
Restores the packable from the "packed_form".
#define MAXINT32
Maximum 32-bit integer value.
Definition: definitions.h:75
#define REPLACEMENT_CHARACTER
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
Definition: functions.h:121
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
bool negative(const type &a)
negative returns true if "a" is less than zero.
Definition: functions.h:43
An extension to floating point primitives providing approximate equality.
Definition: averager.h:21
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55