1 |
william |
36 |
using System; |
2 |
|
|
using System.Collections.Generic; |
3 |
|
|
using System.Linq; |
4 |
|
|
using System.Text; |
5 |
|
|
using libxmltv.Interfaces; |
6 |
william |
46 |
using System.ComponentModel; |
7 |
william |
54 |
using System.Windows.Forms; |
8 |
|
|
using System.Threading; |
9 |
william |
56 |
using System.Reflection; |
10 |
|
|
using System.Globalization; |
11 |
|
|
using System.Diagnostics; |
12 |
william |
36 |
|
13 |
|
|
namespace libxmltv.Core |
14 |
|
|
{ |
15 |
william |
49 |
[Serializable] |
16 |
william |
56 |
internal class XMLTVRuntimeInstance : MarshalByRefObject, |
17 |
|
|
IXMLTVRuntimeInstance, |
18 |
|
|
ISerializer<XMLTVRuntimeInstance>, |
19 |
|
|
IGetCreatedInstanceEvent, |
20 |
|
|
ISetCreatedInstanceEvent, |
21 |
|
|
IDestroyInstance, |
22 |
|
|
IRuntimeInstanceLoader<XMLTVRuntimeInstance> |
23 |
william |
36 |
{ |
24 |
william |
56 |
[NonSerialized] |
25 |
william |
54 |
Thread worker; |
26 |
william |
56 |
public XMLTVRuntimeInstance() |
27 |
|
|
{ |
28 |
|
|
} |
29 |
william |
54 |
public XMLTVRuntimeInstance(string xmlfile) |
30 |
|
|
{ |
31 |
|
|
worker = new Thread(new ParameterizedThreadStart(CreateInstance)); |
32 |
|
|
//CreateInstance(xmlfile); |
33 |
|
|
worker.Start(xmlfile); |
34 |
|
|
} |
35 |
william |
52 |
//public XMLTVRuntimeInstance(string xmlfile) : this(xmlfile, null) { } |
36 |
|
|
//public XMLTVRuntimeInstance(string xmlfile, EventHandler<CancelEventArgs> t) { CreateInstance(xmlfile,t); } |
37 |
william |
54 |
private void CreateInstance(object xmlfile) |
38 |
|
|
{ |
39 |
william |
52 |
//CancelEvent = t; |
40 |
william |
54 |
using (XMLTVInstance instance = new XMLTVInstance(xmlfile.ToString(), this)) |
41 |
william |
45 |
{ |
42 |
william |
54 |
if (OnInstanceCreated != null) |
43 |
|
|
{ |
44 |
|
|
OnInstanceCreated.Invoke(this,new EventArgs()); |
45 |
|
|
} |
46 |
william |
45 |
} |
47 |
|
|
} |
48 |
william |
46 |
|
49 |
william |
49 |
//internal XMLTVInstance Instance { get; private set; } |
50 |
william |
36 |
|
51 |
|
|
#region IXMLTV_LOADER members |
52 |
william |
56 |
private System.IO.FileInfo _XmlFile; |
53 |
|
|
public System.IO.FileInfo XmlFile { get { return _XmlFile; } set { _XmlFile = value; } } |
54 |
|
|
private string _XmlDoc; |
55 |
|
|
public string XmlDoc { get { return _XmlDoc; } set { _XmlDoc = value; } } |
56 |
william |
36 |
#endregion |
57 |
|
|
#region IXMLTV_PARSER Members |
58 |
william |
56 |
private IXMLTVSource _Source; |
59 |
|
|
public IXMLTVSource Source { get { return _Source; } set { _Source = value; } } |
60 |
|
|
private Dictionary<string, IXMLTVChannel> _Channels; |
61 |
|
|
public Dictionary<string, IXMLTVChannel> Channels { get { return _Channels; } set { _Channels = value; } } |
62 |
|
|
private Dictionary<int, IXMLTVProgram> _Programs; |
63 |
|
|
public Dictionary<int, IXMLTVProgram> Programs { get { return _Programs; } set { _Programs = value; } } |
64 |
william |
36 |
#endregion |
65 |
william |
44 |
|
66 |
william |
56 |
[NonSerialized] |
67 |
|
|
private EventHandler<EventArgs> _OnInstanceCreated; |
68 |
|
|
public EventHandler<EventArgs> OnInstanceCreated { get { return _OnInstanceCreated; } set { _OnInstanceCreated = value; } } |
69 |
william |
54 |
public EventHandler<EventArgs> GetOnInstanceCreated() { return OnInstanceCreated; } |
70 |
|
|
public void SetOnInstanceCreated(EventHandler<EventArgs> event_instance) { OnInstanceCreated = event_instance; } |
71 |
|
|
|
72 |
william |
51 |
public IXMLTVSerializer<XMLTVRuntimeInstance> Serializer |
73 |
|
|
{ |
74 |
william |
52 |
get |
75 |
|
|
{ |
76 |
|
|
///* We have to set CancelEvent to null, before returning a new instance of the serializer otherwise all subscribers to the event will have to be marked as serializeable. |
77 |
|
|
// Most subscribers will be of type: System.Windows.Forms which is not marked as serializable and will fail to serialize. */ |
78 |
|
|
//if (CancelEvent != null) { CancelEvent = null; } |
79 |
|
|
return new XMLTVSerializer<XMLTVRuntimeInstance>(this); |
80 |
|
|
} |
81 |
william |
51 |
} |
82 |
william |
54 |
public void DestroyInstance() |
83 |
william |
46 |
{ |
84 |
william |
55 |
xmltv_logger.Debug.WriteLine("Destoying Instance of: '{0}'", this.GetType().Name); |
85 |
william |
54 |
this.IsAborting = true; |
86 |
|
|
if (worker == null) |
87 |
william |
46 |
{ |
88 |
william |
55 |
xmltv_logger.Debug.WriteLine("Unable to destroy instance of: '{0}' - worker thread is null", this.GetType().Name); |
89 |
william |
54 |
return; |
90 |
william |
46 |
} |
91 |
william |
54 |
else |
92 |
|
|
{ |
93 |
|
|
if (worker.IsAlive) |
94 |
|
|
{ |
95 |
william |
55 |
xmltv_logger.Verbose.Debug.WriteLine("Requesting Instance to Abort..."); |
96 |
william |
54 |
while (worker.IsAlive) |
97 |
|
|
{ |
98 |
|
|
worker.Abort(); |
99 |
|
|
Application.DoEvents(); |
100 |
|
|
} |
101 |
|
|
} |
102 |
|
|
else |
103 |
|
|
{ |
104 |
william |
55 |
xmltv_logger.Debug.WriteLine("Instance of: '{0}'- already destroyed.", this.GetType().Name); |
105 |
william |
54 |
} |
106 |
|
|
} |
107 |
william |
46 |
} |
108 |
william |
56 |
private bool _IsAborting; |
109 |
william |
54 |
public bool IsAborting |
110 |
|
|
{ |
111 |
william |
56 |
get { return _IsAborting; } |
112 |
|
|
private set { _IsAborting = value; } |
113 |
|
|
} |
114 |
|
|
public XMLTVRuntimeInstance LoadFromInstance(XMLTVRuntimeInstance instance) |
115 |
|
|
{ |
116 |
|
|
if (instance == null) |
117 |
|
|
{ |
118 |
|
|
throw new NullReferenceException("Failed to load from instance because the instance is null."); |
119 |
|
|
} |
120 |
william |
58 |
|
121 |
william |
56 |
if (instance.Source != null) |
122 |
|
|
{ |
123 |
william |
58 |
xmltv_logger.Debug.WriteLine("Loading from instance..."); |
124 |
|
|
xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Created by '{1}' - original source file: '{2}'", instance.Source.SourceName, instance.Source.GeneratorName, instance.XmlFile.FullName); |
125 |
william |
56 |
} |
126 |
|
|
else |
127 |
|
|
{ |
128 |
|
|
xmltv_logger.Error.WriteLine("The Instance's Source Property is null."); |
129 |
william |
58 |
throw new NullReferenceException("The Instance's Source Property is null."); |
130 |
william |
56 |
} |
131 |
|
|
if (instance.Channels != null) |
132 |
|
|
{ |
133 |
william |
58 |
xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Channels from source '{1}'", instance.Channels.Count, instance.Source.SourceName); |
134 |
william |
56 |
} |
135 |
|
|
else |
136 |
|
|
{ |
137 |
|
|
xmltv_logger.Error.WriteLine("The Instance's Channels Property is null."); |
138 |
william |
58 |
throw new NullReferenceException("The Instance's Channels Property is null."); |
139 |
william |
56 |
} |
140 |
|
|
if (instance.Programs != null) |
141 |
|
|
{ |
142 |
william |
58 |
xmltv_logger.Info.WriteLine("Source Loaded: '{0}' Programs from source '{1}", instance.Programs.Count, instance.Source.SourceName); |
143 |
william |
56 |
} |
144 |
|
|
else |
145 |
|
|
{ |
146 |
|
|
xmltv_logger.Error.WriteLine("The Instance's Programs Property is null."); |
147 |
william |
58 |
throw new NullReferenceException("The Instance's Programs Property is null."); |
148 |
william |
56 |
} |
149 |
|
|
CloneFromInstance(ref instance); |
150 |
|
|
//if (OnInstanceCreated != null) |
151 |
|
|
//{ |
152 |
|
|
// OnInstanceCreated.Invoke(this, new EventArgs()); |
153 |
|
|
//} |
154 |
|
|
return instance; |
155 |
|
|
} |
156 |
|
|
private void CloneFromInstance(ref XMLTVRuntimeInstance instance) |
157 |
|
|
{ |
158 |
|
|
//if (!instance.GetType().IsSerializable) |
159 |
|
|
//{ |
160 |
|
|
// throw new ArgumentException("Loaded instance is not serializable.", "instance"); |
161 |
|
|
//} |
162 |
|
|
//if (Object.ReferenceEquals(instance, null)) |
163 |
|
|
//{ |
164 |
|
|
// throw new NullReferenceException("Failed to load from instance because the instance is null."); |
165 |
|
|
//} |
166 |
|
|
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; |
167 |
|
|
CultureInfo culture = CultureInfo.CurrentCulture; |
168 |
|
|
Type t = typeof(XMLTVRuntimeInstance); |
169 |
|
|
foreach (var field in t.GetFields(flags)) |
170 |
|
|
{ |
171 |
|
|
try |
172 |
|
|
{ |
173 |
|
|
if (field.IsNotSerialized) |
174 |
|
|
{ |
175 |
|
|
continue; |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
var f = instance.GetType().GetField(field.Name, flags); |
179 |
|
|
|
180 |
|
|
var v = f.GetValue(instance); |
181 |
|
|
field.SetValue(this, v); |
182 |
|
|
} |
183 |
|
|
catch (Exception ex) |
184 |
|
|
{ |
185 |
|
|
throw new Exception(string.Format("Unable to copy value for field: '{0}' from instance", field.Name), ex); |
186 |
|
|
} |
187 |
|
|
} |
188 |
|
|
foreach (var property in t.GetProperties(flags)) |
189 |
|
|
{ |
190 |
|
|
try |
191 |
|
|
{ |
192 |
|
|
//if (property.Attributes.HasFlag(FieldAttributes.NotSerialized)) |
193 |
|
|
//{ |
194 |
|
|
// continue; |
195 |
|
|
//} |
196 |
|
|
var f = instance.GetType().GetProperty(property.Name); |
197 |
|
|
object value = null; |
198 |
|
|
|
199 |
|
|
try |
200 |
|
|
{ |
201 |
|
|
value = f.GetValue(instance, null); |
202 |
|
|
} |
203 |
|
|
catch (ArgumentException ex) { if (ex.Message == "Property get method not found.") { Debug.WriteLine(ex.ToString()); } else { throw ex; } } |
204 |
|
|
try |
205 |
|
|
{ |
206 |
|
|
property.SetValue(this, value, null); |
207 |
|
|
} |
208 |
|
|
catch (ArgumentException ex) { if (ex.Message == "Property set method not found.") { Debug.WriteLine(ex.ToString()); } else { throw ex; } } |
209 |
|
|
|
210 |
|
|
} |
211 |
|
|
catch (Exception ex) |
212 |
|
|
{ |
213 |
|
|
throw new Exception(string.Format("Unable to copy value for property: '{0}' from instance", property.Name), ex); |
214 |
|
|
} |
215 |
|
|
} |
216 |
|
|
} |
217 |
william |
36 |
} |
218 |
|
|
|
219 |
william |
44 |
internal class XMLTVInstance : IDisposable |
220 |
william |
36 |
{ |
221 |
|
|
public XMLTVInstance(string xmlfile, XMLTVRuntimeInstance instance) |
222 |
|
|
{ |
223 |
|
|
CreateLoader(xmlfile, instance); |
224 |
|
|
CreateParser(instance); |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
private void CreateLoader(string xml_file, XMLTVRuntimeInstance instance) |
228 |
|
|
{ |
229 |
william |
44 |
//XMLTVLoader loader = new XMLTVLoader(xml_file, instance); |
230 |
|
|
XMLTVLoader.CreateInstance(xml_file, instance); |
231 |
william |
36 |
} |
232 |
|
|
private void CreateParser(XMLTVRuntimeInstance instance) |
233 |
|
|
{ |
234 |
william |
44 |
//XMLTVParser parser = new XMLTVParser(instance); |
235 |
|
|
XMLTVParser.CreateInstance(instance); |
236 |
william |
36 |
} |
237 |
william |
44 |
|
238 |
|
|
public void Dispose() |
239 |
|
|
{ |
240 |
|
|
//throw new NotImplementedException(); |
241 |
|
|
} |
242 |
william |
36 |
} |
243 |
|
|
} |