feisty meow concerns codebase 2.140
cromp_decoder.cpp
Go to the documentation of this file.
1/*****************************************************************************\
2* *
3* Name : cromp_decoder app *
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
17#include <basis/astring.h>
18#include <cromp/cromp_common.h>
21#include <loggers/file_logger.h>
22#include <octopus/entity_defs.h>
23#include <sockets/machine_uid.h>
26#include <unit_test/unit_base.h>
27
28#include <stdio.h>
29
30using namespace application;
31using namespace basis;
32using namespace configuration;
33using namespace cromp;
34using namespace mathematics;
35using namespace filesystem;
36using namespace loggers;
37using namespace octopi;
38using namespace processes;
39using namespace sockets;
40using namespace structures;
41using namespace textual;
42using namespace timely;
43using namespace unit_test;
44
45#define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), astring(s))
46#define BASE_LOG(s) EMERGENCY_LOG(program_wide_logger::get(), astring(s))
47
48const int MAX_LINE = 2048;
49 // the longest line we'll bother to try to process.
50
51class cromp_decoder : virtual public unit_base, virtual public application_shell
52{
53public:
54 cromp_decoder();
55 ~cromp_decoder();
56
57 virtual int execute();
58
59 DEFINE_CLASS_NAME("cromp_decoder");
60};
61
63
64cromp_decoder::cromp_decoder() : application_shell() {}
65
66cromp_decoder::~cromp_decoder() {}
67
68int cromp_decoder::execute()
69{
70 FUNCDEF("execute");
71
72 BASE_LOG("\
73This application will decode a cromp entity and report the different values");
74 BASE_LOG("\
75that are encoded into it.");
76
77 astring buffer; // we'll read input from the user into this.
78
79 while (true) {
80 BASE_LOG("Please enter the entity (or hit just enter to exit).")
81
82 buffer = astring('\0', MAX_LINE + 10); // reset the buffer.
83 char *buf2 = fgets(buffer.s(), MAX_LINE, stdin);
84 if (buf2 != buffer.s()) {
85 deadly_error(class_name(), func,
86 "memory was allocated when we didn't want it to be.");
87 }
88
89 buffer.shrink();
90 buffer.strip("\r\n", astring::FROM_END);
91 if (!buffer.length()) break;
92
93 // make sure they didn't actually give us a request id.
94 int indy = buffer.find('/');
95 if (non_negative(indy)) buffer.zap(indy, buffer.end());
96
98 if (ent.blank()) {
99 BASE_LOG("That entity was blank or invalid.");
100 BASE_LOG("");
101 continue;
102 }
103
104 BASE_LOG("");
105 BASE_LOG("");
106 BASE_LOG("Entity contains:");
107 BASE_LOG("");
108 BASE_LOG(a_sprintf("\tProcess ID=%d", ent.process_id()));
109 BASE_LOG(a_sprintf("\tSequencer=%d", ent.sequencer()));
110 BASE_LOG(a_sprintf("\tChaotic Addin=%d", ent.add_in()));
111
112 astring host;
113 machine_uid machine;
114 bool worked = cromp_common::decode_host(ent.hostname(), host, machine);
115 if (!worked) {
116 BASE_LOG("Failed to decode the hostname! Was it a valid entity?");
117 continue;
118 }
119 BASE_LOG(astring("\tPartial Hostname=") + host);
120 BASE_LOG(astring("\tMachine UID=") + machine.text_form());
121 BASE_LOG("");
122 }
123
124 return 0;
125}
126
128
129HOOPLE_MAIN(cromp_decoder, )
130
The application_shell is a base object for console programs.
virtual int execute()=0
< retrieves the command line from the /proc hierarchy on linux.
a_sprintf is a specialization of astring that provides printf style support.
Definition astring.h:440
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
virtual void zap(int start, int end)
Deletes the characters between "start" and "end" inclusively.
Definition astring.cpp:524
void strip(const astring &strip_list, how_to_strip way=FROM_BOTH_SIDES)
strips all chars from "strip_list" out of "this" given the "way".
Definition astring.cpp:951
void shrink()
changes all occurrences of "to_replace" into "new_string".
Definition astring.cpp:168
int end() const
returns the index of the last (non-null) character in the string.
Definition astring.h:86
int length() const
Returns the current length of the string.
Definition astring.cpp:132
int find(char to_find, int position=0, bool reverse=false) const
Locates "to_find" in "this".
Definition astring.cpp:577
static bool decode_host(const basis::astring &coded_host, basis::astring &hostname, sockets::machine_uid &machine)
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.
const basis::astring & hostname() const
returns the hostname portion of the id.
int add_in() const
returns the random add-in from the id.
int sequencer() const
returns the sequencing number from the id.
static octopus_entity from_text(const basis::astring &to_convert)
conversion from text format, parsing parameters out.
bool blank() const
true if the entity is blank, as constructed by default constructor.
basis::astring text_form() const
#define deadly_error(c, f, i)
const int MAX_LINE
#define BASE_LOG(s)
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition enhance_cpp.h:42
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition enhance_cpp.h:54
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition hoople_main.h:61
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
bool non_negative(const type &a)
non_negative returns true if "a" is greater than or equal to zero.
Definition functions.h:45
A platform independent way to obtain the timestamp of a file.
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
Definition averager.h:21
Provides access to the operating system's socket methods.
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55
#include <time.h>
Useful support functions for unit testing, especially within hoople.
Definition unit_base.cpp:35