|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 using System; |
|
19 using System.Collections.Generic; |
|
20 using System.Text; |
|
21 using System.Reflection; |
|
22 using SymbianUtils.BasicTypes; |
|
23 using SymbianETMLib.Common.Types; |
|
24 |
|
25 namespace SymbianETMLib.Common.Packets.Factory |
|
26 { |
|
27 public static class ETMPacketFactory |
|
28 { |
|
29 public static ETMPcktBase Create( SymByte aByte ) |
|
30 { |
|
31 ETMPcktBase ret = null; |
|
32 // |
|
33 if ( iPackets.Count == 0 ) |
|
34 { |
|
35 CreatePackets(); |
|
36 } |
|
37 // |
|
38 ret = FindPacket( aByte ); |
|
39 // |
|
40 return ret; |
|
41 } |
|
42 |
|
43 #region Internal methods |
|
44 private static void CreatePackets() |
|
45 { |
|
46 Type pluginType = typeof( ETMPcktBase ); |
|
47 Assembly assembly = Assembly.GetExecutingAssembly(); |
|
48 Type[] types = assembly.GetTypes(); |
|
49 foreach ( Type type in types ) |
|
50 { |
|
51 if ( !type.IsAbstract && pluginType.IsAssignableFrom( type ) ) |
|
52 { |
|
53 ConstructorInfo ctor = type.GetConstructor( System.Type.EmptyTypes ); |
|
54 if ( ctor != null ) |
|
55 { |
|
56 object instance = Activator.CreateInstance( type, null ); |
|
57 if ( instance != null ) |
|
58 { |
|
59 iPackets.Add( (ETMPcktBase) instance ); |
|
60 } |
|
61 } |
|
62 } |
|
63 } |
|
64 |
|
65 // Sort the packets into priority order |
|
66 Comparison<ETMPcktBase> comparer = delegate( ETMPcktBase aLeft, ETMPcktBase aRight ) |
|
67 { |
|
68 return ( aLeft.Priority.CompareTo( aRight.Priority ) * -1 ); |
|
69 }; |
|
70 iPackets.Sort( comparer ); |
|
71 } |
|
72 |
|
73 private static ETMPcktBase FindPacket( SymByte aByte ) |
|
74 { |
|
75 ETMPcktBase ret = new ETMPcktUnknown(); |
|
76 // |
|
77 int count = iPackets.Count; |
|
78 for ( int i = 0; i < count; i++ ) |
|
79 { |
|
80 ETMPcktBase packet = iPackets[ i ]; |
|
81 if ( packet.Matches( aByte ) ) |
|
82 { |
|
83 ret = packet; |
|
84 ret.RawByte = aByte; |
|
85 break; |
|
86 } |
|
87 } |
|
88 // |
|
89 return ret; |
|
90 } |
|
91 #endregion |
|
92 |
|
93 #region Data members |
|
94 private static List<ETMPcktBase> iPackets = new List<ETMPcktBase>(); |
|
95 #endregion |
|
96 } |
|
97 } |