forked from tango-controls/JTango
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
290 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.tango.transport; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* @author Igor Khokhriakov <[email protected]> | ||
* @since 19.02.2020 | ||
*/ | ||
public class Message { | ||
public static final String PATTERN_STR = "(read|write|exec|response):([\\w\\W]+);(double|float|int|long|String|null):([\\w.]+)"; | ||
public static final Pattern PATTERN = Pattern.compile(PATTERN_STR); | ||
|
||
public static final String TARGET_PATTERN_STR = "(\\w+)/(\\w+)/(\\w+)/(\\w+)"; | ||
public static final Pattern TARGET_PATTERN = Pattern.compile(TARGET_PATTERN_STR); | ||
|
||
public String action; | ||
public String target; | ||
public String dataType; | ||
public String value; | ||
|
||
public Message(String action, String target, String dataType, String value) { | ||
this.action = action; | ||
this.target = target; | ||
this.dataType = dataType; | ||
this.value = value; | ||
} | ||
|
||
public static Message fromString(String str) { | ||
Matcher matcher = PATTERN.matcher(str); | ||
|
||
if (matcher.matches()) | ||
return new Message(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(4)); | ||
else | ||
throw new IllegalArgumentException("Unrecognized message: " + str); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format( | ||
"%s:%s;%s:%s", action, target, dataType, value | ||
); | ||
} | ||
|
||
public static class Target { | ||
public final String device; | ||
public final String member; | ||
|
||
public Target(String device, String member) { | ||
this.device = device; | ||
this.member = member; | ||
} | ||
|
||
public static Target fromString(String str) { | ||
Matcher matcher = TARGET_PATTERN.matcher(str); | ||
|
||
if (matcher.matches()) | ||
return new Target(String.format("%s/%s/%s", matcher.group(1), matcher.group(2), matcher.group(3)), matcher.group(4)); | ||
else | ||
throw new IllegalArgumentException("Unrecognized message: " + str); | ||
} | ||
} | ||
|
||
public static class Error extends Message { | ||
public Error(String value) { | ||
super("response", "error", "String", value); | ||
} | ||
} | ||
|
||
public static class Ok extends Message { | ||
public Ok() { | ||
super("response", "ok", null, null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
server/src/main/java/org/tango/server/transport/ReadMessageProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.tango.server.transport; | ||
|
||
import fr.esrf.Tango.AttrDataFormat; | ||
import fr.esrf.Tango.DevFailed; | ||
import org.omg.CORBA.Any; | ||
import org.tango.server.idl.CleverAnyAttribute; | ||
import org.tango.server.servant.DeviceImpl; | ||
import org.tango.transport.Message; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* @author Igor Khokhriakov <[email protected]> | ||
* @since 19.02.2020 | ||
*/ | ||
public class ReadMessageProcessor implements ZmqMessageProcessor { | ||
private final DeviceImpl device; | ||
private final String attributeName; | ||
private final String dataType; | ||
|
||
public ReadMessageProcessor(DeviceImpl device, String attributeName, String dataType) { | ||
this.device = device; | ||
this.attributeName = attributeName; | ||
this.dataType = dataType; | ||
} | ||
|
||
@Override | ||
public Message process() { | ||
try { | ||
Any any = Arrays.stream(device.read_attributes(new String[]{attributeName})).map(attributeValue -> attributeValue.value).findFirst().get(); | ||
|
||
Object result = CleverAnyAttribute.get(any, Integer.parseInt(dataType), AttrDataFormat.SCALAR); | ||
return new Message("response", attributeName, dataType, String.valueOf(result)); | ||
} catch (DevFailed devFailed) { | ||
return new Message.Error(devFailed.errors[0].reason); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
server/src/main/java/org/tango/server/transport/WriteMessageProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.tango.server.transport; | ||
|
||
import fr.esrf.Tango.AttributeValue; | ||
import fr.esrf.Tango.DevFailed; | ||
import org.tango.server.idl.TangoIDLAttributeUtil; | ||
import org.tango.server.servant.DeviceImpl; | ||
import org.tango.transport.Message; | ||
|
||
/** | ||
* @author Igor Khokhriakov <[email protected]> | ||
* @since 19.02.2020 | ||
*/ | ||
public class WriteMessageProcessor implements ZmqMessageProcessor { | ||
private final DeviceImpl device; | ||
private final String attributeName; | ||
private final Object value; | ||
|
||
public WriteMessageProcessor(DeviceImpl device, String attributeName, Object value) { | ||
this.device = device; | ||
this.attributeName = attributeName; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public Message process() { | ||
try { | ||
device.write_attributes( | ||
new AttributeValue[]{ | ||
TangoIDLAttributeUtil.toAttributeValue( | ||
device.getAttributeImpl(attributeName).get(), | ||
new org.tango.server.attribute.AttributeValue(value))}); | ||
return new Message.Ok(); | ||
} catch (DevFailed devFailed) { | ||
return new Message.Error(devFailed.errors[0].reason); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
server/src/main/java/org/tango/server/transport/ZmqMessageProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.tango.server.transport; | ||
|
||
import org.tango.transport.Message; | ||
|
||
/** | ||
* @author Igor Khokhriakov <[email protected]> | ||
* @since 19.02.2020 | ||
*/ | ||
public interface ZmqMessageProcessor { | ||
Message process(); | ||
} |
41 changes: 41 additions & 0 deletions
41
server/src/main/java/org/tango/server/transport/ZmqMessageProcessorImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.tango.server.transport; | ||
|
||
import com.google.gson.Gson; | ||
import org.tango.server.admin.AdminDevice; | ||
import org.tango.server.servant.DeviceImpl; | ||
import org.tango.transport.Message; | ||
|
||
/** | ||
* @author Igor Khokhriakov <[email protected]> | ||
* @since 19.02.2020 | ||
*/ | ||
public class ZmqMessageProcessorImpl implements ZmqMessageProcessor { | ||
private final String msg; | ||
private final AdminDevice admin; | ||
|
||
public ZmqMessageProcessorImpl(String msg, AdminDevice admin) { | ||
this.msg = msg; | ||
this.admin = admin; | ||
} | ||
|
||
@Override | ||
public Message process() { | ||
Message message = Message.fromString(msg); | ||
|
||
Message.Target target = Message.Target.fromString(message.target); | ||
|
||
DeviceImpl device = admin.getDeviceImpl(target.device); | ||
|
||
switch (message.action) { | ||
case "read": | ||
return new ReadMessageProcessor(device, target.member, message.dataType).process(); | ||
case "write": | ||
return new WriteMessageProcessor(device, target.member, new Gson().fromJson(message.value, Object.class)).process(); | ||
case "exec": | ||
default: | ||
return new Message.Error("Unsupported message action - " + message.action); | ||
} | ||
} | ||
|
||
|
||
} |
37 changes: 37 additions & 0 deletions
37
server/src/main/java/org/tango/server/transport/ZmqTransportListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.tango.server.transport; | ||
|
||
import org.tango.server.admin.AdminDevice; | ||
import org.zeromq.ZMQ; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* @author Igor Khokhriakov <[email protected]> | ||
* @since 19.02.2020 | ||
*/ | ||
public class ZmqTransportListener implements Runnable { | ||
private final ZMQ.Socket socket; | ||
private final AdminDevice admin; | ||
|
||
public ZmqTransportListener(ZMQ.Socket socket, AdminDevice admin) { | ||
this.socket = socket; | ||
this.admin = admin; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
while (!Thread.currentThread().isInterrupted()) { | ||
byte[] data = socket.recv(); | ||
String msg = new String(data, StandardCharsets.UTF_8); | ||
|
||
//TODO non blocking | ||
//TODO thread pool | ||
socket.send( | ||
new ZmqMessageProcessorImpl(msg, admin) | ||
.process() | ||
.toString() | ||
.getBytes(StandardCharsets.UTF_8)); | ||
|
||
} | ||
} | ||
} |