Commit 4d785351 authored by legoc's avatar legoc

Replaced protocol buffers array encoding with Xavier training's project

parent 446d2185
3.1.2 DD/MM/YYYY
3.1.2 11/12/2017
-----
* Add methods for getting controller and family icon keys map from server (needed for tablette client)
* Add methods for getting controller and family icon keys map from server (needed for tablette client).
* Replaced protocol buffers array encoding with Xavier training's project to speed up the transfers.
3.1.1 19/10/2017
-----
......
......@@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fr.ill.ics</groupId>
<artifactId>nomadcommandsystem</artifactId>
<version>3.1.2-SNAPSHOT</version>
<version>3.1.2</version>
<name>NomadCommandSystem</name>
<description>Java bridge for the communication with the Nomad server</description>
<scm>
......
......@@ -18,6 +18,7 @@
package fr.ill.ics.nscclient.dataprovider;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -31,6 +32,9 @@ import fr.ill.ics.cameo.Application;
import fr.ill.ics.cameo.RequesterCreationException;
import fr.ill.ics.nomadserver.common.Common;
import fr.ill.ics.nomadserver.database.DatabaseRequest;
import fr.ill.ics.nscclient.dataprovider.PortableByteArray.NullException;
import fr.ill.ics.nscclient.dataprovider.PortableByteArray.SizeException;
import fr.ill.ics.nscclient.dataprovider.PortableByteArray.TypeException;
import fr.ill.ics.nscclient.servant.Servant.UnknownCommandException;
import fr.ill.ics.nscclient.serverconnection.ServerInstance;
......@@ -552,33 +556,23 @@ public class DataAccessor {
.setDatabaseID(databaseID)
.setPropertyID(propertyID)
.build();
databaseRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
byte[] data = databaseRequester.receive();
try {
Common.Int32ArrayResponse response = Common.Int32ArrayResponse.parseFrom(databaseRequester.receive());
PortableByteArray portableByteArray = new PortableByteArray();
int[] result = portableByteArray.parseToInt(data);
// Test the error.
if (response.hasError()) {
logError("getInt32Array", response.getError(), propertyID);
}
else {
int[] result = new int[response.getValueList().size()];
List<Integer> values = response.getValueList();
int i = 0;
for (Integer v : values) {
result[i] = v;
++i;
}
return result;
}
}
catch (InvalidProtocolBufferException e) {
return result;
} catch (TypeException e) {
logError("getInt32Array", Common.Error.Type.BAD_PROPERTY_TYPE, propertyID);
} catch (NullException e) {
logError("getInt32Array", Common.Error.Type.NO_SUCH_PROPERTY, propertyID);
} catch (SizeException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "getInt32Array", "error in parsing response for property with ID " + propertyID);
}
......@@ -622,32 +616,25 @@ public class DataAccessor {
databaseRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
byte[] data = databaseRequester.receive();
try {
Common.Float64ArrayResponse response = Common.Float64ArrayResponse.parseFrom(databaseRequester.receive());
PortableByteArray portableByteArray = new PortableByteArray();
double[] result = portableByteArray.parseToDouble(data);
// Test the error.
if (response.hasError()) {
logError("getFloat64Array", response.getError(), propertyID);
}
else {
double[] result = new double[response.getValueList().size()];
List<Double> values = response.getValueList();
int i = 0;
for (Double v : values) {
result[i] = v;
++i;
}
return result;
}
}
catch (InvalidProtocolBufferException e) {
return result;
} catch (TypeException e) {
logError("getFloat64Array", Common.Error.Type.BAD_PROPERTY_TYPE, propertyID);
} catch (NullException e) {
logError("getFloat64Array", Common.Error.Type.NO_SUCH_PROPERTY, propertyID);
} catch (SizeException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "getFloat64Array", "error in parsing response for property with ID " + propertyID);
}
return null;
}
public void setFloat64Array(int databaseID, int propertyID, double[] data) {
......
/*
* 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.dataprovider;
public class PortableByteArray {
private enum Endianess {
LITTLE,BIG;
}
private enum EncodedType {
ERROR, NULL, DOUBLE, INT64, INT32, FLOAT;
}
public class TypeException extends Exception {};
public class NullException extends Exception {};
public class SizeException extends Exception {};
// Only used for deserialization.
private Endianess endianness;
private int size;
private byte[] encodedData;
private int pos;
// Only used for serialization.
private Endianess outputEndian = Endianess.LITTLE;
// Methods related to decode.
private short readShort() {
short result;
if (endianness == Endianess.BIG) {
result = (short) ((encodedData[pos+1] & 0xff) |
(encodedData[pos+0] & 0xff) << 8);
} else {
result = (short) ((encodedData[pos+0] & 0xff) |
(encodedData[pos+1] & 0xff) << 8);
}
pos = pos + 2;
return result;
}
private int readInt() {
int result;
if (endianness == Endianess.BIG) {
result = (int) ((encodedData[pos+3] & 0xff) |
(encodedData[pos+2] & 0xff) << 8 |
(encodedData[pos+1] & 0xff) << 16 |
(encodedData[pos+0] & 0xff) << 24);
} else {
result = (int) ((encodedData[pos+0] & 0xff) |
(encodedData[pos+1] & 0xff) << 8 |
(encodedData[pos+2] & 0xff) << 16 |
(encodedData[pos+3] & 0xff) << 24);
}
pos = pos + 4;
return result;
}
private long readLong() {
long result;
if (endianness == Endianess.BIG) {
result = (long) ((encodedData[pos+7] & 0xff) |
(encodedData[pos+6] & 0xff) << 8 |
(encodedData[pos+5] & 0xff) << 16 |
(encodedData[pos+4] & 0xff) << 24 |
(long)((encodedData[pos+3] & 0xff) << 0 |
(encodedData[pos+2] & 0xff) << 8 |
(encodedData[pos+1] & 0xff) << 16 |
(encodedData[pos+0] & 0xff) << 24) << 32);
} else {
result = (long) ((encodedData[pos] & 0xff) |
(encodedData[pos+1] & 0xff) << 8 |
(encodedData[pos+2] & 0xff) << 16 |
(encodedData[pos+3] & 0xff) << 24 |
(long)((encodedData[pos+4] & 0xff) << 0 |
(encodedData[pos+5] & 0xff) << 8 |
(encodedData[pos+6] & 0xff) << 16 |
(encodedData[pos+7] & 0xff) << 24) << 32);
}
pos = pos + 8;
return result;
}
private void initDecode(EncodedType type) throws TypeException, SizeException, NullException {
pos = 0;
endianness = Endianess.BIG;
int bytesLength;
if (type == EncodedType.DOUBLE || type == EncodedType.INT64) {
bytesLength = 8;
} else {
bytesLength = 4;
}
// Detect the endianness of the input
short endian = readShort();
if (endian == 1) {
endianness = Endianess.BIG;
} else {
endianness = Endianess.LITTLE;
}
short typeData = readShort();
if (typeData != type.ordinal()) {
throw new TypeException();
}
else if (typeData == 0) {
throw new TypeException();
}
else if (typeData == 1) {
throw new NullException();
}
size = readInt();
if (size*bytesLength != encodedData.length-8) {
throw new SizeException();
}
}
public double[] parseToDouble(byte[] inputArray) throws TypeException, SizeException, NullException {
// Parse byte array to retrieve double array
double[] result = null;
encodedData = inputArray;
initDecode(EncodedType.DOUBLE);
result = new double[size];
// Parse the array
for (int i = 0; i < size; i++) {
result[i] = Double.longBitsToDouble(readLong());
}
return result;
}
public int[] parseToInt(byte[] inputArray) throws TypeException, SizeException, NullException {
// Parse byte array to retrieve int array
int[] result = null;
encodedData = inputArray;
initDecode(EncodedType.INT32);
result = new int[size];
// Parse the array
for (int i = 0; i < size; i++) {
result[i] = readInt();
}
return result;
}
public float[] parseToFLoat(byte[] inputArray) throws TypeException, SizeException, NullException {
// Parse byte array to retrieve float array
float[] result = null;
encodedData = inputArray;
initDecode(EncodedType.FLOAT);
result = new float[size];
// Parse the array
for (int i = 0; i < size; i++) {
result[i] = Float.intBitsToFloat(readInt());
}
return result;
}
public long[] parseToLong(byte[] inputArray) throws TypeException, SizeException, NullException {
// Parse byte array to retrieve long array
long[] result = null;
encodedData = inputArray;
initDecode(EncodedType.INT64);
result = new long[size];
// Parse the array
for (int i = 0; i < size; i++) {
result[i] = readLong();
}
return result;
}
// Method related to serialization.
private void writeShort(short value) {
if (outputEndian == Endianess.BIG) {
encodedData[pos] = (byte)((value >>> 8) & 0xFF);
encodedData[pos+1] = (byte)(value & 0xFF);
} else {
encodedData[pos+1] = (byte)((value >>> 8) & 0xFF);
encodedData[pos] = (byte)(value & 0xFF);
}
pos = pos + 2;
}
private void writeInt(int value) {
if (outputEndian == Endianess.BIG) {
encodedData[pos] = (byte)((value >>> 24) & 0xFF);
encodedData[pos+1] = (byte)((value >>> 16) & 0xFF);
encodedData[pos+2] = (byte)((value >>> 8) & 0xFF);
encodedData[pos+3] = (byte)(value & 0xFF);
} else {
encodedData[pos+3] = (byte)((value >>> 24) & 0xFF);
encodedData[pos+2] = (byte)((value >>> 16) & 0xFF);
encodedData[pos+1] = (byte)((value >>> 8) & 0xFF);
encodedData[pos] = (byte)(value & 0xFF);
}
pos = pos + 4;
}
private void writeLong(long value) {
if (outputEndian == Endianess.BIG) {
encodedData[pos] = (byte)((value >>> 56) & 0xFF);
encodedData[pos+1] = (byte)((value >>> 48) & 0xFF);
encodedData[pos+2] = (byte)((value >>> 40) & 0xFF);
encodedData[pos+3] = (byte)((value >>> 32) & 0xFF);
encodedData[pos+4] = (byte)((value >>> 24) & 0xFF);
encodedData[pos+5] = (byte)((value >>> 16) & 0xFF);
encodedData[pos+6] = (byte)((value >>> 8) & 0xFF);
encodedData[pos+7] = (byte) (value & 0xFF);
} else {
encodedData[pos+7] = (byte)((value >>> 56) & 0xFF);
encodedData[pos+6] = (byte)((value >>> 48) & 0xFF);
encodedData[pos+5] = (byte)((value >>> 40) & 0xFF);
encodedData[pos+4] = (byte)((value >>> 32) & 0xFF);
encodedData[pos+3] = (byte)((value >>> 24) & 0xFF);
encodedData[pos+2] = (byte)((value >>> 16) & 0xFF);
encodedData[pos+1] = (byte)((value >>> 8) & 0xFF);
encodedData[pos] = (byte) (value & 0xFF);
}
pos += 8;
}
private void initSerialize(EncodedType type, int length) {
pos = 0;
encodedData = new byte[length*8+8];
writeShort((short)1);
writeShort((short)type.ordinal());
writeInt(length);
}
public byte[] serializeFromDouble(double[] inputArray) {
initSerialize(EncodedType.DOUBLE,inputArray.length);
long d = 0;
for (int i = 0; i < inputArray.length; i++) {
d = Double.doubleToLongBits(inputArray[i]);
writeLong(d);
}
return encodedData;
}
public byte[] serializeFromLong(long[] inputArray) {
initSerialize(EncodedType.INT64,inputArray.length);
for (int i = 0; i < inputArray.length; i++) {
writeLong(inputArray[i]);
}
return encodedData;
}
public byte[] serializeFromInt(int[] inputArray) {
initSerialize(EncodedType.INT32,inputArray.length);
for (int i = 0; i < inputArray.length; i++) {
writeInt(inputArray[i]);
}
return encodedData;
}
public byte[] serializeFromFloat(float[] inputArray) {
initSerialize(EncodedType.FLOAT,inputArray.length);
int d = 0;
for (int i = 0; i < inputArray.length; i++) {
d = Float.floatToIntBits(inputArray[i]);
writeInt(d);
}
return encodedData;
}
public void setOutputEndianess(int number) {
if (number == 1) {
outputEndian = Endianess.LITTLE;
} else {
outputEndian = Endianess.BIG;
}
}
}
\ No newline at end of file
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