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