当前位置 |首页 > 作业代写 > Essay代写栏目 >
分享这个代写网站给同学或者朋友吧!


 

Designing and Implementing Environment Monitoring System with CORBA


Table of contents

1. System Design 4

1.1 Design Overview 4

1.2 Main Components 5

1.3 Use Cases 5

1.4 Class Design 8

2. IDL Specification 10

3. Test and Evidence 12

3.1 Test: Start EnvServer 13

3.2 Test: Start One RegionalServer and Connect to EnvSever 13

3.3 Test: Start Another RegionalServer and Connect to EnvSever 14

3.4 Test: Start One MonitorServer and Connect to RegionalServer 14

3.5 Test: Start Another MonitorServer and Connect to RegionalServer 15

3.6 Test: Start AgencyServer 15

3.7 Test: Two MonitorServers and Connect to Two RegionalServers 16

3.8 Test: MonitorServer Send Readings to RegionalServer 16

3.9 Test: EnvServer Receives Readings from RegionalServers 17

3.10 Test: EnvServer Raises Alarm 18

4. Critique 19

4.1 Comparisons of Architectures 19

4.2 Alternative Solution 20

5. Source Codes 22

5.1 EnviromentalCentrelmpl 22

5.2 RegionalCentreImpl 24

5.3 MonitorImpl 27

5.4 AgencyImpl 29

5.5 EnvServer 30

5.6 RegionServer 34

5.7 MonitorServer 38

5.8 AgencyServer 43

6. References 46

 

 

 


1. System Design

1.1 Design Overview

As the company, i.e., Environmental Monitor asked for, we are going to design and implement an environment monitoring system with CORBA technology. In this proof of concept project, we are going to use following technologies for implementation.

1) CORBA: The Common Object Request Broker Architecture (CORBA) is a standard defined by the Object Management Group (OMG) designed to facilitate the communication of systems that are deployed on diverse platforms. CORBA enables collaboration between systems on different operating systems, programming languages, and computing hardware.

As you see, CORBA is a standard, which is easy to follow and supported by lots of different programming languages, which we will follow in this project for design and implementation.

2) Java Programming Language: Java is a general-purpose computer-programming language that is concurrent, class-based, object-oriented.

In this project, we will use Java language for CORBA implementation of the monitoring system. Because Java language is very popular, easy to learn and maintain, and the most important it has a very good support for CORBA.

3) Swing: Swing is a GUI widget toolkit for Java. Swing provides a look and feel that emulates the look and feel of several platforms, and also supports a pluggable look and feel that allows applications to have a look and feel unrelated to the underlying platform.

In this project, we will use Swing to provide a very simple user interface for interactions. Because Swing is actually de facto standard for implementing GUI in Java programming language, and it has a very concise look and feel.

1.2 Main Components

Based on the requirements of this project, and using OOP (object-oriented programming) design principles, we can extract and abstract several objects for this project.

 

This project mainly has following components, which will be further explained in the following sub sections.

1) EnvironmentalCentre: A singleton server pulling monitors data from RegionalCentres.

2) RegionalCentre: Servers pulling monitors data from Monitors, and raise alarms to EnvironmentalCentre.

3) Monitor: Small servers, which are used to monitor the environment status and collect environmental data, read current monitoring value and raise alarms to RegionalCentre.

4) Agency: Any entity interested in some regional center's data. It can be a person, an organization, and so on.

1.3 Use Cases

In this section, we will illustrate some UML diagrams, including use case diagrams and sequence diagrams to analyze the requirements in depth, which will help us to design and implement the system.

 

Fig 1.1 demonstrate how the Agency and Operator interacts with our Environment Monitor System. In general, the Operator can maintain the environmental centre and the regional centre (e.g., add monitor, turn on/off monitor, and so on); the Agency can raise alarms.

image.png 

Figure 1.1. Use case diagram

Fig 1.2 shows how the messages are passed between Monitor and Regional Centre. If there are dangerous readings captured by the Monitor, it will raise alarm and notify Regional Centre.

image.png 

Figure 1.2. Sequence diagram 1

Figure 1.3 demonstrates how the Regional Centre communicates with the Environmental Centre. Dangerous readings will be further passed to the Environmental Centre from the Regional Centre.

image.png 

Figure 1.3. Sequence diagram 2

 

Fig 1.4 shows the overall communications between different components.

 

image.png 

Figure 1.4. Sequence diagram 3

1.4 Class Design

image.png 

Figure 1.5. Class diagram

Based on the before mentioned discussion, in this sub section we will present the detail design of this project.

In CORBA, we will have IDL specification (see next section for detail) which defines the interfaces, standing for the functionalities which will be provided by the system. Based on these interfaces, we will have several Java classes to implement the interfaces (i.e., class names are ended with “impl”). Each method in these implementation classes will be responsible for a specific function.

At the same time, we will have several classes for acting as CORBA servers (i.e., class names are ended with “server”).  All these classes have “main” methods, which are the initialize methods. And all these classes have references to the implementation classes.

 

Fig 1.5 shows the class diagram in this project. For details,

1) EnvironmentalCentreImpl: The implementation of the component EnvironmentalCentre described in section 1.2.

2) RegionalCentreImpl: The implementation of the component RegionalCentre described in section 1.2.

3) MonitorImpl: The implementation of the component Monitor described in section 1.2.

4) AgencyImpl: The implementation of the component Agency described in section 1.2.

5) EnvServer: CORBA server for Environmental Centre.

6) RegionServer: CORBA server for Regional Centre.

7) MonitorServer: CORBA server for Monitor.

8) AgencyServer: CORBA server for Agency.


2. IDL Specification

For IDL specification, we are following the requirements and putting all the IDL specifications into following file. As you can see, there are EnvironmentalCentre, RegionalCentre, and Monitor interfaces, as well as the struct Reading.

module EnvMon {
   struct Reading {
      long date, time, value;
      string monitor;
   };
   interface Monitor {
      readonly attribute string name;
      readonly attribute string location;
      Reading get_reading();
      void turn_on();
      void turn_off();
      void reset();
   };
   typedef sequence<Reading> LogOfAlarmReadings;
   typedef sequence<Reading> SetOfReadings;
   interface RegionalCentre {
      readonly attribute string name;
      readonly attribute LogOfAlarmReadings log;
      void raise_alarm(in Reading alarm);
      SetOfReadings get_readings();
      boolean add_monitor(in string name, in string location);
   };
   interface EnvironmentalCentre {
      void raise_alarm(in Reading alarm);
      void register_agency(in string who, in string contact, in string region);
      void register_regional_centre(in string name);
      SetOfReadings get_readings();
   };
   interface Agency {
      readonly attribute string name, contact, region;
      void raise_alarm(in Reading alarm);
   };
};

 

 


3. Test and Evidence

In this section, we will use following tables with screenshots to demonstrate the tests have been done for this project.

 

The following main features are implemented in this project.

NO

Feature

1

Monitor can register itself with a Regional Centre upon initial activation.

Monitor can be switched on/switched off, and return current sensor value.

Monitor can send alarms to Regional Centre if the sensor value is very high.

2

Regional Centre can receive requests to register Monitoring Stations and maintains a list Monitors.

Regional Centre can receive alerts from connected Monitoring Stations. and maintains a log of these alerts

Regional Centre can trigger an alarm at the Environmental Centre when two alarms happen within a specified time frame.

Regional Centre can maintain the logs.

3

Environmental Centre can receive confirmed alarms from Regional Centres.

Environmental Centre can allow agencies to register for notifications in case of alarms.

Environmental Centre maintains a list of connected Regional Centres

Environmental Centre can poll all Regional Centres upon request and displays the results of readings returned, highlighting readings of concern.

 

3.1 Test: Start EnvServer

Expect Result

EnvServer can start successfully

Steps

Run command: java EnvServer

Test Result

Passed

Evidence

image.png 

 

3.2 Test: Start One RegionalServer and Connect to EnvSever

Expect Result

First RegionalServer can start and connect EnvServer successfully

Steps

Run command: java RegionalServer region_server1

Test Result

Passed

Evidence

image.png 

 

3.3 Test: Start Another RegionalServer and Connect to EnvSever

Expect Result

Second RegionalServer can start and connect EnvServer successfully

Steps

Run command: java RegionalServer region_server2

Test Result

Passed

Evidence

image.png 

 

3.4 Test: Start One MonitorServer and Connect to RegionalServer

Expect Result

First MonitorServer can start successfully

Steps

Run command: java MonitorServer monitor1 location1

Test Result

Passed

Evidence

image.png 

 

3.5 Test: Start Another MonitorServer and Connect to RegionalServer

Expect Result

Second MonitorServer can start successfully

Steps

Run command: java MonitorServer monitor2 location2

Test Result

Passed

Evidence

image.png 

 

3.6 Test: Start AgencyServer

Expect Result

AgencyServer can start successfully

Steps

Run command: java AgencyServer agency_server 123456 location1

Test Result

Passed

Evidence

image.png 

 

3.7 Test: Two MonitorServers and Connect to Two RegionalServers

Expect Result

MonitorServers can connect RegionalServers successfully

Steps

1. Start two RegionalServers as mentioned earlier

2. Start two MonitorServers as mentioned earlier

3. For each MonitorServer, input the RegionalServer which the MonitorServer wants to connect, and click “Connect”button

Test Result

Passed

Evidence

1538231007669101.png 

 

3.8 Test: MonitorServer Send Readings to RegionalServer

Expect Result

RegionalServer can receive readings from MonitorServer

Steps

1. Start one RegionalServer as mentioned earlier

2. Start one MonitorServer as mentioned earlier

3. MonitorServer connects to RegionalServer as mentioned earlier

4. MonitorServer uses the scroll bar to send readings to ReginonalServer

Test Result

Passed

Evidence

image.png 

 

3.9 Test: EnvServer Receives Readings from RegionalServers

Expect Result

EnvServer can receive readings from RegionalServer

Steps

1. Start one RegionalServer as mentioned earlier

2. Start one MonitorServer as mentioned earlier

3. MonitorServer connects to RegionalServer as mentioned earlier

4. MonitorServer uses the scroll bar to send readings to ReginonalServer

Test Result

Passed

Evidence

image.png 

 

3.10 Test: EnvServer Raises Alarm 

Expect Result

EnvServer will raise alarm when two MonitorServers send alarms in a short period

Steps

1. Start one RegionalServer as mentioned earlier

2. Start one MonitorServer as mentioned earlier

3. MonitorServers connects to RegionalServers as mentioned earlier

4. MonitorServers uses the scroll bar to send alarms (i.e., large readings) to ReginonalServer

Test Result

Passed

Evidence

image.png 

 

4. Critique

4.1 Comparisons of Architectures

Let's think about why we need CORBA/RPC/Web Service: we want to make a more complicated communication between applications distributed on different servers through the Internet. So at first we have sockets. Servers and clients written in different languages could talk to each other by exchanging raw data, which might be too simple to hold a big application. Thus protocols are defined to help working methodically.

 

The first distributed application model is RPC (Remote Procedure Call protocol), which is procedure-oriented. It is based on C/S architecture's calling model. Clients make requests of calling some procedure to servers and waiting for it. Its disadvantage is too simple to support complicated environment like OO. What's worse, it doesn't support asynchronous calling. However, its performance is usually very good on heavily distributed applications.

 

CORBA is such a protocol targeting on components and OO. It's well designed and can be reflected to IDL, ORB and related services. In fact, DCOM comes first on object-oriented remote calling. If the components of the application are OO, then CORBA will be a good choice. Just like this environment monitor, since all of its servers are written in Java, there's no reason on not choosing CORBA as its communication technology.

 

CORBA comes out when Web is not very popular. But for now, Web Services/Web Sockets might be a better choice where applications are running on browsers. The communication of web services is based on HTTP, which uses the 80 port, which is default opened. And there are many technologies to use for both coarser-grained and fine-grained remote calling/messaging. CORBA has been deployed in a wide range of industries including aerospace and defense, banking and finance, chemical/petrochemical, consulting, education, electronic commerce, Government, healthcare and insurance, human resources, manufacturing, publishing and multimedia, real estate, research, retail, software/hardware, telecommunications, transportation, and utilities.

4.2 Alternative Solution

There are at least two requirements for the given project:

1) Real-time showing monitoring data;

2) Raising alarms of dangerous data to clients;

 

In fact, the second one can be integrated into the first one: clients deal with every data pulling from monitors. Therefore, there is only one thing to do. Deploy a simple and small web server in every monitor.

 

Regional centers pull data from these monitors by making web requests (which can be visual on web browsers) periodically. Environmental Centers pulls data from these regional centers as you know. There might be more than one Environmental Center distributed at places and they just sync data like CDNs.

 

The Environmental Center should maintain a message queue for each agency. It replies all of the messages whenever the agency makes a request for getting alarms from the center.

 

The solution above only uses simple web requests (like POST) to pull data from servers. Therefore, it will be very easy to implement one.

 

Aspect

CORBA

Web Service

Advantages

Object-Oriented, invoking remote objects like a local one.

No problem of firewall, easy to implement and deploy. Agencies only need a web browser (almost nothing).

Disadvantages

Complicated.

Communication consumes too much resources.

Table 4.1 Comparison between CORBA and Web Service for this project.

 

 


5. Source Codes

5.1 EnviromentalCentrelmpl

package impl;

import EnvMon.*;
import org.omg.CORBA.ORB;
import org.omg.CORBA.Object;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;

import java.util.*;
import java.util.stream.Collectors;

public class EnvironmentalCentreImpl extends EnvironmentalCentrePOA {
    public Set<Agency> agencies = new HashSet<Agency>();
    public Set<RegionalCentre> regions = new HashSet<RegionalCentre>();

    public EnvironmentalCentreImpl() {
        super();
    }

    @Override
    public void raise_alarm(Reading alarm) {
        System.out.println("EnvironmentalCentre raises an alarm: " + alarm);
    }

    @Override
    public void register_agency(String name, String contact, String region) {
        String[] args = {"-ORBInitialPort", "1050"};
        try {
            ORB orb = ORB.init(args, null);
            Object objref = orb.resolve_initial_references("NameService");
            NamingContextExt ncref = NamingContextExtHelper.narrow(objref);
            Agency agency = AgencyHelper.narrow(ncref.resolve_str("Agency" + name));
            agencies.add(agency);
            System.out.println("EnvironmentalCentre << Agency " + name);
        } catch (Exception e) {
            System.out.println("EnvironmentalCentre << Agency " + name + " failed");
        }
    }

    @Override
    public void register_regional_centre(String name) {
        String[] args = {"-ORBInitialPort", "1050"};
        try {
            ORB orb = ORB.init(args, null);
            Object objref = orb.resolve_initial_references("NameService");
            NamingContextExt ncref = NamingContextExtHelper.narrow(objref);
            RegionalCentre region = RegionalCentreHelper.narrow(ncref.resolve_str("Region" + name));
            regions.add(region);
            System.out.println("EnvironmentalCentre << RegionalCentre " + name);
        } catch (Exception e) {
            System.out.println("EnvironmentalCentre << RegionalCentre " + name + " failed");
        }
    }

    @Override
    public Reading[] get_readings() {
        List<Reading> list = new ArrayList<Reading>();
        Set<RegionalCentre> sm = new HashSet<RegionalCentre>();
        for (RegionalCentre region : regions) {
            try {
                list.addAll(Arrays.stream(region.get_readings()).collect(Collectors.toList()));
            } catch (Exception x) {
                sm.add(region);
            }
        }
        regions.removeAll(sm);
        return list.toArray(new Reading[list.size()]);
    }
}

 

5.2 RegionalCentreImpl

package impl;

import EnvMon.Monitor;
import EnvMon.MonitorHelper;
import EnvMon.Reading;
import EnvMon.RegionalCentrePOA;
import org.omg.CORBA.ORB;
import org.omg.CORBA.Object;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;

import java.util.*;

public class RegionalCentreImpl extends RegionalCentrePOA {
    public String name = null;
    public Queue<Reading> log = new LinkedList<Reading>();
    public Set<Monitor> monitors = new HashSet<Monitor>();

    public RegionalCentreImpl(String name) {
        super();
        this.name = name;
    }

    @Override
    public String name() {
        return name;
    }

    @Override
    public Reading[] log() {
        return log.toArray(new Reading[log.size()]);
    }

    @Override
    public void raise_alarm(Reading alarm) {
        log.add(alarm);
        System.out.println("RegionalCentre [" + name + "] raises an alarm: " + alarm);
    }

    @Override
    public Reading[] get_readings() {
        ArrayList<Reading> list = new ArrayList<Reading>();
        Set<Monitor> sm = new HashSet<Monitor>();
        for (Monitor monitor : monitors) {
            try {
                list.add(monitor.get_reading());
            } catch (Exception x) {
                sm.add(monitor);
            }
        }
        monitors.removeAll(sm);
        return list.toArray(new Reading[list.size()]);
    }

    @Override
    public boolean add_monitor(String name, String location) {
        String[] args = {"-ORBInitialPort", "1050"};
        try {
            ORB orb = ORB.init(args, null);
            Object objref = orb.resolve_initial_references("NameService");
            NamingContextExt ncref = NamingContextExtHelper.narrow(objref);
            Monitor monitor = MonitorHelper.narrow(ncref.resolve_str("Monitor" + name));
            if (monitors.stream().anyMatch(m -> m.location().equals(monitor.location()))) return false;
            monitor.turn_on();
            monitors.add(monitor);
            System.out.println("Region " + this.name + " << Monitor " + name);
            return true;
        } catch (Exception e) {
            System.out.println("Region " + this.name + " << Monitor " + name + " failed");
            return false;
        }
    }
}

 

5.3 MonitorImpl

package impl;

import EnvMon.MonitorPOA;
import EnvMon.Reading;

import java.util.Calendar;
import java.util.Date;

public class MonitorImpl extends MonitorPOA {
    private String name = null, location = null;
    private boolean power = false;
    public int value = 0;

    public MonitorImpl(String name, String location) {
        super();
        this.name = name;
        this.location = location;
    }

    @Override
    public String name() {
        return name;
    }

    @Override
    public String location() {
        return location;
    }

    @Override
    public Reading get_reading() {
        if (!power) return null;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int date = calendar.get(Calendar.DAY_OF_YEAR);
        int time = calendar.get(Calendar.HOUR) * 60 + calendar.get(Calendar.MINUTE);
        return new Reading(date, time, value, name);
    }

    @Override
    public void turn_on() {
        power = true;
        System.out.println("Monitor [" + name + "] (" + location + ") is turned on");
    }

    @Override
    public void turn_off() {
        power = false;
        System.out.println("Monitor [" + name + "] (" + location + ") is turned off");
    }

    @Override
    public void reset() {
        power = false;
        value = 0;
        System.out.println("Monitor [" + name + "] (" + location + ") is reset");
    }
}

 

5.4 AgencyImpl

package impl;

import EnvMon.AgencyPOA;
import EnvMon.Reading;

public class AgencyImpl extends AgencyPOA {
    private String name = null, contact = null, region = null;

    public AgencyImpl(String name, String contact, String region) {
        super();
        this.name = name;
        this.contact = contact;
        this.region = region;
    }

    @Override
    public String name() {
        return name;
    }

    @Override
    public String contact() {
        return contact;
    }

    @Override
    public String region() {
        return region;
    }

    @Override
    public void raise_alarm(Reading alarm) {
        System.out.println("Agency [" + name + "] (" + contact + ") raises an alarm: " + alarm);
    }
}

 

5.5 EnvServer

package server;

import EnvMon.*;
import impl.EnvironmentalCentreImpl;
import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CORBA.Object;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.omg.PortableServer.POAPackage.ServantNotActive;
import org.omg.PortableServer.POAPackage.WrongPolicy;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import javax.swing.text.Highlighter.HighlightPainter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Set;

public class EnvServer {
    public static void main(String[] args) throws InvalidName, AdapterInactive, ServantNotActive, WrongPolicy,
            org.omg.CosNaming.NamingContextPackage.InvalidName, NotFound, CannotProceed {
        EnvironmentalCentreImpl impl = new EnvironmentalCentreImpl();
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Env");
                JLabel label = new JLabel("EnvironmentalCentre");
                JTextArea area = new JTextArea();
                Highlighter highlighter = area.getHighlighter();
                HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.pink);
                area.setEditable(false);
                ActionListener task = new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        // Polls all Regional Centres upon request and displays the results of readings
                        // returned, highlighting readings of concern
                        StringBuilder sb = new StringBuilder();
                        Set<Integer> highlights = new HashSet<Integer>();
                        Integer len = null;
                        for (Reading reading : impl.get_readings()) {
                            if (reading.value > 90) highlights.add(sb.length());
                            sb.append(reading.toString() + "\n");
                            if (len == null) len = sb.length();
                        }
                        area.setText(sb.toString());
                        for (Integer pos : highlights) {
                            try {
                                highlighter.addHighlight(pos, pos + len, painter);
                            } catch (BadLocationException e1) {
                                e1.printStackTrace();
                            }
                        }
                        // Allows agencies (e.g. the Environment Agency, local councils, local pressure
                        // groups, etc.) to register for notifications in case of alarms
                        for (RegionalCentre region : impl.regions)
                            for (Reading reading : region.get_readings())
                                if (reading.value > 90) for (Agency agency : impl.agencies)
                                    if (agency.region().equals(region.name())) agency.raise_alarm(reading);
                    }
                };
                Timer timer = new Timer(10000, task);
                timer.start();
                label.setBorder(new EmptyBorder(8, 8, 8, 8));
                frame.getContentPane().setLayout(new BorderLayout());
                frame.getContentPane().add(BorderLayout.NORTH, label);
                frame.getContentPane().add(BorderLayout.CENTER, area);
                frame.setSize(300, 200);
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
        String[] init = {"-ORBInitialPort", "1050"};
        ORB orb = ORB.init(init, null);
        Object obj = orb.resolve_initial_references("RootPOA");
        POA rootpoa = POAHelper.narrow(obj);
        rootpoa.the_POAManager().activate();
        Object ref = rootpoa.servant_to_reference(impl);
        EnvironmentalCentre env = EnvironmentalCentreHelper.narrow(ref);
        Object objref = orb.resolve_initial_references("NameService");
        NamingContextExt ncref = NamingContextExtHelper.narrow(objref);
        NameComponent path[] = ncref.to_name("Env");
        ncref.rebind(path, env);
        System.out.println("EnvironmentalCentre ready.");
        orb.run();
    }
}

 

5.6 RegionServer

package server;

import EnvMon.*;
import impl.RegionalCentreImpl;
import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CORBA.Object;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.omg.PortableServer.POAPackage.ServantNotActive;
import org.omg.PortableServer.POAPackage.WrongPolicy;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class RegionServer {
    private static String name = null;
    private static EnvironmentalCentre env = null;

    public static void main(String[] args) throws InvalidName, AdapterInactive, ServantNotActive, WrongPolicy,
            org.omg.CosNaming.NamingContextPackage.InvalidName, NotFound, CannotProceed {
        if (args.length < 1) {
            System.out.println("java RegionServer <name>");
            return;
        }
        name = args[0];
        RegionalCentreImpl impl = new RegionalCentreImpl(name);
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame(name);
                JLabel label = new JLabel("Region: " + name + " | connect(env) " + (connect_env() ? "ok" : "failed"));
                JTextArea area = new JTextArea();
                area.setEditable(false);
                ActionListener task = new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String readings = Arrays.stream(impl.get_readings()).map(a -> a.toString())
                                .collect(Collectors.joining("\n"));
                        String log = impl.log.stream().map(a -> a.toString()).collect(Collectors.joining("\n"));
                        area.setText(readings + "\nvvvv Log vvvv\n" + log);
                        // Triggers an alarm at the Environmental Centre when two alarms happen within a
                        // specified time frame
                        if (env != null) {
                            Date date = new Date();
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(date);
                            int time = calendar.get(Calendar.HOUR) * 60 + calendar.get(Calendar.MINUTE) - 1;
                            Stream<Reading> alarms = impl.log.stream().filter(a -> a.time > time);
                            if (alarms.count() > 1) {
                                try {
                                    alarms.forEach(a -> env.raise_alarm(a));
                                } catch (Exception ex) {
                                    env = null;
                                }
                                label.setText("Region: " + name + " | connect(env) failed");
                            }
                        }
                    }
                };
                label.setBorder(new EmptyBorder(8, 8, 8, 8));
                frame.getContentPane().setLayout(new BorderLayout());
                frame.getContentPane().add(BorderLayout.NORTH, label);
                frame.getContentPane().add(BorderLayout.CENTER, area);
                frame.setSize(300, 200);
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                Timer timer = new Timer(10000, task);
                timer.start();
            }
        });
        String[] init = {"-ORBInitialPort", "1050"};
        ORB orb = ORB.init(init, null);
        Object obj = orb.resolve_initial_references("RootPOA");
        POA rootpoa = POAHelper.narrow(obj);
        rootpoa.the_POAManager().activate();
        Object ref = rootpoa.servant_to_reference(impl);
        RegionalCentre region = RegionalCentreHelper.narrow(ref);
        Object objref = orb.resolve_initial_references("NameService");
        NamingContextExt ncref = NamingContextExtHelper.narrow(objref);
        NameComponent path[] = ncref.to_name("Region" + name);
        ncref.rebind(path, region);
        System.out.println("RegionalCentre " + name + " ready.");
        orb.run();
    }




    public static boolean connect_env() {
        String[] args = {"-ORBInitialPort", "1050"};
        try {
            ORB orb = ORB.init(args, null);
            Object objref = orb.resolve_initial_references("NameService");
            NamingContextExt ncref = NamingContextExtHelper.narrow(objref);
            env = (EnvironmentalCentre) EnvironmentalCentreHelper.narrow(ncref.resolve_str("Env"));
            env.register_regional_centre(name);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

 

5.7 MonitorServer

package server;

import EnvMon.*;
import impl.MonitorImpl;
import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CORBA.Object;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.omg.PortableServer.POAPackage.ServantNotActive;
import org.omg.PortableServer.POAPackage.WrongPolicy;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class MonitorServer {
    private static String name = null, location = null;
    private static RegionalCentre region = null;
    private static int minute = 0;

    public static void main(String[] args) throws InvalidName, AdapterInactive, ServantNotActive, WrongPolicy,
            org.omg.CosNaming.NamingContextPackage.InvalidName, NotFound, CannotProceed {
        if (args.length < 2) {
            System.out.println("java Monitor <name> <location>");
            return;
        }
        name = args[0];
        location = args[1];
        MonitorImpl impl = new MonitorImpl(name, location);
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame(name);
                frame.getContentPane().setLayout(new GridLayout(3, 1));
                JLabel label = new JLabel("Monitor: " + name + " | Location: " + location + " | " + impl.value);
                JSlider slider = new JSlider();
                slider.setValue(0);
                slider.addChangeListener(e -> {
                    label.setText("Monitor: " + name + " | Location: " + location + " | " + impl.value);
                    impl.value = slider.getValue();
                    // Can identify anomalous or potentially dangerous readings of nitrogen oxides
                    // and alert the Regional Centre immediately
                    Reading reading = impl.get_reading();
                    if (reading != null) {
                        int time = reading.time;
                        if (region != null && impl.value > 90 && minute != time) {
                            try {
                                region.raise_alarm(impl.get_reading());
                            } catch (Exception e1) {
                                region = null;
                            }
                            minute = time;
                        }
                    }
                });
                JTextField textField = new JTextField(20);
                JButton button = new JButton("Connect RegionalCentre");
                button.addActionListener(e -> {
                    if (region != null) {
                        textField.setEditable(false);
                        button.setText("Connected");
                        button.setEnabled(false);
                        return;
                    }
                    String id = textField.getText();
                    textField.setText("Connecting...");
                    textField.setEditable(false);
                    if (connect_region(id)) {
                        textField.setText(id);
                        button.setText("Connected");
                        button.setEnabled(false);
                    } else {
                        textField.setText("Connect failed.");
                        textField.setEditable(true);
                    }
                });
                label.setBorder(new EmptyBorder(16, 16, 0, 16));
                slider.setBorder(new EmptyBorder(0, 8, 0, 8));
                JPanel panel = new JPanel();
                panel.add(textField);
                panel.add(button);
                panel.setBorder(new EmptyBorder(8, 8, 8, 8));
                frame.getContentPane().add(label);
                frame.getContentPane().add(slider);
                frame.getContentPane().add(panel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
        args[0] = "-ORBInitialPort";
        args[1] = "1050";
        ORB orb = ORB.init(args, null);
        Object obj = orb.resolve_initial_references("RootPOA");
        POA rootpoa = POAHelper.narrow(obj);
        rootpoa.the_POAManager().activate();
        Object ref = rootpoa.servant_to_reference(impl);
        Monitor monitor = MonitorHelper.narrow(ref);
        Object objref = orb.resolve_initial_references("NameService");
        NamingContextExt ncref = NamingContextExtHelper.narrow(objref);
        NameComponent path[] = ncref.to_name("Monitor" + name);
        ncref.rebind(path, monitor);
        System.out.println("Monitor " + name + " ready.");
        orb.run();
    }

    public static boolean connect_region(String id) {
        String[] args = {"-ORBInitialPort", "1050"};
        try {
            ORB orb = ORB.init(args, null);
            Object objref = orb.resolve_initial_references("NameService");
            NamingContextExt ncref = NamingContextExtHelper.narrow(objref);
            region = (RegionalCentre) RegionalCentreHelper.narrow(ncref.resolve_str("Region" + id));
            return region.add_monitor(name, location);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

 

5.8 AgencyServer

package server;

import EnvMon.Agency;
import EnvMon.AgencyHelper;
import EnvMon.EnvironmentalCentre;
import EnvMon.EnvironmentalCentreHelper;
import impl.AgencyImpl;
import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CORBA.Object;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.omg.PortableServer.POAPackage.ServantNotActive;
import org.omg.PortableServer.POAPackage.WrongPolicy;

public class AgencyServer {
    private static String name = null, contact = null, region = null;
    private static EnvironmentalCentre env = null;

    public static void main(String[] args) throws InvalidName, AdapterInactive, ServantNotActive, WrongPolicy,
            org.omg.CosNaming.NamingContextPackage.InvalidName, NotFound, CannotProceed {
        if (args.length < 3) {
            System.out.println("java Agancy <name> <contact> <region>");
            return;
        }
        name = args[0];
        contact = args[1];
        region = args[2];
        AgencyImpl impl = new AgencyImpl(name, contact, region);
        String[] init = {"-ORBInitialPort", "1050"};
        ORB orb = ORB.init(init, null);
        Object obj = orb.resolve_initial_references("RootPOA");
        POA rootpoa = POAHelper.narrow(obj);
        rootpoa.the_POAManager().activate();
        Object ref = rootpoa.servant_to_reference(impl);
        Agency agency = AgencyHelper.narrow(ref);
        Object objref = orb.resolve_initial_references("NameService");
        NamingContextExt ncref = NamingContextExtHelper.narrow(objref);
        NameComponent path[] = ncref.to_name("Agency" + name);
        ncref.rebind(path, agency);
        System.out.println("Agency " + name + " ready.");
        if (connect_env()) {
            System.out.println("Agency >> EnvironmentalCentre ok.");
        } else {
            System.out.println("Agency >> EnvironmentalCentre failed.");
        }
        orb.run();
    }

    public static boolean connect_env() {
        String[] args = {"-ORBInitialPort", "1050"};
        try {
            ORB orb = ORB.init(args, null);
            Object objref = orb.resolve_initial_references("NameService");
            NamingContextExt ncref = NamingContextExtHelper.narrow(objref);
            env = (EnvironmentalCentre) EnvironmentalCentreHelper.narrow(ncref.resolve_str("Env"));
            env.register_agency(name, contact, region);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

 

 

 

6. References

1. https://en.wikipedia.org/wiki/Common_Object_Request_Broker_Architecture

2. S. Vinoski. CORBA overview: CORBA: Integrating Diverse Applications Within Distributed Heterogeneous Environments[J]. IEEE Communications Magazine, 1997, 35(2):46-55.

3. Orfali R, Dan H. Client/server programming with Java and CORBA[M]// Client/server programming with Java and CORBA /. J. Wiley & Sons, 1998.

4. https://en.wikipedia.org/wiki/Java_(programming_language)

5. https://scotch.io/bar-talk/s-o-l-i-d-the-first-five-principles-of-object-oriented-design

6. Eckel B. Thinking in Java[M]. China Machine Press, 2007.

7. https://docs.oracle.com/javase/7/docs/api/

8. Hu J, Zhou B, Jia Y, et al. Research and Implementation of CORBA Web Services[M]// Conceptual Modeling for Advanced Application Domains. Springer Berlin Heidelberg, 2004:314-322.

9. Emmerich W, Kaveh N. Component technologies: Java Beans, COM, CORBA, RMI, EJB and the CORBA Component Model[C]// European Software Engineering Conference Held Jointly with, ACM Sigsoft International Symposium on Foundations of Software Engineering. ACM, 2001:311-312.

10. https://en.wikipedia.org/wiki/Swing_(Java)

11. http://www.ciaranmchale.com/corba-explained-simply/benefits-of-corba.html

 


代写