sampleFilter Example

This example shows how to make a simple packet filter plugin.


The main application:

/*******************************************************************************
** Copyright (c) 1992-2010 VT MAK
** All rights reserved.
*******************************************************************************/

#define DT_DLL_SAMPLE __declspec ( dllexport )
#define DT_DLL_SAMPLE_C extern "C" __declspec ( dllexport )

#ifdef _WIN32
#ifdef LGRSAMPLE_EXPORTS
#define DT_DLL_SAMPLE __declspec ( dllexport )
#else
#define DT_DLL_SAMPLE __declspec ( dllimport )
#endif
#else
#define DT_DLL_SAMPLE 
#endif


#include <lgrApplication.h>
#include <lgrPacketFilter.h>
#include <lgrPacketFilterFactory.h>
#include <lgrPrint.h>
#include <lgrSimInterface.h>
#include <logger.h>

#define DT_LGR_PLUGIN_EXPORT_MACRO DT_DLL_SAMPLE
#include <lgrPlugin.h>

using namespace MAKLogger;

class DT_DLL_SAMPLE SampleFilter : public DtLgrPacketFilter
{
public:

   SampleFilter() : DtLgrPacketFilter(),
      myFilterAllPacketsFlag(false)
   {
   }
   
   virtual ~SampleFilter()
   {
   }
   
   static DtLgrPacketFilter* staticCreator()
   {
      return new SampleFilter();
   }

   virtual bool filterPacket(DtPacket* packet)
   {       
      DtLgrPrinter::info("Filtering Packet via Plugin");
      return myFilterAllPacketsFlag;   
   }

   virtual DtString type() const { return DtString("SampleFilter");}

   virtual void activate(const DtMessageIOInfo* info) 
   {
      DtLgrPrinter::info("Activated Plugin");
   }

   virtual void deactivate() 
   {
      DtLgrPrinter::info("Deactivated Plugin");
   }

   virtual char* serialize(int& size) const 
   { 
      char* buffer = new char[1]; 
      size = 1; 
      unsigned char* flagPtr = (unsigned char*) buffer;
      *flagPtr = myFilterAllPacketsFlag;
      return buffer;
   }

   virtual void setFromSerialization(const char* data,int size) 
   {
      if(size != 1) 
      {
         DtTHROW_NEW(DtInvalidInput,
         "SampleFilter: Size does not match expected size");
      }
      unsigned char* flagPtr = (unsigned char*) data;
      myFilterAllPacketsFlag = *flagPtr;
   }

protected:
   bool myFilterAllPacketsFlag;

};

extern "C" {

void LoadPlugin()
{
}

void UnloadPlugin()
{
}

void EnableLoggerPlugin(DtLogger& logger)
{
}

void EnableLoggerAppPlugin(DtLoggerApplication& application)
{  
   // Create a filter object.
   SampleFilter* aFilter = new SampleFilter();

   // Register the Filter Type to allow the logger to know how to create them if necessary.
   DtLgrPacketFilterFactory::registerFilterType(aFilter->type(),&SampleFilter::staticCreator);

   // Create a sim interface to add a filter to process data.
   DtLgrSimInterface* simInterface = DtLgrSimInterface::create(application.logger());

   // Add a filter object to process data.
   simInterface->addFilter(aFilter);

   // Do not use the filter after it has been added.
   aFilter = 0;

   // Clean up the interface
   delete simInterface;
}

}

Document ID: Generated on Tue Oct 11 19:41:03 EDT 2011 from SVN revision 107422
Copyright © 2005-2011 VT MÄK Inc. All Rights Reserved (www.mak.com)