/// <summary>
        /// Initializes a new instance
        /// </summary>
        /// <param name="version">Version Number</param>
        /// <param name="Service_ID">USN, if blank a new GUID will be created for you</param>
        /// <param name="Service_Type">The Type</param>
        /// <param name="IsStandard">True if standard, false if domain specific</param>
        public ServiceDescription(double version, String Service_ID, String Service_Type, bool IsStandard)
        {
            IsStandardService = IsStandard;
            if (Service_ID == "") {
                ServiceID = Guid.NewGuid ().ToString ();
            } else {
                ServiceID = Service_ID;
            }
            ServiceType = Service_Type;

            SCPDURL = "{" + Service_ID + "}scpd.xml";
            ControlURL = "{" + Service_ID + "}control";
            EventURL = "{" + Service_ID + "}event";

            if (version == 0) {
                Major = 1;
                Minor = 0;
            } else {
                DText TempNum = new DText ();
                Major = int.Parse (TempNum.FIELD (version.ToString (), ".", 1));
                if (TempNum.DCOUNT (version.ToString (), ".") == 2) {
                    Minor = int.Parse (TempNum.FIELD (version.ToString (), ".", 2));
                } else {
                    Minor = 0;
                }
            }
        }
Example #2
0
		/// <summary>
		/// Generates Device side implementation from SCPD XML
		/// </summary>
		/// <param name="ClassName">Class Name to build</param>
		/// <param name="ns">Namespace to use</param>
		/// <param name="SavePath">Path to save source</param>
		/// <param name="ServiceID">Service ID to use</param>
		/// <param name="ServiceURN">Service URN to use</param>
		/// <param name="SCPD_XML">SCPD XML String</param>
		public static void Generate(String ClassName, String ns, String SavePath, String ServiceID, String ServiceURN, String SCPD_XML)
		{
			UPnPService s = new UPnPService(1);
			DText p = new DText();
			p.ATTRMARK = ":";
			p[0] = ServiceURN;

			string v = p[p.DCOUNT()];
			s.SetVersion(v);
			s.ParseSCPD(SCPD_XML);
			String cl = "\r\n";
			
			StringBuilder cs = new StringBuilder();
			UPnPArgument[] Args;
			UPnPArgument arg;
			UPnPStateVariable[] SV = s.GetStateVariables();

			cs.Append("using Intel.UPNP;" + cl + cl);
			cs.Append("namespace " + ns + cl);
			cs.Append("{\r\n");
			cs.Append("    /// <summary>" + cl);
			cs.Append("    /// Transparent DeviceSide UPnP Service" + cl);
			cs.Append("    /// </summary>" + cl);
			cs.Append("    public class " + ClassName + " : IUPnPService" + cl);
			cs.Append("    {" + cl + cl);
			cs.Append("        // Place your declarations above this line\r\n");
			cs.Append("\r\n");
			cs.Append("        #region AutoGenerated Code Section [Do NOT Modify, unless you know what you're doing]\r\n");
			cs.Append("        //{{{{{ Begin Code Block\r\n");
			cs.Append("\r\n");
			cs.Append("        private _" + ClassName + " _S;\r\n");
			cs.Append("        public static string URN = \"" + ServiceURN + "\";\r\n");
			cs.Append("        public double VERSION\r\n");
			cs.Append("        {\r\n");
			cs.Append("           get\r\n");
			cs.Append("           {\r\n");
			cs.Append("               return(double.Parse(_S.GetUPnPService().Version));\r\n");
			cs.Append("           }\r\n");
			cs.Append("        }\r\n\r\n");

			// Build Enumerations
			Hashtable elist = BuildEnumerations(SV);
			IDictionaryEnumerator el = elist.GetEnumerator();
			VarData vd;
			while(el.MoveNext())
			{
				vd = (VarData)el.Value;
				cs.Append("        public enum Enum_" + vd.VariableName + "\r\n");
				cs.Append("        {\r\n");
				foreach(EnumStruct vs in vd.Enumerations)
				{
					cs.Append("            " + vs.EnumName + ",\r\n");
				}
				cs.Append("        }\r\n");

				cs.Append("        public Enum_" + vd.VariableName + " ");
				if(s.GetStateVariableObject(vd.VariableName).SendEvent==true)
				{
					cs.Append("Evented_");
				}
				cs.Append(vd.VariableName + "\r\n");
				cs.Append("        {\r\n");
				cs.Append("            set\r\n");
				cs.Append("            {\r\n");
				cs.Append("               string v = \"\";\r\n");
				cs.Append("               switch(value)\r\n");
				cs.Append("               {\r\n");
				foreach(EnumStruct vs in vd.Enumerations)
				{
					cs.Append("                  case Enum_" + vd.VariableName + "." + vs.EnumName + ":\r\n");
					cs.Append("                     v = \"" + vs.EnumValue + "\";\r\n");
					cs.Append("                     break;\r\n");
				}
				cs.Append("               }\r\n");
				cs.Append("               _S.SetStateVariable(\"" + vd.VariableName + "\",v);\r\n");
				cs.Append("            }\r\n");
				cs.Append("            get\r\n");
				cs.Append("            {\r\n");
				cs.Append("               Enum_" + vd.VariableName + " RetVal = 0;\r\n");
				cs.Append("               string v = (string)_S.GetStateVariable(\"" + vd.VariableName + "\");\r\n");
				cs.Append("               switch(v)\r\n");
				cs.Append("               {\r\n");
				foreach(EnumStruct vs in vd.Enumerations)
				{
					cs.Append("                  case \"" + vs.EnumValue + "\":\r\n");
					cs.Append("                     RetVal = Enum_" + vd.VariableName + "." + vs.EnumName + ";\r\n");
					cs.Append("                     break;\r\n");
				}
				cs.Append("               }\r\n");
				cs.Append("               return(RetVal);\r\n");
				cs.Append("           }\r\n");
				cs.Append("        }\r\n");
			}

			el.Reset();
			while(el.MoveNext())
			{
				vd = (VarData)el.Value;
				cs.Append("        static public string Enum_" + vd.VariableName + "_ToString(Enum_" + vd.VariableName + " en)\r\n");
				cs.Append("        {\r\n");
				cs.Append("            string RetVal = \"\";\r\n");
				cs.Append("            switch(en)\r\n");
				cs.Append("            {\r\n");
				foreach(EnumStruct vs in vd.Enumerations)
				{
					cs.Append("                case Enum_" + vd.VariableName + "." + vs.EnumName + ":\r\n");
					cs.Append("                    RetVal = \"" + vs.EnumValue + "\";\r\n");
					cs.Append("                    break;\r\n");
				}
				cs.Append("            }\r\n");
				cs.Append("            return(RetVal);\r\n");
				cs.Append("        }\r\n");

				// Build Easy Way to get All Values
				cs.Append("        static public string[] Values_" + vd.VariableName + "\r\n");
				cs.Append("        {\r\n");
				cs.Append("            get\r\n");
				cs.Append("            {\r\n");
				cs.Append("                string[] RetVal = new string[" + vd.Enumerations.Count.ToString() + "]{");
				string EasyStrings = "";
				foreach(EnumStruct vs in vd.Enumerations)
				{
					if(EasyStrings == "")
					{
						EasyStrings = "\"" + vs.EnumValue + "\"";
					}
					else
					{
						EasyStrings = "\"" + vs.EnumValue + "\"," + EasyStrings;
					}
				}
				cs.Append(EasyStrings + "};\r\n");
				cs.Append("                return(RetVal);\r\n");
				cs.Append("            }\r\n");
				cs.Append("        }\r\n");
			}

			cs.Append("        public delegate void OnStateVariableModifiedHandler(" + ClassName + " sender);\r\n");
			foreach(UPnPStateVariable V in SV)
			{
				cs.Append("        public event OnStateVariableModifiedHandler OnStateVariableModified_" + V.Name + ";\r\n");
			}
			
			foreach(UPnPStateVariable V in SV)
			{
				if(elist.ContainsKey(V.Name)==false)
				{
					// Build Accessors
					cs.Append("        public " + V.GetNetType().FullName + " ");
					if(V.SendEvent==true)
					{
						cs.Append("Evented_");
					}
					cs.Append(V.Name + "\r\n");
					cs.Append("        {\r\n");
					cs.Append("            get\r\n");
					cs.Append("            {\r\n");
					cs.Append("               return((" + V.GetNetType().FullName + ")_S.GetStateVariable(\"" + V.Name + "\"));\r\n");
					cs.Append("            }\r\n");
					cs.Append("            set\r\n");
					cs.Append("            {\r\n");
					cs.Append("               _S.SetStateVariable(\"" + V.Name + "\", value);\r\n");
					cs.Append("            }\r\n");
					cs.Append("        }\r\n");
				}
			}
			foreach(UPnPStateVariable V in SV)
			{
				cs.Append("        public UPnPModeratedStateVariable.IAccumulator Accumulator_");
				cs.Append(V.Name + "\r\n");
				cs.Append("        {\r\n");
				cs.Append("            get\r\n");
				cs.Append("            {\r\n");
				cs.Append("                 return(((UPnPModeratedStateVariable)_S.GetUPnPService().GetStateVariableObject(\"" + V.Name + "\")).Accumulator);\r\n");
				cs.Append("            }\r\n");
				cs.Append("            set\r\n");
				cs.Append("            {\r\n");
				cs.Append("                 ((UPnPModeratedStateVariable)_S.GetUPnPService().GetStateVariableObject(\"" + V.Name + "\")).Accumulator = value;\r\n");
				cs.Append("            }\r\n");
				cs.Append("        }\r\n");
				cs.Append("        public double ModerationDuration_" + V.Name + "\r\n");
				cs.Append("        {\r\n");
				cs.Append("            get\r\n");
				cs.Append("            {\r\n");
				cs.Append("                 return(((UPnPModeratedStateVariable)_S.GetUPnPService().GetStateVariableObject(\"" + V.Name + "\")).ModerationPeriod);\r\n");
				cs.Append("            }\r\n");
				cs.Append("            set\r\n");
				cs.Append("            {\r\n");
				cs.Append("                 ((UPnPModeratedStateVariable)_S.GetUPnPService().GetStateVariableObject(\"" + V.Name + "\")).ModerationPeriod = value;\r\n");
				cs.Append("            }\r\n");
				cs.Append("        }\r\n");
			}

			// Build MethodDelegates
			foreach(UPnPAction A in s.Actions)
			{
				cs.Append("        public delegate ");
				if(A.HasReturnValue==false)
				{
					cs.Append("void ");
				}
				else
				{
					cs.Append(A.GetRetArg().RelatedStateVar.GetNetType().FullName + " ");
				}
				cs.Append("Delegate_" + A.Name + "(");
				Args = A.ArgumentList;
				for(int i=0;i<Args.Length;++i)
				{
					arg = Args[i];
					if(arg.IsReturnValue==false)
					{
						if(arg.Direction=="out")
						{
							cs.Append("out ");
						}
						if(arg.RelatedStateVar.AllowedStringValues==null)
						{
							cs.Append(arg.RelatedStateVar.GetNetType().FullName + " ");
						}
						else
						{
							cs.Append(ClassName + ".Enum_" + arg.RelatedStateVar.Name + " ");
						}
						cs.Append(arg.Name);
						if(i<Args.Length-1)
						{
							cs.Append(", ");
						}
					}
				}
				cs.Append(");\r\n");
			}

			// Build Overriding Delegates
			cs.Append("\r\n");
			foreach(UPnPAction A in s.Actions)
			{
				cs.Append("        public Delegate_" + A.Name + " External_" + A.Name + " = null;\r\n");
			}
			cs.Append("\r\n");

			// Build Ability to remove Optional State Variables
			foreach(UPnPStateVariable V in s.GetStateVariables())
			{
				cs.Append("        public void RemoveStateVariable_" + V.Name + "()\r\n");
				cs.Append("        {\r\n");
				cs.Append("            _S.GetUPnPService().RemoveStateVariable(_S.GetUPnPService().GetStateVariableObject(\"" + V.Name + "\"));\r\n");
				cs.Append("        }\r\n");
			}

			// Build Ability to remove Optional Actions
			foreach(UPnPAction A in s.Actions)
			{
				cs.Append("        public void RemoveAction_" + A.Name + "()\r\n");
				cs.Append("        {\r\n");
				cs.Append("             _S.GetUPnPService().RemoveMethod(\"" + A.Name + "\");\r\n");
				cs.Append("        }\r\n");
			}
			// Add Helper Methods
			cs.Append("        public System.Net.IPEndPoint GetCaller()\r\n");
			cs.Append("        {\r\n");
			cs.Append("             return(_S.GetUPnPService().GetCaller());\r\n");
			cs.Append("        }\r\n");
			cs.Append("        public System.Net.IPEndPoint GetReceiver()\r\n");
			cs.Append("        {\r\n");
			cs.Append("             return(_S.GetUPnPService().GetReceiver());\r\n");
			cs.Append("        }\r\n");
			cs.Append("\r\n");
											
			// Build Private Class
			cs.Append("        private class _" + ClassName + "\r\n");
			cs.Append("        {\r\n");
			cs.Append("            private " + ClassName + " Outer = null;\r\n");
			cs.Append("            private UPnPService S;\r\n");
			cs.Append("            internal _" + ClassName + "(" + ClassName + " n)\r\n");
			cs.Append("            {\r\n");
			cs.Append("                Outer = n;\r\n");
			cs.Append("                S = BuildUPnPService();\r\n");
			cs.Append("            }\r\n");
			cs.Append("            public UPnPService GetUPnPService()\r\n");
			cs.Append("            {\r\n");
			cs.Append("                return(S);\r\n");
			cs.Append("            }\r\n");
			cs.Append("            public void SetStateVariable(string VarName, object VarValue)\r\n");
			cs.Append("            {\r\n");
			cs.Append("               S.SetStateVariable(VarName,VarValue);\r\n");
			cs.Append("            }\r\n");
			cs.Append("            public object GetStateVariable(string VarName)\r\n");
			cs.Append("            {\r\n");
			cs.Append("               return(S.GetStateVariable(VarName));\r\n");
			cs.Append("            }\r\n");
			cs.Append("            protected UPnPService BuildUPnPService()\r\n");
			cs.Append("            {\r\n");
			cs.Append("                UPnPStateVariable[] RetVal = new UPnPStateVariable[" + SV.Length.ToString() + "];\r\n");
			for(int i=0;i<SV.Length;++i)
			{
				cs.Append("                RetVal[" + i.ToString() +"] = new UPnPModeratedStateVariable(\"" + SV[i].Name + "\", typeof(" + SV[i].GetNetType().FullName + "), " + SV[i].SendEvent.ToString().ToLower() + ");\r\n");
				
				if((SV[i].Maximum!=null)||
					(SV[i].Minimum!=null)||
					(SV[i].Step!=null))
				{
					cs.Append("                RetVal[" + i.ToString() +"].SetRange(");
					if(SV[i].Minimum==null)
					{
						cs.Append("null");
					}
					else
					{
						cs.Append("(" + SV[i].Minimum.GetType().FullName + ")(" + SV[i].Minimum.ToString() + ")");
					}
					cs.Append(",");

					if(SV[i].Maximum==null)
					{
						cs.Append("null");
					}
					else
					{
						cs.Append("(" + SV[i].Maximum.GetType().FullName + ")(" + SV[i].Maximum.ToString() + ")");
					}
					cs.Append(",");
					
					if(SV[i].Step==null)
					{
						cs.Append("null");
					}
					else
					{
						cs.Append("(" + SV[i].Step.GetType().FullName + ")" + SV[i].Step.ToString());
					}
					cs.Append(");\r\n");
				}
				
				if(SV[i].DefaultValue!=null)
				{
					cs.Append("                RetVal[" + i.ToString() + "].DefaultValue = UPnPService.CreateObjectInstance(typeof(" + SV[i].GetNetType().FullName + "),\"" + UPnPService.SerializeObjectInstance(SV[i].DefaultValue) + "\");\r\n");
					//cs.Append("                RetVal[" + i.ToString() + "].DefaultValue = (" + SV[i].GetNetType().FullName + ")(\"" + UPnPService.SerializeObjectInstance(SV[i].DefaultValue) + "\";\r\n");
				}
				if(SV[i].AllowedStringValues!=null)
				{
					cs.Append("                RetVal[" + i.ToString() + "].AllowedStringValues = new string[" +
						SV[i].AllowedStringValues.Length.ToString() + "]{");
					for(int ai=0;ai<SV[i].AllowedStringValues.Length;++ai)
					{
						cs.Append("\"" + SV[i].AllowedStringValues[ai] + "\"");
						if(ai<SV[i].AllowedStringValues.Length-1)
						{
							cs.Append(", ");
						}
					}
					cs.Append("};\r\n");
				}
				
				System.Collections.IList e = s.Actions;
				foreach(UPnPAction A in e)
				{
					foreach(UPnPArgument ag in A.ArgumentList)
					{
						if(ag.RelatedStateVar.Name==SV[i].Name)
						{
							cs.Append("                RetVal[" + i.ToString() + "].AddAssociation(\"" + A.Name + "\", \"" + ag.Name + "\");\r\n");
						}
					}
				}
			}
			// Build UPnPService
			cs.Append("\r\n");
			cs.Append("                UPnPService S = new UPnPService(" +
				s.Version + ", \"" + ServiceID + "\", \"" + ServiceURN + "\", true, this);\r\n");
			cs.Append("                for(int i=0;i<RetVal.Length;++i)\r\n");
			cs.Append("                {\r\n");
			cs.Append("                   S.AddStateVariable(RetVal[i]);\r\n");
			cs.Append("                }\r\n");
			foreach(UPnPAction A in s.Actions)
			{
				cs.Append("                S.AddMethod(\"" + A.Name + "\");\r\n");
			}

			cs.Append("                return(S);\r\n");
			cs.Append("            }\r\n\r\n");

			System.Collections.IList ee = s.Actions;
			foreach(UPnPAction A in ee)
			{
				if(A.HasReturnValue)
				{
					cs.Append("            [Intel.UPNP.ReturnArgument(\""+A.GetRetArg().Name+"\")]"+cl);
				}
				cs.Append("            public ");
				if(A.HasReturnValue==false)
				{
					cs.Append("void ");
				}
				else
				{
					cs.Append(A.GetRetArg().RelatedStateVar.GetNetType().FullName + " ");
				}

				cs.Append(A.Name+"(");
				Args = A.ArgumentList;
				for(int i=0;i<Args.Length;++i)
				{
					arg = Args[i];
					if(arg.IsReturnValue==false)
					{
						if(arg.Direction=="out")
						{
							cs.Append("out ");
						}
						cs.Append(arg.RelatedStateVar.GetNetType().FullName + " ");
						cs.Append(arg.Name);
						if(i<Args.Length-1)
						{
							cs.Append(", ");
						}
					}
				}
				cs.Append(")" + cl);
				cs.Append("            {\r\n");

				// Convert to Enum if neccessary
				Args = A.ArgumentList;
				for(int i=0;i<Args.Length;++i)
				{
					arg = Args[i];
					if((arg.IsReturnValue==false)&&
					(arg.RelatedStateVar.AllowedStringValues!=null))
					{
						cs.Append("                Enum_" + arg.RelatedStateVar.Name + " e_" + arg.Name + ";\r\n");
						if(arg.Direction=="in")
						{
							cs.Append("                switch(" + arg.Name + ")\r\n");
							cs.Append("                {\r\n");
							vd = (VarData)elist[arg.RelatedStateVar.Name];
							foreach(EnumStruct ss in vd.Enumerations)
							{
								cs.Append("                    case \"" + ss.EnumValue + "\":\r\n");
								cs.Append("                        e_" + arg.Name + " = Enum_" + arg.RelatedStateVar.Name + "." + ss.EnumName + ";\r\n");
								cs.Append("                        break;\r\n");
							}
							cs.Append("                    default:\r\n");
							cs.Append("                        e_" + arg.Name + " = 0;\r\n");
							cs.Append("                        break;\r\n");
							cs.Append("                }\r\n");
						
						}
					}
				}

				// Make Method Call
				if(A.HasReturnValue==true)
				{
					cs.Append("                object RetObj = null;\r\n");
				}
				cs.Append("                if(Outer.External_" + A.Name + " != null)\r\n");
				cs.Append("                {\r\n");
				cs.Append("                    ");
				if(A.HasReturnValue==true)
				{
					cs.Append("RetObj = ");
				}
				cs.Append("Outer.External_" + A.Name + "(");
				Args = A.ArgumentList;
				for(int i=0;i<Args.Length;++i)
				{
					arg = Args[i];
					if(arg.IsReturnValue==false)
					{
						if(arg.Direction=="out")
						{
							cs.Append("out ");
						}
						if(arg.RelatedStateVar.AllowedStringValues!=null)
						{
							cs.Append("e_");
						}
						cs.Append(arg.Name);
						if(i<Args.Length-1)
						{
							cs.Append(", ");
						}
					}
				}
				cs.Append(");\r\n");
				cs.Append("                }\r\n");
				cs.Append("                else\r\n");
				cs.Append("                {\r\n");
				cs.Append("                    ");
				if(A.HasReturnValue==true)
				{
					cs.Append("RetObj = ");
				}
				cs.Append("Sink_" + A.Name + "(");
				Args = A.ArgumentList;
				for(int i=0;i<Args.Length;++i)
				{
					arg = Args[i];
					if(arg.IsReturnValue==false)
					{
						if(arg.Direction=="out")
						{
							cs.Append("out ");
						}
						if(arg.RelatedStateVar.AllowedStringValues!=null)
						{
							cs.Append("e_");
						}
						cs.Append(arg.Name);
						if(i<Args.Length-1)
						{
							cs.Append(", ");
						}
					}
				}
				cs.Append(");\r\n");
				cs.Append("                }\r\n");

				Args = A.ArgumentList;
				for(int i=0;i<Args.Length;++i)
				{
					arg = Args[i];
					if((arg.IsReturnValue==false)&&
						(arg.RelatedStateVar.AllowedStringValues!=null))
					{
						if(arg.Direction=="out")
						{
							cs.Append("                switch(e_" + arg.Name + ")\r\n");
							cs.Append("                {\r\n");
							vd = (VarData)elist[arg.RelatedStateVar.Name];
							foreach(EnumStruct ss in vd.Enumerations)
							{
								cs.Append("                    case Enum_" + arg.RelatedStateVar.Name + "." + ss.EnumName + ":\r\n");
								cs.Append("                        " + arg.Name + " = \"" + ss.EnumValue  + "\";\r\n");
								cs.Append("                        break;\r\n");
							}
							cs.Append("                    default:\r\n");
							cs.Append("                        " + arg.Name + " = \"\";\r\n");
							cs.Append("                        break;\r\n");
							cs.Append("                }\r\n");
						
						}
					}
				}
								
				if(A.HasReturnValue==true)
				{
					cs.Append("                return((" + A.GetRetArg().RelatedStateVar.GetNetType().FullName + ")RetObj);\r\n");
				}
				cs.Append("            }\r\n");
			}

			cs.Append("\r\n");
			foreach(UPnPAction A in s.Actions)
			{
				cs.Append("            public Delegate_" + A.Name + " Sink_" + A.Name + ";\r\n");
			}
			cs.Append("        }\r\n"); // End of Private Class
			
			// Build Constructor
			cs.Append("        public " + ClassName + "()\r\n");
			cs.Append("        {\r\n");
			cs.Append("            _S = new _" + ClassName + "(this);\r\n");
			foreach(UPnPStateVariable V in SV)
			{
				cs.Append("            _S.GetUPnPService().GetStateVariableObject(\"" + V.Name + "\").OnModified += new UPnPStateVariable.ModifiedHandler(OnModifiedSink_" + V.Name + ");\r\n");
			}
			foreach(UPnPAction A in s.Actions)
			{
				cs.Append("            _S.Sink_" + A.Name + " = new Delegate_" + A.Name + "(" + A.Name + ");\r\n");
			}


			cs.Append("        }\r\n");
			cs.Append("        public " + ClassName + "(string ID):this()\r\n");
			cs.Append("        {\r\n");
			cs.Append("            _S.GetUPnPService().ServiceID = ID;\r\n");
			cs.Append("        }\r\n");
			cs.Append("        public UPnPService GetUPnPService()\r\n");
			cs.Append("        {\r\n");
			cs.Append("            return(_S.GetUPnPService());\r\n");
			cs.Append("        }\r\n");
			foreach(UPnPStateVariable V in SV)
			{
				cs.Append("        private void OnModifiedSink_" + V.Name + "(UPnPStateVariable sender, object NewValue)\r\n");
				cs.Append("        {\r\n");
				cs.Append("            if(OnStateVariableModified_" + V.Name + " != null) OnStateVariableModified_" + V.Name + "(this);\r\n");
				cs.Append("        }\r\n");
			}
			cs.Append("        //}}}}} End of Code Block\r\n\r\n");
			cs.Append("        #endregion\r\n");
			cs.Append("\r\n");
			
			foreach(UPnPAction A in s.Actions)
			{
				cs.Append("        /// <summary>\r\n");
				cs.Append("        /// Action: " + A.Name + "\r\n"); 
				cs.Append("        /// </summary>\r\n");
				Args = A.ArgumentList;
				for(int i=0;i<Args.Length;++i)
				{
					arg = Args[i];
					if(arg.IsReturnValue==false)
					{
						        cs.Append("        /// <param name=\"" + arg.Name + "\">Associated State Variable: " + arg.RelatedStateVar.Name + "</param>\r\n");
					}
				}
				if(A.HasReturnValue==true)
				{
					cs.Append("        /// <returns>Associated StateVariable: " + A.GetRetArg().RelatedStateVar.Name + "</returns>\r\n");
				}
				cs.Append("        public ");
				if(A.HasReturnValue==false)
				{
					cs.Append("void ");
				}
				else
				{
					cs.Append(A.GetRetArg().RelatedStateVar.GetNetType().FullName + " ");
				}

				cs.Append(A.Name+"(");
				Args = A.ArgumentList;
				for(int i=0;i<Args.Length;++i)
				{
					arg = Args[i];
					if(arg.IsReturnValue==false)
					{
						if(arg.Direction=="out")
						{
							cs.Append("out ");
						}

						if(arg.RelatedStateVar.AllowedStringValues!=null)
						{
							cs.Append("Enum_" + arg.RelatedStateVar.Name + " ");
						}
						else
						{
							cs.Append(arg.RelatedStateVar.GetNetType().FullName + " ");
						}
						cs.Append(arg.Name);
						if(i<Args.Length-1)
						{
							cs.Append(", ");
						}
					}
				}
				cs.Append(")" + cl);
				cs.Append("        {\r\n");
				cs.Append("            //ToDo: Add Your implementation here, and remove exception\r\n");
				cs.Append("            throw(new UPnPCustomException(800,\"This method has not been completely implemented...\"));\r\n");
				cs.Append("        }\r\n");
			}


			cs.Append("    }\r\n");
			cs.Append("}");
			
			UTF8Encoding UTF8 = new UTF8Encoding();
			FileStream ws = new FileStream(SavePath, FileMode.Create, FileAccess.Write);
			byte[] buffer = UTF8.GetBytes(cs.ToString());
			ws.Write(buffer,0,buffer.Length);
			ws.Close();
		}
Example #3
0
		/// <summary>
		/// Generates Control Point Implementation
		/// </summary>
		/// <param name="ClassName">Class Name to generate</param>
		/// <param name="ns">Namespace to use</param>
		/// <param name="SavePath">Path to save source</param>
		/// <param name="ServiceURN">Service URN to use</param>
		/// <param name="SCPD_XML">SCPD XML</param>
		public static void GenerateCP(String ClassName, String ns, String SavePath, String ServiceURN, String SCPD_XML)
		{
			string urn = ServiceURN.Substring(0,
				ServiceURN.LastIndexOf(":")) + ":";

			UPnPService s = new UPnPService(1);
			DText p = new DText();
			ArrayList tlist = new ArrayList();

			s.ParseSCPD(SCPD_XML);
			String cl = "\r\n";
			
			StringBuilder cs = new StringBuilder();
			UPnPArgument[] Args;
			UPnPArgument arg;
			UPnPStateVariable[] SV = s.GetStateVariables();
			Hashtable elist = BuildEnumerations(SV);

			cs.Append("using System;\r\n");
			cs.Append("using System.Collections;\r\n");
			cs.Append("using System.Threading;\r\n");
			cs.Append("using Intel.Utilities;\r\n");
			cs.Append("using Intel.UPNP;" + cl + cl);
			cs.Append("namespace " + ns + cl);
			cs.Append("{\r\n");
			cs.Append("    /// <summary>" + cl);
			cs.Append("    /// Transparent ClientSide UPnP Service" + cl);
			cs.Append("    /// </summary>" + cl);
			cs.Append("    public class " + ClassName + "\r\n");
			cs.Append("    {" + cl);
			cs.Append("       private Hashtable UnspecifiedTable = Hashtable.Synchronized(new Hashtable());\r\n");
			cs.Append("       internal UPnPService _S;\r\n\r\n");
			cs.Append("       public UPnPService GetUPnPService()\r\n");
			cs.Append("       {\r\n");
			cs.Append("            return(_S);\r\n");
			cs.Append("       }\r\n");
			cs.Append("       public static string SERVICE_NAME = \"" + urn + "\";\r\n");
			cs.Append("       public double VERSION\r\n");
			cs.Append("       {\r\n");
			cs.Append("           get\r\n");
			cs.Append("           {\r\n");
			cs.Append("               return(double.Parse(_S.Version));\r\n");
			cs.Append("           }\r\n");
			cs.Append("       }\r\n\r\n");

			foreach(UPnPStateVariable v in SV)
			{
				if(v.SendEvent==true)
				{
					cs.Append("       public delegate void StateVariableModifiedHandler_" + v.Name + "(" + ClassName + " sender, ");
					if(elist.ContainsKey(v.Name)==true)
					{
						cs.Append("Enum_" + v.Name + " NewValue");
					}
					else
					{
						cs.Append(v.GetNetType().FullName + " NewValue");
					}
					cs.Append(");\r\n");
					cs.Append("       private WeakEvent OnStateVariable_"+v.Name+"_Event = new WeakEvent();"+cl);				
					cs.Append("       public event StateVariableModifiedHandler_" + v.Name + " OnStateVariable_" + v.Name + "\r\n");
					cs.Append("       {"+cl);
					cs.Append("			add{OnStateVariable_"+v.Name+"_Event.Register(value);}"+cl);
					cs.Append("			remove{OnStateVariable_"+v.Name+"_Event.UnRegister(value);}"+cl);
					cs.Append("       }"+cl);
					cs.Append("       protected void " + v.Name + "_ModifiedSink(UPnPStateVariable Var, object NewValue)\r\n");
					cs.Append("       {\r\n");
					cs.Append("            OnStateVariable_" + v.Name + "_Event.Fire(this, "+v.Name+");"+cl);
					cs.Append("       }\r\n");
				}
			}
			cs.Append("       public delegate void SubscribeHandler(" + ClassName + " sender, bool Success);\r\n");
			cs.Append("       public event SubscribeHandler OnSubscribe;\r\n");
			
			// Build Events/Delegates
			foreach(UPnPAction A in s.Actions)
			{
				cs.Append("       public delegate void Delegate_OnResult_" + A.Name + "(" + ClassName + " sender, ");
				foreach(UPnPArgument _arg in A.ArgumentList)
				{
					if(_arg.IsReturnValue==false)
					{
						if(_arg.RelatedStateVar.AllowedStringValues==null)
						{
							cs.Append(_arg.RelatedStateVar.GetNetType().FullName + " ");
						}
						else
						{
							cs.Append("Enum_" + _arg.RelatedStateVar.Name + " ");
						}
						cs.Append(_arg.Name + ", ");
					}
				}
				if(A.HasReturnValue==true)
				{
					if(A.GetRetArg().RelatedStateVar.AllowedStringValues==null)
					{
						cs.Append(A.GetRetArg().RelatedStateVar.GetNetType().FullName + " ");
					}
					else
					{
						cs.Append("Enum_" + A.GetRetArg().RelatedStateVar.Name + " ");
					}
					cs.Append("ReturnValue, ");
				}
				cs.Append("UPnPInvokeException e, object _Tag);\r\n");
				cs.Append("       private WeakEvent OnResult_"+A.Name+"_Event = new WeakEvent();"+cl);
				cs.Append("       public event Delegate_OnResult_" + A.Name + " OnResult_" + A.Name + "\r\n");
				cs.Append("       {"+cl);
				cs.Append("			add{OnResult_"+A.Name+"_Event.Register(value);}"+cl);
				cs.Append("			remove{OnResult_"+A.Name+"_Event.UnRegister(value);}"+cl);
				cs.Append("       }"+cl);
			}


			// Build Constructor
			cs.Append("\r\n");
			cs.Append("        public " + ClassName + "(UPnPService s)\r\n");
			cs.Append("        {\r\n");
			cs.Append("            _S = s;\r\n");
			cs.Append("            _S.OnSubscribe += new UPnPService.UPnPEventSubscribeHandler(_subscribe_sink);\r\n");
			foreach(UPnPStateVariable v in SV)
			{
				if(v.SendEvent==true)
				{
					cs.Append("            if(HasStateVariable_" + v.Name + ") _S.GetStateVariableObject(\"" + v.Name + "\").OnModified += new UPnPStateVariable.ModifiedHandler(" + v.Name + "_ModifiedSink);\r\n");
				}
			}
			cs.Append("        }\r\n");
			// Build Dispose
			cs.Append("        public void Dispose()\r\n");
			cs.Append("        {\r\n");
			cs.Append("            _S.OnSubscribe -= new UPnPService.UPnPEventSubscribeHandler(_subscribe_sink);\r\n");
			cs.Append("            OnSubscribe = null;\r\n");

			foreach(UPnPStateVariable v in SV)
			{
				if(v.SendEvent==true)
				{
					cs.Append("            if(HasStateVariable_" + v.Name + ") _S.GetStateVariableObject(\"" + v.Name + "\").OnModified -= new UPnPStateVariable.ModifiedHandler(" + v.Name + "_ModifiedSink);\r\n");
				}
			}
			cs.Append("        }\r\n");

			cs.Append("        public void _subscribe(int Timeout)\r\n");
			cs.Append("        {\r\n");
			cs.Append("            _S.Subscribe(Timeout, null);\r\n");
			cs.Append("        }\r\n");
			cs.Append("        protected void _subscribe_sink(UPnPService sender, bool OK)\r\n");
			cs.Append("        {\r\n");
			cs.Append("            if(OnSubscribe!=null)\r\n");
			cs.Append("            {\r\n");
			cs.Append("                OnSubscribe(this, OK);\r\n");
			cs.Append("            }\r\n");
			cs.Append("        }\r\n");

			// Build Enumerations
			cs.Append("        public void SetUnspecifiedValue(string EnumType, string val)\r\n");
			cs.Append("        {\r\n");
			cs.Append("            string hash = Thread.CurrentThread.GetHashCode().ToString() + \":\" + EnumType;\r\n");
			cs.Append("            UnspecifiedTable[hash] = val;\r\n");
			cs.Append("        }\r\n");
			cs.Append("        public string GetUnspecifiedValue(string EnumType)\r\n");
			cs.Append("        {\r\n");
			cs.Append("            string hash = Thread.CurrentThread.GetHashCode().ToString() + \":\" + EnumType;\r\n");
			cs.Append("            if(UnspecifiedTable.ContainsKey(hash)==false)\r\n");
			cs.Append("            {\r\n");
			cs.Append("               return(\"\");\r\n");
			cs.Append("            }\r\n");
			cs.Append("            string RetVal = (string)UnspecifiedTable[hash];\r\n");
			cs.Append("            return(RetVal);\r\n");
			cs.Append("        }\r\n");
			IDictionaryEnumerator el = elist.GetEnumerator();
			VarData vd;
			while(el.MoveNext())
			{
				vd = (VarData)el.Value;
				
				cs.Append("        public string[] Values_" + vd.VariableName + "\r\n");
				cs.Append("        {\r\n");
				cs.Append("            get\r\n");
				cs.Append("            {\r\n");
				cs.Append("                UPnPStateVariable sv = _S.GetStateVariableObject(\"" + vd.VariableName + "\");\r\n");
				cs.Append("                return(sv.AllowedStringValues);\r\n");
				cs.Append("            }\r\n");
				cs.Append("        }\r\n");
				cs.Append("        public string Enum_" + vd.VariableName + "_ToString(Enum_" + vd.VariableName + " en)\r\n");
				cs.Append("        {\r\n");
				cs.Append("            string RetVal = \"\";\r\n");
				cs.Append("            switch(en)\r\n");
				cs.Append("            {\r\n");
				foreach(EnumStruct vs in vd.Enumerations)
				{
					cs.Append("                case Enum_" + vd.VariableName + "." + vs.EnumName + ":\r\n");
					cs.Append("                    RetVal = \"" + vs.EnumValue + "\";\r\n");
					cs.Append("                    break;\r\n");
				}
				cs.Append("                case Enum_" + vd.VariableName + "._UNSPECIFIED_:\r\n");
				cs.Append("                    RetVal = GetUnspecifiedValue(\"Enum_" + vd.VariableName + "\");\r\n");
				cs.Append("                    break;\r\n");
				cs.Append("            }\r\n");
				cs.Append("            return(RetVal);\r\n");
				cs.Append("        }\r\n");
				
 
				cs.Append("        public enum Enum_" + vd.VariableName + "\r\n");
				cs.Append("        {\r\n");
				cs.Append("            _UNSPECIFIED_,\r\n");        
				foreach(EnumStruct vs in vd.Enumerations)
				{
					cs.Append("            " + vs.EnumName + ",\r\n");
				}
				cs.Append("        }\r\n");
				

				cs.Append("        public Enum_" + vd.VariableName + " " + vd.VariableName + "\r\n");
				cs.Append("        {\r\n");
				cs.Append("            get\r\n");
				cs.Append("            {\r\n");
				cs.Append("               Enum_" + vd.VariableName + " RetVal = 0;\r\n");
				cs.Append("               string v = (string)_S.GetStateVariable(\"" + vd.VariableName + "\");\r\n");
				cs.Append("               switch(v)\r\n");
				cs.Append("               {\r\n");
				foreach(EnumStruct vs in vd.Enumerations)
				{
					cs.Append("                  case \"" + vs.EnumValue + "\":\r\n");
					cs.Append("                     RetVal = Enum_" + vd.VariableName + "." + vs.EnumName + ";\r\n");
					cs.Append("                     break;\r\n");
				}
				cs.Append("                  default:\r\n");
				cs.Append("                     RetVal = Enum_" + vd.VariableName + "._UNSPECIFIED_;\r\n");
				cs.Append("                     SetUnspecifiedValue(\"Enum_" + vd.VariableName + "\", v);\r\n");
				cs.Append("                     break;\r\n");
				cs.Append("               }\r\n");
				cs.Append("               return(RetVal);\r\n");
				cs.Append("           }\r\n");
				cs.Append("        }\r\n");
			}

			foreach(UPnPStateVariable V in SV)
			{
				if(elist.ContainsKey(V.Name)==false)
				{
					// Build Accessors
					cs.Append("        public " + V.GetNetType().FullName + " " + V.Name + "\r\n");
					cs.Append("        {\r\n");
					cs.Append("            get\r\n");
					cs.Append("            {\r\n");
					cs.Append("               return((" + V.GetNetType().FullName + ")_S.GetStateVariable(\"" + V.Name + "\"));\r\n");
					cs.Append("            }\r\n");
					cs.Append("        }\r\n");
				}
			}

			foreach(UPnPStateVariable V in SV)
			{
				// Range Stuff
				if((V.Maximum!=null)||(V.Minimum!=null))
				{
					cs.Append("        public bool HasMaximum_" + V.Name + "\r\n");
					cs.Append("        {\r\n");
					cs.Append("             get\r\n");
					cs.Append("             {\r\n");
					cs.Append("                 return(_S.GetStateVariableObject(\"" + V.Name + "\").Maximum!=null);\r\n");
					cs.Append("             }\r\n");
					cs.Append("        }\r\n");
					cs.Append("        public bool HasMinimum_" + V.Name + "\r\n");
					cs.Append("        {\r\n");
					cs.Append("             get\r\n");
					cs.Append("             {\r\n");
					cs.Append("                 return(_S.GetStateVariableObject(\"" + V.Name + "\").Minimum!=null);\r\n");
					cs.Append("             }\r\n");
					cs.Append("        }\r\n");
					cs.Append("        public " + V.GetNetType().FullName + " Maximum_" + V.Name + "\r\n");
					cs.Append("        {\r\n");
					cs.Append("             get\r\n");
					cs.Append("             {\r\n");
					cs.Append("                 return((" + V.GetNetType().FullName +")_S.GetStateVariableObject(\"" + V.Name + "\").Maximum);\r\n");
					cs.Append("             }\r\n");
					cs.Append("        }\r\n");
					cs.Append("        public " + V.GetNetType().FullName + " Minimum_" + V.Name + "\r\n");
					cs.Append("        {\r\n");
					cs.Append("             get\r\n");
					cs.Append("             {\r\n");
					cs.Append("                 return((" + V.GetNetType().FullName +")_S.GetStateVariableObject(\"" + V.Name + "\").Minimum);\r\n");
					cs.Append("             }\r\n");
					cs.Append("        }\r\n");
				}
				// Has Stuff
				cs.Append("        public bool HasStateVariable_" + V.Name + "\r\n");
				cs.Append("        {\r\n");
				cs.Append("            get\r\n");
				cs.Append("            {\r\n");
				cs.Append("               if(_S.GetStateVariableObject(\"" + V.Name + "\")==null)\r\n");
				cs.Append("               {\r\n");
				cs.Append("                   return(false);\r\n");
				cs.Append("               }\r\n");
				cs.Append("               else\r\n");
				cs.Append("               {\r\n");
				cs.Append("                   return(true);\r\n");
				cs.Append("               }\r\n");
				cs.Append("            }\r\n");
				cs.Append("        }\r\n");
			}
			foreach(UPnPAction A in s.Actions)
			{
				cs.Append("        public bool HasAction_" + A.Name + "\r\n");
				cs.Append("        {\r\n");
				cs.Append("            get\r\n");
				cs.Append("            {\r\n");
				cs.Append("               if(_S.GetAction(\"" + A.Name + "\")==null)\r\n");
				cs.Append("               {\r\n");
				cs.Append("                   return(false);\r\n");
				cs.Append("               }\r\n");
				cs.Append("               else\r\n");
				cs.Append("               {\r\n");
				cs.Append("                   return(true);\r\n");
				cs.Append("               }\r\n");
				cs.Append("            }\r\n");
				cs.Append("        }\r\n");
			}

			foreach(UPnPAction A in s.Actions)
			{
				// Build Sync Version
				cs.Append("        public ");
				if(A.HasReturnValue==false)
				{
					cs.Append("void ");
				}
				else
				{
					if(elist.ContainsKey(A.GetRetArg().RelatedStateVar.Name)==true)
					{
						cs.Append("Enum_" + A.GetRetArg().RelatedStateVar.Name + " ");
					}
					else
					{
						cs.Append(A.GetRetArg().RelatedStateVar.GetNetType().FullName + " ");
					}
				}
				cs.Append("Sync_" + A.Name + "(");
				tlist.Clear();
				foreach(UPnPArgument _arg in A.ArgumentList)
				{
					if(_arg.IsReturnValue==false)
					{
						tlist.Add(_arg);
					}
				}
				Args = (UPnPArgument[])tlist.ToArray(typeof(UPnPArgument));
				for(int i=0;i<Args.Length;++i)
				{
					arg = Args[i];
					if(arg.Direction=="out")
					{
						cs.Append("out ");
					}
					if(elist.ContainsKey(arg.RelatedStateVar.Name))
					{
						cs.Append("Enum_" + arg.RelatedStateVar.Name + " ");
					}
					else
					{
						cs.Append(arg.RelatedStateVar.GetNetType().FullName + " ");
					}
					cs.Append(arg.Name);
					if(i<Args.Length-1)
					{
						cs.Append(", ");
					}
				}
				cs.Append(")\r\n");
				cs.Append("        {\r\n");
				cs.Append("           UPnPArgument[] args = new UPnPArgument[");
				if(A.HasReturnValue==false)
				{
					cs.Append(A.ArgumentList.Length.ToString());
				}
				else
				{
					cs.Append((A.ArgumentList.Length-1).ToString());
				}
				cs.Append("];\r\n");
				int ia=0;
				foreach(UPnPArgument _arg in A.ArgumentList)
				{
					if(_arg.IsReturnValue==false)
					{
						if(_arg.Direction=="in")
						{
							if(elist.ContainsKey(_arg.RelatedStateVar.Name)==true)
							{
								cs.Append("           switch(" + _arg.Name + ")\r\n");
								cs.Append("           {\r\n");
								foreach(EnumStruct ess in ((VarData)elist[_arg.RelatedStateVar.Name]).Enumerations)
								{
									cs.Append("               case Enum_" + _arg.RelatedStateVar.Name + "." + ess.EnumName + ":\r\n");
									cs.Append("                   args[" + ia.ToString() + "] = new UPnPArgument(\"" + _arg.Name + "\", \"" + ess.EnumValue + "\");\r\n");
									cs.Append("                   break;\r\n");
								}
								cs.Append("               default:\r\n");
								cs.Append("                  args[" + ia.ToString() + "] = new UPnPArgument(\"" + _arg.Name + "\", GetUnspecifiedValue(\"Enum_" + _arg.RelatedStateVar.Name + "\"));\r\n");
								cs.Append("                  break;\r\n");
								cs.Append("           }\r\n");
							}
							else
							{
								cs.Append("           args[" + ia.ToString() + "] = new UPnPArgument(\"" + _arg.Name + "\", " + _arg.Name + ");\r\n");
							}
						}
						else
						{
							cs.Append("           args[" + ia.ToString() + "] = new UPnPArgument(\"" + _arg.Name + "\", \"\");\r\n");
						}
						++ia;
					}
				}
				if(A.HasReturnValue==true)
				{
					cs.Append("           object RetVal = null;\r\n");
					cs.Append("           RetVal = ");
				}
				else
				{
					cs.Append("            ");
				}
				cs.Append("_S.InvokeSync(\"" + A.Name + "\", args);\r\n");
				if(A.HasReturnValue==true)
				{
					if(A.GetRetArg().RelatedStateVar.AllowedStringValues!=null)
					{
						cs.Append("           Enum_" + A.GetRetArg().RelatedStateVar.Name + " RetEnum = 0;\r\n");
						cs.Append("           switch((string)RetVal)\r\n");
						cs.Append("           {\r\n");
						foreach(EnumStruct ess in ((VarData)elist[A.GetRetArg().RelatedStateVar.Name]).Enumerations)
						{
							cs.Append("               case \"" + ess.EnumValue + "\":\r\n");
							cs.Append("                   RetEnum = Enum_" + A.GetRetArg().RelatedStateVar.Name + "." + ess.EnumName + ";\r\n");
							cs.Append("                   break;\r\n");
						}
						cs.Append("               default:\r\n");
						cs.Append("                   RetEnum = Enum_" + A.GetRetArg().RelatedStateVar.Name + "._UNSPECIFIED_;\r\n");
						cs.Append("                   SetUnspecifiedValue(\"Enum_" + A.GetRetArg().RelatedStateVar.Name + "\",(string)RetVal);\r\n");
						cs.Append("                   break;\r\n");
						cs.Append("           }\r\n");
					}
				}

				// Check if this is necessary
				bool IsNeccessary = false;
				foreach(UPnPArgument _arg in A.ArgumentList)
				{
					if((_arg.RelatedStateVar.AllowedStringValues!=null)&&
						(_arg.IsReturnValue==false))
					{
						IsNeccessary = true;
						break;
					}
				}
				if(IsNeccessary==true)
				{
					cs.Append("            for(int i=0;i<args.Length;++i)\r\n");
					cs.Append("            {\r\n");
					cs.Append("                switch(args[i].Name)\r\n");
					cs.Append("                {\r\n");
					tlist.Clear();
					foreach(UPnPArgument _arg in A.ArgumentList)
					{
						if(_arg.IsReturnValue==false)
						{
							tlist.Add(_arg);
						}
					}
				
					Args = (UPnPArgument[])tlist.ToArray(typeof(UPnPArgument));
					foreach(UPnPArgument _arg in Args)
					{
						if(_arg.RelatedStateVar.AllowedStringValues!=null)
						{
							cs.Append("                    case \"" + _arg.Name + "\":\r\n");
							cs.Append("                        switch((string)args[i].DataValue)\r\n");
							cs.Append("                        {\r\n");
							foreach(EnumStruct ess in ((VarData)elist[_arg.RelatedStateVar.Name]).Enumerations)
							{
								cs.Append("                            case \"" + ess.EnumValue + "\":\r\n");
								cs.Append("                                args[i].DataValue = Enum_" + _arg.RelatedStateVar.Name + "." + ess.EnumName + ";\r\n");
								cs.Append("                                break;\r\n");
							}
							cs.Append("                            default:\r\n");
							cs.Append("                               SetUnspecifiedValue(\"Enum_" + _arg.RelatedStateVar.Name + "\", (string)args[i].DataValue);\r\n");
							cs.Append("                               args[i].DataValue = Enum_" + _arg.RelatedStateVar.Name + "._UNSPECIFIED_;\r\n");
							cs.Append("                               break;\r\n");
							cs.Append("                        }\r\n");
							cs.Append("                        break;\r\n");
						}
					}
					cs.Append("                }\r\n"); // End of Switch
					cs.Append("            }\r\n"); // End of For Loop
					
				}
				int argid = 0;
				foreach(UPnPArgument _arg in A.Arguments)
				{
					if(_arg.IsReturnValue==false)
					{
						cs.Append("            " + _arg.Name + " = (");
						if(_arg.RelatedStateVar.AllowedStringValues==null)
						{
							cs.Append(_arg.RelatedStateVar.GetNetType().FullName);
						}
						else
						{
							cs.Append("Enum_" + _arg.RelatedStateVar.Name);
						}
						cs.Append(") args[" + argid.ToString() + "].DataValue;\r\n");
					}
					++argid;
				}
				if(A.HasReturnValue==true)
				{
					if(A.GetRetArg().RelatedStateVar.AllowedStringValues==null)
					{
						cs.Append("           return((" + A.GetRetArg().RelatedStateVar.GetNetType().FullName + ")RetVal);\r\n");
					}
					else
					{
						cs.Append("           return(RetEnum);\r\n");
					}
				}
				else
				{
					cs.Append("            return;\r\n");
				}
				cs.Append("        }\r\n"); // End of SyncAction Class

				// Build Async Version [Overload]
				cs.Append("        public ");
				cs.Append("void ");
				cs.Append(A.Name + "(");
				tlist.Clear();
				foreach(UPnPArgument _arg in A.ArgumentList)
				{
					if((_arg.IsReturnValue==false)&&
						(_arg.Direction=="in"))
					{
						tlist.Add(_arg);
					}
				}
				Args = (UPnPArgument[])tlist.ToArray(typeof(UPnPArgument));
				for(int i=0;i<Args.Length;++i)
				{
					arg = Args[i];
					if(elist.ContainsKey(arg.RelatedStateVar.Name))
					{
						cs.Append("Enum_" + arg.RelatedStateVar.Name + " ");
					}
					else
					{
						cs.Append(arg.RelatedStateVar.GetNetType().FullName + " ");
					}
					cs.Append(arg.Name);
					if(i<Args.Length-1)
					{
						cs.Append(", ");
					}
				}
				cs.Append(")\r\n");
				cs.Append("        {\r\n");
				cs.Append("            " + A.Name + "(");
				for(int i=0;i<Args.Length;++i)
				{
					arg = Args[i];
					cs.Append(arg.Name);
					cs.Append(", ");
				}
				cs.Append("null, null);\r\n");

				cs.Append("        }\r\n");

				// Build Async Version
				cs.Append("        public ");
				cs.Append("void ");
				cs.Append(A.Name + "(");
				tlist.Clear();
				foreach(UPnPArgument _arg in A.ArgumentList)
				{
					if((_arg.IsReturnValue==false)&&
						(_arg.Direction=="in"))
					{
						tlist.Add(_arg);
					}
				}
				Args = (UPnPArgument[])tlist.ToArray(typeof(UPnPArgument));
				for(int i=0;i<Args.Length;++i)
				{
					arg = Args[i];
					if(elist.ContainsKey(arg.RelatedStateVar.Name))
					{
						cs.Append("Enum_" + arg.RelatedStateVar.Name + " ");
					}
					else
					{
						cs.Append(arg.RelatedStateVar.GetNetType().FullName + " ");
					}
					cs.Append(arg.Name);
					cs.Append(", ");

				}
				cs.Append("object _Tag, Delegate_OnResult_" + A.Name + " _Callback");
				cs.Append(")\r\n");
				cs.Append("        {\r\n");
				cs.Append("           UPnPArgument[] args = new UPnPArgument[");
				if(A.HasReturnValue==false)
				{
					cs.Append(A.ArgumentList.Length.ToString());
				}
				else
				{
					cs.Append((A.ArgumentList.Length-1).ToString());
				}
				cs.Append("];\r\n");

				ia=0;
				foreach(UPnPArgument _arg in A.ArgumentList)
				{
					if(_arg.IsReturnValue==false)
					{
						if(_arg.Direction=="in")
						{
							if(elist.ContainsKey(_arg.RelatedStateVar.Name)==true)
							{
								cs.Append("           switch(" + _arg.Name + ")\r\n");
								cs.Append("           {\r\n");
								foreach(EnumStruct ess in ((VarData)elist[_arg.RelatedStateVar.Name]).Enumerations)
								{
									cs.Append("               case Enum_" + _arg.RelatedStateVar.Name + "." + ess.EnumName + ":\r\n");
									cs.Append("                   args[" + ia.ToString() + "] = new UPnPArgument(\"" + _arg.Name + "\", \"" + ess.EnumValue + "\");\r\n");
									cs.Append("                   break;\r\n");
								}
								cs.Append("               default:\r\n");
								cs.Append("                  args[" + ia.ToString() + "] = new UPnPArgument(\"" + _arg.Name + "\", GetUnspecifiedValue(\"Enum_" + _arg.RelatedStateVar.Name + "\"));\r\n");
								cs.Append("                  break;\r\n");
								cs.Append("           }\r\n");
							}
							else
							{
								cs.Append("           args[" + ia.ToString() + "] = new UPnPArgument(\"" + _arg.Name + "\", " + _arg.Name + ");\r\n");
							}
						}
						else
						{
							cs.Append("           args[" + ia.ToString() + "] = new UPnPArgument(\"" + _arg.Name + "\", \"\");\r\n");
						}
						++ia;
					}
				}
				cs.Append("           _S.InvokeAsync(\"" + A.Name + "\", args, new object[2]{_Tag,_Callback},new UPnPService.UPnPServiceInvokeHandler(Sink_" + A.Name + "), new UPnPService.UPnPServiceInvokeErrorHandler(Error_Sink_" + A.Name + "));\r\n");
				cs.Append("        }\r\n"); // End of Action Class

				// Build Async Sink
				cs.Append("        private void Sink_" + A.Name + "(UPnPService sender, string MethodName, UPnPArgument[] Args, object RetVal, object _Tag)\r\n");
				cs.Append("        {\r\n");
				if(A.HasReturnValue==true)
				{
					if(A.GetRetArg().RelatedStateVar.AllowedStringValues!=null)
					{
						cs.Append("           Enum_" + A.GetRetArg().RelatedStateVar.Name + " RetEnum = 0;\r\n");
						cs.Append("           switch((string)RetVal)\r\n");
						cs.Append("           {\r\n");
						foreach(EnumStruct ess in ((VarData)elist[A.GetRetArg().RelatedStateVar.Name]).Enumerations)
						{
							cs.Append("               case \"" + ess.EnumValue + "\":\r\n");
							cs.Append("                   RetEnum = Enum_" + A.GetRetArg().RelatedStateVar.Name + "." + ess.EnumName + ";\r\n");
							cs.Append("                   break;\r\n");
						}
						cs.Append("               default:\r\n");
						cs.Append("                   RetEnum = Enum_" + A.GetRetArg().RelatedStateVar.Name + "._UNSPECIFIED_;\r\n");
						cs.Append("                   SetUnspecifiedValue(\"Enum_" + A.GetRetArg().RelatedStateVar.Name + "\",(string)RetVal);\r\n");
						cs.Append("                   break;\r\n");
						cs.Append("           }\r\n");		
					}
				}
				
				// Check if this is necessary
				IsNeccessary = false;
				foreach(UPnPArgument _arg in A.ArgumentList)
				{
					if((_arg.RelatedStateVar.AllowedStringValues!=null)&&
						(_arg.IsReturnValue==false))
					{
						IsNeccessary = true;
						break;
					}
				}

				if(IsNeccessary==true)
				{
					cs.Append("            for(int i=0;i<Args.Length;++i)\r\n");
					cs.Append("            {\r\n");
					cs.Append("                switch(Args[i].Name)\r\n");
					cs.Append("                {\r\n");
					tlist.Clear();
					foreach(UPnPArgument _arg in A.ArgumentList)
					{
						if(_arg.IsReturnValue==false)
						{
							tlist.Add(_arg);
						}
					}
				
					Args = (UPnPArgument[])tlist.ToArray(typeof(UPnPArgument));
					foreach(UPnPArgument _arg in Args)
					{
						if(_arg.RelatedStateVar.AllowedStringValues!=null)
						{
							cs.Append("                    case \"" + _arg.Name + "\":\r\n");
							cs.Append("                        switch((string)Args[i].DataValue)\r\n");
							cs.Append("                        {\r\n");
							foreach(EnumStruct ess in ((VarData)elist[_arg.RelatedStateVar.Name]).Enumerations)
							{
								cs.Append("                            case \"" + ess.EnumValue + "\":\r\n");
								cs.Append("                                Args[i].DataValue = Enum_" + _arg.RelatedStateVar.Name + "." + ess.EnumName + ";\r\n");
								cs.Append("                                break;\r\n");
							}
							cs.Append("                            default:\r\n");
							cs.Append("                               SetUnspecifiedValue(\"Enum_" + _arg.RelatedStateVar.Name + "\", (string)Args[i].DataValue);\r\n");
							cs.Append("                               Args[i].DataValue = Enum_" + _arg.RelatedStateVar.Name + "._UNSPECIFIED_;\r\n");
							cs.Append("                               break;\r\n");
							cs.Append("                        }\r\n");
							cs.Append("                        break;\r\n");
						}
					}
					cs.Append("                }\r\n"); // End of Switch
					cs.Append("            }\r\n"); // End of For Loop
				}
				cs.Append("            object[] StateInfo = (object[])_Tag;\r\n");
				cs.Append("            if(StateInfo[1]!=null)\r\n");
				cs.Append("            {\r\n");
				cs.Append("                ((Delegate_OnResult_" + A.Name + ")StateInfo[1])(this, ");
				argid = 0;
				foreach(UPnPArgument _arg in A.ArgumentList)
				{
					if(_arg.IsReturnValue==false)
					{
						cs.Append("(");
						if(_arg.RelatedStateVar.AllowedStringValues==null)
						{
							cs.Append(_arg.RelatedStateVar.GetNetType().FullName + " ");
						}
						else
						{
							cs.Append("Enum_" + _arg.RelatedStateVar.Name + " ");
						}
						cs.Append(")");
						cs.Append("Args[" + argid.ToString() + "].DataValue, ");
						++argid;
					}
				}
				if(A.HasReturnValue==true)
				{
					cs.Append("(");
					if(A.GetRetArg().RelatedStateVar.AllowedStringValues==null)
					{
						cs.Append(A.GetRetArg().RelatedStateVar.GetNetType().FullName);
						cs.Append(")");
						cs.Append("RetVal, ");
					}
					else
					{
						cs.Append("Enum_" + A.GetRetArg().RelatedStateVar.Name);
						cs.Append(")");
						cs.Append("RetEnum, ");
					}
				}
				cs.Append("null, StateInfo[0]);\r\n");
				cs.Append("            }\r\n");
				cs.Append("            else\r\n");
				cs.Append("            {\r\n");
				cs.Append("                OnResult_" + A.Name + "_Event.Fire(this, ");
				argid = 0;
				foreach(UPnPArgument _arg in A.ArgumentList)
				{
					if(_arg.IsReturnValue==false)
					{
						cs.Append("(");
						if(_arg.RelatedStateVar.AllowedStringValues==null)
						{
							cs.Append(_arg.RelatedStateVar.GetNetType().FullName + " ");
						}
						else
						{
							cs.Append("Enum_" + _arg.RelatedStateVar.Name + " ");
						}
						cs.Append(")");
						cs.Append("Args[" + argid.ToString() + "].DataValue, ");
						++argid;
					}
				}
				if(A.HasReturnValue==true)
				{
					cs.Append("(");
					if(A.GetRetArg().RelatedStateVar.AllowedStringValues==null)
					{
						cs.Append(A.GetRetArg().RelatedStateVar.GetNetType().FullName);
						cs.Append(")");
						cs.Append("RetVal, ");
					}
					else
					{
						cs.Append("Enum_" + A.GetRetArg().RelatedStateVar.Name);
						cs.Append(")");
						cs.Append("RetEnum, ");
					}
				}
				cs.Append("null, StateInfo[0]);\r\n");
				cs.Append("            }\r\n");
				cs.Append("        }\r\n");

				//Build Error Sink
				cs.Append("        private void Error_Sink_" + A.Name + "(UPnPService sender, string MethodName, UPnPArgument[] Args, UPnPInvokeException e, object _Tag)\r\n");
				cs.Append("        {\r\n");
				
				// Check if this is necessary
				IsNeccessary = false;
				foreach(UPnPArgument _arg in A.ArgumentList)
				{
					if((_arg.RelatedStateVar.AllowedStringValues!=null)&&
						(_arg.Direction=="in"))
					{
						IsNeccessary = true;
						break;
					}
				}
				if(IsNeccessary==true)
				{
					cs.Append("            for(int i=0;i<Args.Length;++i)\r\n");
					cs.Append("            {\r\n");
					cs.Append("                switch(Args[i].Name)\r\n");
					cs.Append("                {\r\n");
					foreach(UPnPArgument _arg in A.ArgumentList)
					{
						if(_arg.IsReturnValue==false)
						{
							if(_arg.RelatedStateVar.AllowedStringValues!=null)
							{
								cs.Append("                    case \"" + _arg.Name + "\":\r\n");
								cs.Append("                        switch((string)Args[i].DataValue)\r\n");
								cs.Append("                        {\r\n");
								foreach(EnumStruct ess in ((VarData)elist[_arg.RelatedStateVar.Name]).Enumerations)
								{
									cs.Append("                            case \"" + ess.EnumValue + "\":\r\n");
									cs.Append("                                Args[i].DataValue = Enum_" + _arg.RelatedStateVar.Name + "." + ess.EnumName + ";\r\n");
									cs.Append("                                break;\r\n");
								}
								cs.Append("                        }\r\n");

								cs.Append("                        break;\r\n");
							}
						}
					}
					cs.Append("                }\r\n"); // End of Switch
					cs.Append("            }\r\n"); // End of For Loop
				}
				if(A.HasReturnValue==true)
				{
					if(A.GetRetArg().RelatedStateVar.AllowedStringValues!=null)
					{
						cs.Append("            SetUnspecifiedValue(\"Enum_" + A.GetRetArg().RelatedStateVar.Name + "\",\"* error *\");\r\n");
					}
				}
				cs.Append("            object[] StateInfo = (object[])_Tag;\r\n");
				cs.Append("            if(StateInfo[1]!=null)\r\n");
				cs.Append("            {\r\n");
				cs.Append("                ((Delegate_OnResult_" + A.Name + ")StateInfo[1])(this, ");
				argid = 0;
				foreach(UPnPArgument _arg in A.ArgumentList)
				{
					if(_arg.IsReturnValue==false)
					{
						if(_arg.Direction=="in")
						{
							cs.Append("(");
							if(_arg.RelatedStateVar.AllowedStringValues==null)
							{
								cs.Append(_arg.RelatedStateVar.GetNetType().FullName + " ");
							}
							else
							{
								cs.Append("Enum_" + _arg.RelatedStateVar.Name + " ");
							}
							cs.Append(")");
							cs.Append("Args[" + argid.ToString() + "].DataValue, ");
						}
						else
						{
							if(_arg.RelatedStateVar.AllowedStringValues!=null)
							{
								cs.Append("(Enum_" + _arg.RelatedStateVar.Name + ")0, ");
							}
							else
							{
								cs.Append("(" + _arg.RelatedStateVar.GetNetType().FullName + ")UPnPService.CreateObjectInstance(typeof(" + _arg.RelatedStateVar.GetNetType().FullName + "),null), ");
							}
						}
						++argid;
					}
				}
				if(A.HasReturnValue==true)
				{
					if(A.GetRetArg().RelatedStateVar.AllowedStringValues==null)
					{
						cs.Append("(" + A.GetRetArg().RelatedStateVar.GetNetType().FullName + ")UPnPService.CreateObjectInstance(typeof(" + A.GetRetArg().RelatedStateVar.GetNetType().FullName + "),null), ");
					}
					else
					{
						cs.Append("Enum_" + A.GetRetArg().RelatedStateVar.Name + "._UNSPECIFIED_, ");
					}
				}
				cs.Append("e, StateInfo[0]);\r\n");
				cs.Append("            }\r\n");



				cs.Append("            else\r\n");
				cs.Append("            {\r\n");
				cs.Append("                OnResult_" + A.Name + "_Event.Fire(this, ");
				argid = 0;
				foreach(UPnPArgument _arg in A.ArgumentList)
				{
					if(_arg.IsReturnValue==false)
					{
						if(_arg.Direction=="in")
						{
							cs.Append("(");
							if(_arg.RelatedStateVar.AllowedStringValues==null)
							{
								cs.Append(_arg.RelatedStateVar.GetNetType().FullName + " ");
							}
							else
							{
								cs.Append("Enum_" + _arg.RelatedStateVar.Name + " ");
							}
							cs.Append(")");
							cs.Append("Args[" + argid.ToString() + "].DataValue, ");
						}
						else
						{
							if(_arg.RelatedStateVar.AllowedStringValues!=null)
							{
								cs.Append("(Enum_" + _arg.RelatedStateVar.Name + ")0, ");
							}
							else
							{
								cs.Append("(" + _arg.RelatedStateVar.GetNetType().FullName + ")UPnPService.CreateObjectInstance(typeof(" + _arg.RelatedStateVar.GetNetType().FullName + "),null), ");
							}
						}
						++argid;
					}
				}
				if(A.HasReturnValue==true)
				{
					if(A.GetRetArg().RelatedStateVar.AllowedStringValues==null)
					{
						cs.Append("(" + A.GetRetArg().RelatedStateVar.GetNetType().FullName + ")UPnPService.CreateObjectInstance(typeof(" + A.GetRetArg().RelatedStateVar.GetNetType().FullName + "),null), ");
					}
					else
					{
						cs.Append("Enum_" + A.GetRetArg().RelatedStateVar.Name + "._UNSPECIFIED_, ");
					}
				}
				cs.Append("e, StateInfo[0]);\r\n");
				cs.Append("            }\r\n");
				cs.Append("        }\r\n");

				// End of Error Sink
			}

			cs.Append("    }\r\n");
			cs.Append("}");

			UTF8Encoding UTF8 = new UTF8Encoding();
			FileStream ws = new FileStream(SavePath, FileMode.Create, FileAccess.Write);
			byte[] buffer = UTF8.GetBytes(cs.ToString());
			ws.Write(buffer,0,buffer.Length);
			ws.Close();
		}
        /// <summary>
        /// Method executes when a contentdirectory events a change in a container.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="NewValue"></param>
        private void Sink_OnContainerUpdateIDsChanged(CpContentDirectory sender, System.String NewValue)
        {
            string csv_containers = NewValue;
            Hashtable cache = new Hashtable();
            DText parser = new DText();
            DText parser2 = new DText();
            parser.ATTRMARK = ",";
            parser2.ATTRMARK = ",";

            if (csv_containers != "")
            {
                parser[0] = csv_containers;
                int dcnt = parser.DCOUNT();

                for (int i=1; i <= dcnt; i++)
                {
                    string id, update;
                    if (Accumulator_ContainerUpdateIDs.Delimitor == ",")
                    {
                        id = parser[i++];
                        update = parser[i];
                    }
                    else
                    {
                        string pair = parser[i];
                        parser2[0] = pair;
                        id = parser2[1];
                        update = parser2[2];
                    }

                    CpMediaContainer cpc = (CpMediaContainer) this.GetDescendent(id, cache);

                    if (cpc !=null)
                    {
                        try
                        {
                            UInt32 updateId = UInt32.Parse(update);
                            if (updateId != cpc.UpdateID)
                            {
                                cpc.ForceUpdate(false);
                            }
                        }
                        catch
                        {
                            cpc.ForceUpdate(false);
                        }
                    }
                }
            }

            cache.Clear();
        }
        protected void PositionInfoSink(CpAVTransport sender, UInt32 InstanceID, 
			UInt32 Track,
			string TrackDuration,
			string TrackMetaData,
			string TrackURI,
			string RelTime,
			string AbsTime,
			int RelCount,
			int AbsCount,
			UPnPInvokeException e,
			object Handle)
        {
            DText p = new DText();
            p.ATTRMARK = ":";
            try
            {
                p[0] = TrackDuration;
                _CurrentDuration = new TimeSpan(int.Parse(p[1]),int.Parse(p[2]),int.Parse(p[3]));
            }
            catch(Exception)
            {
            }

            try
            {
                p[0]= RelTime;
                _CurrentPosition = new TimeSpan(int.Parse(p[1]),int.Parse(p[2]),int.Parse(p[3]));
            }
            catch(Exception)
            {
            }

            if(OnCurrentPositionChanged!=null) OnCurrentPositionChanged(this);

            _CurrentTrack = Track;
            this._TrackURI = HTTPMessage.UnEscapeString(TrackURI);
            if(this.OnTrackURIChanged!=null) OnTrackURIChanged(this);
            if(OnCurrentTrackChanged!=null) OnCurrentTrackChanged(this);
            lock(this)
            {
                --StateCounter;
                if(StateCounter==0)
                {
                    if(OnReady!=null)OnReady(this);
                }
            }
        }
        protected void PollSink(CpAVTransport sender, UInt32 InstanceID, 
			UInt32 Track,
			string TrackDuration,
			string TrackMetaData,
			string TrackURI,
			string RelTime,
			string AbsTime,
			int RelCount,
			int AbsCount,
			UPnPInvokeException e,
			object Handle)
        {
            DText p = new DText();
            p.ATTRMARK = ":";
            TimeSpan ts;

            try
            {
                p[0]= RelTime;
                _CurrentPosition = new TimeSpan(int.Parse(p[1]),int.Parse(p[2]),int.Parse(p[3]));

                if (this._CurrentTrack != Track)
                {
                    this._CurrentTrack = Track;
                    if (this.OnCurrentTrackChanged != null)
                    {
                        this.OnCurrentTrackChanged(this);
                    }
                }

                p[0] = TrackDuration;
                ts = new TimeSpan(int.Parse(p[1]), int.Parse(p[2]), int.Parse(p[3]));
                if (this._CurrentDuration != ts)
                {
                    this._CurrentDuration = ts;
                }

                if (this._TrackURI.CompareTo(TrackURI) != 0)
                {
                    this._TrackURI = TrackURI;
                    if (this.OnCurrentTrackChanged != null)
                    {
                        this.OnCurrentTrackChanged(this);
                    }
                }

                if (this._AVTransportMetaData.CompareTo(TrackMetaData) != 0)
                {
                    this._AVTransportMetaData = TrackMetaData;
                    if (this.OnCurrentURIMetaDataChanged != null)
                    {
                        this.OnCurrentURIMetaDataChanged(this);
                    }
                }
            }
            catch(Exception)
            {
            }

            if(OnCurrentPositionChanged!=null) OnCurrentPositionChanged(this);
        }
        protected void LastChangeSink(CpAVTransport sender, string LC)
        {
            if(LC=="")return;
            //LC = UPnPStringFormatter.UnEscapeString(LC);
            if(LC.Substring(0,1)!="<") LC = UPnPStringFormatter.UnEscapeString(LC);
            if(LC.Substring(0,1)!="<") LC = UPnPStringFormatter.UnEscapeString(LC);

            StringReader MyString = new StringReader(LC);
            XmlTextReader XMLDoc = new XmlTextReader(MyString);

            Hashtable T = new Hashtable();

            int ID = -1;
            string VarName = "";
            string VarValue = "";
            string AttrName = "";
            string AttrValue = "";

            XMLDoc.Read();
            XMLDoc.MoveToContent();

            XMLDoc.Read();
            XMLDoc.MoveToContent();

            while((XMLDoc.LocalName!="Event")&&(XMLDoc.EOF==false))
            {
                // At Start, should be InstanceID
                if(XMLDoc.LocalName=="InstanceID")
                {
                    XMLDoc.MoveToAttribute("val");
                    ID = int.Parse(XMLDoc.GetAttribute("val"));
                    if(T.ContainsKey(ID)==false) T[ID] = new Hashtable();
                    XMLDoc.MoveToContent();

                    XMLDoc.Read();
                    XMLDoc.MoveToContent();

                    while(XMLDoc.LocalName!="InstanceID")
                    {
                        if(XMLDoc.IsStartElement()==true)
                        {
                            VarName = XMLDoc.LocalName;
                            VarValue = "";
                            AttrName = "";
                            AttrValue = "";

                            for(int a_idx=0;a_idx<XMLDoc.AttributeCount;++a_idx)
                            {
                                XMLDoc.MoveToAttribute(a_idx);
                                if(XMLDoc.LocalName=="val")
                                {
                                    VarValue = UPnPStringFormatter.UnEscapeString(XMLDoc.GetAttribute(a_idx));
                                }
                                else
                                {
                                    AttrName = XMLDoc.LocalName;
                                    AttrValue = XMLDoc.GetAttribute(a_idx);
                                }
                            }

                            XMLDoc.MoveToContent();

                            if(AttrName=="")
                            {
                                ((Hashtable)T[ID])[VarName] = VarValue;
                            }
                            else
                            {
                                if(((Hashtable)T[ID]).ContainsKey(VarName)==false)
                                {
                                    ((Hashtable)T[ID])[VarName] = new Hashtable();
                                }
                                if(((Hashtable)((Hashtable)T[ID])[VarName]).ContainsKey(AttrName)==false)
                                {
                                    ((Hashtable)((Hashtable)T[ID])[VarName])[AttrName] = new Hashtable();
                                }
                                ((Hashtable)((Hashtable)((Hashtable)T[ID])[VarName])[AttrName])[AttrValue] = VarValue;
                            }
                        }
                        XMLDoc.Read();
                        XMLDoc.MoveToContent();
                    }
                }
                else
                {
                    XMLDoc.Skip();
                }
                XMLDoc.Read();
                XMLDoc.MoveToContent();
            }

            XMLDoc.Close();

            IDictionaryEnumerator inEn = T.GetEnumerator();
            IDictionaryEnumerator EvEn;
            Hashtable TT;
            string TempString;
            DText TempParser = new DText();

            while(inEn.MoveNext())
            {
                if(inEn.Key.ToString() == InstanceID)
                {
                    TT = (Hashtable)inEn.Value;
                    EvEn = TT.GetEnumerator();
                    while(EvEn.MoveNext())
                    {
                        switch((string)EvEn.Key)
                        {
                            case "AVTransportURI":
                                _AVTransportURI = (string)EvEn.Value;
                                if(OnAVTransportURIChanged!=null) OnAVTransportURIChanged(this);
                                break;

                            case "TransportState":
                                _PlayState = (string)EvEn.Value;
                                if(OnPlayStateChanged!=null) OnPlayStateChanged(this);
                                break;

                            case "CurrentTrackDuration":
                                TempString = (string)EvEn.Value;
                                TempParser.ATTRMARK = ":";
                                TempParser[0] = TempString;
                                try
                                {
                                    this._CurrentDuration = new TimeSpan(int.Parse(TempParser[1]),int.Parse(TempParser[2]),int.Parse(TempParser[3]));
                                }
                                catch(Exception)
                                {}
                                if(this.OnCurrentPositionChanged!=null) this.OnCurrentPositionChanged(this);
                                break;

                            case "RelativeTimePosition":
                                TempString = (string)EvEn.Value;
                                TempParser.ATTRMARK = ":";
                                TempParser[0] = TempString;
                                this._CurrentPosition = new TimeSpan(int.Parse(TempParser[1]),int.Parse(TempParser[2]),int.Parse(TempParser[3]));
                                if(this.OnCurrentPositionChanged!=null) this.OnCurrentPositionChanged(this);
                                break;

                            case "NumberOfTracks":
                                try
                                {
                                    _NumberOfTracks = UInt32.Parse((string)EvEn.Value);
                                    if(OnNumberOfTracksChanged!=null) OnNumberOfTracksChanged(this);
                                }
                                catch(Exception)
                                {}
                                break;

                            case "CurrentTrack":
                                try
                                {
                                    _CurrentTrack = UInt32.Parse((string)EvEn.Value);
                                    if(OnCurrentTrackChanged!=null) OnCurrentTrackChanged(this);
                                }
                                catch(Exception)
                                {}
                                break;

                            case "CurrentTrackURI":
                                _TrackURI = (string)EvEn.Value;
                                if(OnTrackURIChanged!=null) OnTrackURIChanged(this);
                                break;
                            case "TransportStatus":
                                _TransportStatus = (string)EvEn.Value;
                                if(OnTransportStatusChanged!=null) OnTransportStatusChanged(this);
                                break;
                            case "AVTransportURIMetaData":
                                _AVTransportMetaData = (string)EvEn.Value;
                                if(this.OnCurrentURIMetaDataChanged!=null) OnCurrentURIMetaDataChanged(this);
                                break;

                            case "CurrentPlayMode":
                                _CurrentPlayMode = (string)EvEn.Value;
                                if(this.OnCurrentPlayModeChanged!=null) OnCurrentPlayModeChanged(this);
                                break;
                        }
                    }
                }
            }
        }
Example #8
0
        /// <summary>
        /// This method gets called when a new Request is received
        /// </summary>
        /// <param name="request"></param>
        /// <param name="WebSession"></param>
        protected void ReceiveSink(HTTPMessage request, HTTPSession WebSession)
        {
            HTTPMessage rsp = new HTTPMessage();
            UTF8Encoding U = new UTF8Encoding();

            if ((request.Directive == "HEAD") ||
                (request.Directive == "GET"))
            {
                if (request.DirectiveObj == "/item.m3u")
                {/*
					rsp.StatusCode = 200;
					rsp.StatusData = "OK";
					rsp.ContentType = "audio/mpegurl";
					rsp.StringBuffer = M3UString;
					*/

                    string r = request.GetTag("Range");
                    if (r == "")
                    {
                        rsp.StatusCode = 200;
                        rsp.StatusData = "OK";
                        rsp.ContentType = "audio/mpegurl";
                        rsp.StringBuffer = M3UString;
                    }
                    else
                    {
                        rsp.StatusCode = 206;
                        rsp.StatusData = "Partial Content";
                        rsp.ContentType = "audio/mpegurl";

                        DText rp0 = new DText();
                        rp0.ATTRMARK = "=";
                        rp0[0] = r;
                        DText rp = new DText();
                        rp.ATTRMARK = "-";
                        rp[0] = rp0[2].Trim();
                        if (rp[1] == "")
                        {
                            // LastBytes
                            try
                            {
                                if (int.Parse(rp[2]) > M3UString.Length)
                                {
                                    rsp.AddTag("Content-Range", "bytes 0-" + M3UString.Length.ToString() + "/" + M3UString.Length.ToString());
                                    rsp.StringBuffer = M3UString;
                                }
                                else
                                {
                                    rsp.AddTag("Content-Range", "bytes " + (M3UString.Length - int.Parse(rp[2])).ToString() + "-" + M3UString.Length.ToString() + "/" + M3UString.Length.ToString());
                                    rsp.StringBuffer = M3UString.Substring(M3UString.Length - int.Parse(rp[2]));
                                }
                            }
                            catch (Exception)
                            {
                                rsp = new HTTPMessage();
                                rsp.StatusCode = 400;
                                rsp.StatusData = "Bad Request";
                            }
                        }
                        else if (rp[2] == "")
                        {
                            // Offset till end
                            try
                            {
                                rsp.AddTag("Content-Range", "bytes " + rp[1] + "-" + M3UString.Length.ToString() + "/" + M3UString.Length.ToString());
                                rsp.StringBuffer = M3UString.Substring(int.Parse(rp[1]));
                            }
                            catch (Exception)
                            {
                                rsp = new HTTPMessage();
                                rsp.StatusCode = 400;
                                rsp.StatusData = "Bad Request";
                            }

                        }
                        else
                        {
                            // Range
                            try
                            {
                                if (int.Parse(rp[2]) > M3UString.Length + 1)
                                {
                                    rsp.AddTag("Content-Range", "bytes " + rp[1] + "-" + (M3UString.Length - 1).ToString() + "/" + M3UString.Length.ToString());
                                    rsp.StringBuffer = M3UString.Substring(int.Parse(rp[1]));
                                }
                                else
                                {
                                    rsp.AddTag("Content-Range", "bytes " + rp[1] + "-" + rp[2] + "/" + M3UString.Length.ToString());
                                    rsp.StringBuffer = M3UString.Substring(int.Parse(rp[1]), 1 + int.Parse(rp[2]) - int.Parse(rp[1]));
                                }
                            }
                            catch (Exception)
                            {
                                rsp = new HTTPMessage();
                                rsp.StatusCode = 400;
                                rsp.StatusData = "Bad Request";
                            }
                        }
                    }
                }
                else
                {
                    try
                    {
                        int i = request.DirectiveObj.LastIndexOf("/");
                        string tmp = request.DirectiveObj.Substring(1, i - 1);
                        bool hasRange = true;
                        if (tmp.IndexOf("/") != -1)
                        {
                            tmp = tmp.Substring(0, tmp.IndexOf("/"));
                        }

                        if (FileInfoTable.ContainsKey(tmp))
                        {
                            FileInfo f;
                            if (FileInfoTable[tmp].GetType() == typeof(string))
                            {
                                f = new FileInfo((string)FileInfoTable[tmp]);
                                FileInfoTable[tmp] = f;
                            }
                            else
                            {
                                f = (FileInfo)FileInfoTable[tmp];
                            }
                            ProtocolInfoString pis = ProtocolInfoString.CreateHttpGetProtocolInfoString(f);
                            rsp.StatusCode = 200;
                            rsp.StatusData = "OK";
                            rsp.ContentType = pis.MimeType;
                            if (request.Directive == "HEAD")
                            {
                                rsp.AddTag("Content-Length", f.Length.ToString());
                                rsp.OverrideContentLength = true;
                            }
                            else
                            {
                                HTTPSession.Range[] RNG = null;
                                if (request.GetTag("Range") != "")
                                {
                                    long x, y;
                                    ArrayList RList = new ArrayList();
                                    DText rp = new DText();
                                    rp.ATTRMARK = "=";
                                    rp.MULTMARK = ",";
                                    rp.SUBVMARK = "-";
                                    rp[0] = request.GetTag("Range");

                                    for (int I = 1; I <= rp.DCOUNT(2); ++I)
                                    {
                                        if (rp[2, I, 1] == "")
                                        {
                                            // Final Bytes
                                            y = long.Parse(rp[2, I, 2].Trim());
                                            x = f.Length - y;
                                            y = x + y;
                                        }
                                        else if (rp[2, I, 2] == "")
                                        {
                                            // Offset till end
                                            x = long.Parse(rp[2, I, 1].Trim());
                                            y = f.Length - x;
                                        }
                                        else
                                        {
                                            // Full Range
                                            x = long.Parse(rp[2, I, 1].Trim());
                                            y = long.Parse(rp[2, I, 2].Trim());
                                        }
                                        RList.Add(new HTTPSession.Range(x, 1 + y - x));
                                    }
                                    RNG = (HTTPSession.Range[])RList.ToArray(typeof(HTTPSession.Range));
                                }
                                else
                                {
                                    hasRange = false;
                                }

                                if (request.Version == "1.0")
                                {
                                    WebSession.OnStreamDone += new HTTPSession.StreamDoneHandler(DoneSink);
                                }
                                //								((HTTPMessage)(new UPnPDebugObject(WebSession)).GetField("Headers")).Version = "1.0";
                                if (hasRange)
                                {
                                    WebSession.SendStreamObject(f.OpenRead(), RNG, pis.MimeType);
                                }
                                else
                                {
                                    FileStream fs = f.OpenRead();
                                    long length = fs.Length;
                                    WebSession.SendStreamObject(fs, length, pis.MimeType);
                                }
                                return;
                            }
                        }
                        else
                        {
                            rsp.StatusCode = 404;
                            rsp.StatusData = "Not Found";
                        }
                    }
                    catch (Exception)
                    {
                        rsp.StatusCode = 404;
                        rsp.StatusData = "Not Found";
                    }
                }
                WebSession.Send(rsp);
                return;
            }

            rsp.StatusCode = 500;
            rsp.StatusData = "Not implemented";
            WebSession.Send(rsp);
        }