Commit 28a87ac3 authored by yannick legoc's avatar yannick legoc
Browse files

Replaced Condition Manager corba classes.

parent 82374a32
......@@ -243,11 +243,6 @@
<arg line="${idlFlags} ${idlCoreDir}/commandzone/CommandZoneEventSubscriber.idl" />
</exec>
<echo message="Generating ConditionManager.java" />
<exec executable="idl">
<arg line="${idlFlags} ${idlCoreDir}/ConditionManager.idl" />
</exec>
<!-- common -->
<echo message="Generating ListIterator.java" />
<exec executable="idl">
......@@ -345,6 +340,11 @@
<arg line="${protoFlags} ${protoDir}/CommandLineRequests.proto" />
</exec>
<echo message="Generating ConditionManagerRequests.java" />
<exec executable="protoc">
<arg line="${protoFlags} ${protoDir}/ConditionManagerRequests.proto" />
</exec>
</target>
</configuration>
<goals>
......
/*
* Nomad Instrument Control Software
*
* Copyright 2011 Institut Laue-Langevin
*
* Licensed under the EUPL, Version 1.1 only (the "License");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
package fr.ill.ics.nscclient.condition;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.protobuf.InvalidProtocolBufferException;
import fr.ill.ics.bridge.listeners.ServerConditionChangeListener.ClientConditionState;
import fr.ill.ics.cameo.Application;
import fr.ill.ics.cameo.RequesterCreationException;
import fr.ill.ics.nomadserver.common.Common;
import fr.ill.ics.nomadserver.common.Common.ConditionStateResponse.ConditionState;
import fr.ill.ics.nomadserver.common.Common.Error.Type;
import fr.ill.ics.nomadserver.condition.ConditionManagerRequest;
import fr.ill.ics.nscclient.serverconnection.ServerInstance;
public class ConditionManagerAccessor {
private static final Logger LOGGER = Logger.getLogger(ConditionManagerAccessor.class.getName());
private String serverId;
private Application.Requester conditionManagerRequester;
private static Map<String, ConditionManagerAccessor> instances = new HashMap<String, ConditionManagerAccessor>();
public class ConditionExistsException extends Exception {};
public class NoSuchConditionException extends Exception {};
public class UnremovableConditionException extends Exception {};
public class UnrenamableConditionException extends Exception {};
public class InvalidExpressionException extends Exception {
private String message;
public InvalidExpressionException(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
};
/**
*
*/
public ConditionManagerAccessor(String serverId) {
this.serverId = serverId;
}
public static ConditionManagerAccessor getInstance(String serverId) {
if (!instances.containsKey(serverId)) {
ConditionManagerAccessor instance = new ConditionManagerAccessor(serverId);
instances.put(serverId, instance);
}
return instances.get(serverId);
}
public void init() {
// Connect nomad server.
Application.Instance nomad = ServerInstance.getInstance().getApplicationInstance(serverId);
if (nomad == null) {
System.err.println("Problem to connect to the nomad server " + serverId);
return;
}
System.out.println("Trying to create condition manager requester");
// Create the requester.
try {
conditionManagerRequester = Application.Requester.create(nomad, "condition_manager");
} catch (RequesterCreationException e) {
System.err.println("Problem to connect to the condition manager responder: " + e.getMessage());
return;
}
System.out.println("Created requester " + conditionManagerRequester);
}
public void reset() {
// Terminate the requester.
conditionManagerRequester.terminate();
}
private void processError(Common.ConditionResponse response) throws ConditionExistsException, NoSuchConditionException, InvalidExpressionException, UnremovableConditionException, UnrenamableConditionException {
if (!response.hasError()) {
return;
}
Type error = response.getError();
if (error == Type.CONDITION_EXISTS) {
throw new ConditionExistsException();
}
else if (error == Type.NO_SUCH_CONDITION) {
throw new NoSuchConditionException();
}
else if (error == Type.INVALID_EXPRESSION) {
throw new InvalidExpressionException(response.getMessage());
}
else if (error == Type.UNREMOVABLE_CONDITION) {
throw new UnremovableConditionException();
}
else if (error == Type.UNRENAMABLE_CONDITION) {
throw new UnrenamableConditionException();
}
}
public void verifyExpression(String expression) throws InvalidExpressionException, ConditionExistsException, NoSuchConditionException, UnremovableConditionException, UnrenamableConditionException {
// Create the message type.
ConditionManagerRequest.Message type = ConditionManagerRequest.Message.newBuilder()
.setType(ConditionManagerRequest.Message.Type.VerifyExpression)
.build();
// Create the request.
ConditionManagerRequest.VerifyExpressionRequest request = ConditionManagerRequest.VerifyExpressionRequest.newBuilder()
.setExpression(expression)
.build();
conditionManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.ConditionResponse response = Common.ConditionResponse.parseFrom(conditionManagerRequester.receive());
// Test the error.
processError(response);
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "verifyExpression", "error in parsing response");
}
}
public void addAlarm(String name, String expression, String message, int activationDelay, boolean stopCommandZone, boolean on, String mailRecipients, String smsNumber) throws ConditionExistsException, InvalidExpressionException, NoSuchConditionException, UnremovableConditionException, UnrenamableConditionException {
// Create the message type.
ConditionManagerRequest.Message type = ConditionManagerRequest.Message.newBuilder()
.setType(ConditionManagerRequest.Message.Type.AddAlarm)
.build();
// Create the request.
ConditionManagerRequest.AddAlarmRequest request = ConditionManagerRequest.AddAlarmRequest.newBuilder()
.setName(name)
.setExpression(expression)
.setMessage(message)
.setActivationDelay(activationDelay)
.setStopCommandZone(stopCommandZone)
.setOn(on)
.setMailRecipients(mailRecipients)
.setSmsNumber(smsNumber)
.build();
conditionManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.ConditionResponse response = Common.ConditionResponse.parseFrom(conditionManagerRequester.receive());
// Test the error.
processError(response);
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "addAlarm", "error in parsing response");
}
}
public void addWarning(String name, String expression, String message, boolean on, String mailRecipients, String smsNumber) throws ConditionExistsException, InvalidExpressionException, NoSuchConditionException, UnremovableConditionException, UnrenamableConditionException {
// Create the message type.
ConditionManagerRequest.Message type = ConditionManagerRequest.Message.newBuilder()
.setType(ConditionManagerRequest.Message.Type.AddWarning)
.build();
// Create the request.
ConditionManagerRequest.AddWarningRequest request = ConditionManagerRequest.AddWarningRequest.newBuilder()
.setName(name)
.setExpression(expression)
.setMessage(message)
.setOn(on)
.setMailRecipients(mailRecipients)
.setSmsNumber(smsNumber)
.build();
conditionManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.ConditionResponse response = Common.ConditionResponse.parseFrom(conditionManagerRequester.receive());
// Test the error.
processError(response);
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "addWarning", "error in parsing response");
}
}
public void addWatchdog(String name, String expression, boolean on, String actions) throws ConditionExistsException, InvalidExpressionException, NoSuchConditionException, UnremovableConditionException, UnrenamableConditionException {
// Create the message type.
ConditionManagerRequest.Message type = ConditionManagerRequest.Message.newBuilder()
.setType(ConditionManagerRequest.Message.Type.AddWatchdog)
.build();
// Create the request.
ConditionManagerRequest.AddWatchdogRequest request = ConditionManagerRequest.AddWatchdogRequest.newBuilder()
.setName(name)
.setExpression(expression)
.setOn(on)
.setActions(actions)
.build();
conditionManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.ConditionResponse response = Common.ConditionResponse.parseFrom(conditionManagerRequester.receive());
// Test the error.
processError(response);
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "addWatchdog", "error in parsing response");
}
}
public void removeCondition(int conditionId) throws NoSuchConditionException, UnremovableConditionException, ConditionExistsException, InvalidExpressionException, UnrenamableConditionException {
// Create the message type.
ConditionManagerRequest.Message type = ConditionManagerRequest.Message.newBuilder()
.setType(ConditionManagerRequest.Message.Type.RemoveCondition)
.build();
// Create the request.
ConditionManagerRequest.RemoveConditionRequest request = ConditionManagerRequest.RemoveConditionRequest.newBuilder()
.setConditionId(conditionId)
.build();
conditionManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.ConditionResponse response = Common.ConditionResponse.parseFrom(conditionManagerRequester.receive());
// Test the error.
processError(response);
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "removeCondition", "error in parsing response");
}
}
public void setConditionOn(int conditionId, boolean value) throws NoSuchConditionException, ConditionExistsException, InvalidExpressionException, UnremovableConditionException, UnrenamableConditionException {
// Create the message type.
ConditionManagerRequest.Message type = ConditionManagerRequest.Message.newBuilder()
.setType(ConditionManagerRequest.Message.Type.SetConditionOn)
.build();
// Create the request.
ConditionManagerRequest.SetConditionOnRequest request = ConditionManagerRequest.SetConditionOnRequest.newBuilder()
.setConditionId(conditionId)
.setValue(value)
.build();
conditionManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.ConditionResponse response = Common.ConditionResponse.parseFrom(conditionManagerRequester.receive());
// Test the error.
processError(response);
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "setConditionOn", "error in parsing response");
}
}
public void renameCondition(int conditionId, String newName) throws NoSuchConditionException, UnrenamableConditionException, ConditionExistsException, InvalidExpressionException, UnremovableConditionException {
// Create the message type.
ConditionManagerRequest.Message type = ConditionManagerRequest.Message.newBuilder()
.setType(ConditionManagerRequest.Message.Type.RenameCondition)
.build();
// Create the request.
ConditionManagerRequest.RenameConditionRequest request = ConditionManagerRequest.RenameConditionRequest.newBuilder()
.setConditionId(conditionId)
.setNewName(newName)
.build();
conditionManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.ConditionResponse response = Common.ConditionResponse.parseFrom(conditionManagerRequester.receive());
// Test the error.
processError(response);
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "renameCondition", "error in parsing response");
}
}
public void editAlarm(int conditionId, String expression, String message, int activationDelay, boolean stopCommandZone, String mailRecipients, String smsNumber) throws NoSuchConditionException, InvalidExpressionException, ConditionExistsException, UnremovableConditionException, UnrenamableConditionException {
// Create the message type.
ConditionManagerRequest.Message type = ConditionManagerRequest.Message.newBuilder()
.setType(ConditionManagerRequest.Message.Type.EditAlarm)
.build();
// Create the request.
ConditionManagerRequest.EditAlarmRequest request = ConditionManagerRequest.EditAlarmRequest.newBuilder()
.setConditionId(conditionId)
.setExpression(expression)
.setMessage(message)
.setActivationDelay(activationDelay)
.setStopCommandZone(stopCommandZone)
.setMailRecipients(mailRecipients)
.setSmsNumber(smsNumber)
.build();
conditionManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.ConditionResponse response = Common.ConditionResponse.parseFrom(conditionManagerRequester.receive());
// Test the error.
processError(response);
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "renameCondition", "error in parsing response");
}
}
public void editWarning(int conditionId, String expression, String message, String mailRecipients, String smsNumber) throws NoSuchConditionException, InvalidExpressionException, ConditionExistsException, UnremovableConditionException, UnrenamableConditionException {
// Create the message type.
ConditionManagerRequest.Message type = ConditionManagerRequest.Message.newBuilder()
.setType(ConditionManagerRequest.Message.Type.EditWarning)
.build();
// Create the request.
ConditionManagerRequest.EditWarningRequest request = ConditionManagerRequest.EditWarningRequest.newBuilder()
.setConditionId(conditionId)
.setExpression(expression)
.setMessage(message)
.setMailRecipients(mailRecipients)
.setSmsNumber(smsNumber)
.build();
conditionManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.ConditionResponse response = Common.ConditionResponse.parseFrom(conditionManagerRequester.receive());
// Test the error.
processError(response);
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "addWarning", "error in parsing response");
}
}
public void editWatchdog(int conditionId, String expression, String actions) throws NoSuchConditionException, InvalidExpressionException, ConditionExistsException, UnremovableConditionException, UnrenamableConditionException {
// Create the message type.
ConditionManagerRequest.Message type = ConditionManagerRequest.Message.newBuilder()
.setType(ConditionManagerRequest.Message.Type.EditWatchdog)
.build();
// Create the request.
ConditionManagerRequest.EditWatchdogRequest request = ConditionManagerRequest.EditWatchdogRequest.newBuilder()
.setConditionId(conditionId)
.setExpression(expression)
.setActions(actions)
.build();
conditionManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.ConditionResponse response = Common.ConditionResponse.parseFrom(conditionManagerRequester.receive());
// Test the error.
processError(response);
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "addWatchdog", "error in parsing response");
}
}
public void editCommandZoneTermination(String message, String mailRecipients, String smsNumber) throws NoSuchConditionException, ConditionExistsException, InvalidExpressionException, UnremovableConditionException, UnrenamableConditionException {
// Create the message type.
ConditionManagerRequest.Message type = ConditionManagerRequest.Message.newBuilder()
.setType(ConditionManagerRequest.Message.Type.EditCommandZoneTermination)
.build();
// Create the request.
ConditionManagerRequest.EditCommandZoneTerminationRequest request = ConditionManagerRequest.EditCommandZoneTerminationRequest.newBuilder()
.setMessage(message)
.setMailRecipients(mailRecipients)
.setSmsNumber(smsNumber)
.build();
conditionManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.ConditionResponse response = Common.ConditionResponse.parseFrom(conditionManagerRequester.receive());
// Test the error.
processError(response);
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "addWatchdog", "error in parsing response");
}
}
public ClientConditionState getConditionState(int conditionId) throws NoSuchConditionException, ConditionExistsException, InvalidExpressionException, UnremovableConditionException, UnrenamableConditionException {
// Create the message type.
ConditionManagerRequest.Message type = ConditionManagerRequest.Message.newBuilder()
.setType(ConditionManagerRequest.Message.Type.GetConditionState)
.build();
// Create the request.
ConditionManagerRequest.GetConditionStateRequest request = ConditionManagerRequest.GetConditionStateRequest.newBuilder()
.setConditionId(conditionId)
.build();
conditionManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.ConditionStateResponse response = Common.ConditionStateResponse.parseFrom(conditionManagerRequester.receive());
// Test the error.
//processError(response);
ConditionState responseState = response.getValue();
if (responseState == ConditionState.CONDITION_ACTIVE) {
return ClientConditionState.ACTIVE;
}
else if (responseState == ConditionState.CONDITION_INACTIVE) {
return ClientConditionState.INACTIVE;
}
else if (responseState == ConditionState.CONDITION_ON_ACTIVATION_DELAY) {
return ClientConditionState.ON_ACTIVATION_DELAY;
}
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "addWatchdog", "error in parsing response");
}
return ClientConditionState.INACTIVE;
}
public String getConditionsXMLContent() {
// Create the message type.
ConditionManagerRequest.Message type = ConditionManagerRequest.Message.newBuilder()
.setType(ConditionManagerRequest.Message.Type.GetConditionsXMLContent)
.build();
// Create the request.
ConditionManagerRequest.GetConditionsXMLContentRequest request = ConditionManagerRequest.GetConditionsXMLContentRequest.newBuilder()
.build();
conditionManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.StringResponse response = Common.StringResponse.parseFrom(conditionManagerRequester.receive());
return response.getValue();
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "addWatchdog", "error in parsing response");
}
return "";
}
}
\ No newline at end of file
/*
* Nomad Instrument Control Software
*
* Copyright 2011 Institut Laue-Langevin
*
* Licensed under the EUPL, Version 1.1 only (the "License");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
package fr.ill.ics.nscclient.condition;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import fr.ill.ics.bridge.command.CommandZoneWrapper;
import fr.ill.ics.bridge.listeners.ServerConditionChangeListener.ClientConditionState;
import fr.ill.ics.nomadserver.common.DataChangeSubscriberPackage.ConditionState;
import fr.ill.ics.nomadserver.core.ConditionManager;
import fr.ill.ics.nomadserver.core.ConditionManagerHelper;
import fr.ill.ics.nscclient.corbabase.CorbaNamingService;
public class ConditionManagerProxy {
private static final Logger LOGGER = Logger.getLogger(ConditionManagerProxy.class.getName());
private String serverId;
private ConditionManager conditionManager;
private static Map<String, ConditionManagerProxy> instances = new HashMap<String, ConditionManagerProxy>();
public class ConditionExistsException extends Exception {};
public class NoSuchConditionException extends Exception {};
public class UnremovableConditionException extends Exception {};
public class UnrenamableConditionException extends Exception {};
public class InvalidExpressionException extends Exception {
private String message;
public InvalidExpressionException(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
};