/// <summary> /// Scans the assembly for a given search pattern to identify types of commands. /// </summary> /// <param name='assemblyName'> /// Assembly name. /// </param> /// <param name='searchPattern'> /// Search pattern. /// </param> public virtual void ScanForIdentity(String assemblyName, String searchPattern) { Regex regex = new Regex(searchPattern); Assembly assembly = null; try { assembly = Assembly.Load(assemblyName); } catch (FileNotFoundException) { Debug.WriteLine(String.Format( "Warning: assembly by the name of {0} when scanning " + "for ICommand types was not found", assemblyName) ); return; } Type[] types = assembly.GetTypes(); foreach (Type type in types) { // TODO should check for ICommand interface... String name = type.FullName; if (regex.IsMatch(name)) { Console.Out.WriteLine(string.Format("Matched Command Type {0}", type)); Object serial = BuildSerial(type); CommandIdentifier identifier = new CommandIdentifier(serial, type); AddIdentity(identifier); } } return; }
/// <summary> /// Adds the command identifier, used when serializing or deserializing /// commands, this is necessary to determine the type of command to /// deserialize to. e.g. EchoCommand in XML codecs will be defined by /// "EchoCommand", versus binary Codec may define it as serial 1000 /// /// Each codec type is resonsible for building its own serial command types. /// /// </summary> /// <param name='identity'> /// Identity. /// </param> public virtual void AddIdentity(CommandIdentifier identity) { if (this.Identities == null) { this.Identities = new Hashtable(); } // if the serial already exists, check to see if its a different command type? if (Identities.ContainsKey(identity.Serial)) { CommandIdentifier ci = Identities [identity.Serial] as CommandIdentifier; if (!ci.CommandType.Equals(identity.CommandType)) { throw new InvalidDataException(String.Format( "Command identity already exists for command serial {0} on {1} != serial {2} on {3}", ci.Serial, ci.CommandType.ToString(), identity.Serial, identity.CommandType) ); } } Identities [identity.Serial] = identity; }
public ICommand ToCommand(Object serial, Stream stream, Encoding encoding) { if (stream == null) { return(default(ICommand)); } CommandIdentifier identifier = null; if (Identities != null && Identities.ContainsKey(serial)) { identifier = (CommandIdentifier)Identities [serial]; } if (identifier == null) { return(default(ICommand)); } Object command = null; // create a reader, using the specific encoding of the socket using (StreamReader reader = new StreamReader(stream, encoding)) { //XmlDocument xmldoc = new XmlDocument(); //xmldoc.Load (reader); // create a serializer for the given object type. XmlSerializer serializer = new XmlSerializer(identifier.CommandType); //XmlWriterSettings settings = new XmlWriterSettings(); //settings.Encoding = encoding; //settings.Indent = false; //XmlReader reader = XmlReader.Create(stream, settings); command = serializer.Deserialize(reader); //xmldoc. /* * using (XmlReader xmlr = XmlReader.Create(reader)) { * XmlDocument xml = new XmlDocument(); * * } */ // // serializer lock, making it thread safe. // lock (SerializerLock) { // c = Serializer.Deserialize(reader); // reader.Close(); // } } if (!(command is ICommand)) { // TODO throw exception return(default(ICommand)); } return((ICommand)command); }
public override void AddIdentity(CommandIdentifier identity) { // if the command is not assignable-from if (!(typeof(IBinarySerializable).IsAssignableFrom(identity.CommandType))) { throw new InvalidCastException( String.Format( "Cannot register command {0} to type {1}, make sure your command implements {2}", identity.CommandType, typeof(IBinarySerializable), identity.CommandType ) ); } base.AddIdentity(identity); }