diff options
Diffstat (limited to 'sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangNode.java')
-rw-r--r-- | sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangNode.java | 100 |
1 files changed, 78 insertions, 22 deletions
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangNode.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangNode.java index a5cfbe6d90..78ffdfaa49 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangNode.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangNode.java @@ -1,25 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.apache.tuscany.sca.binding.erlang.impl; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.apache.tuscany.sca.binding.erlang.ErlangBinding; import org.apache.tuscany.sca.binding.erlang.impl.exceptions.ErlangException; +import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.runtime.RuntimeComponentService; import com.ericsson.otp.erlang.OtpConnection; import com.ericsson.otp.erlang.OtpSelf; +/** + * @version $Rev$ $Date$ + */ public class ErlangNode implements Runnable { - private Map<String, RuntimeComponentService> services = new HashMap<String, RuntimeComponentService>(); - private Map<String, ErlangBinding> bindings = new HashMap<String, ErlangBinding>(); + private Map<String, ErlangNodeElement> erlangModules = new HashMap<String, ErlangNodeElement>(); + private ErlangNodeElement erlangMbox; + private boolean mboxNode; private String name; private OtpSelf self; private ExecutorService executors; private boolean stopRequested; + private Map<String, List<Operation>> groupedOperations; public ErlangNode(String name) throws Exception { this.name = name; @@ -42,8 +69,8 @@ public class ErlangNode implements Runnable { while (!stopRequested) { try { OtpConnection connection = self.accept(); - executors.execute(new RpcExecutor(services, bindings, - connection)); + executors.execute(new ServiceExecutor(connection, + groupedOperations, erlangModules, erlangMbox)); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); @@ -51,30 +78,59 @@ public class ErlangNode implements Runnable { } } - public void registerModule(ErlangBinding binding, + public void registerBinding(ErlangBinding binding, RuntimeComponentService service) throws ErlangException { - if (services.containsKey(binding.getModule())) { - // TODO: externalize message - // TODO: really want to throw exception? Log only? - throw new ErlangException("Module " + binding.getModule() - + " already defined under " + name - + " node. Duplicate module won't be started"); + if (binding.isMbox()) { + if (mboxNode) { + // TODO: externalize message + // TODO: really want to throw exception? Log only? + throw new ErlangException("Node " + binding.getNode() + + " already defined as mbox node"); + } else { + List<Operation> operations = service.getInterfaceContract() + .getInterface().getOperations(); + groupedOperations = new HashMap<String, List<Operation>>(); + for (Operation operation : operations) { + List<Operation> operationsGroup = groupedOperations + .get(operation.getName()); + if (operationsGroup == null) { + operationsGroup = new ArrayList<Operation>(); + groupedOperations.put(operation.getName(), + operationsGroup); + } + operationsGroup.add(operation); + } + mboxNode = true; + erlangMbox = new ErlangNodeElement(); + erlangMbox.setService(service); + erlangMbox.setBinding(binding); + } } else { - if (services.size() == 0) { - // TODO: should ErlangNode manage its thread? - Thread selfThread = new Thread(this); - selfThread.start(); + if (erlangModules.containsKey(binding.getModule())) { + // TODO: externalize message + // TODO: really want to throw exception? Log only? + throw new ErlangException("Module " + binding.getModule() + + " already defined under " + name + + " node. Duplicate module won't be started"); + } else { + if (erlangModules.size() == 0) { + // TODO: should ErlangNode manage its thread? + Thread selfThread = new Thread(this); + selfThread.start(); + } + ErlangNodeElement module = new ErlangNodeElement(); + module.setService(service); + module.setBinding(binding); + erlangModules.put(binding.getModule(), module); } - services.put(binding.getModule(), service); - bindings.put(binding.getModule(), binding); } } - public void unregisterModule(ErlangBinding binding) throws ErlangException { - if (services.containsKey(binding.getModule())) { - services.remove(binding.getModule()); - bindings.remove(binding.getModule()); - if (services.size() == 0) { + public void unregisterBinding(ErlangBinding binding) throws ErlangException { + if (erlangModules.containsKey(binding.getModule())) { + erlangModules.remove(binding.getModule()); + erlangModules.remove(binding.getModule()); + if (erlangModules.size() == 0) { stop(); } } |