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 |
> |
_logger = _refman.getLogger(); |
47 |
|
|
48 |
< |
// get our name from the command line |
49 |
< |
String ourName = ""; |
50 |
< |
if (args.length == 1) { |
51 |
< |
ourName = args[0]; |
48 |
> |
_logger.write(toString(), Logger.SYSINIT, "coming up"); |
49 |
> |
|
50 |
> |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
51 |
> |
|
52 |
> |
int UDPListenPort, TCPListenPort; |
53 |
> |
try { |
54 |
> |
UDPListenPort = Integer.parseInt(cp.getProperty(FilterMain.NAME, "Filter.UDPListenPort")); |
55 |
> |
TCPListenPort = Integer.parseInt(cp.getProperty(FilterMain.NAME, "Filter.TCPListenPort")); |
56 |
> |
} catch (PropertyNotFoundException e) { |
57 |
> |
throw new ComponentStartException("Unable to obtain requried configuration property for component: " + e); |
58 |
|
} |
59 |
< |
else { |
60 |
< |
usage(); |
59 |
> |
|
60 |
> |
// setup a queue |
61 |
> |
Queue queue = new Queue(); |
62 |
> |
|
63 |
> |
// startup a monitor on this queue |
64 |
> |
try { |
65 |
> |
// try to get the interval, if this fails, we won't start up the monitor |
66 |
> |
int queueMonitorInterval = Integer.parseInt(cp.getProperty(FilterMain.NAME, "Queue.MonitorInterval")); |
67 |
> |
String queueName = NAME + " Filter"; |
68 |
> |
queue.startMonitor(queueMonitorInterval*1000, queueName); |
69 |
> |
} catch (PropertyNotFoundException e) { |
70 |
> |
_logger.write(toString(), Logger.WARNING, "failed to find queue monitor config, disabling. " + e); |
71 |
|
} |
72 |
|
|
73 |
< |
// can't have a real toString() :) |
74 |
< |
String toString = "Filter{" + ourName + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
73 |
> |
// Start a filter thread |
74 |
> |
_logger.write(toString(), Logger.SYSINIT, "starting Filter Thread / Queue consumer"); |
75 |
> |
FilterThread filterThread = new FilterThread(queue); |
76 |
> |
filterThread.start(); |
77 |
|
|
78 |
< |
try { |
79 |
< |
ORB orb = ORB.init(args, null); |
80 |
< |
|
81 |
< |
// something to hold objects |
48 |
< |
org.omg.CORBA.Object objRef = null; |
49 |
< |
|
50 |
< |
// get the Root POA |
51 |
< |
objRef = orb.resolve_initial_references("RootPOA"); |
52 |
< |
POA poa = POAHelper.narrow(objRef); |
53 |
< |
|
54 |
< |
// get a hook to the name service |
55 |
< |
objRef = orb.resolve_initial_references("NameService"); |
56 |
< |
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef); |
57 |
< |
|
58 |
< |
// get a ref to the ConfigurationManager, Logger & the FilterManager |
59 |
< |
|
60 |
< |
objRef = ncRef.resolve(ncRef.to_name("iscream.ConfigurationManager")); |
61 |
< |
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); |
78 |
> |
// FilterServant start (for inbound child filter data) |
79 |
> |
_logger.write(toString(), Logger.DEBUG, "starting Servant to listen for downstream filters"); |
80 |
> |
FilterServant filterServant = new FilterServant(TCPListenPort, UDPListenPort, queue); |
81 |
> |
_refman.bindToOrb(filterServant, "iscream.Filter." + FilterMain.NAME); |
82 |
|
|
83 |
< |
// SETUP our Servant |
84 |
< |
|
85 |
< |
// create the FilterServant |
86 |
< |
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()); |
83 |
> |
// UDL Reader start (for inbound host data) |
84 |
> |
_logger.write(toString(), Logger.SYSINIT, "starting Filter UDP listener"); |
85 |
> |
UDPReader udpReader = new UDPReader(UDPListenPort, queue); |
86 |
> |
udpReader.start(); |
87 |
|
|
88 |
< |
// register ourselves |
89 |
< |
|
90 |
< |
// and advertise it to the naming context |
91 |
< |
objRef = poa.servant_to_reference(filterServant); |
92 |
< |
ncRef.bind(ncRef.to_name("iscream.Filter."+ourName), objRef); |
93 |
< |
|
94 |
< |
// **** END COMMENT |
95 |
< |
|
96 |
< |
// END SETUP |
97 |
< |
|
98 |
< |
|
99 |
< |
/************************************************************** |
100 |
< |
Here would be an ideal place to start another thread to do |
101 |
< |
the listening part of the Filter. Ideally it should just be |
102 |
< |
created and then run(). It may be necessary to pass some of |
103 |
< |
the following parameters into the thread by the constructor. |
104 |
< |
|
105 |
< |
Logger logger |
106 |
< |
- a reference to a Logger object |
107 |
< |
FilterManager filterManager |
108 |
< |
- a reference to the system filter manager |
109 |
< |
ConfigurationManager configManager |
110 |
< |
- a reference to the system configuration manager |
111 |
< |
Configuration myConfig |
112 |
< |
- a reference to the configuration object for this |
113 |
< |
filter instance |
114 |
< |
String ourName |
115 |
< |
- our "identifier" name |
116 |
< |
|
117 |
< |
**************************************************************/ |
118 |
< |
|
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); |
88 |
> |
// TCP Reader start (for heartbeats) |
89 |
> |
_logger.write(toString(), Logger.SYSINIT, "starting Filter TCP listener"); |
90 |
> |
TCPReader tcpReader = new TCPReader(TCPListenPort, queue); |
91 |
> |
tcpReader.start(); |
92 |
> |
|
93 |
> |
_logger.write(toString(), Logger.SYSINIT, "started"); |
94 |
> |
} |
95 |
> |
|
96 |
> |
/** |
97 |
> |
* Does a dependency check. Used mainly at startup to |
98 |
> |
* see if the required dependencies (components) are up |
99 |
> |
* and running. |
100 |
> |
* |
101 |
> |
* @return a boolean value, true if the depdencies are satisfied |
102 |
> |
*/ |
103 |
> |
public boolean depCheck() { |
104 |
> |
try { |
105 |
> |
org.omg.CORBA.Object obj; |
106 |
> |
// first check the ConfigurationManager is alive |
107 |
> |
obj = _refman.getCORBARef("iscream.ConfigurationManager"); |
108 |
> |
// then suss out our parent filter |
109 |
> |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
110 |
> |
String parentFilterName = cp.getProperty(NAME, "Filter.parentFilter"); |
111 |
> |
// finally check the parent filter is alive |
112 |
> |
obj = _refman.getCORBARef("iscream.Filter." + parentFilterName); |
113 |
> |
} catch(ComponentCORBAException e) { |
114 |
> |
_logger.write(toString(), Logger.WARNING, "Dependency Failure: "+e); |
115 |
> |
return false; |
116 |
> |
} catch(PropertyNotFoundException e) { |
117 |
> |
_logger.write(toString(), Logger.WARNING, "Unable to obtain configuration: "+e); |
118 |
> |
return false; |
119 |
|
} |
120 |
+ |
// dependency check suceeded |
121 |
+ |
return true; |
122 |
|
} |
123 |
< |
|
124 |
< |
/** |
125 |
< |
* A simple method to print the usage of this class. |
126 |
< |
* It never returns, but instead exits to the system |
127 |
< |
* with a value 1, to indicate the system did not start |
128 |
< |
* properly. |
123 |
> |
|
124 |
> |
/** |
125 |
> |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
126 |
> |
* method to provide clean logging (every class should have this). |
127 |
> |
* |
128 |
> |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
129 |
> |
* to format the toString() |
130 |
> |
* |
131 |
> |
* @return the name of this class and its CVS revision |
132 |
|
*/ |
133 |
< |
public static void usage() { |
134 |
< |
System.out.println("USAGE: java FilterMain <name>"); |
135 |
< |
System.out.println("WHERE <name>:"); |
136 |
< |
System.out.println(" The unique identifier for the Filter in the system."); |
137 |
< |
System.exit(1); |
133 |
> |
public String toString() { |
134 |
> |
return FormatName.getName( |
135 |
> |
NAME, |
136 |
> |
getClass().getName(), |
137 |
> |
REVISION); |
138 |
|
} |
139 |
|
|
186 |
– |
//---CONSTRUCTORS--- |
187 |
– |
|
188 |
– |
//---PUBLIC METHODS--- |
189 |
– |
|
140 |
|
//---PRIVATE METHODS--- |
141 |
|
|
142 |
|
//---ACCESSOR/MUTATOR METHODS--- |
143 |
|
|
144 |
|
//---ATTRIBUTES--- |
145 |
|
|
146 |
+ |
/** |
147 |
+ |
* This holds a reference to the |
148 |
+ |
* system logger that is being used. |
149 |
+ |
*/ |
150 |
+ |
private Logger _logger; |
151 |
+ |
|
152 |
+ |
/** |
153 |
+ |
* A reference to the reference manager in use |
154 |
+ |
*/ |
155 |
+ |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
156 |
+ |
|
157 |
|
//---STATIC ATTRIBUTES--- |
158 |
|
|
159 |
< |
} |
159 |
> |
/** |
160 |
> |
* The friendly name for this component, used by |
161 |
> |
* all related classes. |
162 |
> |
* This is set from the configuration. |
163 |
> |
*/ |
164 |
> |
public static String NAME; |
165 |
> |
|
166 |
> |
} |