Skip to content

Commit

Permalink
* Updated mpg.pl to generate BufferFilter stubs
Browse files Browse the repository at this point in the history
  * Updated app.cpp to have placeholders for BufferFilter includes/instances
  • Loading branch information
jredmondson committed Feb 23, 2019
1 parent 6ae6b4f commit c52cd6d
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 0 deletions.
3 changes: 3 additions & 0 deletions scripts/projects/common/src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,9 @@ int main(int argc, char ** argv)
// create threader for thread launching and control
madara::threads::Threader threader (kb);

// begin buffer filters
// end buffer filters

// begin on receive filters
// end on receive filters

Expand Down
43 changes: 43 additions & 0 deletions scripts/projects/common/src/buffer_filter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "MyFilter.h"

#include "madara/utility/Utility.h"

#include "madara/logger/GlobalLogger.h"

filters::MyFilter::MyFilter()
{
}

filters::MyFilter::~MyFilter()
{
}

int filters::MyFilter::encode(
char* source, int size, int max_size) const
{
int encoded_size = 0;

return encoded_size;
}

int filters::MyFilter::decode(
char* source, int size, int max_size) const
{
int decoded_size = 0;

return decoded_size;
}

std::string filters::MyFilter::get_id(void)
{
return "MyFilter";
}

/**
* Gets the version of the filter. @see madara::utility::get_uint_version
* for one way to get this from a string version
**/
uint32_t filters::MyFilter::get_version(void)
{
return madara::utility::get_uint_version("1.0.0");
}
70 changes: 70 additions & 0 deletions scripts/projects/common/src/buffer_filter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@


#ifndef _FILTERS_MYFILTER_H_
#define _FILTERS_MYFILTER_H_

/**
* @file MyFilter.h
* @author Your name <handle@youremail.com>
*
* This file contains a filter functor generated by gpl.pl
**/

#include <string>
#include "madara/filters/BufferFilter.h"

namespace filters
{
/**
* @class MyFilter
* @brief A new filter generated by gpl.pl
*/
class MyFilter : public madara::filters::BufferFilter
{
public:
/**
* Constructor
**/
MyFilter();

/**
* Destructor
**/
virtual ~MyFilter();

/**
* Encodes the buffer in place using AES encryption
* @param source the source and destination buffer
* @param size the amount of data in the buffer in bytes
* @param max_size the amount of bytes the buffer can hold
* @return the new size after encoding
**/
virtual int encode(char* source, int size, int max_size) const;

/**
* Decodes the buffer in place using AES encryption
* @param source the source and destination buffer
* @param size the amount of data in the buffer in bytes
* @param max_size the amount of bytes the buffer can hold
* @return the new size after decoding
**/
virtual int decode(char* source, int size, int max_size) const;

/**
* Gets the id of the filter. This is used in the serialization process
* for transports and checkpoints to identify which filter is used.
**/
virtual std::string get_id(void);

/**
* Gets the version of the filter. @see madara::utility::get_uint_version
* for one way to get this from a string version
**/
virtual uint32_t get_version(void);

private:

};
}

#endif // _MADARA_FILTERS_SSL_AES_H_
98 changes: 98 additions & 0 deletions scripts/projects/mpg.pl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
my @transports;
my @receive_filters;
my @send_filters;
my @buffer_filters;
my @threads = ();
my $path = '.';
my ($script, $script_dir) = fileparse($0);
Expand All @@ -30,6 +31,7 @@
# setup options parser
GetOptions(
'app|a=s' => \@apps,
'buffer-filter|b=s' => \@buffer_filters,
'on-receive|receive=s' => \@receive_filters,
'on-send|send=s' => \@send_filters,
'path|dir|p=s' => \$path,
Expand All @@ -48,6 +50,7 @@
options:
--app|-a name adds or updates a generated MADARA application
--buffer-filter|-b name create or use a buffer filter
--thread|t|nt name create or use an existing custom thread
--transport|r|nr name create or use an existing custom network transport
--on-send|send name create or use an on-send filter
Expand All @@ -66,6 +69,8 @@
$script is using the following configuration:
apps = " . (scalar @apps > 0 ?
("\n " . join ("\n ", @apps)) : 'no') . "
buffer_filters = " . (scalar @buffer_filters > 0 ?
("\n " . join ("\n ", @buffer_filters)) : 'no') . "
thread = " . (scalar @threads > 0 ?
("\n " . join ("\n ", @threads)) : 'no') . "
transport = " . (scalar @transports > 0 ?
Expand Down Expand Up @@ -202,6 +207,54 @@
}
}

##################### create filter for user ########################

for my $filter (@buffer_filters)
{
if (not -f "$path/src/filters/$filter.cpp")
{
copy "$script_dir/common/src/buffer_filter.cpp", "$path/src/filters/$filter.cpp"
or die "Copy failed: $!";
copy "$script_dir/common/src/buffer_filter.h", "$path/src/filters/$filter.h"
or die "Copy failed: $!";

# save an upper case version of the filter name
my $filter_uc = uc $filter;

# read the cpp contents and insert our filter name
my $contents;
open cpp_file, "$path/src/filters/$filter.cpp" or
die "ERROR: Couldn't open $path/src/filters/$filter.cpp\n";
$contents = join("", <cpp_file>);
close cpp_file;

$contents =~ s/MyFilter/$filter/g;

open cpp_file, ">$path/src/filters/$filter.cpp" or
die "ERROR: Couldn't open $path/src/filters/$filter.cpp\n";
print cpp_file $contents;
close cpp_file;

# read the header contents and insert our filter name
open h_file, "$path/src/filters/$filter.h" or
die "ERROR: Couldn't open $path/src/filters/$filter.h\n";
$contents = join("", <h_file>);
close h_file;

$contents =~ s/MyFilter/$filter/g;
$contents =~ s/MYFILTER/$filter_uc/g;

open h_file, ">$path/src/filters/$filter.h" or
die "ERROR: Couldn't open $path/src/filters/$filter.h\n";
print h_file $contents;
close h_file;
}
else
{
print "src/filters/$filter already exists. Not creating a new one.\n"
}
}

##################### create thread for user ########################

for my $thread (@threads)
Expand Down Expand Up @@ -409,6 +462,51 @@
s/\/\/ begin transport creation(.|\s)*\/\/ end transport creation/\/\/ begin transport creation${transport_creation}\n \/\/ end transport creation/;
} # end if there are custom threads

my $buffer_filter_includes;
my $buffer_filter_creation;

if (scalar @buffer_filters > 0)
{
if ($verbose)
{
print (" Custom buffer filters detected. Updating...\n");
}

if (not $contents =~ /\/\/ end filter includes/)
{
$contents =~
s/\/\/ end transport includes/\/\/ end transport includes\n\n\/\/ begin filter includes\n\/\/ end filter includes/;
}

if (not $contents =~ /\/\/ end buffer filters/)
{
$contents =~
s/KnowledgeBase kb;/KnowledgeBase kb;\n\n \/\/ begin buffer filters\n \/\/ end buffer filters/;
}

for (my $i = 0; $i < scalar @buffer_filters; ++$i)
{
if (not $contents =~ /#include \"filters\/${buffer_filters[$i]}\.h\"/)
{
$buffer_filter_includes .= "\n#include \"filters/${buffer_filters[$i]}.h\"";
}
$buffer_filter_creation .= "
filters::${buffer_filters[$i]} buffer_filter_$i;
settings.add_filter(&buffer_filter_$i);";
}

if ($buffer_filter_includes)
{
# change the includes
$contents =~
s/[\n \r]+\/\/ end filter includes/${buffer_filter_includes}\n\/\/ end filter includes/;
}
# change the creation process
$contents =~
s/\/\/ begin buffer filters(.|\s)*\/\/ end buffer filters/\/\/ begin buffer filters${buffer_filter_creation}\n \/\/ end buffer filters/;

} # end if there are custom read filters

my $receive_filter_includes;
my $receive_filter_creation;

Expand Down

0 comments on commit c52cd6d

Please sign in to comment.