PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/interpreter/branches/multinetwork/src/edu/vub/at/actors/net/NATNetworks.java

http://ambienttalk.googlecode.com/
Java | 250 lines | 151 code | 24 blank | 75 comment | 22 complexity | fe3d7672517fbf34cf15d65dc5d2ea74 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, LGPL-2.1
  1. /**
  2. * AmbientTalk/2 Project
  3. * OBJSystem.java created on 21-sep-2006 at 17:18:32
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.actors.net;
  29. import edu.vub.at.actors.eventloops.Event;
  30. import edu.vub.at.actors.natives.ELActor;
  31. import edu.vub.at.actors.natives.ELVirtualMachine;
  32. import edu.vub.at.actors.natives.NATFarReference;
  33. import edu.vub.at.actors.net.comm.Address;
  34. import edu.vub.at.eval.Evaluator;
  35. import edu.vub.at.exceptions.InterpreterException;
  36. import edu.vub.at.exceptions.XIOProblem;
  37. import edu.vub.at.exceptions.XIllegalArgument;
  38. import edu.vub.at.exceptions.XNetworkError;
  39. import edu.vub.at.objects.ATClosure;
  40. import edu.vub.at.objects.ATNil;
  41. import edu.vub.at.objects.ATObject;
  42. import edu.vub.at.objects.ATTable;
  43. import edu.vub.at.objects.natives.NATByCopy;
  44. import edu.vub.at.objects.natives.NATTable;
  45. import edu.vub.at.objects.natives.NATText;
  46. import edu.vub.at.util.logging.Logging;
  47. import java.io.IOException;
  48. import java.net.InetAddress;
  49. import java.net.InterfaceAddress;
  50. import java.net.NetworkInterface;
  51. import java.net.SocketException;
  52. import java.util.ArrayList;
  53. import java.util.Collection;
  54. import java.util.Enumeration;
  55. import java.util.HashMap;
  56. import java.util.Iterator;
  57. import java.util.List;
  58. import java.util.Set;
  59. /**
  60. * Instances of this class represent the 'networks' object,
  61. * that configures network port objects in an actor
  62. * No more than one threads access this object
  63. *
  64. * The interface of the networks object is as follows:
  65. *
  66. * def system := object: {
  67. * def getAll() { returns all port objects in this actor}
  68. * def createPort(name) { creates virtual port object }
  69. * def getByFarRef(farRef) { return port object that is related to the far reference }
  70. * }
  71. *
  72. * @author suztomo
  73. */
  74. public final class NATNetworks extends NATByCopy {
  75. private NATNetworkPort[] ports_;
  76. private NATNetworkPort defaultPort;
  77. private HashMap nameToPort;
  78. private HashMap virtualNameToPort;
  79. public NATNetworks() {
  80. virtualNameToPort = new HashMap();
  81. nameToPort = new HashMap();
  82. }
  83. public NATText meta_print() throws InterpreterException {
  84. return NATText.atValue("<native object: netoworks>");
  85. }
  86. /**
  87. * Returns all NATNetworkPort objects available in this actor
  88. * This includes virutual ports that have been created in this actor
  89. */
  90. public NATTable base_getAll() {
  91. NATNetworkPort[] ports = getAllNetworks();
  92. return NATTable.atValue(ports);
  93. }
  94. @SuppressWarnings("unchecked")
  95. public NATNetworkPort[] getAllNetworks() {
  96. /*
  97. * ifnameToAddress instance is read only after its initialization,
  98. * so actors can share the Address instances
  99. */
  100. HashMap ifnameToAddress = ELVirtualMachine.currentVM().getIfnameToAddress();
  101. Set keys;
  102. int len;
  103. int c = 0;
  104. NATNetworkPort[] ret;
  105. synchronized(ifnameToAddress) {
  106. len = ifnameToAddress.size() + virtualNameToPort.size();
  107. ret = new NATNetworkPort[len];
  108. keys = ifnameToAddress.keySet();
  109. for (Iterator i = keys.iterator(); i.hasNext();) {
  110. String name = (String)i.next();
  111. NATNetworkPort p = (NATNetworkPort)nameToPort.get(name);
  112. if (p == null) {
  113. Address a = (Address) ifnameToAddress.get(name);
  114. p = new NATNetworkPort(name, a);
  115. nameToPort.put(name, p);
  116. }
  117. ret[c] = p;
  118. c++;
  119. }
  120. }
  121. // The latter is actor specific. No need to lock
  122. keys = virtualNameToPort.keySet();
  123. for (Iterator i = keys.iterator(); i.hasNext();) {
  124. ret[c] = (NATNetworkPort) virtualNameToPort.get(i.next());
  125. c++;
  126. }
  127. return ret;
  128. }
  129. /**
  130. * Returns a port object that represents the network of the given far reference
  131. * @param obj a far reference
  132. * @return object that represents a network.
  133. */
  134. public ATObject base_getByFarRef(ATObject obj) throws InterpreterException {
  135. if (!obj.isFarReference()) {
  136. throw new XIllegalArgument("Parameter is not far reference: " + obj);
  137. }
  138. NATFarReference ref = (NATFarReference)obj;
  139. Address target = ELVirtualMachine.currentVM().vmAddressBook_.getAddressOf(ref.getObjectId().getVirtualMachineId());
  140. if (target == null) { // local far reference
  141. throw new XIllegalArgument("Parameter is local far reference: " + obj);
  142. }
  143. if (target.isVirtualNetwork()) { // ?
  144. return createVirtualPortOf(target.virtualNetworkName_);
  145. }
  146. for (int i=0; i<ports_.length; i++) {
  147. NATNetworkPort port = ports_[i];
  148. if (target.equals(port.address)) {
  149. return port;
  150. }
  151. }
  152. return Evaluator.getNil();
  153. }
  154. @SuppressWarnings("unchecked")
  155. private NATNetworkPort createVirtualPortOf(String virtualPortName) {
  156. NATNetworkPort port = (NATNetworkPort)virtualNameToPort.get(virtualPortName);
  157. if (port == null) {
  158. port = new NATNetworkPort(virtualPortName);
  159. virtualNameToPort.put(virtualPortName, port);
  160. }
  161. return port;
  162. }
  163. /**
  164. * Creates a object that represents a virtual network, which can be used
  165. * as communication among several VMs in one computer
  166. * @param name of the network
  167. * @return the object representing network
  168. */
  169. public ATObject base_createPort(ATObject name) throws InterpreterException {
  170. if (!name.isNativeText()) {
  171. throw new XIllegalArgument("Parameter is not text");
  172. }
  173. String portName = ((NATText)name).javaValue;
  174. // port is unique in an actor, thus, in a NATNetwork object
  175. NATNetworkPort port = createVirtualPortOf(portName);
  176. return port;
  177. }
  178. public ATObject base_setDefault(ATObject obj) throws InterpreterException {
  179. if (! (obj instanceof NATNetworkPort)) {
  180. throw new XIllegalArgument("Parameter is not a port object");
  181. }
  182. defaultPort = (NATNetworkPort)obj;
  183. return Evaluator.getNil();
  184. }
  185. public ATObject base_getDefault() throws InterpreterException {
  186. if (defaultPort != null) {
  187. return defaultPort;
  188. }
  189. /*
  190. * Default port object is the first port object
  191. * unless it is not set before this function is called
  192. */
  193. NATNetworkPort[] ports = getAllNetworks();
  194. if (ports.length == 0) {
  195. return Evaluator.getNil();
  196. }
  197. defaultPort = ports[0];
  198. return defaultPort;
  199. }
  200. /**
  201. * Connects the interpreter to the network
  202. * this method can be called by a default actor
  203. */
  204. public ATObject base_online() throws InterpreterException {
  205. ELActor actor = ELActor.currentActor();
  206. if (actor.isDefault) {
  207. ELVirtualMachine.currentVM().event_goOnline();
  208. } else {
  209. throw new XNetworkError("Only default actor can change the interpreter's status");
  210. }
  211. return Evaluator.getNil();
  212. }
  213. /**
  214. * Disonnects the interpreter to the network
  215. * this method can be called by a default actor
  216. */
  217. public ATObject base_offline() throws InterpreterException {
  218. ELActor actor = ELActor.currentActor();
  219. if (actor.isDefault) {
  220. ELVirtualMachine.currentVM().event_goOffline();
  221. } else {
  222. throw new XNetworkError("Only default actor can change the interpreter's status");
  223. }
  224. return Evaluator.getNil();
  225. }
  226. }