大学の課題では、洪水センサーを管理し、水位が高くなった場合に一度警告する方法を作成する必要があります。そこで私の考えは、センサー サーバーの arrayList を保持し、そこからそれらを管理する LMS (Local Monitoring Station) を作成することでした。
そこで、それらを追加する実装をテストするテスト クラスを作成しました。
public class testClass
{
static public void main(String[] args)
{
ArrayList<FloodSensorServer> sensors = new ArrayList<FloodSensorServer>();
sensors.add(new FloodSensorServer("Sensor 1"));
sensors.add(new FloodSensorServer("Sensor 2"));
sensors.add(new FloodSensorServer("Sensor 3"));
sensors.add(new FloodSensorServer("Sensor 4"));
}
}
論理的には、これはうまくいくかもしれないと思っていましたが、実行してみると、最初の .add が実行されてから停止したことがわかりました。その後、このコード行が原因で停止していることに気付きました。
orb.run();
public class FloorSensorServer
{
Sensor counter;
String sensorName;
public FloorSensorServer(String sensorName)
{
this.sensorName = sensorName;
createServer();
}
public void createServer()
{
try {
// Initialize the ORB
System.out.println("Creating sensor: " + sensorName);
String[] newArgs = {"-ORBInitialPort", "1050"};
ORB orb = ORB.init(newArgs, null);
// get reference to rootpoa & activate the POAManager
POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
// Create the Count servant object
FloorSensorImpl sensor = new FloorSensorImpl(sensorName);
// get object reference from the servant
org.omg.CORBA.Object ref = rootpoa.servant_to_reference(sensor);
Sensor cref = SensorHelper.narrow(ref);
// Get a reference to the Naming service
org.omg.CORBA.Object nameServiceObj =
orb.resolve_initial_references ("NameService");
if (nameServiceObj == null) {
System.out.println("nameServiceObj = null");
return;
}
// Use NamingContextExt which is part of the Interoperable
// Naming Service (INS) specification.
NamingContextExt nameService = NamingContextExtHelper.narrow(nameServiceObj);
if (nameService == null) {
System.out.println("nameService = null");
return;
}
NameComponent[] countName = nameService.to_name(sensorName);
nameService.rebind(countName, cref);
// wait for invocations from clients
orb.run();
} catch(Exception e) {
System.err.println(e);
}
}
}
質問:
このような状況にどのように対処しますか?それとも、私はこれを完全に間違った方法で行っていますか?