entity_defs.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : entity definitions for octopus                                    *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 2002-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include "entity_defs.h"
00016 
00017 #include <configuration/application_configuration.h>
00018 #include <mathematics/chaos.h>
00019 #include <structures/amorph.h>
00020 #include <structures/static_memory_gremlin.h>
00021 #include <textual/byte_formatter.h>
00022 #include <textual/parser_bits.h>
00023 #include <textual/string_manipulation.h>
00024 
00025 #include <stdio.h>  // for sscanf.
00026 
00027 using namespace basis;
00028 using namespace configuration;
00029 using namespace mathematics;
00030 using namespace textual;
00031 using namespace structures;
00032 
00033 namespace octopi {
00034 
00035 octopus_entity::octopus_entity()
00036 : _hostname(new astring),
00037   _pid(0),
00038   _sequencer(0),
00039   _add_in(0)
00040 {}
00041 
00042 octopus_entity::octopus_entity(const astring &hostname,
00043     int process_id, int sequencer, int add_in)
00044 : _hostname(new astring(hostname)),
00045   _pid(process_id),
00046   _sequencer(sequencer),
00047   _add_in(add_in)
00048 {}
00049 
00050 octopus_entity::octopus_entity(const octopus_entity &to_copy)
00051 : packable(),
00052   _hostname(new astring)
00053 { operator = (to_copy); }
00054 
00055 octopus_entity::~octopus_entity() { WHACK(_hostname); }
00056 
00057 octopus_entity octopus_entity::from_text(const astring &to_convert)
00058 {
00059   astring host;
00060   int process_id;
00061   int sequencer;
00062   int add_in;
00063   breakout(to_convert, host, process_id, sequencer, add_in);
00064 
00065   byte_array temp_form;
00066   byte_formatter::string_to_bytes(host, temp_form);
00067   host = astring((char *)temp_form.observe());
00068 
00069   return octopus_entity(host, process_id, sequencer, add_in);
00070 }
00071 
00072 octopus_entity &octopus_entity::operator =(const octopus_entity &to_copy)
00073 {
00074   if (this == &to_copy) return *this;
00075   *_hostname = *to_copy._hostname;
00076   _pid = to_copy._pid;
00077   _sequencer = to_copy._sequencer;
00078   _add_in = to_copy._add_in;
00079   return *this;
00080 }
00081 
00082 const astring &octopus_entity::hostname() const { return *_hostname; }
00083 
00084 int octopus_entity::process_id() const { return _pid; }
00085 
00086 int octopus_entity::sequencer() const { return _sequencer; }
00087 
00088 int octopus_entity::add_in() const { return _add_in; }
00089 
00090 void octopus_entity::process_id(int id) { _pid = id; }
00091 
00092 void octopus_entity::sequencer(int seq) { _sequencer = seq; }
00093 
00094 void octopus_entity::add_in(int add) { _add_in = add; }
00095 
00096 void octopus_entity::hostname(const astring &new_host)
00097 { *_hostname = new_host; }
00098 
00099 bool octopus_entity::blank() const
00100 { return !_sequencer && !_add_in && !_pid && _hostname->empty(); }
00101 
00102 int octopus_entity::packed_size() const
00103 { return sizeof(int) * 3 + _hostname->length() + 1; }
00104 
00105 #define REPLACEMENT_CHARACTER '#'
00106   // used to replace unprintable characters in the entity text_form().
00107 
00108 astring octopus_entity::text_form() const
00109 {
00110   astring chewed_host = *_hostname;
00111   // make sure the name we're going to show is totally printable.  some
00112   // users of entities have odd notions of hostname strings.  there are no
00113   // rules requiring these to be readable normal strings.
00114   for (int i = 0; i < chewed_host.length(); i++) {
00115     if (!parser_bits::is_printable_ascii(chewed_host[i]))
00116       chewed_host[i] = REPLACEMENT_CHARACTER;
00117   }
00118   return a_sprintf("%d.%d.%d.%s", _add_in, _sequencer, _pid, chewed_host.s());
00119 }
00120 
00121 astring octopus_entity::mangled_form() const
00122 {
00123   astring hostdump;
00124   byte_array temp_form(_hostname->length() + 1, (abyte *)_hostname->observe());
00125   byte_formatter::bytes_to_string(temp_form, hostdump, false);
00126   return a_sprintf("%d.%d.%d.%s", _add_in, _sequencer, _pid, hostdump.s());
00127 }
00128 
00129 void octopus_entity::breakout(const astring &mangled_form, astring &hostname,
00130     int &process_id, int &sequencer, int &add_in)
00131 {
00132   // there is pretty much no error checking here.
00133   astring dupe = mangled_form;  // allows us to destroy the id.
00134   sscanf(dupe.s(), "%d", &add_in);
00135   dupe.zap(0, dupe.find('.'));
00136   sscanf(dupe.s(), "%d", &sequencer);
00137   dupe.zap(0, dupe.find('.'));
00138   sscanf(dupe.s(), "%d", &process_id);
00139   dupe.zap(0, dupe.find('.'));
00140   hostname = dupe;
00141 }
00142 
00143 bool octopus_entity::operator == (const octopus_entity &that) const
00144 {
00145   return (_add_in == that._add_in)
00146       && (_pid == that._pid)
00147       && (_sequencer == that._sequencer)
00148       && (*_hostname == *that._hostname);
00149 }
00150 
00151 void octopus_entity::pack(byte_array &packed_form) const
00152 {
00153   _hostname->pack(packed_form);
00154   attach(packed_form, _pid);
00155   attach(packed_form, _sequencer);
00156   attach(packed_form, _add_in);
00157 }
00158        
00159 bool octopus_entity::unpack(byte_array &packed_form)
00160 {
00161   if (!_hostname->unpack(packed_form)) return false;
00162   if (!detach(packed_form, _pid)) return false;
00163   if (!detach(packed_form, _sequencer)) return false;
00164   if (!detach(packed_form, _add_in)) return false;
00165   return true;
00166 }
00167 
00169 
00170 int octopus_request_id::packed_size() const
00171 { return _entity.packed_size() + sizeof(int); }
00172 
00173 astring octopus_request_id::mangled_form() const
00174 { return _entity.mangled_form() + a_sprintf("/%d", _request_num); }
00175 
00176 astring octopus_request_id::text_form() const
00177 { return _entity.text_form() + a_sprintf("/%d", _request_num); }
00178 
00179 bool octopus_request_id::blank() const
00180 { return _entity.blank() || !_request_num; }
00181 
00182 octopus_request_id octopus_request_id::randomized_id()
00183 {
00184   chaos randona;
00185   return octopus_request_id(
00186       octopus_entity(string_manipulation::make_random_name(),
00187           application_configuration::process_id(), randona.inclusive(0, MAXINT32 / 2),
00188           randona.inclusive(0, MAXINT32 / 2)),
00189       randona.inclusive(0, MAXINT32 / 2));
00190 }
00191 
00192 octopus_request_id octopus_request_id::from_text(const astring &to_convert)
00193 {
00194   // find the slash, starting at the end.
00195   int indy = to_convert.find('/', to_convert.end(), true);
00196   if (negative(indy)) return octopus_request_id();  // bad format.
00197   astring partial = to_convert.substring(0, indy - 1);
00198   int req_id = to_convert.substring(indy + 1, to_convert.end()).convert(0);
00199   return octopus_request_id(octopus_entity::from_text(partial), req_id);
00200 }
00201 
00202 void octopus_request_id::pack(byte_array &packed_form) const
00203 {
00204   _entity.pack(packed_form);
00205   attach(packed_form, _request_num);
00206 }
00207 
00208 bool octopus_request_id::unpack(byte_array &packed_form)
00209 {
00210   if (!_entity.unpack(packed_form)) return false;
00211   if (!detach(packed_form, _request_num)) return false;
00212   return true;
00213 }
00214 
00215 } // namespace.
00216 
Generated on Sat Jan 28 04:22:42 2012 for hoople2 project by  doxygen 1.6.3