├── .gitignore ├── .project ├── .settings └── .gitignore ├── AUTHORS.md ├── LICENSE.md ├── README.md ├── common ├── .gitignore ├── .project ├── .settings │ └── .gitignore ├── pom.xml └── src │ ├── .gitignore │ ├── main │ ├── .gitignore │ └── java │ │ └── org │ │ └── jspace │ │ ├── .gitignore │ │ ├── ActualField.java │ │ ├── FormalField.java │ │ ├── PileSpace.java │ │ ├── QueueSpace.java │ │ ├── RandomSpace.java │ │ ├── RemoteSpace.java │ │ ├── SequentialSpace.java │ │ ├── Space.java │ │ ├── SpaceFactory.java │ │ ├── SpaceRepository.java │ │ ├── StackSpace.java │ │ ├── Template.java │ │ ├── TemplateField.java │ │ ├── Tuple.java │ │ ├── gate │ │ ├── ClientGate.java │ │ ├── ClientHandler.java │ │ ├── ConnClientGate.java │ │ ├── ConnClientHandler.java │ │ ├── ConnServerGate.java │ │ ├── ExceptionLogger.java │ │ ├── GateBuilder.java │ │ ├── GateFactory.java │ │ ├── KeepClientGate.java │ │ ├── KeepClientHandler.java │ │ ├── KeepServerGate.java │ │ ├── ServerGate.java │ │ ├── TcpGateBuilder.java │ │ ├── TcpServerGate.java │ │ └── UdpGateBuilder.java │ │ ├── io │ │ ├── ClassDictionary.java │ │ ├── JSonMarshaller.java │ │ ├── MarshalFactory.java │ │ ├── jSpaceMarshaller.java │ │ └── json │ │ │ ├── TemplateDeserializer.java │ │ │ ├── TemplateSerializer.java │ │ │ ├── TupleDeserializer.java │ │ │ ├── TupleSerializer.java │ │ │ └── jSonUtils.java │ │ ├── protocol │ │ ├── ClientMessage.java │ │ ├── ClientMessageType.java │ │ ├── InteractionMode.java │ │ ├── PutRequest.java │ │ ├── ServerMessage.java │ │ ├── ServerMessageType.java │ │ └── pSpaceMessage.java │ │ └── util │ │ └── Rendezvous.java │ └── test │ └── java │ └── org │ └── jspace │ └── tests │ ├── TestCloseKeepConnection.java │ ├── TestFieldMatching.java │ ├── TestGates.java │ ├── TestRemoteSpace.java │ ├── TestSpaceRepository.java │ ├── TestSpaces.java │ ├── TestTcpGates.java │ ├── TestTemplate.java │ ├── TestTuple.java │ └── TestjSonMessageSerialization.java ├── docs ├── README.md ├── eclipse_jspace.md ├── figs │ ├── README.md │ ├── jspace_eclipse_tutorial_step1.png │ ├── jspace_eclipse_tutorial_step2.png │ ├── jspace_eclipse_tutorial_step3.png │ ├── jspace_eclipse_tutorial_step4.png │ ├── jspace_eclipse_tutorial_step5.png │ ├── jspace_eclipse_tutorial_step6.png │ ├── jspace_eclipse_tutorial_step7.png │ └── jspace_eclipse_tutorial_step8.png ├── first_application_eclipse.md ├── first_application_maven.md ├── getting_started.md └── jspace_serialization.md ├── examples ├── .gitignore ├── .project ├── .settings │ └── .gitignore ├── DiningPhilosophers │ ├── .gitignore │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── org │ │ └── jspace │ │ └── examples │ │ └── df │ │ ├── DiningPhilosophers.java │ │ ├── DistributedDiningPhilosophers.java │ │ └── Philosopher.java ├── HelloWorld │ ├── .gitignore │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── org │ │ └── jspace │ │ └── examples │ │ └── helloworld │ │ ├── ConcurrentHelloWorld.java │ │ ├── HelloWorld.java │ │ └── RemoteHelloWorld.java ├── PingPong │ ├── .gitignore │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── org │ │ └── jspace │ │ └── examples │ │ └── pingpong │ │ ├── PingAgent.java │ │ ├── PingPong.java │ │ ├── PingPongConn.java │ │ ├── PingPongKeep.java │ │ └── PongAgent.java ├── ProducerConsumer │ ├── .gitignore │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── org │ │ └── jspace │ │ └── examples │ │ └── pc │ │ ├── ConsumerAgent.java │ │ ├── DistributedProducerConsumer.java │ │ ├── ProducerAgent.java │ │ └── ProducerConsumer.java └── pom.xml └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /.DS_Store 2 | /settings_xml 3 | 4 | .classpath 5 | .project 6 | .settings/ 7 | bin/ -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | jspace 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.m2e.core.maven2Builder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.m2e.core.maven2Nature 16 | 17 | 18 | -------------------------------------------------------------------------------- /.settings/.gitignore: -------------------------------------------------------------------------------- 1 | /org.eclipse.core.resources.prefs 2 | /org.eclipse.m2e.core.prefs 3 | -------------------------------------------------------------------------------- /AUTHORS.md: -------------------------------------------------------------------------------- 1 | The goSpace Developers: 2 | 3 | - [Michele Loreti](http://www.micheleloreti.comn/) 4 | - [Alberto Lluch Lafuente](http://www.imm.dtu.dk/~albl/) 5 | - Francesco Terrosi 6 | - [Lorenzo Crociani](https://github.com/lorecro) -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 Michele Loreti and the jSpace Developers (see and include the full [authors file](AUTHORS.md)). 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jSpace 2 | jSpace is a Java library that supports programming of Concurrent and Distributed applications with Spaces. 3 | 4 | jSpace borns as a fork of jResp (https://github.com/michele-loreti/jResp). 5 | 6 | ## Requirements 7 | jSpace requires [Java 1.8](https://java.com/) (or higher). Moreover, jSpace is using [Maven](https://maven.apache.org/index.html) for dependency management and building (you can also use one of the IDE that supports it). 8 | 9 | ## Tutorial 10 | 11 | 1. [Building and Installing jSpace](./docs/getting_started.md) 12 | 2. [Import jSpace Code in Eclipse](./docs/eclipse_jspace.md) 13 | 3. [Your first jSpace App in Eclipse](./docs/first_application_eclipse.md) 14 | 4. [Your first jSpace App with Maven](./docs/first_application_maven.md) 15 | 5. [Configuring Serialization in jSpace](./docs/jspace_serialization.md) 16 | -------------------------------------------------------------------------------- /common/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.classpath 3 | /.DS_Store 4 | -------------------------------------------------------------------------------- /common/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | common 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /common/.settings/.gitignore: -------------------------------------------------------------------------------- 1 | /org.eclipse.core.resources.prefs 2 | /org.eclipse.jdt.core.prefs 3 | /org.eclipse.m2e.core.prefs 4 | -------------------------------------------------------------------------------- /common/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | io.github.pspaces 7 | jspace 8 | 1.0-SNAPSHOT 9 | .. 10 | 11 | 12 | common 13 | 14 | Core API 15 | jSpace core API. 16 | 17 | io.github.pspaces.jspace 18 | 19 | -------------------------------------------------------------------------------- /common/src/.gitignore: -------------------------------------------------------------------------------- 1 | /.DS_Store 2 | -------------------------------------------------------------------------------- /common/src/main/.gitignore: -------------------------------------------------------------------------------- 1 | /.DS_Store 2 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/.gitignore: -------------------------------------------------------------------------------- 1 | /.DS_Store 2 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/ActualField.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | package org.jspace; 24 | 25 | /** 26 | * Instances of this class identifies actual template fields. 27 | */ 28 | public class ActualField implements TemplateField { 29 | 30 | /** 31 | * 32 | */ 33 | private static final long serialVersionUID = 1L; 34 | 35 | /** 36 | * Field value. 37 | */ 38 | private Object value; 39 | 40 | /** 41 | * Create an actual field with value value 42 | * 43 | * @param value 44 | * field value 45 | */ 46 | public ActualField(Object value) { 47 | if (value == null) { 48 | throw new NullPointerException(); 49 | } 50 | this.value = value; 51 | } 52 | 53 | /* 54 | * (non-Javadoc) 55 | * 56 | * @see org.cmg.scel.knowledge.TemplateField#match(java.lang.Object) 57 | */ 58 | @Override 59 | public boolean match(Object o) { 60 | return (value == o) || ((value != null) && value.equals(o)); 61 | } 62 | 63 | @Override 64 | public boolean equals(Object obj) { 65 | if (obj instanceof ActualField) { 66 | return value.equals(((ActualField) obj).value); 67 | } 68 | return false; 69 | } 70 | 71 | @Override 72 | public int hashCode() { 73 | return value.hashCode(); 74 | } 75 | 76 | @Override 77 | public String toString() { 78 | return value.toString(); 79 | } 80 | 81 | public Object getValue() { 82 | return value; 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/FormalField.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace; 25 | 26 | /** 27 | * Identifies a formal template field. 28 | */ 29 | public class FormalField implements TemplateField { 30 | 31 | /** 32 | * 33 | */ 34 | private static final long serialVersionUID = 1L; 35 | 36 | /** 37 | * Type of matching value. 38 | */ 39 | private Class type; 40 | 41 | /** 42 | * Creates a template field matching any value of type type. 43 | * 44 | * @param type Class of expected field. 45 | */ 46 | public FormalField(Class type) { 47 | this.type = type; 48 | } 49 | 50 | /*** 51 | * Returns true if parameters o is instance of this.type. 52 | * 53 | * @see org.pspaces.jspace.cmg.jresp.knowledge.TemplateField#match(java.lang.Object) 54 | */ 55 | @Override 56 | public boolean match(Object o) { 57 | Object value = o; 58 | if (value == null) {//TODO: Are 'null' values allowed in Tuples? 59 | return false; 60 | } 61 | if (value instanceof ActualField) { 62 | value = ((ActualField) value).getValue(); 63 | } 64 | return (o == null) || (type.isInstance(o)); 65 | } 66 | 67 | @Override 68 | public boolean equals(Object obj) { 69 | if (obj instanceof FormalField) { 70 | return type.equals(((FormalField) obj).type); 71 | } 72 | return false; 73 | } 74 | 75 | @Override 76 | public int hashCode() { 77 | return type.hashCode(); 78 | } 79 | 80 | @Override 81 | public String toString() { 82 | return "?{" + type.getName()+ "}"; 83 | } 84 | 85 | public Class getFormalFieldType() { 86 | return type; 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/PileSpace.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace; 25 | 26 | public class PileSpace extends SequentialSpace { 27 | 28 | public PileSpace() { 29 | this(-1); 30 | } 31 | 32 | public PileSpace(int bound) { 33 | super(bound); 34 | } 35 | 36 | @Override 37 | protected void addTuple(Tuple tuple) { 38 | tuples.push(tuple); 39 | } 40 | 41 | 42 | } -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/QueueSpace.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace; 25 | 26 | import java.util.Iterator; 27 | import java.util.LinkedList; 28 | import java.util.List; 29 | 30 | 31 | public class QueueSpace extends SequentialSpace { 32 | 33 | public QueueSpace() { 34 | this(-1); 35 | } 36 | 37 | public QueueSpace(int bound) { 38 | super(bound); 39 | } 40 | 41 | @Override 42 | protected void addTuple(Tuple tuple) { 43 | tuples.add(tuple); 44 | } 45 | 46 | @Override 47 | protected Tuple findTuple(Template template,boolean toRemove) { 48 | Tuple t = tuples.peek(); 49 | if ((t!=null)&&(template.match(t))) { 50 | if (toRemove) { 51 | tuples.poll(); 52 | } 53 | return t; 54 | } else { 55 | return null; 56 | } 57 | } 58 | 59 | @Override 60 | protected LinkedList findAllTuples(Template template,boolean toRemove) { 61 | LinkedList result = new LinkedList(); 62 | Iterator tuplesIterator = tuples.iterator(); 63 | Tuple t; 64 | while (tuplesIterator.hasNext()){ 65 | t = tuplesIterator.next(); 66 | if (template.match(t)) { 67 | result.add(t.getTuple()); 68 | if (toRemove) 69 | tuplesIterator.remove(); 70 | } else { 71 | break ; 72 | } 73 | } 74 | return result; 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/RandomSpace.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace; 25 | 26 | import java.util.Random; 27 | 28 | public class RandomSpace extends SequentialSpace { 29 | 30 | private Random random; 31 | 32 | public RandomSpace() { 33 | this(-1,new Random()); 34 | } 35 | 36 | public RandomSpace(int bound) { 37 | this(bound,new Random()); 38 | } 39 | 40 | public RandomSpace(long seed) { 41 | this(-1,new Random(seed)); 42 | } 43 | 44 | public RandomSpace(int bound, long seed) { 45 | this(bound,new Random(seed)); 46 | } 47 | 48 | public RandomSpace(int bound, Random r) { 49 | super(bound); 50 | this.random = r; 51 | } 52 | 53 | // New version: does not provide uniform distribution on matching tuples but is more performant 54 | protected Tuple findTuple(Template template,boolean toRemove) { 55 | if (tuples.size() == 0) return null; 56 | Tuple t; 57 | int j = 0; 58 | int startI = random.nextInt(tuples.size()); 59 | for (int i = 0; i < tuples.size(); i++) { 60 | // randomise starting index 61 | j = (startI+i)%tuples.size(); 62 | t = tuples.get((startI+i)%tuples.size()); 63 | if (template.match(t)) { 64 | if (toRemove) { 65 | tuples.remove(j); 66 | } 67 | return t; 68 | } 69 | } 70 | return null; 71 | 72 | } 73 | 74 | // Previous version: less performant but provides uniform choice on the set of matching tuples 75 | /* 76 | protected Tuple findTuple(Template template,boolean toRemove) { 77 | ArrayList data = new ArrayList<>(); 78 | Iterator tuplesIterator = tuples.iterator(); 79 | while (tuplesIterator.hasNext()) { 80 | Tuple t = tuplesIterator.next(); 81 | if (template.match(t)) { 82 | data.add(t); 83 | } 84 | } 85 | if (data.isEmpty()) { 86 | return null; 87 | } 88 | Tuple t = data.get(random.nextInt(data.size())); 89 | if (toRemove) { 90 | tuples.remove(t); 91 | } 92 | return t; 93 | } 94 | */ 95 | 96 | } 97 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/RemoteSpace.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace; 25 | 26 | import java.io.IOException; 27 | import java.net.URI; 28 | import java.net.UnknownHostException; 29 | import java.util.Arrays; 30 | import java.util.List; 31 | 32 | import org.apache.commons.lang3.NotImplementedException; 33 | import org.jspace.gate.ClientGate; 34 | import org.jspace.gate.GateFactory; 35 | import org.jspace.protocol.ClientMessage; 36 | import org.jspace.protocol.ServerMessage; 37 | 38 | /** 39 | * @author loreti 40 | * 41 | */ 42 | public class RemoteSpace implements Space { 43 | 44 | private final URI uri; 45 | private final ClientGate gate; 46 | 47 | public RemoteSpace( URI uri ) throws UnknownHostException, IOException { 48 | this.uri = uri; 49 | this.gate = GateFactory.getInstance().getGateBuilder( uri.getScheme() ).createClientGate(uri); 50 | this.gate.open(); 51 | } 52 | 53 | 54 | public RemoteSpace(String uri) throws UnknownHostException, IOException { 55 | this(URI.create(uri)); 56 | } 57 | 58 | 59 | @Override 60 | public int size() { 61 | throw new NotImplementedException("RemoteSpace does not support the `size()` method"); 62 | } 63 | 64 | @Override 65 | public boolean put(Object ... fields) throws InterruptedException { 66 | ServerMessage response; 67 | try { 68 | response = gate.send(ClientMessage.putRequest(new Tuple(fields))); 69 | } catch (IOException e) { 70 | //TODO: Replace with a specific exception 71 | throw new InterruptedException(e.getMessage()); 72 | } 73 | return response.isSuccessful(); 74 | } 75 | 76 | @Override 77 | public Object[] get(TemplateField ... fields) throws InterruptedException { 78 | return _get(new Template(fields),true); 79 | } 80 | 81 | @Override 82 | public Object[] getp(TemplateField ... fields) throws InterruptedException { 83 | return _get(new Template(fields),false); 84 | } 85 | 86 | private Object[] _get(Template template, boolean isBlocking) throws InterruptedException { 87 | ServerMessage response; 88 | try { 89 | response = gate.send(ClientMessage.getRequest(template,isBlocking,false)); 90 | } catch (IOException e) { 91 | //TODO: Replace with a specific exception 92 | throw new InterruptedException(e.getMessage()); 93 | } 94 | 95 | if (response.isSuccessful()) { 96 | List tuples = response.getTuples(); 97 | if (tuples.size()==0) { 98 | return null; 99 | } 100 | return tuples.get(0); 101 | } 102 | 103 | if (isBlocking) throw new InterruptedException("remote space does not exist!"); 104 | return null; 105 | } 106 | 107 | @Override 108 | public List getAll(TemplateField ... fields) throws InterruptedException { 109 | ServerMessage response; 110 | try { 111 | response = gate.send(ClientMessage.getRequest(new Template(fields),false,true)); 112 | } catch (IOException e) { 113 | //TODO: Replace with a specific exception 114 | throw new InterruptedException(e.getMessage()); 115 | } 116 | if (response.isSuccessful()) { 117 | return response.getTuples(); 118 | } 119 | 120 | return null; 121 | } 122 | 123 | 124 | @Override 125 | public Object[] query(TemplateField ... fields) throws InterruptedException { 126 | return _query(new Template(fields), true); 127 | } 128 | 129 | @Override 130 | public Object[] queryp(TemplateField ... fields) throws InterruptedException { 131 | return _query(new Template(fields), false); 132 | } 133 | 134 | private Object[] _query(Template template, boolean isBlocking) throws InterruptedException { 135 | ServerMessage response; 136 | try { 137 | response = gate.send(ClientMessage.queryRequest(template,isBlocking,false)); 138 | } catch (IOException e) { 139 | throw new InterruptedException(e.getMessage()); 140 | } 141 | 142 | if (response.isSuccessful()) { 143 | List tuples = response.getTuples(); 144 | if (tuples.size()==0) { 145 | return null; 146 | } 147 | return tuples.get(0); 148 | } 149 | 150 | if (isBlocking) throw new InterruptedException("remote space does not exist!"); 151 | return null; 152 | } 153 | 154 | @Override 155 | public List queryAll(TemplateField ... fields) throws InterruptedException { 156 | ServerMessage response; 157 | try { 158 | response = gate.send(ClientMessage.queryRequest(new Template(fields),false,true)); 159 | } catch (IOException e) { 160 | //TODO: Replace with a specific exception 161 | throw new InterruptedException(e.getMessage()); 162 | } 163 | if (response.isSuccessful()) { 164 | return response.getTuples(); 165 | } 166 | return null; 167 | } 168 | 169 | 170 | public URI getUri() { 171 | return uri; 172 | } 173 | 174 | 175 | public ClientGate getGate() { 176 | return gate; 177 | } 178 | 179 | // @Override 180 | // public Space map(Function f) throws InterruptedException { 181 | // // TODO Auto-generated method stub 182 | // return null; 183 | // } 184 | // 185 | // @Override 186 | // public T1 reduce(BiFunction f, Comparator comp, T1 v) throws InterruptedException { 187 | // // TODO Auto-generated method stub 188 | // return null; 189 | // } 190 | 191 | public void close() throws IOException { 192 | gate.close(); 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/SequentialSpace.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace; 25 | 26 | import java.util.Arrays; 27 | import java.util.Iterator; 28 | import java.util.LinkedList; 29 | 30 | public class SequentialSpace implements Space { 31 | 32 | protected final int bound; 33 | 34 | protected final LinkedList tuples = new LinkedList<>(); 35 | 36 | /** 37 | * Create an unbounded sequential space. 38 | */ 39 | public SequentialSpace() { 40 | this(-1); 41 | } 42 | 43 | /** 44 | * Create a new sequential space with bound that limits the number of tuples that can be inserted in the space. 45 | * 46 | * @param bound max number of tuples in the space. 47 | */ 48 | public SequentialSpace(int bound) { 49 | this.bound = (bound<=0?-1:bound); 50 | } 51 | 52 | /* 53 | * (non-Javadoc) 54 | * @see org.jspace.Space#put(java.lang.Object[]) 55 | */ 56 | @Override 57 | public synchronized boolean put(Object ... fields) throws InterruptedException { 58 | while ((this.bound>0)&&(this.tuples.size()>=bound)) { 59 | wait(); 60 | } 61 | addTuple(new Tuple(Arrays.copyOf(fields, fields.length))); 62 | notifyAll(); 63 | return true; 64 | } 65 | 66 | protected synchronized void addTuple(Tuple tuple) { 67 | tuples.add(tuple); 68 | } 69 | 70 | /* 71 | * (non-Javadoc) 72 | * @see org.jspace.Space#get(org.jspace.TemplateField[]) 73 | */ 74 | @Override 75 | public synchronized Object[] get(TemplateField ... fields) throws InterruptedException { 76 | while (true) { 77 | Tuple result = findTuple(new Template(Arrays.copyOf(fields,fields.length)),true); 78 | if (result != null) { 79 | notifyAll(); 80 | return result.getTuple(); 81 | } 82 | wait(); 83 | } 84 | } 85 | 86 | protected synchronized Tuple findTuple(Template template,boolean toRemove) { 87 | Iterator tuplesIterator = tuples.iterator(); 88 | while (tuplesIterator.hasNext()) { 89 | Tuple t = tuplesIterator.next(); 90 | if (template.match(t)) { 91 | if (toRemove) { 92 | tuplesIterator.remove(); 93 | } 94 | return t; 95 | } 96 | } 97 | return null; 98 | } 99 | 100 | protected synchronized LinkedList findAllTuples(Template template,boolean toRemove) { 101 | LinkedList result = new LinkedList(); 102 | Iterator tuplesIterator = tuples.iterator(); 103 | Tuple t; 104 | while (tuplesIterator.hasNext()){ 105 | t = tuplesIterator.next(); 106 | if (template.match(t)){ 107 | result.add(t.getTuple()); 108 | if (toRemove) 109 | tuplesIterator.remove(); 110 | } 111 | } 112 | return result; 113 | } 114 | 115 | 116 | /* 117 | * (non-Javadoc) 118 | * @see org.jspace.Space#getp(org.jspace.TemplateField[]) 119 | */ 120 | @Override 121 | public synchronized Object[] getp(TemplateField ... fields) { 122 | Tuple result = findTuple(new Template(Arrays.copyOf(fields,fields.length)),true); 123 | if (result != null) { 124 | notifyAll(); 125 | return result.getTuple(); 126 | } 127 | return null; 128 | } 129 | 130 | /* 131 | * (non-Javadoc) 132 | * @see org.jspace.Space#getAll(org.jspace.TemplateField[]) 133 | */ 134 | @Override 135 | public synchronized LinkedList getAll(TemplateField ... fields){ 136 | LinkedList result = findAllTuples(new Template(Arrays.copyOf(fields,fields.length)),true); 137 | if (result.size()>0) { 138 | notifyAll(); 139 | } 140 | return result; 141 | } 142 | 143 | /* 144 | * (non-Javadoc) 145 | * @see org.jspace.Space#query(org.jspace.TemplateField[]) 146 | */ 147 | @Override 148 | public synchronized Object[] query(TemplateField ... fields) throws InterruptedException { 149 | while(true){ 150 | Tuple result = findTuple(new Template(Arrays.copyOf(fields,fields.length)),false); 151 | if (result != null) { 152 | return result.getTuple(); 153 | } 154 | wait(); 155 | } 156 | } 157 | 158 | /* 159 | * (non-Javadoc) 160 | * @see org.jspace.Space#queryp(org.jspace.TemplateField[]) 161 | */ 162 | @Override 163 | public synchronized Object[] queryp(TemplateField ... fields){ 164 | Tuple result = findTuple(new Template(Arrays.copyOf(fields,fields.length)),false); 165 | return (result==null?null:result.getTuple()); 166 | } 167 | 168 | /* 169 | * (non-Javadoc) 170 | * @see org.jspace.Space#queryAll(org.jspace.TemplateField[]) 171 | */ 172 | @Override 173 | public synchronized LinkedList queryAll(TemplateField ... fields){ 174 | return findAllTuples(new Template(Arrays.copyOf(fields,fields.length)),false); 175 | } 176 | 177 | 178 | // @Override 179 | // public Space map(Function f) throws InterruptedException { 180 | // LinkedList result = new LinkedList(); 181 | // Tuple temp; 182 | // for(Tuple t:this.tuples){ 183 | // temp = f.apply(t); 184 | // result.add(temp); 185 | // } 186 | // return new SequentialSpace(result); 187 | // } 188 | // 189 | // @Override 190 | // public T1 reduce(BiFunction f, Comparator comp, T1 v) throws InterruptedException { 191 | // LinkedList temp = queryAll(); 192 | // temp.sort(comp); 193 | // for (Tuple t:temp) 194 | // v = f.apply(t, v); 195 | // return v; 196 | // } 197 | 198 | @Override 199 | public synchronized int size() { 200 | return tuples.size(); 201 | } 202 | 203 | } -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/SpaceFactory.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | package org.jspace; 24 | 25 | /** 26 | * Factory used to build spaces. 27 | * 28 | * @author loreti 29 | * 30 | */ 31 | public interface SpaceFactory { 32 | 33 | /** 34 | * Create a new space with a given max number of elements inside. If bound 35 | * is <=0 then the space is unbounded. 36 | * 37 | * @param bound a limit on the number of elements in the space. 38 | * @return a new space. 39 | */ 40 | public Space newSpace( int bound ); 41 | 42 | } 43 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/StackSpace.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace; 25 | 26 | public class StackSpace extends QueueSpace { 27 | 28 | /** 29 | * Create an unbounded space based on stack. 30 | */ 31 | public StackSpace() { 32 | this(-1); 33 | } 34 | 35 | /** 36 | * Create a space based on stack of max size bound. 37 | * @param bound 38 | */ 39 | public StackSpace(int bound) { 40 | super(bound); 41 | } 42 | 43 | @Override 44 | protected void addTuple(Tuple tuple) { 45 | tuples.push(tuple); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/Template.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace; 25 | 26 | import java.io.Serializable; 27 | import java.util.Arrays; 28 | import java.util.Iterator; 29 | 30 | /** 31 | * An instance of class Template is used as a pattern to select 32 | * tuples in a space. 33 | */ 34 | public final class Template implements Iterable, Serializable { 35 | 36 | /** 37 | * 38 | */ 39 | private static final long serialVersionUID = 1L; 40 | 41 | /** 42 | * A list of template fields. 43 | */ 44 | private TemplateField[] fields; 45 | 46 | /** 47 | * Creates a new template starting from its fields. 48 | * 49 | * @param fields 50 | */ 51 | public Template(TemplateField...fields) { 52 | this.fields = fields; 53 | } 54 | 55 | public Template(Object...fields) { 56 | this(Template.toTemplateFields(fields)); 57 | } 58 | 59 | private static TemplateField[] toTemplateFields(Object[] values) { 60 | TemplateField[] fields = new TemplateField[values.length]; 61 | for( int i=0 ; it matches the tempalte. 73 | * 74 | * @param t 75 | * tuple to match 76 | * @return true if the tuple matches against this template, 77 | * false otherwise. 78 | */ 79 | public boolean match(Tuple t) { 80 | int size = this.length(); 81 | if (size != t.length()) { 82 | return false; 83 | } 84 | for (int i = 0; i < size; i++) { 85 | if (!fields[i].match(t.getElementAt(i))) { 86 | return false; 87 | } 88 | } 89 | return true; 90 | } 91 | 92 | /** 93 | * Returns the number of fields in the template. 94 | * 95 | * @return number of fields in the template. 96 | */ 97 | public int length() { 98 | return fields.length; 99 | } 100 | 101 | @Override 102 | public boolean equals(Object obj) { 103 | if (obj instanceof Template) { 104 | return Arrays.deepEquals(fields, ((Template) obj).fields); 105 | } 106 | return false; 107 | } 108 | 109 | @Override 110 | public int hashCode() { 111 | return Arrays.hashCode(fields); 112 | } 113 | 114 | @Override 115 | public String toString() { 116 | return Arrays.deepToString(fields); 117 | } 118 | 119 | @Override 120 | public Iterator iterator() { 121 | return new Iterator() { 122 | 123 | private int current = 0; 124 | 125 | @Override 126 | public boolean hasNext() { 127 | return (current < fields.length); 128 | } 129 | 130 | @Override 131 | public TemplateField next() { 132 | return fields[current++]; 133 | } 134 | 135 | @Override 136 | public void remove() { 137 | } 138 | }; 139 | } 140 | 141 | public TemplateField getElementAt(int i) { 142 | return fields[i]; 143 | } 144 | 145 | public TemplateField[] getFields() { 146 | return fields; 147 | } 148 | 149 | } 150 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/TemplateField.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace; 25 | 26 | 27 | import java.io.Serializable; 28 | 29 | /** 30 | * Identifies a generic template field. 31 | */ 32 | public interface TemplateField extends Serializable { 33 | 34 | /** 35 | * Checks if the object o matches against this field. 36 | * 37 | * @param o 38 | * a generic object 39 | * @return true if the object o matches against 40 | * this field. 41 | */ 42 | public boolean match(Object o); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/Tuple.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace; 25 | 26 | import java.io.Serializable; 27 | import java.util.Arrays; 28 | import java.util.Iterator; 29 | 30 | /** 31 | * A tuple identifies the basic information item. It consists of a sequence of 32 | * values that can be collected into a knowledge repository. 33 | */ 34 | public final class Tuple implements Iterable, Serializable { 35 | 36 | /** 37 | * 38 | */ 39 | private static final long serialVersionUID = 1L; 40 | 41 | /** 42 | * Tuple fields. 43 | */ 44 | private Object[] fields; 45 | 46 | /** 47 | * Creates a new tuple. 48 | * 49 | * @param fields 50 | * fields of new created tuple. 51 | */ 52 | public Tuple(Object... fields) { 53 | this.fields = fields; 54 | } 55 | 56 | /** 57 | * Returns the tuple length. 58 | * 59 | * @return the tuple length. 60 | */ 61 | public int length() { 62 | return fields.length; 63 | } 64 | 65 | /** 66 | * Returns the element at index i. 67 | * 68 | * @param i 69 | * element index. 70 | * @return the element at index i. 71 | */ 72 | public Object getElementAt(int i) { 73 | return fields[i]; 74 | } 75 | 76 | /** 77 | * Returns the class c of the element with index i 78 | * . 79 | * 80 | * @param i 81 | * element index 82 | * @return the class c of the element with index i 83 | * . 84 | */ 85 | public Class getTypeAt(int i) { 86 | return fields[i].getClass(); 87 | } 88 | 89 | /** 90 | * Returns the instance of class c at element i. 91 | * This method is equivalent to c.cast(getElementAt(i)). A 92 | * ClassCastException is thrown if the i-th 93 | * element of the tuple is not an instance if c. 94 | * 95 | * @param c 96 | * expected class 97 | * @param i 98 | * element index 99 | * @return the instance of class c at element i. 100 | */ 101 | public T getElementAt(Class c, int i) { 102 | Object o = getElementAt(i); 103 | if (o == null) { 104 | return null; 105 | } 106 | if (c.isInstance(o)) { 107 | return c.cast(o); 108 | } 109 | throw new ClassCastException(); 110 | } 111 | 112 | /** 113 | * Checks if the element at position i is instance of c. 114 | * 115 | * @param c Class 116 | * @param i Element index 117 | * @return true if element at i is instance of c. 118 | */ 119 | public boolean isInstance(Class c, int i) { 120 | return c.isInstance(fields[i]); 121 | } 122 | 123 | @Override 124 | public boolean equals(Object obj) { 125 | if (obj instanceof Tuple) { 126 | return Arrays.deepEquals(fields, ((Tuple) obj).fields); 127 | } 128 | return false; 129 | } 130 | 131 | @Override 132 | public int hashCode() { 133 | return Arrays.hashCode(fields); 134 | } 135 | 136 | @Override 137 | public String toString() { 138 | return Arrays.deepToString(fields); 139 | } 140 | 141 | @Override 142 | public Iterator iterator() { 143 | return new Iterator() { 144 | 145 | private int current = 0; 146 | 147 | @Override 148 | public boolean hasNext() { 149 | return current < fields.length; 150 | } 151 | 152 | @Override 153 | public Object next() { 154 | return fields[current++]; 155 | } 156 | 157 | @Override 158 | public void remove() { 159 | } 160 | 161 | }; 162 | } 163 | 164 | public Object[] getTuple() { 165 | return Arrays.copyOf(this.fields,this.fields.length); 166 | } 167 | 168 | } 169 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/gate/ClientGate.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.gate; 25 | 26 | import java.io.IOException; 27 | import java.net.UnknownHostException; 28 | 29 | import org.jspace.protocol.ClientMessage; 30 | import org.jspace.protocol.ServerMessage; 31 | 32 | /** 33 | * This interface 34 | */ 35 | public interface ClientGate { 36 | 37 | public void open() throws UnknownHostException, IOException; 38 | 39 | public ServerMessage send( ClientMessage m ) throws UnknownHostException, IOException, InterruptedException; 40 | 41 | public void close() throws IOException; 42 | 43 | } 44 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/gate/ClientHandler.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.gate; 25 | 26 | import java.io.IOException; 27 | 28 | import org.jspace.protocol.ClientMessage; 29 | import org.jspace.protocol.ServerMessage; 30 | 31 | /** 32 | * 33 | */ 34 | public interface ClientHandler { 35 | 36 | public ClientMessage receive( ) throws IOException; 37 | 38 | public boolean send( ServerMessage m ); 39 | 40 | public boolean isActive(); 41 | 42 | public void close() throws IOException; 43 | 44 | public boolean isClosed(); 45 | 46 | } 47 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/gate/ConnClientGate.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.gate; 25 | 26 | import java.io.BufferedReader; 27 | import java.io.IOException; 28 | import java.io.InputStreamReader; 29 | import java.io.OutputStreamWriter; 30 | import java.io.PrintWriter; 31 | import java.net.InetSocketAddress; 32 | import java.net.Socket; 33 | import java.net.UnknownHostException; 34 | 35 | import org.jspace.io.jSpaceMarshaller; 36 | import org.jspace.protocol.ClientMessage; 37 | import org.jspace.protocol.ServerMessage; 38 | 39 | /** 40 | * @author loreti 41 | * 42 | */ 43 | public class ConnClientGate implements ClientGate { 44 | 45 | 46 | private final jSpaceMarshaller marshaller; 47 | private String host; 48 | private int port; 49 | private String target; 50 | 51 | public ConnClientGate( jSpaceMarshaller marshaller , String host, int port, String target) { 52 | this.marshaller = marshaller; 53 | this.host = host; 54 | this.port = port; 55 | this.target = target; 56 | } 57 | 58 | @Override 59 | public ServerMessage send(ClientMessage m) throws InterruptedException, UnknownHostException, IOException { 60 | ConnInteractionHandler handler = new ConnInteractionHandler(); 61 | new Thread( () -> handler.send(m) ).start(); 62 | return handler.getResponce(); 63 | } 64 | 65 | @Override 66 | public void open() throws UnknownHostException, IOException { 67 | } 68 | 69 | @Override 70 | public void close() throws IOException { 71 | } 72 | 73 | public class ConnInteractionHandler { 74 | 75 | private ServerMessage message; 76 | private IOException exception; 77 | private Socket socket; 78 | private BufferedReader reader; 79 | private PrintWriter writer; 80 | 81 | public ConnInteractionHandler( ) throws UnknownHostException, IOException { 82 | socket = new Socket(host, port); 83 | reader = new BufferedReader( new InputStreamReader(socket.getInputStream()) ); 84 | writer = new PrintWriter(socket.getOutputStream()); 85 | } 86 | 87 | public void send( ClientMessage m ) { 88 | m.setTarget(target); 89 | marshaller.write(m, writer); 90 | try { 91 | setMessage(marshaller.read(ServerMessage.class, reader)); 92 | } catch (IOException e) { 93 | setException(e); 94 | } 95 | } 96 | 97 | public synchronized void setMessage( ServerMessage message ) { 98 | this.message = message; 99 | notifyAll(); 100 | } 101 | 102 | public synchronized void setException( IOException exception ) { 103 | this.exception = exception; 104 | notifyAll(); 105 | } 106 | 107 | public synchronized ServerMessage getResponce( ) throws InterruptedException, IOException { 108 | while ((message == null)&&(exception==null)) { 109 | wait(); 110 | } 111 | if (exception != null) { 112 | throw exception; 113 | } 114 | return message; 115 | } 116 | } 117 | 118 | } 119 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/gate/ConnClientHandler.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.gate; 25 | 26 | import java.io.BufferedReader; 27 | import java.io.IOException; 28 | import java.io.InputStreamReader; 29 | import java.io.PrintWriter; 30 | import java.net.Socket; 31 | 32 | import org.jspace.io.jSpaceMarshaller; 33 | import org.jspace.protocol.ClientMessage; 34 | import org.jspace.protocol.ServerMessage; 35 | 36 | /** 37 | * @author loreti 38 | * 39 | */ 40 | public class ConnClientHandler implements ClientHandler { 41 | 42 | private jSpaceMarshaller marshaller; 43 | private Socket client; 44 | private BufferedReader reader; 45 | private PrintWriter writer; 46 | private boolean isActive = true; 47 | private boolean isClosed = false; 48 | 49 | public ConnClientHandler(jSpaceMarshaller marshaller, Socket client) throws IOException { 50 | this.marshaller = marshaller; 51 | this.client = client; 52 | this.reader = new BufferedReader(new InputStreamReader( client.getInputStream() )); 53 | this.writer = new PrintWriter( client.getOutputStream() ); 54 | } 55 | 56 | /* (non-Javadoc) 57 | * @see org.jspace.gate.ClientHandler#receive() 58 | */ 59 | @Override 60 | public ClientMessage receive() throws IOException { 61 | ClientMessage message = marshaller.read(ClientMessage.class, reader); 62 | isActive = (message == null); 63 | return message; 64 | } 65 | 66 | /* (non-Javadoc) 67 | * @see org.jspace.gate.ClientHandler#send(org.jspace.protocol.ServerMessage) 68 | */ 69 | @Override 70 | public boolean send(ServerMessage m) { 71 | marshaller.write(m, writer); 72 | isActive = false; 73 | try { 74 | close(); 75 | } catch (IOException e) { 76 | e.printStackTrace(); 77 | } 78 | return true; 79 | } 80 | 81 | /* (non-Javadoc) 82 | * @see org.jspace.gate.ClientHandler#isActive() 83 | */ 84 | @Override 85 | public boolean isActive() { 86 | return isActive; 87 | } 88 | 89 | @Override 90 | public synchronized void close() throws IOException { 91 | client.close(); 92 | isClosed = true; 93 | } 94 | 95 | @Override 96 | public synchronized boolean isClosed() { 97 | return isClosed; 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/gate/ConnServerGate.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.gate; 25 | 26 | import java.io.IOException; 27 | import java.net.InetSocketAddress; 28 | import java.net.ServerSocket; 29 | import java.net.Socket; 30 | import java.net.URI; 31 | import java.net.URISyntaxException; 32 | 33 | import org.jspace.io.jSpaceMarshaller; 34 | 35 | /** 36 | * @author loreti 37 | * 38 | */ 39 | public class ConnServerGate extends TcpServerGate implements ServerGate { 40 | 41 | private static final String CONN_CODE = "conn"; 42 | 43 | public ConnServerGate(jSpaceMarshaller marshaller, InetSocketAddress address, int backlog) { 44 | super(marshaller,address,backlog); 45 | } 46 | 47 | @Override 48 | protected ClientHandler getClientHandler(Socket socket) throws IOException { 49 | return new ConnClientHandler(marshaller,socket); 50 | } 51 | 52 | @Override 53 | protected String getConnectionCode() { 54 | return CONN_CODE; 55 | } 56 | 57 | 58 | 59 | } 60 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/gate/ExceptionLogger.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | package org.jspace.gate; 24 | 25 | /** 26 | * @author loreti 27 | * 28 | */ 29 | public interface ExceptionLogger { 30 | 31 | public void logException( Exception e ); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/gate/GateBuilder.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.gate; 25 | 26 | import java.net.URI; 27 | 28 | /** 29 | * @author loreti 30 | * 31 | */ 32 | public interface GateBuilder { 33 | 34 | public ClientGate createClientGate(URI uri); 35 | 36 | public ServerGate createServerGate(URI uri); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/gate/GateFactory.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.gate; 25 | 26 | import java.net.URI; 27 | import java.util.HashMap; 28 | import java.util.function.Function; 29 | 30 | import org.jspace.io.MarshalFactory; 31 | 32 | /** 33 | * @author loreti 34 | * 35 | */ 36 | public class GateFactory { 37 | 38 | public final static String LANGUAGE_QUERY_ELEMENT = "lang"; 39 | public final static String MODE_QUERY_ELEMENT = "mode"; 40 | 41 | public final static String TCP_PROTOCOL = "tcp"; 42 | public final static String UDP_PROTOCOL = "udp"; 43 | public final static String HTTP_PROTOCOL = "http"; 44 | public final static String HTTPS_PROTOCOL = "https"; 45 | private static GateFactory instance; 46 | 47 | private HashMap gateBuilders; 48 | 49 | private GateFactory( ) { 50 | this.gateBuilders = new HashMap<>(); 51 | init(); 52 | } 53 | 54 | private void init() { 55 | this.gateBuilders.put(TCP_PROTOCOL, new TcpGateBuilder()); 56 | this.gateBuilders.put(UDP_PROTOCOL, new UdpGateBuilder()); 57 | } 58 | 59 | public static HashMap parseQuery(String query) { 60 | String[] elements = query.split("&"); 61 | HashMap values = new HashMap<>(); 62 | for (String string : elements) { 63 | String[] pair = string.split("="); 64 | if (pair.length>1) { 65 | values.put(pair[0], pair[1]); 66 | } else { 67 | values.put(pair[0], ""); 68 | } 69 | } 70 | return values; 71 | } 72 | 73 | public static GateFactory getInstance() { 74 | if (instance == null) { 75 | instance = new GateFactory(); 76 | } 77 | return instance; 78 | } 79 | 80 | public GateBuilder getGateBuilder(String scheme) { 81 | return gateBuilders.get(scheme); 82 | } 83 | 84 | public void register( String scheme, GateBuilder builder) { 85 | gateBuilders.put(scheme, builder); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/gate/KeepClientGate.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.gate; 25 | 26 | import java.io.BufferedReader; 27 | import java.io.IOException; 28 | import java.io.InputStreamReader; 29 | import java.io.PrintWriter; 30 | import java.net.Socket; 31 | import java.net.SocketException; 32 | import java.net.UnknownHostException; 33 | import java.util.LinkedList; 34 | 35 | import org.jspace.io.jSpaceMarshaller; 36 | import org.jspace.protocol.ClientMessage; 37 | import org.jspace.protocol.ServerMessage; 38 | import org.jspace.util.Rendezvous; 39 | 40 | /** 41 | * @author loreti 42 | * 43 | */ 44 | public class KeepClientGate implements ClientGate { 45 | 46 | private final jSpaceMarshaller marshaller; 47 | private String host; 48 | private int port; 49 | private Socket socket; 50 | private BufferedReader reader; 51 | private PrintWriter writer; 52 | private String target; 53 | private final Rendezvous inbox; 54 | private final LinkedList outbox; 55 | private boolean status = true; 56 | private int sessionCounter = 0; 57 | 58 | /** 59 | * Fields used in tests to access (via the Reflection API) the threads if needed. 60 | */ 61 | private Thread outboxThread; 62 | private Thread inboxThread; 63 | 64 | public KeepClientGate( jSpaceMarshaller marshaller , String host, int port, String target) { 65 | this.marshaller = marshaller; 66 | this.host = host; 67 | this.port = port; 68 | this.target = target; 69 | this.inbox = new Rendezvous<>(); 70 | this.outbox = new LinkedList<>(); 71 | } 72 | 73 | @Override 74 | public ServerMessage send(ClientMessage m) throws IOException, InterruptedException { 75 | String sessionId; 76 | 77 | synchronized (outbox) { 78 | if (!this.status) { 79 | throw new InterruptedException("Gate is closed!"); 80 | } 81 | sessionId = ""+sessionCounter; 82 | sessionCounter++; 83 | 84 | m.setTarget(target); 85 | m.setClientSession(sessionId); 86 | 87 | outbox.add(m); 88 | outbox.notify(); 89 | } 90 | 91 | return inbox.call(sessionId); 92 | } 93 | 94 | @Override 95 | public void open() throws UnknownHostException, IOException { 96 | this.socket = new Socket(host, port); 97 | this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 98 | this.writer = new PrintWriter(socket.getOutputStream()); 99 | this.outboxThread = new Thread(() -> outboxHandlingMethod()); 100 | this.inboxThread = new Thread(() -> inboxHandlingMethod()); 101 | this.outboxThread.start(); 102 | this.inboxThread.start(); 103 | } 104 | 105 | @Override 106 | public void close() throws IOException { 107 | synchronized (outbox) { 108 | this.status = false; 109 | outbox.notify(); 110 | } 111 | this.socket.close(); 112 | // Closing the socket above should also close the reader and writer. 113 | this.reader.close(); 114 | this.writer.close(); 115 | inbox.interruptAll(); 116 | } 117 | 118 | 119 | private void outboxHandlingMethod() { 120 | try { 121 | synchronized (outbox) { 122 | while (status) { 123 | while (status && outbox.isEmpty()) { 124 | outbox.wait(); 125 | } 126 | if (status) { 127 | marshaller.write(outbox.poll(), writer); 128 | } 129 | } 130 | } 131 | } catch (InterruptedException e) { 132 | // TODO Add Log! 133 | e.printStackTrace(); 134 | } 135 | } 136 | 137 | private void inboxHandlingMethod() { 138 | try { 139 | while (true) { 140 | ServerMessage m = marshaller.read(ServerMessage.class, reader); 141 | if (m != null) { 142 | String session = m.getClientSession(); 143 | if (session != null && inbox.canSet(session)) { 144 | inbox.set(session, m); 145 | } else { 146 | // TODO: Add Log! 147 | System.err.println("Unexpected session id!"); 148 | } 149 | } else { 150 | // m == null ==> EOF ==> Socket was closed from server. 151 | this.close(); 152 | break; 153 | } 154 | } 155 | } catch (IOException e) { 156 | synchronized (outbox) { 157 | if (!this.status) { 158 | // Socket was closed by this client. 159 | return; 160 | } 161 | } 162 | // TODO Add Log! 163 | e.printStackTrace(); 164 | try { 165 | // The gate should be closed in this case. 166 | this.close(); 167 | } catch (IOException e2) { 168 | // TODO Add Log! 169 | e2.printStackTrace(); 170 | } 171 | } 172 | } 173 | 174 | } 175 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/gate/KeepClientHandler.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.gate; 25 | 26 | import java.io.BufferedReader; 27 | import java.io.IOException; 28 | import java.io.InputStreamReader; 29 | import java.io.OutputStreamWriter; 30 | import java.io.PrintWriter; 31 | import java.net.Socket; 32 | 33 | import org.jspace.io.jSpaceMarshaller; 34 | import org.jspace.protocol.ClientMessage; 35 | import org.jspace.protocol.ServerMessage; 36 | 37 | /** 38 | * @author loreti 39 | * 40 | */ 41 | public class KeepClientHandler implements ClientHandler { 42 | 43 | private jSpaceMarshaller marshaller; 44 | private Socket client; 45 | private BufferedReader reader; 46 | private PrintWriter writer; 47 | private boolean isActive = true; 48 | private boolean isClosed = false; 49 | 50 | public KeepClientHandler(jSpaceMarshaller marshaller, Socket client) throws IOException { 51 | this.marshaller = marshaller; 52 | this.client = client; 53 | this.reader = new BufferedReader( new InputStreamReader( client.getInputStream() ) ); 54 | this.writer = new PrintWriter( client.getOutputStream() ); 55 | } 56 | 57 | /* (non-Javadoc) 58 | * @see org.jspace.gate.ClientHandler#receive() 59 | */ 60 | @Override 61 | public ClientMessage receive() throws IOException { 62 | ClientMessage message = marshaller.read(ClientMessage.class, reader); 63 | isActive = (message != null); 64 | return message; 65 | } 66 | 67 | /* (non-Javadoc) 68 | * @see org.jspace.gate.ClientHandler#send(org.jspace.protocol.ServerMessage) 69 | */ 70 | @Override 71 | public boolean send(ServerMessage m) { 72 | if (!isActive()) { 73 | return false; 74 | } 75 | marshaller.write(m, writer); 76 | return true; 77 | } 78 | 79 | /* (non-Javadoc) 80 | * @see org.jspace.gate.ClientHandler#isActive() 81 | */ 82 | @Override 83 | public boolean isActive() { 84 | return isActive; 85 | } 86 | 87 | @Override 88 | public void close() throws IOException { 89 | client.close(); 90 | isClosed = true; 91 | isActive = false; 92 | } 93 | 94 | @Override 95 | public boolean isClosed() { 96 | return isClosed; 97 | } 98 | 99 | 100 | } 101 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/gate/KeepServerGate.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.gate; 25 | 26 | import java.io.IOException; 27 | import java.net.InetSocketAddress; 28 | import java.net.Socket; 29 | 30 | import org.jspace.io.jSpaceMarshaller; 31 | 32 | /** 33 | * @author loreti 34 | * 35 | */ 36 | public class KeepServerGate extends TcpServerGate implements ServerGate { 37 | 38 | private static final String KEEP_CODE = "keep"; 39 | 40 | public KeepServerGate(jSpaceMarshaller marshaller, InetSocketAddress address, int backlog) { 41 | super(marshaller,address,backlog); 42 | } 43 | 44 | @Override 45 | protected ClientHandler getClientHandler(Socket socket) throws IOException { 46 | return new KeepClientHandler(marshaller,socket); 47 | } 48 | 49 | @Override 50 | protected String getConnectionCode() { 51 | return KEEP_CODE; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/gate/ServerGate.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.gate; 25 | 26 | import java.io.IOException; 27 | import java.net.URI; 28 | 29 | /** 30 | * @author loreti 31 | * 32 | */ 33 | public interface ServerGate { 34 | 35 | public void open( ) throws IOException; 36 | 37 | public ClientHandler accept( ) throws IOException; 38 | 39 | public void close( ) throws IOException; 40 | 41 | public URI getURI(); 42 | 43 | public boolean isClosed(); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/gate/TcpGateBuilder.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.gate; 25 | 26 | import java.net.InetSocketAddress; 27 | import java.net.URI; 28 | import java.util.HashMap; 29 | 30 | import org.jspace.io.MarshalFactory; 31 | import org.jspace.io.jSpaceMarshaller; 32 | 33 | /** 34 | * @author loreti 35 | * 36 | */ 37 | public class TcpGateBuilder implements GateBuilder { 38 | 39 | public static final int DEFAULT_BACKLOG = 10; 40 | public static final int DEFAULT_PORT = 9990; 41 | public static final String DEFAULT_MODE = "keep"; 42 | 43 | public static final String KEEP_MODE = "keep"; 44 | public static final String CONN_MODE = "conn"; 45 | public static final String PUSH_MODE = "PUSH"; 46 | public static final String PULL_MODE = "PULL"; 47 | 48 | /* (non-Javadoc) 49 | * @see org.jspace.gate.GateBuilder#createClientGate(java.net.URI) 50 | */ 51 | @Override 52 | public ClientGate createClientGate(URI uri) { 53 | String host = uri.getHost(); 54 | int port = uri.getPort(); 55 | String target = uri.getPath(); 56 | if (target.startsWith("/")) { 57 | target = target.substring(1); 58 | } 59 | if (port < 0) { 60 | port = DEFAULT_PORT; 61 | } 62 | HashMap query = GateFactory.parseQuery(uri.getQuery()); 63 | jSpaceMarshaller marshaller = getMarshaller(query.get(GateFactory.LANGUAGE_QUERY_ELEMENT)); 64 | //String mode = query.getOrDefault(GateFactory.MODE_QUERY_ELEMENT,DEFAULT_MODE).toUpperCase(); 65 | if (query.containsKey(KEEP_MODE)) { 66 | return new KeepClientGate(marshaller, host, port, target); 67 | } 68 | if (query.containsKey(CONN_MODE)) { 69 | return new ConnClientGate(marshaller, host, port, target); 70 | } 71 | //TODO: Add here other modes! 72 | //Default mode 73 | return new KeepClientGate(marshaller, host, port, target); 74 | } 75 | 76 | /* (non-Javadoc) 77 | * @see org.jspace.gate.GateBuilder#createServerGate(java.net.URI) 78 | */ 79 | @Override 80 | public ServerGate createServerGate(URI uri) { 81 | String host = uri.getHost(); 82 | int port = uri.getPort(); 83 | if (port < 0) { 84 | port = DEFAULT_PORT; 85 | } 86 | HashMap query = GateFactory.parseQuery(uri.getQuery()); 87 | jSpaceMarshaller marshaller = getMarshaller(query.get(GateFactory.LANGUAGE_QUERY_ELEMENT)); 88 | String mode = query.getOrDefault(GateFactory.MODE_QUERY_ELEMENT,DEFAULT_MODE).toUpperCase(); 89 | if (query.containsKey(KEEP_MODE)) { 90 | return new KeepServerGate(marshaller,new InetSocketAddress(host, port),DEFAULT_BACKLOG); 91 | } 92 | if (query.containsKey(CONN_MODE)) { 93 | return new ConnServerGate(marshaller, new InetSocketAddress(host, port),DEFAULT_BACKLOG); 94 | } 95 | //TODO: Add here other modes! 96 | return new KeepServerGate(marshaller,new InetSocketAddress(host, port),DEFAULT_BACKLOG); 97 | } 98 | 99 | public jSpaceMarshaller getMarshaller( String code ) { 100 | MarshalFactory mf = MarshalFactory.getInstance(); 101 | if (code == null) { 102 | code = MarshalFactory.DEFAULT_LANGAUGE; 103 | } 104 | return mf.getMarshaller(code); 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/gate/TcpServerGate.java: -------------------------------------------------------------------------------- 1 | package org.jspace.gate; 2 | 3 | import java.io.IOException; 4 | import java.net.InetSocketAddress; 5 | import java.net.ServerSocket; 6 | import java.net.Socket; 7 | import java.net.URI; 8 | import java.net.URISyntaxException; 9 | 10 | import org.jspace.io.jSpaceMarshaller; 11 | 12 | public abstract class TcpServerGate implements ServerGate { 13 | 14 | protected final jSpaceMarshaller marshaller; 15 | protected final InetSocketAddress address; 16 | protected final int backlog; 17 | private ServerSocket ssocket; 18 | protected boolean isClosed; 19 | 20 | public TcpServerGate(jSpaceMarshaller marshaller, InetSocketAddress address, int backlog) { 21 | this.address = address; 22 | this.backlog = backlog; 23 | this.marshaller = marshaller; 24 | this.isClosed = false; 25 | } 26 | 27 | @Override 28 | public synchronized void open() throws IOException { 29 | if (this.isClosed) { 30 | throw new IllegalStateException("Gate is closed!"); 31 | } 32 | if (this.ssocket != null) { 33 | throw new IllegalStateException("Gate is already opened!"); 34 | } 35 | this.ssocket = new ServerSocket(address.getPort(), backlog, address.getAddress()); 36 | } 37 | 38 | @Override 39 | public ClientHandler accept() throws IOException { 40 | if ((this.isClosed)||(this.ssocket == null)) { 41 | //throw new IllegalStateException("Gate is not opened!"); 42 | return null; 43 | } 44 | return getClientHandler(ssocket.accept()); 45 | } 46 | 47 | protected abstract ClientHandler getClientHandler(Socket socket) throws IOException; 48 | 49 | @Override 50 | public synchronized void close() throws IOException { 51 | if ((this.isClosed)||(this.ssocket==null)) { 52 | //throw new IllegalStateException("Gate is not opened!"); 53 | return ; 54 | } 55 | this.isClosed = true; 56 | this.ssocket.close(); 57 | } 58 | 59 | @Override 60 | public URI getURI() { 61 | try { 62 | return new URI("tcp:/"+address+"/"+"?"+getConnectionCode()); 63 | } catch (URISyntaxException e) { 64 | e.printStackTrace(); 65 | return null; 66 | } 67 | } 68 | 69 | protected abstract String getConnectionCode(); 70 | 71 | @Override 72 | public synchronized boolean isClosed() { 73 | return isClosed; 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/gate/UdpGateBuilder.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.gate; 25 | 26 | import java.net.URI; 27 | 28 | /** 29 | * @author loreti 30 | * 31 | */ 32 | public class UdpGateBuilder implements GateBuilder { 33 | 34 | /* (non-Javadoc) 35 | * @see org.jspace.gate.GateBuilder#createClientGate(java.net.URI) 36 | */ 37 | @Override 38 | public ClientGate createClientGate(URI uri) { 39 | // TODO Auto-generated method stub 40 | return null; 41 | } 42 | 43 | /* (non-Javadoc) 44 | * @see org.jspace.gate.GateBuilder#createServerGate(java.net.URI) 45 | */ 46 | @Override 47 | public ServerGate createServerGate(URI uri) { 48 | // TODO Auto-generated method stub 49 | return null; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/io/ClassDictionary.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | package org.jspace.io; 24 | 25 | import java.util.HashMap; 26 | 27 | import org.jspace.Template; 28 | import org.jspace.Tuple; 29 | 30 | /** 31 | * 32 | */ 33 | public class ClassDictionary { 34 | 35 | public static final String BOOLEAN_URI = "pspace:boolean"; 36 | public static final String BYTE_URI = "pspace:byte"; 37 | public static final String CHAR_URI = "pspace:char"; 38 | public static final String INTEGER_URI = "pspace:int"; 39 | public static final String LONG_URI = "pspace:long"; 40 | public static final String FLOAT_URI = "pspace:float"; 41 | public static final String DOUBLE_URI = "pspace:double"; 42 | public static final String STRING_URI = "pspace:string"; 43 | public static final String TUPLE_URI = "pspace:tuple"; 44 | public static final String TEMPLATE_URI = "pspace:template"; 45 | 46 | 47 | 48 | private final HashMap> uriToClass = new HashMap<>(); 49 | private final HashMap,String> classToUri = new HashMap<>(); 50 | 51 | public ClassDictionary() { 52 | init(); 53 | } 54 | 55 | private void init() { 56 | uriToClass.put(BOOLEAN_URI, Boolean.class); 57 | uriToClass.put(BYTE_URI, Byte.class); 58 | uriToClass.put(CHAR_URI, Character.class); 59 | uriToClass.put(INTEGER_URI, Integer.class); 60 | uriToClass.put(LONG_URI, Long.class); 61 | uriToClass.put(FLOAT_URI, Float.class); 62 | uriToClass.put(DOUBLE_URI, Double.class); 63 | uriToClass.put(STRING_URI, String.class); 64 | uriToClass.put(TUPLE_URI, Tuple.class); 65 | uriToClass.put(TEMPLATE_URI, Template.class); 66 | 67 | classToUri.put(Boolean.class, BOOLEAN_URI); 68 | classToUri.put(Byte.class, BYTE_URI); 69 | classToUri.put(Character.class, CHAR_URI); 70 | classToUri.put(Integer.class, INTEGER_URI); 71 | classToUri.put(Long.class, LONG_URI); 72 | classToUri.put(Float.class, FLOAT_URI); 73 | classToUri.put(Double.class, DOUBLE_URI); 74 | classToUri.put(String.class, STRING_URI); 75 | classToUri.put(Tuple.class, TUPLE_URI); 76 | classToUri.put(Template.class, TEMPLATE_URI); 77 | } 78 | 79 | public void register(String uri, Class clazz) { 80 | if (uriToClass.containsKey(uri)) { 81 | throw new IllegalArgumentException("Duplicated uri!"); 82 | } 83 | if (classToUri.containsKey(clazz)) { 84 | throw new IllegalArgumentException("Duplicated class!"); 85 | } 86 | } 87 | 88 | public boolean isRegistered( String uri ) { 89 | return uriToClass.containsKey(uri); 90 | } 91 | 92 | public boolean isRegistered( Class clazz ) { 93 | return classToUri.containsKey(clazz); 94 | } 95 | 96 | public Class getClass( String uri ) throws ClassNotFoundException { 97 | Class toReturn = uriToClass.get(uri); 98 | if (toReturn == null) { 99 | toReturn = Class.forName(uri); 100 | } 101 | return toReturn; 102 | } 103 | 104 | public String getURI( Class clazz ) { 105 | String toReturn = classToUri.get(clazz); 106 | if (toReturn == null) { 107 | toReturn = clazz.getName(); 108 | } 109 | return toReturn; 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/io/JSonMarshaller.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.io; 25 | 26 | import java.io.BufferedReader; 27 | import java.io.IOException; 28 | import java.io.PrintWriter; 29 | import java.io.Reader; 30 | import java.io.Writer; 31 | 32 | import org.jspace.io.jSpaceMarshaller; 33 | import org.jspace.io.json.jSonUtils; 34 | 35 | 36 | /** 37 | * @author loreti 38 | * 39 | */ 40 | public class JSonMarshaller implements jSpaceMarshaller { 41 | 42 | private jSonUtils utils = jSonUtils.getInstance(); 43 | 44 | public byte[] toByte(Object o) { 45 | return utils.toByte(o); 46 | } 47 | 48 | public T fromByte(Class clazz, byte[] data) { 49 | return utils.fromByte(clazz, data); 50 | } 51 | 52 | public T read(Class clazz, BufferedReader reader) throws IOException { 53 | return utils.read(reader, clazz); 54 | } 55 | 56 | public void write(Object o, PrintWriter writer) { 57 | utils.write(writer,o); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/io/MarshalFactory.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.io; 25 | 26 | import java.util.HashMap; 27 | 28 | /** 29 | * @author loreti 30 | * 31 | */ 32 | public class MarshalFactory { 33 | 34 | public final static String LANGUAGE_PARAMETER = "lang"; 35 | 36 | public final static String JSON_CODE = "json"; 37 | 38 | public final static String DEFAULT_LANGAUGE = JSON_CODE; 39 | 40 | private final HashMap> table; 41 | 42 | private MarshalFactory() { 43 | this.table = new HashMap<>(); 44 | init(); 45 | } 46 | 47 | private void init() { 48 | table.put("json", JSonMarshaller.class); 49 | } 50 | 51 | public jSpaceMarshaller getMarshaller( String code ) { 52 | try { 53 | return table.get(code).newInstance(); 54 | } catch (InstantiationException | IllegalAccessException e) { 55 | // TODO: Add log info here! 56 | e.printStackTrace(); 57 | } 58 | return null; 59 | } 60 | 61 | public void register( String code , Class clazz ) { 62 | table.put(code, clazz); 63 | } 64 | 65 | private static MarshalFactory instance; 66 | 67 | public static MarshalFactory getInstance() { 68 | if (instance == null) { 69 | instance = new MarshalFactory(); 70 | } 71 | return instance; 72 | } 73 | 74 | public jSpaceMarshaller getDeaultMarshaller() { 75 | return getMarshaller(DEFAULT_LANGAUGE); 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/io/jSpaceMarshaller.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.io; 25 | 26 | import java.io.BufferedReader; 27 | import java.io.IOException; 28 | import java.io.PrintWriter; 29 | import java.io.Reader; 30 | import java.io.Writer; 31 | 32 | /** 33 | * @author loreti 34 | * 35 | */ 36 | public interface jSpaceMarshaller { 37 | 38 | public byte[] toByte(Object o); 39 | 40 | public T fromByte(Class clazz, byte[] data); 41 | 42 | public T read(Class clazz, BufferedReader reader) throws IOException; 43 | 44 | public void write(Object o, PrintWriter writer); 45 | 46 | } 47 | -------------------------------------------------------------------------------- /common/src/main/java/org/jspace/io/json/TemplateDeserializer.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2017 Michele Loreti and the jSpace Developers (see the included 3 | * authors file). 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | 24 | package org.jspace.io.json; 25 | 26 | import java.lang.reflect.Type; 27 | 28 | import org.jspace.Template; 29 | import org.jspace.TemplateField; 30 | 31 | import com.google.gson.JsonArray; 32 | import com.google.gson.JsonDeserializationContext; 33 | import com.google.gson.JsonDeserializer; 34 | import com.google.gson.JsonElement; 35 | import com.google.gson.JsonParseException; 36 | 37 | /** 38 | * This class is used to deserialize a {@link Template} from a 39 | * {@link JsonElement} (see {@link JsonDeserializer}). 40 | * 41 | * @author Michele Loreti 42 | * 43 | */ 44 | public class TemplateDeserializer implements JsonDeserializer