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