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.filter.*; |
6 |
– |
import org.omg.CORBA.*; |
7 |
– |
import org.omg.CosNaming.*; |
8 |
– |
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 { |
37 |
< |
usage(); |
38 |
< |
} |
39 |
< |
|
40 |
< |
// can't have a real toString() :) |
41 |
< |
String toString = "Filter{" + ourName + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
42 |
< |
|
43 |
< |
try { |
44 |
< |
ORB orb = ORB.init(args, null); |
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 |
> |
//---PUBLIC METHODS--- |
39 |
> |
|
40 |
> |
/** |
41 |
> |
* Starts the Filter component |
42 |
> |
*/ |
43 |
> |
public void start() throws ComponentStartException { |
44 |
> |
|
45 |
> |
_logger.write(toString(), Logger.SYSINIT, "coming up"); |
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 |
< |
//objRef = ncRef.resolve(ncRef.to_name("iscream.FilterManager")); |
66 |
< |
//FilterManager filterManager = FilterManagerHelper.narrow(objRef); |
67 |
< |
|
68 |
< |
logger.write(toString, Logger.SYSINIT, "coming up"); |
69 |
< |
|
70 |
< |
|
71 |
< |
// **** THIS SECTION WILL NEED CHANGING TO GET RELEVANT CONFIG |
72 |
< |
// **** Please ignore this block of code, it's just "copy/paste" :) |
73 |
< |
|
74 |
< |
// get the config |
75 |
< |
Configuration myConfig = configManager.getConfiguration(ourName); |
76 |
< |
|
77 |
< |
// read some config here |
78 |
< |
int port = 0; |
79 |
< |
|
80 |
< |
// did we? |
81 |
< |
if (myConfig == null) { |
82 |
< |
System.out.println("Failed: is it there?, can you read it?"); |
47 |
> |
// configuration variables we require |
48 |
> |
int UDPListenPort = 0; |
49 |
> |
int TCPListenPort = 0; |
50 |
> |
String parentFilterName = null; |
51 |
> |
|
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); |
84 |
– |
} else { |
85 |
– |
|
86 |
– |
// get the property |
87 |
– |
try { |
88 |
– |
//QUERY HERE.... ??? |
89 |
– |
port = new Integer(myConfig.getProperty("Filter.listenPort")).intValue(); |
90 |
– |
} catch (org.omg.CORBA.MARSHAL e) { |
91 |
– |
System.out.println("Caught org.omg.CORBA.MARSHAL, must be a null we got back"); |
92 |
– |
//System.exit(1); |
93 |
– |
} |
68 |
|
} |
95 |
– |
|
96 |
– |
logger.write(toString, Logger.SYSINIT, "configured"); |
97 |
– |
|
98 |
– |
// **** END COMMENT |
99 |
– |
|
100 |
– |
// SETUP our Servant |
101 |
– |
|
102 |
– |
// create the FilterServant |
103 |
– |
FilterServant filterServant = new FilterServant(logger, ourName); |
104 |
– |
|
105 |
– |
// and advertise it to the naming context |
106 |
– |
objRef = poa.servant_to_reference(filterServant); |
107 |
– |
ncRef.bind(ncRef.to_name("iscream.Filter."+ourName), objRef); |
108 |
– |
|
109 |
– |
// END SETUP |
110 |
– |
|
111 |
– |
/************************************************************** |
112 |
– |
Here would be an ideal place to start another thread to do |
113 |
– |
the listening part of the Filter. Ideally it should just be |
114 |
– |
created and then run(). It may be necessary to pass some of |
115 |
– |
the following parameters into the thread by the constructor. |
116 |
– |
|
117 |
– |
Logger logger |
118 |
– |
- a reference to a Logger object |
119 |
– |
FilterManager filterManager |
120 |
– |
- a reference to the system filter manager |
121 |
– |
ConfigurationManager configManager |
122 |
– |
- a reference to the system configuration manager |
123 |
– |
Configuration myConfig |
124 |
– |
- a reference to the configuration object for this |
125 |
– |
filter instance |
126 |
– |
String ourName |
127 |
– |
- our "identifier" name |
128 |
– |
|
129 |
– |
**************************************************************/ |
130 |
– |
|
131 |
– |
logger.write(toString, Logger.SYSINIT, "starting Filter UDP listener"); |
132 |
– |
//UDPReader udpReader = new UDPReader(port, logger); |
133 |
– |
//udpReader.start(); |
134 |
– |
logger.write(toString, Logger.SYSINIT, "Filter UDP listener started"); |
135 |
– |
|
136 |
– |
// **** INITIAL FILTER MANAGER COMMUNICATIONS HERE |
137 |
– |
|
138 |
– |
// get a root (CHANGE to use FilterManager) |
139 |
– |
objRef = ncRef.resolve(ncRef.to_name("iscream.Filter.root1")); |
140 |
– |
Filter root = FilterHelper.narrow(objRef); |
141 |
– |
|
142 |
– |
// register ourselves |
143 |
– |
|
144 |
– |
// **** END COMMENT |
145 |
– |
|
146 |
– |
// TEST |
147 |
– |
root.receiveXML("This is just a debugging test, we ("+ourName+") are live on port: "+port); |
148 |
– |
|
149 |
– |
// start the POA off |
150 |
– |
poa.the_POAManager().activate(); |
151 |
– |
|
152 |
– |
logger.write(toString, Logger.SYSINIT, "started"); |
153 |
– |
|
154 |
– |
// now we are running, we just need to serve |
155 |
– |
// so we ask the orb to block for us until it has finished |
156 |
– |
orb.run(); |
157 |
– |
|
158 |
– |
} catch (Exception e) { |
159 |
– |
System.err.println("FILTER ERROR: " + e); |
160 |
– |
e.printStackTrace(System.out); |
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 |
+ |
// 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 |
+ |
// 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 Filter <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 |
|
|
177 |
– |
//---CONSTRUCTORS--- |
178 |
– |
|
179 |
– |
//---PUBLIC METHODS--- |
180 |
– |
|
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 |
< |
} |
146 |
> |
} |