private static IPacketProcessor InstantiatePacketProcessor(Type type) { #if MOREDEBUG Console.WriteLine("Trying to instantiate {0}", type); #endif // Get the constructor arguments for this type from the config IDictionary <string, string> arguments = PaxConfig.configFile.handlers?.Where(handler => type.Name.Equals(handler.class_name)) .Select(intf => intf.args) .SingleOrDefault(); if (arguments == null) { arguments = new Dictionary <string, string>(); } #if MOREDEBUG Console.WriteLine(" Arguments:"); foreach (var pair in arguments) { Console.WriteLine(" {0} : {1}", pair.Key, pair.Value); } Console.WriteLine(" Public constructors:"); foreach (var ctor in type.GetConstructors(BindingFlags.Instance | BindingFlags.Public)) { Console.WriteLine(" {0}", PacketProcessorHelper.ConstructorString(ctor)); } #endif // Instantiate the packet processor IPacketProcessor pp = PacketProcessorHelper.InstantiatePacketProcessor(type, arguments); if (pp == null) { Console.WriteLine("Couldn't instantiate {0}", type.FullName); } check_version_exc(pp); return(pp); }
private static void LoadExternalHandlersFromDll() { if (!PaxConfig.opt_quiet) { print_heading("Scanning assembly"); } // Inspect each type that implements PacketProcessor, trying to instantiate it for use foreach (Type type in PaxConfig.assembly.GetExportedTypes() .Where(typeof(IPacketProcessor).IsAssignableFrom)) { // Find which network interfaces this class is handling List <int> subscribed = new List <int>(); IPacketProcessor pp = null; for (int idx = 0; idx < PaxConfig_Lite.no_interfaces; idx++) { // Does this interface have this type specified as the lead handler? if ((!String.IsNullOrEmpty(PaxConfig.interface_lead_handler[idx])) && type.Name == PaxConfig.interface_lead_handler[idx]) { // Only instantiate pp if needed if (pp == null) { pp = InstantiatePacketProcessor(type); } if (pp == null) { // If pp is still null, then we couldn't instantiate it. break; } subscribed.Add(idx); PaxConfig.interface_lead_handler_obj[idx] = pp; } } if (!PaxConfig.opt_quiet) { Console.Write(indent); if (!PaxConfig.opt_no_colours) { Console.ForegroundColor = ConsoleColor.Green; } Console.Write(type); } if (PaxConfig.opt_verbose && !PaxConfig.opt_quiet) { // List the Pax interfaces this type implements: if (!PaxConfig.opt_no_colours) { Console.ForegroundColor = ConsoleColor.Gray; } Console.Write(" : "); if (!PaxConfig.opt_no_colours) { Console.ForegroundColor = ConsoleColor.Cyan; } Console.Write(String.Join(", ", PacketProcessorHelper.GetUsedPaxTypes(type).Select(t => t.Name))); } if (!PaxConfig.opt_quiet) { // Print which interfaces this type is the handler for if (subscribed.Count != 0) { if (!PaxConfig.opt_no_colours) { Console.ForegroundColor = ConsoleColor.Gray; } Console.Write(" <- "); if (!PaxConfig.opt_no_colours) { Console.ForegroundColor = ConsoleColor.Yellow; } Console.Write( String.Join(", ", subscribed.ConvertAll <string>(ofs => PaxConfig.deviceMap[ofs].Name))); } Console.WriteLine(""); } } // FIXME add check to see if there's an interface that references a lead_handler that doesn't appear in the assembly. That should be flagged up to the user, and lead to termination of Pax. }