Advertisement
AyrA

C# compiled Module

Mar 30th, 2016
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.76 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4. namespace PingerService
  5. {
  6.     public class Module
  7.     {
  8.         private delegate byte[] Process(string Command);
  9.  
  10.         private Process HandleCommand;
  11.  
  12.         private object _mod;
  13.         private Type _modType;
  14.  
  15.         public string Name
  16.         { get; private set; }
  17.  
  18.         public string Filter
  19.         { get; private set; }
  20.  
  21.         public Module(Type t)
  22.         {
  23.             if (!IsModule(t))
  24.             {
  25.                 Logger.Fail(t.Name + " is not a module");
  26.             }
  27.             _modType = t;
  28.             _mod = Activator.CreateInstance(t);
  29.             Name = (string)t.GetMethod("get_Name").Invoke(_mod, null);
  30.             Filter = (string)t.GetMethod("get_Filter").Invoke(_mod, null);
  31.             Logger.Debug("Calling Init() of {0}", t.Name);
  32.             t.GetMethod("Init").Invoke(_mod, null);
  33.             //bind to command Handler
  34.             HandleCommand = (Process)Delegate.CreateDelegate(typeof(Process), _mod, "HandleCommand");
  35.         }
  36.  
  37.         public byte[] ProcessCommand(string Command)
  38.         {
  39.             if (Command.Split(' ')[0].StartsWith(Filter))
  40.             {
  41.                 var retValue = HandleCommand(Command);
  42.                 return retValue == null ? null : (byte[])retValue;
  43.             }
  44.             return null;
  45.         }
  46.  
  47.         public void Stop()
  48.         {
  49.             try
  50.             {
  51.                 _modType.GetMethod("Stop").Invoke(_mod, null);
  52.             }
  53.             catch (Exception ex)
  54.             {
  55.                 Logger.Error("Calling Stop of {0} failed: {1}", Name, ex.Message);
  56.             }
  57.         }
  58.  
  59.         public static bool IsModule(Type t)
  60.         {
  61.             //Methods we must implement
  62.             MethodInfo[] TemplateMethods = typeof(Template).GetMethods();
  63.  
  64.             //Loop through all must-have methods
  65.             foreach (MethodInfo TemplateMethod in TemplateMethods)
  66.             {
  67.                 //get matching import method
  68.                 MethodInfo ImportMethod = t.GetMethod(TemplateMethod.Name);
  69.                 //make sure the method exists
  70.                 if (ImportMethod != null)
  71.                 {
  72.                     //Make sure the return type matches
  73.                     if (ImportMethod.ReturnType == TemplateMethod.ReturnType)
  74.                     {
  75.                         //get parameters of template and import method
  76.                         ParameterInfo[] TemplateParams = TemplateMethod.GetParameters();
  77.                         ParameterInfo[] ImportParams = ImportMethod.GetParameters();
  78.                         //make sure the number of arguments are identical
  79.                         if (TemplateParams.Length == ImportParams.Length)
  80.                         {
  81.                             //make sure the types line up
  82.                             for (int i = 0; i < TemplateParams.Length; i++)
  83.                             {
  84.                                 if (TemplateParams[i].ParameterType != ImportParams[i].ParameterType)
  85.                                 {
  86.                                     Logger.Warn("Import Method argument type mismatch for {0}. Expected {1} got {2}", ImportMethod.Name, TemplateParams[i].ParameterType, ImportParams[i].ParameterType);
  87.                                     //Import Method argument type mismatch
  88.                                     return false;
  89.                                 }
  90.                             }
  91.                         }
  92.                         else
  93.                         {
  94.                             Logger.Warn("Import Method argument count mismatch for {0}. Expected {1} got {2}", ImportMethod.Name, TemplateParams.Length, ImportParams.Length);
  95.                             //Import Method argument count mismatch
  96.                             return false;
  97.                         }
  98.                     }
  99.                     else
  100.                     {
  101.                         Logger.Warn("Import Method return type mismatch for {0}. Expected {1} got {2}", ImportMethod.Name, TemplateMethod.ReturnType, ImportMethod.ReturnType);
  102.                         //Import Method return type mismatch
  103.                         return false;
  104.                     }
  105.                 }
  106.                 else
  107.                 {
  108.                     Logger.Warn("Import Method not found: {0}", TemplateMethod.Name);
  109.                     //Import Method was not found
  110.                     return false;
  111.                 }
  112.             }
  113.             //everything OK
  114.             return true;
  115.         }
  116.     }
  117.  
  118.     internal interface Template
  119.     {
  120.         string Name
  121.         { get; }
  122.         string Filter
  123.         { get; }
  124.  
  125.         void Init();
  126.         void Stop();
  127.        
  128.         byte[] HandleCommand(string Command);
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement