1 |
|
//---PACKAGE DECLARATION--- |
2 |
+ |
package uk.ac.ukc.iscream.filter; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
+ |
import uk.ac.ukc.iscream.util.*; |
6 |
|
import uk.ac.ukc.iscream.core.*; |
7 |
+ |
import uk.ac.ukc.iscream.componentmanager.*; |
8 |
|
import uk.ac.ukc.iscream.filter.*; |
6 |
– |
import org.omg.CORBA.*; |
7 |
– |
import org.omg.CosNaming.*; |
8 |
– |
import org.omg.PortableServer.*; |
9 |
|
|
10 |
|
/** |
11 |
|
* A Filter Startup Class |
12 |
+ |
* A filter is an iscream component. |
13 |
|
* |
14 |
|
* @author $Author$ |
15 |
|
* @version $Id$ |
16 |
|
*/ |
17 |
< |
class FilterMain { |
17 |
> |
public class FilterMain implements Component { |
18 |
|
|
19 |
|
//---FINAL ATTRIBUTES--- |
20 |
|
|
25 |
|
|
26 |
|
//---STATIC METHODS--- |
27 |
|
|
28 |
< |
public static void main(String[] args) { |
29 |
< |
System.setProperty("org.omg.CORBA.ORBClass","jacorb.orb.ORB"); |
30 |
< |
System.setProperty("org.omg.CORBA.ORBSingletonClass","jacorb.orb.ORBSingleton"); |
28 |
> |
//---CONSTRUCTORS--- |
29 |
> |
|
30 |
> |
/** |
31 |
> |
* Constructs a Filter with the name given |
32 |
> |
* |
33 |
> |
* @param givenName the name |
34 |
> |
*/ |
35 |
> |
public FilterMain(String givenName) { |
36 |
> |
NAME = givenName; |
37 |
> |
} |
38 |
> |
|
39 |
> |
//---PUBLIC METHODS--- |
40 |
> |
|
41 |
> |
/** |
42 |
> |
* Starts the Filter component |
43 |
> |
*/ |
44 |
> |
public void start() throws ComponentStartException { |
45 |
> |
|
46 |
> |
_logger.write(toString(), Logger.SYSINIT, "coming up"); |
47 |
> |
|
48 |
> |
// configuration variables we require |
49 |
> |
int UDPListenPort = 0; |
50 |
> |
int TCPListenPort = 0; |
51 |
> |
String parentFilterName = null; |
52 |
|
|
53 |
< |
// get our name from the command line |
54 |
< |
String ourName = ""; |
55 |
< |
if (args.length == 1) { |
34 |
< |
ourName = args[0]; |
53 |
> |
Configuration config = _refman.getCM().getConfiguration(FilterMain.NAME); |
54 |
> |
if (config == null) { |
55 |
> |
throw new ComponentStartException("Unable to obtain configuration for component"); |
56 |
|
} |
57 |
|
else { |
58 |
< |
usage(); |
58 |
> |
try { |
59 |
> |
// get the configuration properties we need |
60 |
> |
UDPListenPort = Integer.parseInt(config.getProperty("Filter.UDPListenPort")); |
61 |
> |
TCPListenPort = Integer.parseInt(config.getProperty("Filter.TCPListenPort")); |
62 |
> |
parentFilterName = config.getProperty("Filter.parentFilter"); |
63 |
> |
} catch (org.omg.CORBA.MARSHAL e) { |
64 |
> |
throw new ComponentStartException("Unable to obtain requried configuration property for component"); |
65 |
> |
} |
66 |
|
} |
67 |
|
|
68 |
< |
// can't have a real toString() :) |
41 |
< |
String toString = "Filter{" + ourName + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
68 |
> |
_logger.write(toString(), Logger.SYSINIT, "configured"); |
69 |
|
|
70 |
< |
try { |
71 |
< |
ORB orb = ORB.init(args, null); |
72 |
< |
|
73 |
< |
// something to hold objects |
74 |
< |
org.omg.CORBA.Object objRef = null; |
75 |
< |
|
76 |
< |
// get the Root POA |
77 |
< |
objRef = orb.resolve_initial_references("RootPOA"); |
78 |
< |
POA poa = POAHelper.narrow(objRef); |
79 |
< |
|
80 |
< |
// get a hook to the name service |
81 |
< |
objRef = orb.resolve_initial_references("NameService"); |
82 |
< |
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef); |
83 |
< |
|
84 |
< |
// get a ref to the ConfigurationManager, Logger & the FilterManager |
58 |
< |
|
59 |
< |
objRef = ncRef.resolve(ncRef.to_name("iscream.ConfigurationManager")); |
60 |
< |
ConfigurationManager configManager = ConfigurationManagerHelper.narrow(objRef); |
61 |
< |
|
62 |
< |
objRef = ncRef.resolve(ncRef.to_name("iscream.Logger")); |
63 |
< |
Logger logger = LoggerHelper.narrow(objRef); |
64 |
< |
|
65 |
< |
logger.write(toString, Logger.SYSINIT, "coming up"); |
66 |
< |
|
67 |
< |
|
68 |
< |
// **** THIS SECTION WILL NEED CHANGING TO GET RELEVANT CONFIG |
69 |
< |
// **** Please ignore this block of code, it's just "copy/paste" :) |
70 |
< |
|
71 |
< |
// get the config |
72 |
< |
Configuration myConfig = configManager.getConfiguration(ourName); |
73 |
< |
|
74 |
< |
// read some config here |
75 |
< |
int UDPport = 0; |
76 |
< |
int TCPport = 0; |
77 |
< |
String parentFilterName = null; |
78 |
< |
|
79 |
< |
// did we? |
80 |
< |
if (myConfig == null) { |
81 |
< |
System.out.println("Failed: is it there?, can you read it?"); |
82 |
< |
System.exit(1); |
83 |
< |
} else { |
84 |
< |
|
85 |
< |
// get the property |
86 |
< |
try { |
87 |
< |
//QUERY HERE.... ??? |
88 |
< |
UDPport = new Integer(myConfig.getProperty("Filter.UDPlistenPort")).intValue(); |
89 |
< |
TCPport = new Integer(myConfig.getProperty("Filter.TCPlistenPort")).intValue(); |
90 |
< |
parentFilterName = myConfig.getProperty("Filter.parentFilter"); |
91 |
< |
} catch (org.omg.CORBA.MARSHAL e) { |
92 |
< |
System.out.println("Caught org.omg.CORBA.MARSHAL, must be a null we got back"); |
93 |
< |
//System.exit(1); |
94 |
< |
} |
95 |
< |
} |
96 |
< |
|
97 |
< |
logger.write(toString, Logger.SYSINIT, "configured"); |
98 |
< |
|
99 |
< |
// **** END COMMENT |
100 |
< |
|
101 |
< |
// **** INITIAL FILTER MANAGER COMMUNICATIONS HERE |
102 |
< |
|
103 |
< |
// get a root (CHANGE to use FilterManager) |
104 |
< |
objRef = ncRef.resolve(ncRef.to_name("iscream.Filter." + parentFilterName)); |
105 |
< |
Filter parentFilter = FilterHelper.narrow(objRef); |
70 |
> |
// get parent |
71 |
> |
Filter parentFilter = FilterHelper.narrow(_refman.getCORBARef("iscream.Filter." + parentFilterName)); |
72 |
> |
|
73 |
> |
// setup a queue |
74 |
> |
Queue queue = new Queue(); |
75 |
> |
|
76 |
> |
// Start a filter thread |
77 |
> |
_logger.write(toString(), Logger.SYSINIT, "starting Filter Thread / Queue consumer"); |
78 |
> |
FilterThread filterThread = new FilterThread(queue, parentFilter); |
79 |
> |
filterThread.start(); |
80 |
> |
|
81 |
> |
// FilterServant start (for inbound child filter data) |
82 |
> |
_logger.write(toString(), Logger.DEBUG, "starting Filter Child -> Parent link for upstream parent - " + parentFilterName); |
83 |
> |
FilterServant filterServant = new FilterServant(TCPListenPort, UDPListenPort, queue); |
84 |
> |
_refman.bindToOrb(filterServant, "iscream.Filter." + FilterMain.NAME); |
85 |
|
|
86 |
< |
// SETUP our Servant |
87 |
< |
|
88 |
< |
// create the FilterServant |
89 |
< |
FilterServant filterServant = new FilterServant(logger, parentFilter, ourName, new Integer(TCPport).toString() , new Integer(UDPport).toString()); |
86 |
> |
// UDL Reader start (for inbound host data) |
87 |
> |
_logger.write(toString(), Logger.SYSINIT, "starting Filter UDP listener"); |
88 |
> |
UDPReader udpReader = new UDPReader(UDPListenPort, queue); |
89 |
> |
udpReader.start(); |
90 |
|
|
91 |
< |
// register ourselves |
92 |
< |
|
93 |
< |
// and advertise it to the naming context |
94 |
< |
objRef = poa.servant_to_reference(filterServant); |
95 |
< |
ncRef.bind(ncRef.to_name("iscream.Filter."+ourName), objRef); |
96 |
< |
|
118 |
< |
// **** END COMMENT |
119 |
< |
|
120 |
< |
// END SETUP |
121 |
< |
|
122 |
< |
|
123 |
< |
/************************************************************** |
124 |
< |
Here would be an ideal place to start another thread to do |
125 |
< |
the listening part of the Filter. Ideally it should just be |
126 |
< |
created and then run(). It may be necessary to pass some of |
127 |
< |
the following parameters into the thread by the constructor. |
128 |
< |
|
129 |
< |
Logger logger |
130 |
< |
- a reference to a Logger object |
131 |
< |
FilterManager filterManager |
132 |
< |
- a reference to the system filter manager |
133 |
< |
ConfigurationManager configManager |
134 |
< |
- a reference to the system configuration manager |
135 |
< |
Configuration myConfig |
136 |
< |
- a reference to the configuration object for this |
137 |
< |
filter instance |
138 |
< |
String ourName |
139 |
< |
- our "identifier" name |
140 |
< |
|
141 |
< |
**************************************************************/ |
142 |
< |
|
143 |
< |
logger.write(toString, Logger.SYSINIT, "starting Filter UDP listener"); |
144 |
< |
UDPReader udpReader = new UDPReader(UDPport, parentFilter, logger); |
145 |
< |
udpReader.start(); |
146 |
< |
logger.write(toString, Logger.SYSINIT, "Filter UDP listener started"); |
147 |
< |
|
148 |
< |
logger.write(toString, Logger.SYSINIT, "starting Filter TCP listener"); |
149 |
< |
TCPReader tcpReader = new TCPReader(logger, configManager, TCPport, parentFilter); |
150 |
< |
tcpReader.start(); |
151 |
< |
logger.write(toString, Logger.SYSINIT, "Filter TCP listener started"); |
152 |
< |
|
153 |
< |
// TEST |
154 |
< |
//parentFilter.receiveXML("<?xml version=\"1.0\" encoding=\"ISO8859-1\"?><packet><test>This is just a debugging test, we ("+ourName+") are live on UDPport: "+UDPport+"</test></packet>"); |
155 |
< |
|
156 |
< |
// start the POA off |
157 |
< |
poa.the_POAManager().activate(); |
158 |
< |
|
159 |
< |
logger.write(toString, Logger.SYSINIT, "started"); |
160 |
< |
|
161 |
< |
// now we are running, we just need to serve |
162 |
< |
// so we ask the orb to block for us until it has finished |
163 |
< |
orb.run(); |
164 |
< |
|
165 |
< |
} catch (Exception e) { |
166 |
< |
System.err.println("FILTER ERROR: " + e); |
167 |
< |
e.printStackTrace(System.out); |
168 |
< |
} |
91 |
> |
// TCP Reader start (for heartbeats) |
92 |
> |
_logger.write(toString(), Logger.SYSINIT, "starting Filter TCP listener"); |
93 |
> |
TCPReader tcpReader = new TCPReader(TCPListenPort, queue); |
94 |
> |
tcpReader.start(); |
95 |
> |
|
96 |
> |
_logger.write(toString(), Logger.SYSINIT, "started"); |
97 |
|
} |
98 |
|
|
99 |
< |
/** |
100 |
< |
* A simple method to print the usage of this class. |
101 |
< |
* It never returns, but instead exits to the system |
102 |
< |
* with a value 1, to indicate the system did not start |
103 |
< |
* properly. |
99 |
> |
/** |
100 |
> |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
101 |
> |
* method to provide clean logging (every class should have this). |
102 |
> |
* |
103 |
> |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
104 |
> |
* to format the toString() |
105 |
> |
* |
106 |
> |
* @return the name of this class and its CVS revision |
107 |
|
*/ |
108 |
< |
public static void usage() { |
109 |
< |
System.out.println("USAGE: java FilterMain <name>"); |
110 |
< |
System.out.println("WHERE <name>:"); |
111 |
< |
System.out.println(" The unique identifier for the Filter in the system."); |
112 |
< |
System.exit(1); |
108 |
> |
public String toString() { |
109 |
> |
return FormatName.getName( |
110 |
> |
NAME, |
111 |
> |
getClass().getName(), |
112 |
> |
REVISION); |
113 |
|
} |
114 |
|
|
184 |
– |
//---CONSTRUCTORS--- |
185 |
– |
|
186 |
– |
//---PUBLIC METHODS--- |
187 |
– |
|
115 |
|
//---PRIVATE METHODS--- |
116 |
|
|
117 |
|
//---ACCESSOR/MUTATOR METHODS--- |
118 |
|
|
119 |
|
//---ATTRIBUTES--- |
120 |
|
|
121 |
+ |
/** |
122 |
+ |
* This holds a reference to the |
123 |
+ |
* system logger that is being used. |
124 |
+ |
*/ |
125 |
+ |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
126 |
+ |
|
127 |
+ |
/** |
128 |
+ |
* A reference to the reference manager in use |
129 |
+ |
*/ |
130 |
+ |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
131 |
+ |
|
132 |
|
//---STATIC ATTRIBUTES--- |
133 |
+ |
|
134 |
+ |
/** |
135 |
+ |
* The friendly name for this component, used by |
136 |
+ |
* all related classes. |
137 |
+ |
* This is set from the configuration. |
138 |
+ |
*/ |
139 |
+ |
public static String NAME; |
140 |
|
|
141 |
|
} |