Commit d8a9ff67 authored by yannick legoc's avatar yannick legoc
Browse files

First introduction of Protocol buffers in the configuration loading

parent 4f324ebe
...@@ -5,5 +5,6 @@ ...@@ -5,5 +5,6 @@
<classpathentry kind="lib" path="lib64/avalon-framework-4.2.0.jar"/> <classpathentry kind="lib" path="lib64/avalon-framework-4.2.0.jar"/>
<classpathentry kind="lib" path="lib64/jacorb.jar"/> <classpathentry kind="lib" path="lib64/jacorb.jar"/>
<classpathentry kind="lib" path="lib64/logkit-2.0.jar"/> <classpathentry kind="lib" path="lib64/logkit-2.0.jar"/>
<classpathentry kind="lib" path="lib64/protobuf-2.5.0.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>
...@@ -19,6 +19,9 @@ ...@@ -19,6 +19,9 @@
<property name="clientCalculatorLibDir" value="${env.NOMADCLIENT_HOME}/lib/common/calculator" /> <property name="clientCalculatorLibDir" value="${env.NOMADCLIENT_HOME}/lib/common/calculator" />
<property name="clientCalculatorLib64Dir" value="${env.NOMADCLIENT_HOME}/lib64/common/calculator" /> <property name="clientCalculatorLib64Dir" value="${env.NOMADCLIENT_HOME}/lib64/common/calculator" />
<property name="idlFlags" value="-d ${src} -sloppy_forward" /> <property name="idlFlags" value="-d ${src} -sloppy_forward" />
<property name="protoFlags" value="-I${env.NOMAD_HOME}/src/common/protobuf/proto --java_out=${src}"/>
<property name="protoDir" value="${env.NOMAD_HOME}/src/common/protobuf/proto"/>
<target name="init"> <target name="init">
<!-- Create the time stamp --> <!-- Create the time stamp -->
...@@ -266,6 +269,12 @@ ...@@ -266,6 +269,12 @@
</exec> </exec>
</target> </target>
<target name="proto" depends="init">
<exec executable="protoc">
<arg line="${protoFlags} ${protoDir}/ServantConfiguration.proto" />
</exec>
</target>
<target name="jar"> <target name="jar">
<echo message="Creating nomadCommandSystem.jar" /> <echo message="Creating nomadCommandSystem.jar" />
......
...@@ -22,6 +22,7 @@ import java.util.Properties; ...@@ -22,6 +22,7 @@ import java.util.Properties;
import fr.ill.ics.nscclient.corbabase.CorbaNamingService; import fr.ill.ics.nscclient.corbabase.CorbaNamingService;
import fr.ill.ics.nscclient.corbabase.CorbaORB; import fr.ill.ics.nscclient.corbabase.CorbaORB;
import fr.ill.ics.nscclient.servant.ConfigurationManager.LoadFailure;
import fr.ill.ics.nscclient.serverconnection.ServerConnection; import fr.ill.ics.nscclient.serverconnection.ServerConnection;
import fr.ill.ics.nscclient.sessionmanagement.CorbaSessionManager; import fr.ill.ics.nscclient.sessionmanagement.CorbaSessionManager;
import fr.ill.ics.nscclient.sessionmanagement.ServerSessionManager; import fr.ill.ics.nscclient.sessionmanagement.ServerSessionManager;
...@@ -69,7 +70,10 @@ public class LoginManager { ...@@ -69,7 +70,10 @@ public class LoginManager {
throw new ClientAlreadyLaunchedException(); throw new ClientAlreadyLaunchedException();
} catch (ServerSessionManager.ConnectionFailure e) { } catch (ServerSessionManager.ConnectionFailure e) {
throw new ConnectionFailure(); throw new ConnectionFailure();
} catch (LoadFailure e) {
throw new ConnectionFailure();
} }
} }
......
/*
* 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.servant;
import java.io.IOException;
import fr.ill.ics.nscclient.servant.CorbaServantManager.LoadConfigurationFailure;
import fr.ill.ics.nscclient.sessionmanagement.ServerSessionManager;
public class ConfigurationManager {
private static ConfigurationManager instance = null;
private String serverId;
private CorbaServantManager servantManager;
public class LoadFailure extends Exception {}
public static ConfigurationManager getInstance() {
return instance;
}
public static void initInstance(String serverId) throws LoadFailure {
instance = new ConfigurationManager(serverId);
instance.init();
}
public static void resetInstance() {
instance = null;
}
/**
*
*/
private ConfigurationManager(String serverId) {
this.serverId = serverId;
}
/**
* @throws LoadFailure
*
*/
protected void init() throws LoadFailure {
this.servantManager = ServerSessionManager.getInstance(serverId).getServantManager();
loadWholeConfiguration();
}
public void loadWholeConfiguration() throws LoadFailure {
try {
servantManager.loadWholeConfiguration();
} catch (LoadConfigurationFailure e) {
throw new LoadFailure();
}
}
}
\ No newline at end of file
...@@ -23,11 +23,8 @@ import fr.ill.ics.nomadserver.core.Controller; ...@@ -23,11 +23,8 @@ import fr.ill.ics.nomadserver.core.Controller;
public class CorbaController extends CorbaServant { public class CorbaController extends CorbaServant {
private Controller controller;
public CorbaController(int databaseId, int servantId, Controller controller, ClientServantDescriptor descriptor) { public CorbaController(int databaseId, int servantId, Controller controller, ClientServantDescriptor descriptor) {
super(databaseId, servantId, controller, descriptor); super(databaseId, servantId, controller, descriptor);
this.controller = controller;
} }
} }
\ No newline at end of file
...@@ -55,6 +55,11 @@ public class CorbaControllerManager extends ControllerManager implements ServerC ...@@ -55,6 +55,11 @@ public class CorbaControllerManager extends ControllerManager implements ServerC
* *
*/ */
protected void init() { protected void init() {
this.servantManager = ServerSessionManager.getInstance(serverId).getServantManager();
fillControllerNamesByTypeMaps();
}
protected void reset() {
this.servantManager = ServerSessionManager.getInstance(serverId).getServantManager(); this.servantManager = ServerSessionManager.getInstance(serverId).getServantManager();
List controllersList = servantManager.getControllers(); List controllersList = servantManager.getControllers();
...@@ -270,7 +275,7 @@ public class CorbaControllerManager extends ControllerManager implements ServerC ...@@ -270,7 +275,7 @@ public class CorbaControllerManager extends ControllerManager implements ServerC
// reset controller information // reset controller information
ServantDatabase.getInstance().clearControllers(); ServantDatabase.getInstance().clearControllers();
init(); reset();
} }
......
...@@ -19,19 +19,17 @@ ...@@ -19,19 +19,17 @@
package fr.ill.ics.nscclient.servant; package fr.ill.ics.nscclient.servant;
import fr.ill.ics.nomadserver.core.Driver; import fr.ill.ics.nomadserver.core.Driver;
import fr.ill.ics.nomadserver.core.DriverPackage.DriverMode;
public class CorbaDriver extends CorbaServant { public class CorbaDriver extends CorbaServant {
public enum Mode {REAL, SIMULATED, PERFECT}; public enum Mode {REAL, SIMULATED, PERFECT};
private Driver driver; private Mode mode = Mode.REAL;
private Long channel; private Long channel;
public CorbaDriver(int databaseId, int servantId, Driver driver, ClientServantDescriptor descriptor) { public CorbaDriver(int databaseId, int servantId, Driver driver, ClientServantDescriptor descriptor) {
super(databaseId, servantId, driver, descriptor); super(databaseId, servantId, driver, descriptor);
this.driver = driver;
} }
public void setChannel(long channel) { public void setChannel(long channel) {
...@@ -41,19 +39,13 @@ public class CorbaDriver extends CorbaServant { ...@@ -41,19 +39,13 @@ public class CorbaDriver extends CorbaServant {
public Long getChannel() { public Long getChannel() {
return channel; return channel;
} }
public void setMode(Mode mode) {
this.mode = mode;
}
public Mode getMode() { public Mode getMode() {
return mode;
DriverMode corbaMode = driver.getMode(getDatabaseId());
if (corbaMode == DriverMode.REAL) {
return Mode.REAL;
} else if (corbaMode == DriverMode.SIMULATED) {
return Mode.SIMULATED;
} else if (corbaMode == DriverMode.PERFECT) {
return Mode.PERFECT;
}
return Mode.REAL;
} }
} }
\ No newline at end of file
...@@ -18,37 +18,50 @@ ...@@ -18,37 +18,50 @@
package fr.ill.ics.nscclient.servant; package fr.ill.ics.nscclient.servant;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import fr.ill.ics.nomadserver.common.ListItem; import fr.ill.ics.nomadserver.common.ListItem;
import fr.ill.ics.nomadserver.common.ListIterator; import fr.ill.ics.nomadserver.common.ListIterator;
import fr.ill.ics.nomadserver.configuration.ServantDataConfiguration;
import fr.ill.ics.nomadserver.configuration.ServantDataConfiguration.ServantData;
import fr.ill.ics.nomadserver.core.Driver;
import fr.ill.ics.nomadserver.core.ServantManager; import fr.ill.ics.nomadserver.core.ServantManager;
import fr.ill.ics.nomadserver.core.DriverPackage.DriverMode;
import fr.ill.ics.nomadserver.core.ServantManagerPackage.IteratorFailureException; import fr.ill.ics.nomadserver.core.ServantManagerPackage.IteratorFailureException;
import fr.ill.ics.nomadserver.core.ServantManagerPackage.NoSuchServantException; import fr.ill.ics.nomadserver.core.ServantManagerPackage.NoSuchServantException;
import fr.ill.ics.nomadserver.core.ServantManagerPackage.ServantCreationException; import fr.ill.ics.nomadserver.core.ServantManagerPackage.ServantCreationException;
import fr.ill.ics.nomadserver.core.ServantManagerPackage.ServantRemovalException; import fr.ill.ics.nomadserver.core.ServantManagerPackage.ServantRemovalException;
import fr.ill.ics.nomadserver.dataprovider.ChildServantDescriptor;
import fr.ill.ics.nomadserver.dataprovider.ChildServantDescriptorHelper;
import fr.ill.ics.nomadserver.dataprovider.ServantDescriptor; import fr.ill.ics.nomadserver.dataprovider.ServantDescriptor;
import fr.ill.ics.nomadserver.dataprovider.ServantDescriptorHelper; import fr.ill.ics.nomadserver.dataprovider.ServantDescriptorHelper;
import fr.ill.ics.nscclient.dataprovider.CommandDatabase;
import fr.ill.ics.nscclient.dataprovider.PropertyDatabase;
import fr.ill.ics.nscclient.dataprovider.ServantDatabase; import fr.ill.ics.nscclient.dataprovider.ServantDatabase;
import fr.ill.ics.nscclient.servant.CorbaDriver.Mode;
import fr.ill.ics.nscclient.servant.scan.CorbaIcsParameterizableScan1D; import fr.ill.ics.nscclient.servant.scan.CorbaIcsParameterizableScan1D;
public class CorbaServantManager { public class CorbaServantManager {
private static int DATABASE_ID = 0; private static int DATABASE_ID = 0;
private ServantManager servantManager; private ServantManager servantManager;
private Map<Integer, CorbaServant> servants = new HashMap<Integer, CorbaServant>(); private Map<Integer, CorbaServant> servants = new HashMap<Integer, CorbaServant>();
private Map<Integer, List<String>> childServantNames = new HashMap<Integer, List<String>>();
private final static String PARAMETERIZABLE_SCAN_1D = "parameterizable_scan_1d"; private final static String PARAMETERIZABLE_SCAN_1D = "parameterizable_scan_1d";
public class LoadConfigurationFailure extends Exception {};
public CorbaServantManager(ServantManager servantManager) { public CorbaServantManager(ServantManager servantManager) {
this.servantManager = servantManager; this.servantManager = servantManager;
} }
...@@ -84,6 +97,21 @@ public class CorbaServantManager { ...@@ -84,6 +97,21 @@ public class CorbaServantManager {
} }
private CorbaController createCorbaController(ClientServantDescriptor descriptor) {
CorbaController controller = null;
if (descriptor.getType().equals(PARAMETERIZABLE_SCAN_1D)) {
controller = new CorbaIcsParameterizableScan1D(DATABASE_ID, descriptor.getId(), null, descriptor);
} else {
controller = new CorbaController(DATABASE_ID, descriptor.getId(), null, descriptor);
}
// no initialisation here
servants.put(descriptor.getId(), controller);
return controller;
}
public CorbaController cloneController(ClientServantDescriptor descriptor) { public CorbaController cloneController(ClientServantDescriptor descriptor) {
CorbaController controller = null; CorbaController controller = null;
...@@ -128,7 +156,18 @@ public class CorbaServantManager { ...@@ -128,7 +156,18 @@ public class CorbaServantManager {
} }
try { try {
driver = new CorbaDriver(DATABASE_ID, descriptor.getId(), servantManager.getDriver(descriptor.getId()), descriptor); Driver corbaDriver = servantManager.getDriver(descriptor.getId());
driver = new CorbaDriver(DATABASE_ID, descriptor.getId(), corbaDriver, descriptor);
DriverMode corbaMode = corbaDriver.getMode(DATABASE_ID);
if (corbaMode == DriverMode.REAL) {
driver.setMode(Mode.REAL);
} else if (corbaMode == DriverMode.SIMULATED) {
driver.setMode(Mode.SIMULATED);
} else if (corbaMode == DriverMode.PERFECT) {
driver.setMode(Mode.PERFECT);
}
driver.init(); driver.init();
servants.put(descriptor.getId(), driver); servants.put(descriptor.getId(), driver);
return driver; return driver;
...@@ -139,6 +178,19 @@ public class CorbaServantManager { ...@@ -139,6 +178,19 @@ public class CorbaServantManager {
} }
private CorbaDriver createCorbaDriver(ClientServantDescriptor descriptor, int channel, Mode mode) {
CorbaDriver driver = new CorbaDriver(DATABASE_ID, descriptor.getId(), null, descriptor);
driver.setChannel(channel);
driver.setMode(mode);
System.out.println("created driver " + descriptor.getId() + " mode " + mode);
// no init here
servants.put(descriptor.getId(), driver);
return driver;
}
public List getControllers() { public List getControllers() {
List controllers = new ArrayList(); List controllers = new ArrayList();
...@@ -188,43 +240,44 @@ public class CorbaServantManager { ...@@ -188,43 +240,44 @@ public class CorbaServantManager {
} }
public List<String> getChildServants(int id) { public List<String> getChildServants(int id) {
return childServantNames.get(id);
List<String> children = new ArrayList<String>(); // List<String> children = new ArrayList<String>();
//
try { // try {
ListIterator iterator = servantManager.getChildServantsIterator(DATABASE_ID, id); // ListIterator iterator = servantManager.getChildServantsIterator(DATABASE_ID, id);
//
// iterate over the list and create a new non-corba ServantDescriptor for each item // // iterate over the list and create a new non-corba ServantDescriptor for each item
while (iterator.hasNext()) { // while (iterator.hasNext()) {
ListItem item = iterator.next(); // ListItem item = iterator.next();
//
ChildServantDescriptor corbaDescriptor = ChildServantDescriptorHelper.narrow(item); // ChildServantDescriptor corbaDescriptor = ChildServantDescriptorHelper.narrow(item);
String[] nameArray = corbaDescriptor.getNameArray(); // String[] nameArray = corbaDescriptor.getNameArray();
int[] indexArray = corbaDescriptor.getIndexArray(); // int[] indexArray = corbaDescriptor.getIndexArray();
for (int i = 0; i < nameArray.length; i++) { // for (int i = 0; i < nameArray.length; i++) {
children.add(nameArray[i]); // children.add(nameArray[i]);
//
if (indexArray[i] != -1) { // if (indexArray[i] != -1) {
int servantID = ServantDatabase.getInstance().getServantId(nameArray[i]); // int servantID = ServantDatabase.getInstance().getServantId(nameArray[i]);
if (servants.containsKey(servantID)) { // if (servants.containsKey(servantID)) {
CorbaServant servant = servants.get(servantID); // CorbaServant servant = servants.get(servantID);
if (servant instanceof CorbaDriver) { // if (servant instanceof CorbaDriver) {
CorbaDriver driver = (CorbaDriver)servant; // CorbaDriver driver = (CorbaDriver)servant;
driver.setChannel(indexArray[i]); // driver.setChannel(indexArray[i]);
} // }
} // }
} // }
} // }
//
} // }
// release the corba object // // release the corba object
iterator.releaseIterator(); // iterator.releaseIterator();
//
} catch (Exception e) { // } catch (Exception e) {
e.printStackTrace(); // e.printStackTrace();
} // }
//
return children; // return children;
} }
public void pauseSpies(boolean isPaused) { public void pauseSpies(boolean isPaused) {
...@@ -259,4 +312,137 @@ public class CorbaServantManager { ...@@ -259,4 +312,137 @@ public class CorbaServantManager {
System.err.println("Unable to reconnect driver " + id); System.err.println("Unable to reconnect driver " + id);
} }
} }
}
\ No newline at end of file public void loadWholeConfiguration() throws LoadConfigurationFailure {
System.out.println("getting whole configuration");
long begin = new Date().getTime();
byte[] pbResult = servantManager.getWholeConfiguration(DATABASE_ID);
long end = new Date().getTime();
System.out.println("got whole configuration PB in " + (end - begin) + "ms");
begin = new Date().getTime();
ServantData servantData = null;
try {
servantData = ServantData.parseFrom(new ByteArrayInputStream(pbResult));
} catch (IOException e) {
throw new LoadConfigurationFailure();
}
end = new Date().getTime();
System.out.println("servant data parsed into " + (end - begin) + "ms");
begin = new Date().getTime();
initAllMaps(servantData);
end = new Date().getTime();
System.out.println("loaded whole configuration PB in " + (end - begin) + "ms");
}
private void initAllMaps(List<ServantDataConfiguration.ServantDescriptor> servants) {
// iterate over controllers
Iterator<ServantDataConfiguration.ServantDescriptor> c = servants.iterator();
while (c.hasNext()) {
ServantDataConfiguration.ServantDescriptor pbServant = c.next();
// add the controller in ServantDatabase
ClientServantDescriptor descriptor = new ClientServantDescriptor(pbServant.getID(),
pbServant.getName(),
pbServant.getClientType(),
pbServant.getFamily(),
pbServant.getCommand(),
pbServant.getVisible(),
pbServant.getEnabled(),
false,
pbServant.getRoot());
if (pbServant.getDriver()) {
ServantDatabase.getInstance().addDriver(descriptor);
ServantDataConfiguration.ServantDescriptor.Mode pbMode = pbServant.getMode();
CorbaDriver.Mode mode = CorbaDriver.Mode.REAL;
if (pbMode == ServantDataConfiguration.ServantDescriptor.Mode.REAL) {
mode = CorbaDriver.Mode.REAL;
} else if (pbMode == ServantDataConfiguration.ServantDescriptor.Mode.PERFECT) {
mode = CorbaDriver.Mode.PERFECT;
} else if (pbMode == ServantDataConfiguration.ServantDescriptor.Mode.SIMULATED) {
mode = CorbaDriver.Mode.SIMULATED;
}
createCorbaDriver(descriptor, pbServant.getChannel(), mode);
} else {
ServantDatabase.getInstance().addController(descriptor);
createCorbaController(descriptor);
}
// add the list of child names
LinkedList<String> childNames = new LinkedList<String>();
// 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();
Iterator<ServantDataConfiguration.ChildDescriptor.Child> ccd = pbChild1.getChildArrayList().iterator();
while (ccd.hasNext()) {
ServantDataConfiguration.ChildDescriptor.Child pbChild2 = ccd.next();
childNames.add(pbChild2.getName());
}
}