Click here to Skip to main content
15,891,248 members
Articles / Programming Languages / C#

Remote Access .NET CF Devices

Rate me:
Please Sign up or sign in to vote.
4.94/5 (16 votes)
8 Jan 2010CDDL18 min read 36.1K   3.2K   60  
Implementing remote access to .NET enabled devices.
//-----------------------------------------------------------------------
// <copyright file="PluginManager.cs" company="Matjazev.NET">
//     Copyright (c) www.matjazev.net. All rights reserved.
// </copyright>
// <author>Matjaz Prtenjak</author>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Matjazev.Tcp.Plugin.Interfaces;

namespace Matjazev.Tcp.Plugin
{
  public class PluginsManager
  {
    private static PluginsManager inst = null;

    private List<PluginData> serverPlugins = new List<PluginData>();
    private List<PluginData> clientPlugins = new List<PluginData>();
    private Dictionary<ExecuteTime, Dictionary<string, ExecuteAction>> functions = new Dictionary<ExecuteTime, Dictionary<string, ExecuteAction>>();
    private string programPath = string.Empty;

    private PluginData getPlugin(Dictionary<string, PluginData> dict, string action)
    {
      if (dict.ContainsKey(action))
        return dict[action];

      return null;
    }

    protected PluginsManager()
    {
    }

    private bool addAssembly(string pluginFile, Assembly assembly)
    {
      bool found = false;
      foreach (Type type in assembly.GetTypes())
      {
        if (type.IsAbstract) continue;

        bool server = type.IsDefined(typeof(TCPServerPluginAttribute), true);
        bool client = type.IsDefined(typeof(TCPClientPluginAttribute), true);

        if (server || client)
        {
          PluginData data = new PluginData(Activator.CreateInstance(type) as IPlugin, pluginFile);
          if (server) 
            this.serverPlugins.Add(data);
          else
            this.clientPlugins.Add(data);

          foreach (string action in data.Plugin.Actions)
          {
            string uAction = action.ToUpper();
            if (server)
              this.functions[ExecuteTime.OnServer][uAction] = data.Plugin.GetExecuteFunction(ExecuteTime.OnServer, uAction);
            else
            {
              this.functions[ExecuteTime.BeforeServer][uAction] = data.Plugin.GetExecuteFunction(ExecuteTime.BeforeServer, uAction);
              this.functions[ExecuteTime.AfterServer][uAction] = data.Plugin.GetExecuteFunction(ExecuteTime.AfterServer, uAction);
            }
          }

          found = true;
        }
      }

      return found;
    }

    private bool addPlugin(string pluginFile)
    {
      if (!File.Exists(pluginFile))
        return false;

      Assembly assembly = Assembly.LoadFrom(pluginFile);
      if (assembly == null)
        return false;

      return this.addAssembly(pluginFile, assembly);
    }

    public static PluginsManager Inst
    {
      get
      {
        if (inst == null)
          inst = new PluginsManager();

        return inst;
      }
    }

    public void ReLoadPlugins()
    {
      this.LoadPlugins(this.programPath);
    }

    public void LoadPlugins(string programPath)
    {
      this.programPath = programPath;

      string pluginsPath = Path.Combine(programPath, @"PlugIns");
      pluginsPath = Path.GetFullPath(pluginsPath);
      if (!Directory.Exists(pluginsPath))
      {
        pluginsPath = Path.Combine(programPath, @"..\..\PlugIns");
        pluginsPath = Path.GetFullPath(pluginsPath);
      }

      this.Clear();

      this.addAssembly(string.Empty, Assembly.GetExecutingAssembly());
      if (!Directory.Exists(pluginsPath)) return;
      foreach (string f in Directory.GetFiles(pluginsPath))
      {
        FileInfo fi = new FileInfo(f);

        if (fi.Extension.Equals(".dll"))
          this.addPlugin(f);
      }
    }

    public void Clear()
    {
      this.serverPlugins.Clear();
      this.clientPlugins.Clear();

      this.functions.Clear();
      this.functions[ExecuteTime.BeforeServer] = new Dictionary<string, ExecuteAction>();
      this.functions[ExecuteTime.OnServer] = new Dictionary<string, ExecuteAction>();
      this.functions[ExecuteTime.AfterServer] = new Dictionary<string, ExecuteAction>();
    }

    public IList<PluginData> ServerPlugins
    {
      get { return this.serverPlugins; }
    }

    public IList<PluginData> ClientPlugins
    {
      get { return this.clientPlugins; }
    }

    public ExecuteAction GetExecuteFunction(ExecuteTime executeTime, string action)
    {
      try
      {
        return this.functions[executeTime][action];
      }
      catch (Exception)
      {
      }

      return null;
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Software Developer (Senior) MERKUR D.D.
Slovenia Slovenia
I am a software developer in one of the largest retailer in our country. My job is (beside else) also in setting some standards in SW development throughout our company.

I have a M.Sc. in computer science and 20 years of experience. I have written 2 books in Slovenian language and both are sold out (the first about C++ language and the second about VBA language)...

Comments and Discussions