Commit 83035e50 authored by ics's avatar ics

New code to manage files moved from client to server side (scans, xbu,

pal, settings)
parent 9e77bd4a
......@@ -61,28 +61,6 @@ public class ResourceManager {
public String getFileContent(String controllerType, String fileName) throws ResourceNotFoundException {
return ServantManagerAccessor.getInstance(serverId).readResourceFile(controllerType, fileName);
/*
String fullFilePath = "/home/dpt/ortizh/tmp/" + fileName;
if (new File(fullFilePath).exists()) {
try {
String content = "";
String line;
BufferedReader in = new BufferedReader(new FileReader(fullFilePath));
while ((line = in.readLine()) != null) {
content = content + line + "\n";
}
in.close();
return content;
} catch (IOException e) {
System.err.println("IOException while getting content of file " + fullFilePath);
e.printStackTrace();
}
} else {
throw new ResourceNotFoundException(fileName);
}
return null;
*/
}
public void setFileContent(String controllerType, String fileName, String content) {
......@@ -107,6 +85,22 @@ public class ResourceManager {
*/
}
public void saveFile(String fileName, String content) {
ServantManagerAccessor.getInstance(serverId).saveFile(fileName, content);
}
public String getFileContent(String fileName) throws ResourceNotFoundException {
return ServantManagerAccessor.getInstance(serverId).getFileContent(fileName);
}
public String getTree(String rootDirectory) {
return ServantManagerAccessor.getInstance(serverId).getTree(rootDirectory);
}
public HashMap<String, String> getFamilyImageKeys() {
return ServantManagerAccessor.getInstance(serverId).getFamilyImageKeys();
}
......
......@@ -60,26 +60,26 @@ import fr.ill.ics.nscclient.servant.StandardPropertyDescriptor;
import fr.ill.ics.nscclient.serverconnection.ServerInstance;
public class ServantManagerAccessor {
public class LoadConfigurationFailure extends Exception {};
private static class ChildServants {
String name;
List<String> childNames = new LinkedList<String>();
}
private static final Logger LOGGER = Logger.getLogger(ServantManagerAccessor.class.getName());
private static int DATABASE_ID = 0;
private final static String PARAMETERIZABLE_SCAN_1D = "parameterizable_scan_1d";
private String serverId;
private Application.Requester servantManagerRequester;
private static Map<String, ServantManagerAccessor> instances = new HashMap<String, ServantManagerAccessor>();
private Map<Integer, Servant> servants = new HashMap<Integer, Servant>();
private Map<Integer, List<ChildServants>> childServantNames = new HashMap<Integer, List<ChildServants>>();
private ServantManagerAccessor(String serverId) {
this.serverId = serverId;
}
......@@ -91,111 +91,111 @@ public class ServantManagerAccessor {
}
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 servant manager requester");
// Create the requester.
try {
servantManagerRequester = Application.Requester.create(nomad, "servant_manager");
} catch (RequesterCreationException e) {
System.err.println("Problem to connect to the servant manager responder");
return;
}
System.out.println("Created requester " + servantManagerRequester);
}
public void reset() {
// Terminate the requester.
servantManagerRequester.terminate();
}
private void logError(String methodName, Common.Error.Type error, int id) {
if (error == Common.Error.Type.NO_SUCH_SERVANT) {
LOGGER.logp(Level.SEVERE, this.getClass().getName(), methodName, "UnknownServant with ID " + id);
}
}
private ServantData getWholeConfiguration(int databaseID) throws LoadConfigurationFailure {
// Create the message type.
ServantManagerRequest.Message type = ServantManagerRequest.Message.newBuilder()
.setType(ServantManagerRequest.Message.Type.GetWholeConfiguration)
.build();
.setType(ServantManagerRequest.Message.Type.GetWholeConfiguration)
.build();
// Create the request.
ServantManagerRequest.GetWholeConfigurationRequest request = ServantManagerRequest.GetWholeConfigurationRequest.newBuilder()
.setDatabaseID(databaseID)
.build();
.setDatabaseID(databaseID)
.build();
servantManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
ServantData response = ServantData.parseFrom(servantManagerRequester.receive());
return response;
}
catch (InvalidProtocolBufferException e) {
throw new LoadConfigurationFailure();
}
}
private ServantDataUpdate getWholeConfigurationUpdate(int databaseID) throws LoadConfigurationFailure {
// Create the message type.
ServantManagerRequest.Message type = ServantManagerRequest.Message.newBuilder()
.setType(ServantManagerRequest.Message.Type.GetWholeConfigurationUpdate)
.build();
.setType(ServantManagerRequest.Message.Type.GetWholeConfigurationUpdate)
.build();
// Create the request.
ServantManagerRequest.GetWholeConfigurationRequest request = ServantManagerRequest.GetWholeConfigurationRequest.newBuilder()
.setDatabaseID(databaseID)
.build();
.setDatabaseID(databaseID)
.build();
servantManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
ServantDataUpdate response = ServantDataUpdate.parseFrom(servantManagerRequester.receive());
return response;
}
catch (InvalidProtocolBufferException e) {
throw new LoadConfigurationFailure();
}
}
private ServantDescriptor getControllerConfiguration(int databaseID, int servantID) throws LoadConfigurationFailure {
// Create the message type.
ServantManagerRequest.Message type = ServantManagerRequest.Message.newBuilder()
.setType(ServantManagerRequest.Message.Type.GetControllerConfiguration)
.build();
.setType(ServantManagerRequest.Message.Type.GetControllerConfiguration)
.build();
// Create the request.
ServantManagerRequest.ServantRequest request = ServantManagerRequest.ServantRequest.newBuilder()
.setDatabaseID(databaseID)
.setServantID(servantID)
.build();
.setDatabaseID(databaseID)
.setServantID(servantID)
.build();
servantManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
ServantDescriptor response = ServantDescriptor.parseFrom(servantManagerRequester.receive());
return response;
}
catch (InvalidProtocolBufferException e) {
......@@ -203,79 +203,79 @@ public class ServantManagerAccessor {
throw new LoadConfigurationFailure();
}
}
private ServantSpyProperties getServantSpyProperties(int databaseID, int servantID) {
// Create the message type.
ServantManagerRequest.Message type = ServantManagerRequest.Message.newBuilder()
.setType(ServantManagerRequest.Message.Type.GetServantSpyProperties)
.build();
.setType(ServantManagerRequest.Message.Type.GetServantSpyProperties)
.build();
// Create the request.
ServantManagerRequest.ServantRequest request = ServantManagerRequest.ServantRequest.newBuilder()
.setDatabaseID(databaseID)
.setServantID(servantID)
.build();
.setDatabaseID(databaseID)
.setServantID(servantID)
.build();
servantManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
ServantSpyProperties response = ServantSpyProperties.parseFrom(servantManagerRequester.receive());
return response;
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "getServantSpyProperties", "error in parsing response");
}
return null;
}
private ServantDynamicProperties getServantDynamicProperties(int databaseID, int servantID) {
// Create the message type.
ServantManagerRequest.Message type = ServantManagerRequest.Message.newBuilder()
.setType(ServantManagerRequest.Message.Type.GetServantDynamicProperties)
.build();
.setType(ServantManagerRequest.Message.Type.GetServantDynamicProperties)
.build();
// Create the request.
ServantManagerRequest.ServantRequest request = ServantManagerRequest.ServantRequest.newBuilder()
.setDatabaseID(databaseID)
.setServantID(servantID)
.build();
.setDatabaseID(databaseID)
.setServantID(servantID)
.build();
servantManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
ServantDynamicProperties response = ServantDynamicProperties.parseFrom(servantManagerRequester.receive());
return response;
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "getServantDynamicProperties", "error in parsing response");
}
return null;
}
private int cloneController(int servantID) {
// Create the message type.
ServantManagerRequest.Message type = ServantManagerRequest.Message.newBuilder()
.setType(ServantManagerRequest.Message.Type.CloneController)
.build();
.setType(ServantManagerRequest.Message.Type.CloneController)
.build();
// Create the request.
ServantManagerRequest.ServantRequest request = ServantManagerRequest.ServantRequest.newBuilder()
.setDatabaseID(0)
.setServantID(servantID)
.build();
.setDatabaseID(0)
.setServantID(servantID)
.build();
servantManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.Int32Response response = Common.Int32Response.parseFrom(servantManagerRequester.receive());
// Test the error.
if (response.hasError()) {
logError("cloneController", response.getError(), servantID);
......@@ -287,28 +287,28 @@ public class ServantManagerAccessor {
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "cloneController", "error in parsing response for property with ID " + servantID);
}
return 0;
}
private boolean removeController(int servantID) {
// Create the message type.
ServantManagerRequest.Message type = ServantManagerRequest.Message.newBuilder()
.setType(ServantManagerRequest.Message.Type.RemoveController)
.build();
.setType(ServantManagerRequest.Message.Type.RemoveController)
.build();
// Create the request.
ServantManagerRequest.ServantRequest request = ServantManagerRequest.ServantRequest.newBuilder()
.setDatabaseID(0)
.setServantID(servantID)
.build();
.setDatabaseID(0)
.setServantID(servantID)
.build();
servantManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.BooleanResponse response = Common.BooleanResponse.parseFrom(servantManagerRequester.receive());
// Test the error.
if (response.hasError()) {
logError("removeController", response.getError(), servantID);
......@@ -320,92 +320,92 @@ public class ServantManagerAccessor {
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "removeController", "error in parsing response for property with ID " + servantID);
}
return false;
}
private String getPropertiesForGenericRequest() {
// Create the message type.
ServantManagerRequest.Message type = ServantManagerRequest.Message.newBuilder()
.setType(ServantManagerRequest.Message.Type.GetPropertiesForGeneric)
.build();
.setType(ServantManagerRequest.Message.Type.GetPropertiesForGeneric)
.build();
// Create the request.
ServantManagerRequest.EmptyRequest request = ServantManagerRequest.EmptyRequest.newBuilder()
.build();
.build();
servantManagerRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.StringResponse response = Common.StringResponse.parseFrom(servantManagerRequester.receive());
return response.getValue();
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "getPropertiesForGeneric", "error in parsing response");
}
return "";
}
public void loadWholeConfiguration() throws LoadConfigurationFailure {
ServantData servantData = getWholeConfiguration(DATABASE_ID);
initAllMaps(servantData);
}
public void updateWholeConfiguration() throws LoadConfigurationFailure {
ServantDataUpdate servantDataUpdate = getWholeConfigurationUpdate(DATABASE_ID);
updateAllMaps(servantDataUpdate.getDriversList(), true);
updateAllMaps(servantDataUpdate.getControllersList(), false);
}
private void initAllMaps(List<ServantDataConfiguration.ServantDescriptor> servants, boolean drivers) {
// iterate over servants
Iterator<ServantDataConfiguration.ServantDescriptor> s = servants.iterator();
while (s.hasNext()) {
ServantDataConfiguration.ServantDescriptor pbServant = s.next();
initAllMaps(pbServant, drivers);
}
}
private void initAllMaps(ServantData servantData) {
initAllMaps(servantData.getDriversList(), true);
initAllMaps(servantData.getControllersList(), false);
}
private Servant initAllMaps(ServantDataConfiguration.ServantDescriptor pbServant, boolean driver) {
Servant servant = null;
//System.out.println("servant " + pbServant.getName() + " has types #" + pbServant.getTypesList().size());
// add the controller in ServantDatabase
ClientServantDescriptor descriptor = new ClientServantDescriptor(pbServant.getID(),
pbServant.getName(),
pbServant.getClientType(),
pbServant.getFamily(),
pbServant.getSubfamily(),
pbServant.getCommand(),
pbServant.getVisible(),
pbServant.getEnabled(),
false,
pbServant.getRoot());
pbServant.getName(),
pbServant.getClientType(),
pbServant.getFamily(),
pbServant.getSubfamily(),
pbServant.getCommand(),
pbServant.getVisible(),
pbServant.getEnabled(),
false,
pbServant.getRoot());
if (driver) {
ServantDatabase.getInstance().addDriver(descriptor);
ServantDataConfiguration.ServantDescriptor.Mode pbMode = pbServant.getMode();
DriverServant.Mode mode = DriverServant.Mode.REAL;
if (pbMode == ServantDataConfiguration.ServantDescriptor.Mode.REAL) {
mode = DriverServant.Mode.REAL;
......@@ -414,84 +414,84 @@ public class ServantManagerAccessor {
} else if (pbMode == ServantDataConfiguration.ServantDescriptor.Mode.SIMULATED) {
mode = DriverServant.Mode.SIMULATED;
}
servant = createDriver(descriptor, pbServant.getChannel(), mode);
} else {
ServantDatabase.getInstance().addController(descriptor);
servant = createController(descriptor);
}
// add the list of child names
LinkedList<ChildServants> childNames = new LinkedList<ChildServants>();
// first level of child is not necessary to get the information
// ex: for drivers, pbChild1 = "children"
List<ServantDataConfiguration.ChildDescriptor> pbChildren = pbServant.getChildrenList();
Iterator<ServantDataConfiguration.ChildDescriptor> cd = pbChildren.iterator();
while (cd.hasNext()) {
ServantDataConfiguration.ChildDescriptor pbChild1 = cd.next();
ChildServants childServants = new ChildServants();
childServants.name = pbChild1.getName();
Iterator<ServantDataConfiguration.ChildDescriptor.Child> ccd = pbChild1.getChildArrayList().iterator();
while (ccd.hasNext()) {
ServantDataConfiguration.ChildDescriptor.Child pbChild2 = ccd.next();
childServants.childNames.add(pbChild2.getName());
}
childNames.add(childServants);
}
childServantNames.put(descriptor.getId(), childNames);
// initialise the controller
Iterator<ServantDataConfiguration.PropertyDescriptor> p = pbServant.getPropertiesList().iterator();
while (p.hasNext()) {
ServantDataConfiguration.PropertyDescriptor pbProperty = p.next();
// copy sequence values
List<Integer> pbIds = pbProperty.getIDArrayList();
Iterator<Integer> i = pbIds.iterator();
int propertyId = i.next();
List<Integer> ids = new LinkedList<Integer>();
while (i.hasNext()) {
ids.add(i.next());
}
int enumeratedValuesId = 0;
int enumeratedLabelsId = 0;
if (pbProperty.hasEnumeratedValuesID()) {
enumeratedValuesId = pbProperty.getEnumeratedValuesID();
}
if (pbProperty.hasEnumeratedLabelsID()) {
enumeratedLabelsId = pbProperty.getEnumeratedLabelsID();
}
if (pbProperty.getDynamic()) {
PropertyDatabase.getInstance().addDynamicPropertyDescriptor(descriptor.getId(), new DynamicPropertyDescriptor(propertyId, ids, pbProperty.getName(), pbProperty.getType(), enumeratedValuesId, enumeratedLabelsId));
} else {
PropertyDatabase.getInstance().addStandardPropertyDescriptor(descriptor.getId(), new StandardPropertyDescriptor(propertyId, pbProperty.getName(), pbProperty.getType(), enumeratedValuesId, enumeratedLabelsId));
}
}
Iterator<ServantDataConfiguration.CommandDescriptor> d = pbServant.getCommandsList().iterator();
while (d.hasNext()) {
ServantDataConfiguration.CommandDescriptor pbCommand = d.next();
CommandDatabase.getInstance().addCommandDescriptor(descriptor.getId(), new ClientCommandDescriptor(pbCommand.getID(), pbCommand.getName()));