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.filter.*; |
7 |
– |
import org.omg.CORBA.*; |
8 |
– |
import org.omg.CosNaming.*; |
9 |
– |
import org.omg.PortableServer.*; |
8 |
|
|
9 |
|
/** |
10 |
|
* A Filter Startup Class |
11 |
+ |
* A filter is an iscream component. |
12 |
|
* |
13 |
|
* @author $Author$ |
14 |
|
* @version $Id$ |
15 |
|
*/ |
16 |
< |
class FilterMain { |
16 |
> |
public class FilterMain implements uk.ac.ukc.iscream.util.Component { |
17 |
|
|
18 |
|
//---FINAL ATTRIBUTES--- |
19 |
|
|
24 |
|
|
25 |
|
//---STATIC METHODS--- |
26 |
|
|
27 |
< |
public static void main(String[] args) { |
28 |
< |
System.setProperty("org.omg.CORBA.ORBClass","jacorb.orb.ORB"); |
29 |
< |
System.setProperty("org.omg.CORBA.ORBSingletonClass","jacorb.orb.ORBSingleton"); |
30 |
< |
|
31 |
< |
// get our name from the command line |
32 |
< |
String ourName = ""; |
33 |
< |
if (args.length == 1) { |
34 |
< |
ourName = args[0]; |
35 |
< |
} |
36 |
< |
else { |
38 |
< |
usage(); |
39 |
< |
} |
40 |
< |
|
41 |
< |
// can't have a real toString() :) |
42 |
< |
String toString = "Filter{" + ourName + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
43 |
< |
|
44 |
< |
try { |
45 |
< |
ORB orb = ORB.init(args, null); |
46 |
< |
|
47 |
< |
// 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); |
27 |
> |
//---CONSTRUCTORS--- |
28 |
> |
|
29 |
> |
/** |
30 |
> |
* Constructs a Filter with the name given |
31 |
> |
* |
32 |
> |
* @param givenName the name |
33 |
> |
*/ |
34 |
> |
public FilterMain(String givenName) { |
35 |
> |
NAME = givenName; |
36 |
> |
} |
37 |
|
|
38 |
< |
// SETUP our Servant |
109 |
< |
|
110 |
< |
// create the FilterServant |
111 |
< |
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()); |
38 |
> |
//---PUBLIC METHODS--- |
39 |
|
|
40 |
< |
// register ourselves |
41 |
< |
|
42 |
< |
// and advertise it to the naming context |
43 |
< |
objRef = poa.servant_to_reference(filterServant); |
44 |
< |
ncRef.bind(ncRef.to_name("iscream.Filter."+ourName), objRef); |
45 |
< |
|
120 |
< |
// **** END COMMENT |
40 |
> |
/** |
41 |
> |
* Starts the Filter component |
42 |
> |
*/ |
43 |
> |
public void start() throws ComponentStartException { |
44 |
> |
|
45 |
> |
_logger.write(toString(), Logger.SYSINIT, "coming up"); |
46 |
|
|
47 |
< |
// END SETUP |
48 |
< |
|
47 |
> |
// configuration variables we require |
48 |
> |
int UDPListenPort = 0; |
49 |
> |
int TCPListenPort = 0; |
50 |
> |
String parentFilterName = null; |
51 |
|
|
52 |
< |
/************************************************************** |
53 |
< |
Here would be an ideal place to start another thread to do |
54 |
< |
the listening part of the Filter. Ideally it should just be |
55 |
< |
created and then run(). It may be necessary to pass some of |
56 |
< |
the following parameters into the thread by the constructor. |
57 |
< |
|
58 |
< |
Logger logger |
59 |
< |
- a reference to a Logger object |
60 |
< |
FilterManager filterManager |
61 |
< |
- a reference to the system filter manager |
62 |
< |
ConfigurationManager configManager |
63 |
< |
- a reference to the system configuration manager |
64 |
< |
Configuration myConfig |
65 |
< |
- a reference to the configuration object for this |
66 |
< |
filter instance |
67 |
< |
String ourName |
68 |
< |
- our "identifier" name |
69 |
< |
|
70 |
< |
**************************************************************/ |
71 |
< |
|
72 |
< |
logger.write(toString, Logger.SYSINIT, "starting Filter UDP listener"); |
73 |
< |
UDPReader udpReader = new UDPReader(UDPport, parentFilter, logger); |
74 |
< |
udpReader.start(); |
75 |
< |
logger.write(toString, Logger.SYSINIT, "Filter UDP listener started"); |
52 |
> |
Configuration config = _refman.getCM().getConfiguration(FilterMain.NAME); |
53 |
> |
if (config == null) { |
54 |
> |
System.err.println("CRITICAL:Unable to obtain configuration" + |
55 |
> |
"\n Advise you check the i-scream log for more information."); |
56 |
> |
_logger.write(toString(), Logger.FATAL, "ERROR - unable to obtain configuration"); |
57 |
> |
System.exit(1); |
58 |
> |
} else { |
59 |
> |
try { |
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 |
> |
System.err.println ("CRITICAL:Unable to obtain required configuration property" + |
65 |
> |
"\n Advise you check the i-scream log for more information."); |
66 |
> |
_logger.write(toString(), Logger.FATAL, "ERROR - required configuration property not present"); |
67 |
> |
System.exit(1); |
68 |
> |
} |
69 |
> |
} |
70 |
> |
_logger.write(toString(), Logger.SYSINIT, "configured"); |
71 |
> |
|
72 |
> |
// get parent |
73 |
> |
Filter parentFilter = FilterHelper.narrow(_refman.getCORBARef("iscream.Filter." + parentFilterName)); |
74 |
> |
|
75 |
> |
// FilterServant start (for inbound child filter data) |
76 |
> |
_logger.write(toString(), Logger.DEBUG, "starting Filter Child -> Parent link for upstream parent - " + parentFilterName); |
77 |
> |
FilterServant filterServant = new FilterServant(parentFilter, TCPListenPort, UDPListenPort); |
78 |
> |
_refman.bindToOrb(filterServant, "iscream.Filter." + FilterMain.NAME); |
79 |
|
|
80 |
< |
logger.write(toString, Logger.SYSINIT, "starting Filter TCP listener"); |
81 |
< |
TCPReader tcpReader = new TCPReader(logger, configManager, TCPport, parentFilter); |
82 |
< |
tcpReader.start(); |
83 |
< |
logger.write(toString, Logger.SYSINIT, "Filter TCP listener started"); |
80 |
> |
// UDL Reader start (for inbound host data) |
81 |
> |
_logger.write(toString(), Logger.SYSINIT, "starting Filter UDP listener"); |
82 |
> |
UDPReader udpReader = new UDPReader(UDPListenPort, parentFilter); |
83 |
> |
udpReader.start(); |
84 |
|
|
85 |
< |
// TEST |
86 |
< |
//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>"); |
87 |
< |
|
88 |
< |
// start the POA off |
89 |
< |
poa.the_POAManager().activate(); |
90 |
< |
|
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 |
< |
} |
85 |
> |
// TCP Reader start (for heartbeats) |
86 |
> |
_logger.write(toString(), Logger.SYSINIT, "starting Filter TCP listener"); |
87 |
> |
TCPReader tcpReader = new TCPReader(TCPListenPort, parentFilter); |
88 |
> |
tcpReader.start(); |
89 |
> |
|
90 |
> |
_logger.write(toString(), Logger.SYSINIT, "started"); |
91 |
|
} |
92 |
|
|
93 |
< |
/** |
94 |
< |
* A simple method to print the usage of this class. |
95 |
< |
* It never returns, but instead exits to the system |
96 |
< |
* with a value 1, to indicate the system did not start |
97 |
< |
* properly. |
93 |
> |
/** |
94 |
> |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
95 |
> |
* method to provide clean logging (every class should have this). |
96 |
> |
* |
97 |
> |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
98 |
> |
* to format the toString() |
99 |
> |
* |
100 |
> |
* @return the name of this class and its CVS revision |
101 |
|
*/ |
102 |
< |
public static void usage() { |
103 |
< |
System.out.println("USAGE: java FilterMain <name>"); |
104 |
< |
System.out.println("WHERE <name>:"); |
105 |
< |
System.out.println(" The unique identifier for the Filter in the system."); |
106 |
< |
System.exit(1); |
102 |
> |
public String toString() { |
103 |
> |
return FormatName.getName( |
104 |
> |
_name, |
105 |
> |
getClass().getName(), |
106 |
> |
REVISION); |
107 |
|
} |
108 |
|
|
186 |
– |
//---CONSTRUCTORS--- |
187 |
– |
|
188 |
– |
//---PUBLIC METHODS--- |
189 |
– |
|
109 |
|
//---PRIVATE METHODS--- |
110 |
|
|
111 |
|
//---ACCESSOR/MUTATOR METHODS--- |
112 |
|
|
113 |
|
//---ATTRIBUTES--- |
114 |
|
|
115 |
+ |
/** |
116 |
+ |
* This is the friendly identifier of the |
117 |
+ |
* component this class is running in. |
118 |
+ |
* eg, a Filter may be called "filter1", |
119 |
+ |
* If this class does not have an owning |
120 |
+ |
* component, a name from the configuration |
121 |
+ |
* can be placed here. This name could also |
122 |
+ |
* be changed to null for utility classes. |
123 |
+ |
*/ |
124 |
+ |
private String _name = FilterMain.NAME; |
125 |
+ |
|
126 |
+ |
/** |
127 |
+ |
* This holds a reference to the |
128 |
+ |
* system logger that is being used. |
129 |
+ |
*/ |
130 |
+ |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
131 |
+ |
|
132 |
+ |
/** |
133 |
+ |
* A reference to the reference manager in use |
134 |
+ |
*/ |
135 |
+ |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
136 |
+ |
|
137 |
|
//---STATIC ATTRIBUTES--- |
138 |
+ |
|
139 |
+ |
/** |
140 |
+ |
* The friendly name for this component, used by |
141 |
+ |
* all related classes. |
142 |
+ |
* This is set from the configuration. |
143 |
+ |
*/ |
144 |
+ |
public static String NAME; |
145 |
|
|
146 |
|
} |