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 @@
<classpathentry kind="lib" path="lib64/avalon-framework-4.2.0.jar"/>
<classpathentry kind="lib" path="lib64/jacorb.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"/>
</classpath>
......@@ -19,6 +19,9 @@
<property name="clientCalculatorLibDir" value="${env.NOMADCLIENT_HOME}/lib/common/calculator" />
<property name="clientCalculatorLib64Dir" value="${env.NOMADCLIENT_HOME}/lib64/common/calculator" />
<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">
<!-- Create the time stamp -->
......@@ -266,6 +269,12 @@
</exec>
</target>
<target name="proto" depends="init">
<exec executable="protoc">
<arg line="${protoFlags} ${protoDir}/ServantConfiguration.proto" />
</exec>
</target>
<target name="jar">
<echo message="Creating nomadCommandSystem.jar" />
......
......@@ -22,6 +22,7 @@ import java.util.Properties;
import fr.ill.ics.nscclient.corbabase.CorbaNamingService;
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.sessionmanagement.CorbaSessionManager;
import fr.ill.ics.nscclient.sessionmanagement.ServerSessionManager;
......@@ -69,7 +70,10 @@ public class LoginManager {
throw new ClientAlreadyLaunchedException();
} catch (ServerSessionManager.ConnectionFailure e) {
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;
public class CorbaController extends CorbaServant {
private Controller controller;
public CorbaController(int databaseId, int servantId, Controller controller, ClientServantDescriptor 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
*
*/
protected void init() {
this.servantManager = ServerSessionManager.getInstance(serverId).getServantManager();
fillControllerNamesByTypeMaps();
}
protected void reset() {
this.servantManager = ServerSessionManager.getInstance(serverId).getServantManager();
List controllersList = servantManager.getControllers();
......@@ -270,7 +275,7 @@ public class CorbaControllerManager extends ControllerManager implements ServerC
// reset controller information
ServantDatabase.getInstance().clearControllers();
init();
reset();
}
......
......@@ -19,19 +19,17 @@
package fr.ill.ics.nscclient.servant;
import fr.ill.ics.nomadserver.core.Driver;
import fr.ill.ics.nomadserver.core.DriverPackage.DriverMode;
public class CorbaDriver extends CorbaServant {
public enum Mode {REAL, SIMULATED, PERFECT};
private Driver driver;
private Mode mode = Mode.REAL;
private Long channel;
public CorbaDriver(int databaseId, int servantId, Driver driver, ClientServantDescriptor descriptor) {
super(databaseId, servantId, driver, descriptor);
this.driver = driver;
}
public void setChannel(long channel) {
......@@ -41,19 +39,13 @@ public class CorbaDriver extends CorbaServant {
public Long getChannel() {
return channel;
}
public void setMode(Mode mode) {
this.mode = mode;
}
public Mode getMode() {
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;
return mode;
}
}
\ No newline at end of file
......@@ -18,37 +18,50 @@
package fr.ill.ics.nscclient.servant;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import fr.ill.ics.nomadserver.common.ListItem;
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.DriverPackage.DriverMode;
import fr.ill.ics.nomadserver.core.ServantManagerPackage.IteratorFailureException;
import fr.ill.ics.nomadserver.core.ServantManagerPackage.NoSuchServantException;
import fr.ill.ics.nomadserver.core.ServantManagerPackage.ServantCreationException;
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.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.servant.CorbaDriver.Mode;
import fr.ill.ics.nscclient.servant.scan.CorbaIcsParameterizableScan1D;
public class CorbaServantManager {
private static int DATABASE_ID = 0;
private ServantManager servantManager;
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";
public class LoadConfigurationFailure extends Exception {};
public CorbaServantManager(ServantManager servantManager) {
this.servantManager = servantManager;
}
......@@ -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) {
CorbaController controller = null;
......@@ -128,7 +156,18 @@ public class CorbaServantManager {
}
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();
servants.put(descriptor.getId(), driver);
return driver;
......@@ -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() {
List controllers = new ArrayList();
......@@ -188,43 +240,44 @@ public class CorbaServantManager {
}
public List<String> getChildServants(int id) {
return childServantNames.get(id);
List<String> children = new ArrayList<String>();
try {
ListIterator iterator = servantManager.getChildServantsIterator(DATABASE_ID, id);
// iterate over the list and create a new non-corba ServantDescriptor for each item
while (iterator.hasNext()) {
ListItem item = iterator.next();
ChildServantDescriptor corbaDescriptor = ChildServantDescriptorHelper.narrow(item);
String[] nameArray = corbaDescriptor.getNameArray();
int[] indexArray = corbaDescriptor.getIndexArray();
for (int i = 0; i < nameArray.length; i++) {
children.add(nameArray[i]);
if (indexArray[i] != -1) {
int servantID = ServantDatabase.getInstance().getServantId(nameArray[i]);
if (servants.containsKey(servantID)) {
CorbaServant servant = servants.get(servantID);
if (servant instanceof CorbaDriver) {
CorbaDriver driver = (CorbaDriver)servant;
driver.setChannel(indexArray[i]);
}
}
}
}
}
// release the corba object
iterator.releaseIterator();
} catch (Exception e) {
e.printStackTrace();
}
return children;
// List<String> children = new ArrayList<String>();
//
// try {
// ListIterator iterator = servantManager.getChildServantsIterator(DATABASE_ID, id);
//
// // iterate over the list and create a new non-corba ServantDescriptor for each item
// while (iterator.hasNext()) {
// ListItem item = iterator.next();
//
// ChildServantDescriptor corbaDescriptor = ChildServantDescriptorHelper.narrow(item);
// String[] nameArray = corbaDescriptor.getNameArray();
// int[] indexArray = corbaDescriptor.getIndexArray();
// for (int i = 0; i < nameArray.length; i++) {
// children.add(nameArray[i]);
//
// if (indexArray[i] != -1) {
// int servantID = ServantDatabase.getInstance().getServantId(nameArray[i]);
// if (servants.containsKey(servantID)) {
// CorbaServant servant = servants.get(servantID);
// if (servant instanceof CorbaDriver) {
// CorbaDriver driver = (CorbaDriver)servant;
// driver.setChannel(indexArray[i]);
// }
// }
// }
// }
//
// }
// // release the corba object
// iterator.releaseIterator();
//
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// return children;
}
public void pauseSpies(boolean isPaused) {
......@@ -259,4 +312,137 @@ public class CorbaServantManager {
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());
}
}
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());
}
if (pbProperty.getDynamic()) {
PropertyDatabase.getInstance().addDynamicPropertyDescriptor(descriptor.getId(), new DynamicPropertyDescriptor(propertyId, ids, pbProperty.getName(), pbProperty.getType()));
} else {
PropertyDatabase.getInstance().addStandardPropertyDescriptor(descriptor.getId(), new StandardPropertyDescriptor(propertyId, pbProperty.getName(), pbProperty.getType()));
}
}
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()));
}
}
}
private void initAllMaps(ServantData servantData) {
initAllMaps(servantData.getDriversList());
initAllMaps(servantData.getControllersList());
}
}
......@@ -19,6 +19,7 @@
package fr.ill.ics.nscclient.sessionmanagement;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
......@@ -39,6 +40,8 @@ import fr.ill.ics.nscclient.console.DataProviderRemoteConsole;
import fr.ill.ics.nscclient.dataprovider.DataAccessor;
import fr.ill.ics.nscclient.log.LogSubcriberImpl;
import fr.ill.ics.nscclient.notification.commandzone.ServerCommandZoneEventManager;
import fr.ill.ics.nscclient.servant.ConfigurationManager;
import fr.ill.ics.nscclient.servant.ConfigurationManager.LoadFailure;
import fr.ill.ics.nscclient.servant.CorbaServantManager;
import fr.ill.ics.nscclient.survey.SurveySubcriberImpl;
......@@ -102,7 +105,7 @@ public class ServerSessionManager {
return instances.get(serverId);
}
public void login(boolean standAlone) throws CorbaSessionManager.ClientAlreadyLaunchedException, ConnectionFailure {
public void login(boolean standAlone) throws CorbaSessionManager.ClientAlreadyLaunchedException, ConnectionFailure, LoadFailure {
init();
......@@ -116,6 +119,8 @@ public class ServerSessionManager {
// - simulated server with no real server logged
// This is because ControllerManager and DriverManager are accessed by getInstance() without any serverId
if (serverId.equals("real") || (!serverId.equals("real") && !getInstance("real").isLogged())) {
ConfigurationManager.initInstance(serverId);
ControllerManager.initInstance(serverId);
DriverManager.initInstance(serverId);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment