Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenrik Rentz-Reichert2013-08-08 08:17:07 -0400
committerHenrik Rentz-Reichert2013-08-08 08:17:07 -0400
commit2686717b0f4d07f7936d9ea807dcac3ae7af6062 (patch)
treec1ed37f37dd5cc24a164315c9ef26ac504815484
parent3b077b417cfe120a1d6ac0133c9981f5c36d1208 (diff)
downloadorg.eclipse.etrice-2686717b0f4d07f7936d9ea807dcac3ae7af6062.tar.gz
org.eclipse.etrice-2686717b0f4d07f7936d9ea807dcac3ae7af6062.tar.xz
org.eclipse.etrice-2686717b0f4d07f7936d9ea807dcac3ae7af6062.zip
[runtime.java] bug 414661: [runtime.java] let runtime only use IMessageService and make no further assumptions
https://bugs.eclipse.org/bugs/show_bug.cgi?id=414661
-rw-r--r--runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/IMessageService.java3
-rw-r--r--runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageDispatcher.java53
-rw-r--r--runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageService.java12
-rw-r--r--runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageServiceController.java57
-rw-r--r--runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/modelbase/InterfaceItemBase.java21
5 files changed, 67 insertions, 79 deletions
diff --git a/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/IMessageService.java b/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/IMessageService.java
index 836b73b5a..b47fd9063 100644
--- a/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/IMessageService.java
+++ b/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/IMessageService.java
@@ -28,6 +28,9 @@ public interface IMessageService extends IRTObject, IMessageReceiver, Runnable {
void freeAddress(Address addr);
+ void addMessageReceiver(IMessageReceiver receiver);
+ void removeMessageReceiver(IMessageReceiver receiver);
+
/**
* set the thread of this service
* (also sets the thread priority)
diff --git a/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageDispatcher.java b/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageDispatcher.java
index b68583142..68794ef31 100644
--- a/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageDispatcher.java
+++ b/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageDispatcher.java
@@ -8,7 +8,7 @@
package org.eclipse.etrice.runtime.java.messaging;
-import java.util.ArrayList;
+import java.util.LinkedList;
import java.util.HashMap;
/**
@@ -21,9 +21,7 @@ import java.util.HashMap;
public class MessageDispatcher extends RTObject implements IMessageReceiver {
private HashMap<Number, IMessageReceiver> local_map = new HashMap<Number, IMessageReceiver>();
- private HashMap<Number, IMessageReceiver> thread_map = new HashMap<Number, IMessageReceiver>();
- private HashMap<Number, IMessageReceiver> node_map = new HashMap<Number, IMessageReceiver>();
- private ArrayList<Address> freeAdresses = new ArrayList<Address>();
+ private LinkedList<Address> freeAdresses = new LinkedList<Address>();
private Address address = null;
private int nextFreeObjId;
@@ -39,7 +37,7 @@ public class MessageDispatcher extends RTObject implements IMessageReceiver {
return new Address(getAddress().nodeID, getAddress().threadID, nextFreeObjId++);
}
else
- return freeAdresses.remove(0);
+ return freeAdresses.remove();
}
public void freeAddress(Address addr) {
@@ -50,14 +48,8 @@ public class MessageDispatcher extends RTObject implements IMessageReceiver {
if (receiver.getAddress()==null)
return;
- // TODO: does only work same thread (else)
- if (receiver.getAddress().nodeID != address.nodeID){
- node_map.put(receiver.getAddress().objectID, receiver);
- }
- else if(receiver.getAddress().threadID != address.threadID){
- thread_map.put(receiver.getAddress().threadID, receiver);
- }
- else {
+ if (receiver.getAddress().nodeID == address.nodeID
+ && receiver.getAddress().threadID == address.threadID) {
local_map.put(receiver.getAddress().objectID, receiver);
}
}
@@ -66,36 +58,23 @@ public class MessageDispatcher extends RTObject implements IMessageReceiver {
if (receiver.getAddress()==null)
return;
- // TODO: does only work same thread (else)
- if (receiver.getAddress().nodeID != address.nodeID){
- node_map.remove(receiver.getAddress().objectID);
- }
- else if(receiver.getAddress().threadID != address.threadID){
- thread_map.remove(receiver.getAddress().threadID);
- }
- else {
+ if (receiver.getAddress().nodeID == address.nodeID
+ && receiver.getAddress().threadID == address.threadID) {
local_map.remove(receiver.getAddress().objectID);
}
}
@Override
public void receive(Message msg) {
- // TODO: does only work same thread (else)
- IMessageReceiver receiver = null;
- if (msg.getAddress().nodeID != address.nodeID){
- receiver = node_map.get(msg.getAddress().objectID);
- }
- else if(msg.getAddress().threadID != address.threadID){
- receiver = thread_map.get(msg.getAddress().threadID);
- }
- else {
- // Same node, same thread -> local call Dispatch Map
- receiver = local_map.get(msg.getAddress().objectID);
- }
- if(receiver!=null)
- {
- receiver.receive(msg);
- // TODO: error handling for not found addresses
+ if (msg.getAddress().nodeID == address.nodeID
+ && msg.getAddress().threadID == address.threadID) {
+ IMessageReceiver receiver = local_map.get(msg.getAddress().objectID);
+ if(receiver!=null) {
+ receiver.receive(msg);
+ }
+ else {
+ // TODO: error handling for not found addresses
+ }
}
}
diff --git a/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageService.java b/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageService.java
index a5f1a76ae..a2ad65c48 100644
--- a/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageService.java
+++ b/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageService.java
@@ -106,7 +106,7 @@ public class MessageService extends RTObject implements IMessageService {
return messageQueue;
}
- public synchronized MessageDispatcher getMessageDispatcher() {
+ protected synchronized MessageDispatcher getMessageDispatcher() {
return messageDispatcher;
}
@@ -137,4 +137,14 @@ public class MessageService extends RTObject implements IMessageService {
return thread;
}
+ @Override
+ public void addMessageReceiver(IMessageReceiver receiver) {
+ messageDispatcher.addMessageReceiver(receiver);
+ }
+
+ @Override
+ public void removeMessageReceiver(IMessageReceiver receiver) {
+ messageDispatcher.removeMessageReceiver(receiver);
+ }
+
}
diff --git a/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageServiceController.java b/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageServiceController.java
index 8f8509e5f..a47101bbe 100644
--- a/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageServiceController.java
+++ b/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/messaging/MessageServiceController.java
@@ -12,8 +12,8 @@
package org.eclipse.etrice.runtime.java.messaging;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.HashMap;
+import java.util.LinkedList;
import java.util.Map;
/**
@@ -27,33 +27,40 @@ import java.util.Map;
public class MessageServiceController {
- private List<IMessageService> messageServiceList = null;
+ private HashMap<Integer, IMessageService> messageServices = new HashMap<Integer, IMessageService>();
+ private LinkedList<Integer> freeIDs = new LinkedList<Integer>();
private boolean running = false;
+ private int nextFreeID = 0;
- public MessageServiceController(/*IRTObject parent*/){
- // TODOTS: Who is parent of MessageServices and Controller?
- // this.parent = parent;
- messageServiceList = new ArrayList<IMessageService>();
+ public synchronized int getNewID() {
+ if (freeIDs.isEmpty())
+ return nextFreeID;
+ else
+ return freeIDs.remove();
}
-
- public void addMsgSvc(IMessageService msgSvc){
- // TODOTS: Who is parent of MessageServices ?
- assert(msgSvc.getAddress().threadID == messageServiceList.size());
- messageServiceList.add(msgSvc);
+
+ public void freeID(int id) {
+ freeIDs.add(id);
}
- public int getNMsgSvc() {
- return messageServiceList.size();
+ public synchronized void addMsgSvc(IMessageService msgSvc){
+ if (nextFreeID<=msgSvc.getAddress().threadID)
+ nextFreeID = msgSvc.getAddress().threadID+1;
+
+ messageServices.put(msgSvc.getAddress().threadID, msgSvc);
+ }
+
+ public synchronized void removeMsgSvc(IMessageService msgSvc){
+ messageServices.remove(msgSvc.getAddress().threadID);
}
- public IMessageService getMsgSvc(int threadID){
- assert(threadID < messageServiceList.size());
- return messageServiceList.get(threadID);
+ public IMessageService getMsgSvc(int id){
+ return messageServices.get(id);
}
public void start() {
// start all message services
- for (IMessageService msgSvc : messageServiceList){
+ for (IMessageService msgSvc : messageServices.values()){
Thread thread = new Thread(msgSvc, msgSvc.getName());
msgSvc.setThread(thread);
thread.start();
@@ -96,7 +103,7 @@ public class MessageServiceController {
private void terminate() {
// terminate all message services
- for (IMessageService msgSvc : messageServiceList){
+ for (IMessageService msgSvc : messageServices.values()){
msgSvc.terminate();
// TODOTS: stop in order of priorities
}
@@ -104,10 +111,10 @@ public class MessageServiceController {
/**
* waitTerminate waits blocking for all MessageServices to terminate
- * ! not threadsafe !
+ * ! not thread safe !
*/
public void waitTerminate() {
- for (IMessageService msgSvc : messageServiceList) {
+ for (IMessageService msgSvc : messageServices.values()) {
try {
msgSvc.getThread().join(1000); // wait at most 1000ms
if (msgSvc.getThread().isAlive())
@@ -119,8 +126,10 @@ public class MessageServiceController {
}
}
- public void resetAll() {
+ public synchronized void resetAll() {
stop();
- messageServiceList.clear();
-}
+ messageServices.clear();
+ freeIDs.clear();
+ nextFreeID = 0;
+ }
}
diff --git a/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/modelbase/InterfaceItemBase.java b/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/modelbase/InterfaceItemBase.java
index 7489de7ff..5411e150a 100644
--- a/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/modelbase/InterfaceItemBase.java
+++ b/runtime/org.eclipse.etrice.runtime.java/src/org/eclipse/etrice/runtime/java/modelbase/InterfaceItemBase.java
@@ -15,7 +15,6 @@ import org.eclipse.etrice.runtime.java.messaging.Address;
import org.eclipse.etrice.runtime.java.messaging.IMessageReceiver;
import org.eclipse.etrice.runtime.java.messaging.IMessageService;
import org.eclipse.etrice.runtime.java.messaging.IRTObject;
-import org.eclipse.etrice.runtime.java.messaging.MessageService;
import org.eclipse.etrice.runtime.java.messaging.RTServices;
/**
@@ -34,7 +33,7 @@ public abstract class InterfaceItemBase extends AbstractMessageReceiver {
*/
private IReplicatedInterfaceItem replicator = null;
- protected IMessageReceiver ownMsgReceiver;
+ protected IMessageService ownMsgReceiver;
protected IMessageReceiver peerMsgReceiver;
private int localId;
private int idx;
@@ -64,14 +63,9 @@ public abstract class InterfaceItemBase extends AbstractMessageReceiver {
IMessageService msgSvc = RTServices.getInstance().getMsgSvcCtrl().getMsgSvc(thread);
Address addr = msgSvc.getFreeAddress();
setAddress(addr);
+ msgSvc.addMessageReceiver(this);
this.ownMsgReceiver = msgSvc;
-
- if (this.ownMsgReceiver instanceof MessageService) {
- MessageService ms = (MessageService) this.ownMsgReceiver;
- // register at the own dispatcher to receive messages
- ms.getMessageDispatcher().addMessageReceiver(this);
- }
}
connectWithPeer();
@@ -139,10 +133,6 @@ public abstract class InterfaceItemBase extends AbstractMessageReceiver {
protected synchronized IMessageReceiver getPeerMsgReceiver() {
return peerMsgReceiver;
}
-
- public void setMsgReceiver(IMessageReceiver msgReceiver) {
- this.ownMsgReceiver = msgReceiver;
- }
public IEventReceiver getActor() {
return (IEventReceiver) getParent();
@@ -169,11 +159,8 @@ public abstract class InterfaceItemBase extends AbstractMessageReceiver {
replicator.removeItem(this);
}
- if (this.ownMsgReceiver instanceof MessageService) {
- MessageService ms = (MessageService) this.ownMsgReceiver;
- ms.getMessageDispatcher().removeMessageReceiver(this);
- ms.freeAddress(getAddress());
- }
+ ownMsgReceiver.removeMessageReceiver(this);
+ ownMsgReceiver.freeAddress(getAddress());
super.destroy();
}

Back to the top