Skip to content
Snippets Groups Projects
Commit 7973b345 authored by yannick legoc's avatar yannick legoc
Browse files

Implemented SurveyAccessor

parent 5fe0bf92
Branches
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
......@@ -29,5 +30,11 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
......@@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fr.ill.ics</groupId>
<artifactId>nomadcommandsystem</artifactId>
<version>5.2.1</version>
<version>5.3.0-SNAPSHOT</version>
<name>NomadCommandSystem</name>
<description>Java bridge for the communication with the Nomad server</description>
<scm>
......@@ -66,7 +66,7 @@
<dependency>
<groupId>fr.ill.ics</groupId>
<artifactId>nomadcommandsystem-messages</artifactId>
<version>0.0.56</version>
<version>0.0.57</version>
</dependency>
<dependency>
<groupId>fr.ill.ics</groupId>
......
/*
* 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.survey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.protobuf.InvalidProtocolBufferException;
import fr.ill.ics.cameo.base.App;
import fr.ill.ics.cameo.base.InitException;
import fr.ill.ics.cameo.coms.Requester;
import fr.ill.ics.nomadserver.common.Common;
import fr.ill.ics.nomadserver.survey.SurveyRequest;
import fr.ill.ics.nscclient.serverconnection.ServerInstance;
import fr.ill.ics.nscclient.sessionmanagement.SessionManager;
/**
*/
public class SurveyAccessor {
private static final Logger LOGGER = Logger.getLogger(SurveyAccessor.class.getName());
private String serverId;
private Integer clientId;
private Requester surveyRequester;
private static Map<String, SurveyAccessor> instances = new HashMap<String, SurveyAccessor>();
public static class PropertyResponse {
private String controllerName;
private String propertyName;
private int index;
private int activeWriteTime;
PropertyResponse(String controllerName, String propertyName, int index, int activeWriteTime) {
this.controllerName = controllerName;
this.propertyName = propertyName;
this.index = index;
this.activeWriteTime = activeWriteTime;
}
public String getControllerName() {
return controllerName;
}
public String getPropertyName() {
return propertyName;
}
public int getIndex() {
return index;
}
public int getActiveWriteTime() {
return activeWriteTime;
}
}
public SurveyAccessor(String serverId) {
this.serverId = serverId;
}
public static SurveyAccessor getInstance(String serverId) {
if (!instances.containsKey(serverId)) {
SurveyAccessor instance = new SurveyAccessor(serverId);
instances.put(serverId, instance);
}
return instances.get(serverId);
}
public synchronized void init() {
// Connect nomad server.
App 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 survey requester");
// Create the requester.
try {
surveyRequester = Requester.create(nomad, "survey");
surveyRequester.init();
} catch (InitException e) {
System.err.println("Problem to connect to the survey responder: " + e.getMessage());
return;
}
System.out.println("Created requester " + surveyRequester);
}
public synchronized void reset() {
// Terminate the requester.
if (surveyRequester != null) {
surveyRequester.terminate();
}
}
private int getClientID() {
if (clientId == null) {
// Get the client id from the session manager.
clientId = SessionManager.getInstance(serverId).getClientId();
}
return clientId;
}
public synchronized List<PropertyResponse> getProperties() {
// Create the message type.
SurveyRequest.Message type = SurveyRequest.Message.newBuilder()
.setType(SurveyRequest.Message.Type.GetProperties)
.build();
// Create the request.
SurveyRequest.EmptyRequest request = SurveyRequest.EmptyRequest.newBuilder()
.setClientID(getClientID())
.build();
surveyRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
SurveyRequest.GetPropertiesResponse response = SurveyRequest.GetPropertiesResponse.parseFrom(surveyRequester.receive());
List<PropertyResponse> result = new ArrayList<PropertyResponse>();
for (int i = 0; i < response.getPropertiesCount(); i++) {
SurveyRequest.PropertyResponse property = response.getProperties(i);
result.add(new PropertyResponse(property.getControllerName(), property.getPropertyName(), property.getIndex(), property.getSeconds()));
}
return result;
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "getProperties", "error in parsing response");
}
return null;
}
public synchronized boolean addProperty(String controllerName, String propertyName, int index) {
// Create the message type.
SurveyRequest.Message type = SurveyRequest.Message.newBuilder()
.setType(SurveyRequest.Message.Type.AddProperty)
.build();
// Create the request.
SurveyRequest.PropertyRequest request = SurveyRequest.PropertyRequest.newBuilder()
.setClientID(getClientID())
.setControllerName(controllerName)
.setPropertyName(propertyName)
.setIndex(index)
.build();
surveyRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.BooleanResponse response = Common.BooleanResponse.parseFrom(surveyRequester.receive());
// Test the error.
if (response.hasError()) {
return false;
}
else {
return true;
}
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "addProperty", "error in parsing response");
}
return false;
}
public synchronized boolean removeProperty(String controllerName, String propertyName, int index) {
// Create the message type.
SurveyRequest.Message type = SurveyRequest.Message.newBuilder()
.setType(SurveyRequest.Message.Type.RemoveProperty)
.build();
// Create the request.
SurveyRequest.PropertyRequest request = SurveyRequest.PropertyRequest.newBuilder()
.setClientID(getClientID())
.setControllerName(controllerName)
.setPropertyName(propertyName)
.setIndex(index)
.build();
surveyRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.BooleanResponse response = Common.BooleanResponse.parseFrom(surveyRequester.receive());
// Test the error.
if (response.hasError()) {
return false;
}
else {
return true;
}
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "removeProperty", "error in parsing response");
}
return false;
}
public synchronized boolean setActiveWriteTime(String controllerName, String propertyName, int index, int seconds) {
// Create the message type.
SurveyRequest.Message type = SurveyRequest.Message.newBuilder()
.setType(SurveyRequest.Message.Type.SetActiveWriteTime)
.build();
// Create the request.
SurveyRequest.SetActiveWriteTimeRequest request = SurveyRequest.SetActiveWriteTimeRequest.newBuilder()
.setClientID(getClientID())
.setControllerName(controllerName)
.setPropertyName(propertyName)
.setIndex(index)
.setSeconds(seconds)
.build();
surveyRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.BooleanResponse response = Common.BooleanResponse.parseFrom(surveyRequester.receive());
// Test the error.
if (response.hasError()) {
return false;
}
else {
return true;
}
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "setActiveWriteTime", "error in parsing response");
}
return false;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment