entity_defs.cpp

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

Generated on Fri Aug 29 04:28:59 2008 for HOOPLE Libraries by  doxygen 1.5.1