示例#1
0
    /* (non-Javadoc)
     * @see jade.core.Service.Slice#serve(jade.core.HorizontalCommand)
     */
    @Override
    public VerticalCommand serve(HorizontalCommand cmd) {

      try {
        if (cmd == null) {
          return null;
        }
        // if ( ! cmd.getService().equals(NAME) ) return null;

        String cmdName = cmd.getName();
        Object[] params = cmd.getParams();
        switch (cmdName) {
          case LoadServiceSlice.SERVICE_START_AGENT:
            logger.debug("Starting a new agent on this platform");
            String nickName = (String) params[0];
            String agentClassName = (String) params[1];
            Object[] args = (Object[]) params[2];
            cmd.setReturnValue(this.startAgent(nickName, agentClassName, args));
            break;
          case LoadServiceSlice.SERVICE_START_NEW_REMOTE_CONTAINER:
            logger.debug("Starting a new remote-container for this platform");
            RemoteContainerConfig remoteConfig = (RemoteContainerConfig) params[0];
            cmd.setReturnValue(this.startRemoteContainer(remoteConfig));
            break;
          case LoadServiceSlice.SERVICE_SET_DEFAULTS_4_REMOTE_CONTAINER_CONFIG:
            logger.debug("Got the default settings for ");
            RemoteContainerConfig remoteContainerConfig = (RemoteContainerConfig) params[0];
            defaults4RemoteContainerConfig = remoteContainerConfig;
            break;
          case LoadServiceSlice.SERVICE_GET_AUTO_REMOTE_CONTAINER_CONFIG:
            logger.debug("Answering to request for 'get_default_remote_container_config'");
            cmd.setReturnValue(this.getAutoRemoteContainerConfig());
            break;
          case LoadServiceSlice.SERVICE_GET_NEW_CONTAINER_2_WAIT_4_STATUS:
            String container2Wait4 = (String) params[0];
            logger.debug(
                "Answering request for new container status of container " + container2Wait4);
            cmd.setReturnValue(this.getNewContainer2Wait4Status(container2Wait4));
            break;
          case LoadServiceSlice.SERVICE_GET_LOCATION:
            cmd.setReturnValue(myContainer.here());
            break;
          case LoadServiceSlice.SERVICE_SET_THRESHOLD_LEVEL:
            LoadThresholdLevels thresholdLevels = (LoadThresholdLevels) params[0];
            logger.debug("Getting new threshold levels for load");
            this.setThresholdLevels(thresholdLevels);
            break;
          case LoadServiceSlice.SERVICE_MEASURE_LOAD:
            logger.debug("Answering request for Container-Load");
            cmd.setReturnValue(this.measureLoad());
            break;
          case LoadServiceSlice.SERVICE_PUT_CONTAINER_DESCRIPTION:
            logger.debug("Putting in container description");
            this.putContainerDescription((ClientRemoteContainerReply) params[0]);
            break;
          case LoadServiceSlice.SERVICE_GET_CONTAINER_DESCRIPTION:
            logger.debug("Answering request for container description");
            cmd.setReturnValue(this.getContainerDescription());
            break;
          case SimulationServiceSlice.SIM_STEP_SIMULATION:
            logger.debug("Received 'Step Simulation'");
            this.setSimulationCycleStartTimeStamp();
            break;
        }
      } catch (Throwable t) {
        cmd.setReturnValue(t);
      }
      return null;
    }
    public VerticalCommand serve(HorizontalCommand cmd) {
      VerticalCommand result = null;
      try {
        String cmdName = cmd.getName();
        Object[] params = cmd.getParams();

        if (cmdName.equals(AgentManagementSlice.H_CREATEAGENT)) {
          GenericCommand gCmd =
              new GenericCommand(
                  AgentManagementSlice.REQUEST_CREATE, AgentManagementSlice.NAME, null);
          AID agentID = (AID) params[0];
          String className = (String) params[1];
          Object[] arguments = (Object[]) params[2];
          JADEPrincipal owner = (JADEPrincipal) params[3];
          Credentials initialCredentials = (Credentials) params[4];
          Boolean startIt = (Boolean) params[5];
          gCmd.addParam(agentID);
          gCmd.addParam(className);
          gCmd.addParam(arguments);
          gCmd.addParam(owner);
          gCmd.addParam(initialCredentials);
          gCmd.addParam(startIt);

          result = gCmd;
        } else if (cmdName.equals(AgentManagementSlice.H_KILLAGENT)) {
          GenericCommand gCmd =
              new GenericCommand(
                  AgentManagementSlice.REQUEST_KILL, AgentManagementSlice.NAME, null);
          AID agentID = (AID) params[0];
          gCmd.addParam(agentID);

          result = gCmd;
        } else if (cmdName.equals(AgentManagementSlice.H_CHANGEAGENTSTATE)) {
          GenericCommand gCmd =
              new GenericCommand(
                  AgentManagementSlice.REQUEST_STATE_CHANGE, AgentManagementSlice.NAME, null);
          AID agentID = (AID) params[0];
          Integer newState = (Integer) params[1];
          gCmd.addParam(agentID);
          gCmd.addParam(newState);

          result = gCmd;
        } else if (cmdName.equals(AgentManagementSlice.H_BORNAGENT)) {
          GenericCommand gCmd =
              new GenericCommand(
                  AgentManagementSlice.INFORM_CREATED, AgentManagementSlice.NAME, null);
          AID agentID = (AID) params[0];
          ContainerID cid = (ContainerID) params[1];
          gCmd.addParam(agentID);
          gCmd.addParam(cid);

          JADEPrincipal owner = cmd.getPrincipal();
          if (myLogger.isLoggable(Logger.FINE)) {
            String ownerInfo = owner != null ? ", Owner = " + owner : "";
            myLogger.log(
                Logger.CONFIG,
                "Local slice processing H-command BORN_AGENT. Name is "
                    + agentID.getName()
                    + ownerInfo);
          }

          result = gCmd;
        } else if (cmdName.equals(AgentManagementSlice.H_DEADAGENT)) {
          GenericCommand gCmd =
              new GenericCommand(
                  AgentManagementSlice.INFORM_KILLED, AgentManagementSlice.NAME, null);
          AID agentID = (AID) params[0];
          gCmd.addParam(agentID);

          result = gCmd;
        } else if (cmdName.equals(AgentManagementSlice.H_SUSPENDEDAGENT)) {
          GenericCommand gCmd =
              new GenericCommand(
                  AgentManagementSlice.INFORM_STATE_CHANGED, AgentManagementSlice.NAME, null);
          AID agentID = (AID) params[0];
          gCmd.addParam(agentID);
          gCmd.addParam(jade.domain.FIPAAgentManagement.AMSAgentDescription.SUSPENDED);
          gCmd.addParam("*");

          result = gCmd;
        } else if (cmdName.equals(AgentManagementSlice.H_RESUMEDAGENT)) {
          GenericCommand gCmd =
              new GenericCommand(
                  AgentManagementSlice.INFORM_STATE_CHANGED, AgentManagementSlice.NAME, null);
          AID agentID = (AID) params[0];
          gCmd.addParam(agentID);
          gCmd.addParam(jade.domain.FIPAAgentManagement.AMSAgentDescription.ACTIVE);
          gCmd.addParam(jade.domain.FIPAAgentManagement.AMSAgentDescription.SUSPENDED);

          result = gCmd;
        } else if (cmdName.equals(AgentManagementSlice.H_EXITCONTAINER)) {
          GenericCommand gCmd =
              new GenericCommand(
                  AgentManagementSlice.KILL_CONTAINER, AgentManagementSlice.NAME, null);

          result = gCmd;
        }

      } catch (Throwable t) {
        cmd.setReturnValue(t);
      }
      return result;
    }