1 /*
   2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.rmi.*;
  25 import java.rmi.server.*;
  26 import java.util.*;
  27 import java.io.*;
  28 
  29 
  30 /**
  31  * Server accepts agents and could test for validity.  Acts as both a home
  32  * server and a regular server.  The agent will jump to this host and
  33  * the server will create a thread and allow the agent to run inside of
  34  * it.  The agent just queries the system.properties for machine info.
  35  */
  36 public class AgentServerImpl
  37     extends UnicastRemoteObject
  38     implements AgentServer
  39 {
  40 
  41     /**
  42      * Constructor
  43      *
  44      * @exception RemoteException If a network problem occurs.
  45      */
  46     public AgentServerImpl() throws RemoteException {
  47         // Could use to set up state of server
  48     }
  49 
  50     /**
  51      * Instantiates Agent Server Implementation and sets security
  52      * manager
  53      */
  54     public static void main(String args[]) {
  55 
  56         // Set the security Manager
  57         //System.setSecurityManager(new MyRMISecurityManager());
  58 
  59         try {
  60             AgentServerImpl server = new AgentServerImpl();
  61             Naming.rebind("/AgentServer", server);
  62             System.out.println("Ready to receive agents.");
  63                 System.err.println("DTI_DoneInitializing");
  64         } catch (Exception e) {
  65                 System.err.println("DTI_Error");
  66             System.err.println("Did not establish server");
  67             e.printStackTrace();
  68         }
  69     }
  70 
  71     /**
  72      * Remote method called by Agent to have server accept it.
  73      */
  74     public synchronized void accept(Agent agent)
  75         throws RemoteException //, InvalidAgentException
  76     {
  77         Thread t;
  78 
  79         // Could check validity of agent here
  80         // checkValid(agent);
  81 
  82         // Create new thread to run agent
  83         t = new Thread(agent);
  84 
  85         System.out.println("Agent Accepted: " + t);
  86 
  87         // Start agent
  88         t.start();
  89     }
  90 
  91     /**
  92      * Remote method called by Agent to return to final server.
  93      */
  94     public synchronized void returnHome(Agent agent)
  95         throws RemoteException //, InvalidAgentException
  96     {
  97         Enumeration info = null;
  98         boolean bErrorsOccurred = false;
  99 
 100         // Could check validity of agent here
 101         // checkValid(agent);
 102 
 103         // Grab and print collected info from agent
 104         info = agent.getInfo().elements();
 105         System.out.println("Collected information:");
 106         while (info.hasMoreElements()) {
 107             System.out.println("     " + (String) info.nextElement());
 108         }
 109 
 110         System.out.println("\nErrors:");
 111         System.out.println(agent.getErrors());
 112         if(!(agent.getErrors()).equals(""))
 113                 bErrorsOccurred = true;
 114 
 115         if(bErrorsOccurred)
 116     {
 117                 System.err.println("DTI_Error");
 118                 System.err.println("DTI_DoneExecuting");
 119         }
 120         else
 121     {
 122                 System.err.println("DTI_DoneExecuting");
 123     }
 124 
 125         }
 126 }