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

/src/examples/helloworld/HelloWorld.java

https://github.com/tcoraini/Salsa-Apps-Eclipse
Java | 314 lines | 252 code | 36 blank | 26 comment | 77 complexity | 93949256efb04f76b969f4bb0d621ffa MD5 | raw file
  1. package examples.helloworld;
  2. // Import declarations generated by the SALSA compiler, do not modify.
  3. import java.io.IOException;
  4. import java.util.Vector;
  5. import java.lang.reflect.Method;
  6. import java.lang.reflect.Modifier;
  7. import java.lang.reflect.InvocationTargetException;
  8. import salsa.language.Actor;
  9. import salsa.language.ActorReference;
  10. import salsa.language.Message;
  11. import salsa.language.RunTime;
  12. import salsa.language.ServiceFactory;
  13. import gc.WeakReference;
  14. import salsa.language.Token;
  15. import salsa.language.exceptions.*;
  16. import salsa.language.exceptions.CurrentContinuationException;
  17. import salsa.language.UniversalActor;
  18. import salsa.naming.UAN;
  19. import salsa.naming.UAL;
  20. import salsa.naming.MalformedUALException;
  21. import salsa.naming.MalformedUANException;
  22. import salsa.resources.SystemService;
  23. import salsa.resources.ActorService;
  24. // End SALSA compiler generated import delcarations.
  25. public class HelloWorld extends UniversalActor {
  26. public static void main(String args[]) {
  27. UAN uan = null;
  28. UAL ual = null;
  29. if (System.getProperty("uan") != null) {
  30. uan = new UAN( System.getProperty("uan") );
  31. ServiceFactory.getTheater();
  32. RunTime.receivedUniversalActor();
  33. }
  34. if (System.getProperty("ual") != null) {
  35. ual = new UAL( System.getProperty("ual") );
  36. if (uan == null) {
  37. System.err.println("Actor Creation Error:");
  38. System.err.println(" uan: " + uan);
  39. System.err.println(" ual: " + ual);
  40. System.err.println(" Identifier: " + System.getProperty("identifier"));
  41. System.err.println(" Cannot specify an actor to have a ual at runtime without a uan.");
  42. System.err.println(" To give an actor a specific ual at runtime, use the identifier system property.");
  43. System.exit(0);
  44. }
  45. RunTime.receivedUniversalActor();
  46. }
  47. if (System.getProperty("identifier") != null) {
  48. if (ual != null) {
  49. System.err.println("Actor Creation Error:");
  50. System.err.println(" uan: " + uan);
  51. System.err.println(" ual: " + ual);
  52. System.err.println(" Identifier: " + System.getProperty("identifier"));
  53. System.err.println(" Cannot specify an identifier and a ual with system properties when creating an actor.");
  54. System.exit(0);
  55. }
  56. ual = new UAL( ServiceFactory.getTheater().getLocation() + System.getProperty("identifier"));
  57. }
  58. RunTime.receivedMessage();
  59. HelloWorld instance = (HelloWorld)new HelloWorld(uan, ual,null).construct();
  60. gc.WeakReference instanceRef=new gc.WeakReference(uan,ual);
  61. {
  62. Object[] _arguments = { args };
  63. //preAct() for local actor creation
  64. //act() for remote actor creation
  65. if (ual != null && !ual.getLocation().equals(ServiceFactory.getTheater().getLocation())) {instance.send( new Message(instanceRef, instanceRef, "act", _arguments, false) );}
  66. else {instance.send( new Message(instanceRef, instanceRef, "preAct", _arguments, false) );}
  67. }
  68. RunTime.finishedProcessingMessage();
  69. }
  70. public static ActorReference getReferenceByName(UAN uan) { return new HelloWorld(false, uan); }
  71. public static ActorReference getReferenceByName(String uan) { return HelloWorld.getReferenceByName(new UAN(uan)); }
  72. public static ActorReference getReferenceByLocation(UAL ual) { return new HelloWorld(false, ual); }
  73. public static ActorReference getReferenceByLocation(String ual) { return HelloWorld.getReferenceByLocation(new UAL(ual)); }
  74. public HelloWorld(boolean o, UAN __uan) { super(false,__uan); }
  75. public HelloWorld(boolean o, UAL __ual) { super(false,__ual); }
  76. public HelloWorld(UAN __uan,UniversalActor.State sourceActor) { this(__uan, null, sourceActor); }
  77. public HelloWorld(UAL __ual,UniversalActor.State sourceActor) { this(null, __ual, sourceActor); }
  78. public HelloWorld(UniversalActor.State sourceActor) { this(null, null, sourceActor); }
  79. public HelloWorld() { }
  80. public HelloWorld(UAN __uan, UAL __ual, Object obj) {
  81. //decide the type of sourceActor
  82. //if obj is null, the actor must be the startup actor.
  83. //if obj is an actorReference, this actor is created by a remote actor
  84. if (obj instanceof UniversalActor.State || obj==null) {
  85. UniversalActor.State sourceActor;
  86. if (obj!=null) { sourceActor=(UniversalActor.State) obj;}
  87. else {sourceActor=null;}
  88. //remote creation message sent to a remote system service.
  89. if (__ual != null && !__ual.getLocation().equals(ServiceFactory.getTheater().getLocation())) {
  90. WeakReference sourceRef;
  91. if (sourceActor!=null && sourceActor.getUAL() != null) {sourceRef = new WeakReference(sourceActor.getUAN(),sourceActor.getUAL());}
  92. else {sourceRef = null;}
  93. if (sourceActor != null) {
  94. if (__uan != null) {sourceActor.getActorMemory().getForwardList().putReference(__uan);}
  95. else if (__ual!=null) {sourceActor.getActorMemory().getForwardList().putReference(__ual);}
  96. //update the source of this actor reference
  97. setSource(sourceActor.getUAN(), sourceActor.getUAL());
  98. activateGC();
  99. }
  100. createRemotely(__uan, __ual, "examples.helloworld.HelloWorld", sourceRef);
  101. }
  102. // local creation
  103. else {
  104. State state = new State(__uan, __ual);
  105. //assume the reference is weak
  106. muteGC();
  107. //the source actor is the startup actor
  108. if (sourceActor == null) {
  109. state.getActorMemory().getInverseList().putInverseReference("rmsp://me");
  110. }
  111. //the souce actor is a normal actor
  112. else if (sourceActor instanceof UniversalActor.State) {
  113. // this reference is part of garbage collection
  114. activateGC();
  115. //update the source of this actor reference
  116. setSource(sourceActor.getUAN(), sourceActor.getUAL());
  117. /* Garbage collection registration:
  118. * register 'this reference' in sourceActor's forward list @
  119. * register 'this reference' in the forward acquaintance's inverse list
  120. */
  121. String inverseRefString=null;
  122. if (sourceActor.getUAN()!=null) {inverseRefString=sourceActor.getUAN().toString();}
  123. else if (sourceActor.getUAL()!=null) {inverseRefString=sourceActor.getUAL().toString();}
  124. if (__uan != null) {sourceActor.getActorMemory().getForwardList().putReference(__uan);}
  125. else if (__ual != null) {sourceActor.getActorMemory().getForwardList().putReference(__ual);}
  126. else {sourceActor.getActorMemory().getForwardList().putReference(state.getUAL());}
  127. //put the inverse reference information in the actormemory
  128. if (inverseRefString!=null) state.getActorMemory().getInverseList().putInverseReference(inverseRefString);
  129. }
  130. state.updateSelf(this);
  131. ServiceFactory.getNaming().setEntry(state.getUAN(), state.getUAL(), state);
  132. if (getUAN() != null) ServiceFactory.getNaming().update(state.getUAN(), state.getUAL());
  133. }
  134. }
  135. //creation invoked by a remote message
  136. else if (obj instanceof ActorReference) {
  137. ActorReference sourceRef= (ActorReference) obj;
  138. State state = new State(__uan, __ual);
  139. muteGC();
  140. state.getActorMemory().getInverseList().putInverseReference("rmsp://me");
  141. if (sourceRef.getUAN() != null) {state.getActorMemory().getInverseList().putInverseReference(sourceRef.getUAN());}
  142. else if (sourceRef.getUAL() != null) {state.getActorMemory().getInverseList().putInverseReference(sourceRef.getUAL());}
  143. state.updateSelf(this);
  144. ServiceFactory.getNaming().setEntry(state.getUAN(), state.getUAL(),state);
  145. if (getUAN() != null) ServiceFactory.getNaming().update(state.getUAN(), state.getUAL());
  146. }
  147. }
  148. public UniversalActor construct() {
  149. Object[] __arguments = { };
  150. this.send( new Message(this, this, "construct", __arguments, null, null) );
  151. return this;
  152. }
  153. public class State extends UniversalActor .State {
  154. public HelloWorld self;
  155. public void updateSelf(ActorReference actorReference) {
  156. ((HelloWorld)actorReference).setUAL(getUAL());
  157. ((HelloWorld)actorReference).setUAN(getUAN());
  158. self = new HelloWorld(false,getUAL());
  159. self.setUAN(getUAN());
  160. self.setUAL(getUAL());
  161. self.activateGC();
  162. }
  163. public void preAct(String[] arguments) {
  164. getActorMemory().getInverseList().removeInverseReference("rmsp://me",1);
  165. {
  166. Object[] __args={arguments};
  167. self.send( new Message(self,self, "act", __args, null,null,false) );
  168. }
  169. }
  170. public State() {
  171. this(null, null);
  172. }
  173. public State(UAN __uan, UAL __ual) {
  174. super(__uan, __ual);
  175. addClassName( "examples.helloworld.HelloWorld$State" );
  176. addMethodsForClasses();
  177. }
  178. public void construct() {}
  179. public void process(Message message) {
  180. Method[] matches = getMatches(message.getMethodName());
  181. Object returnValue = null;
  182. Exception exception = null;
  183. if (matches != null) {
  184. if (!message.getMethodName().equals("die")) {activateArgsGC(message);}
  185. for (int i = 0; i < matches.length; i++) {
  186. try {
  187. if (matches[i].getParameterTypes().length != message.getArguments().length) continue;
  188. returnValue = matches[i].invoke(this, message.getArguments());
  189. } catch (Exception e) {
  190. if (e.getCause() instanceof CurrentContinuationException) {
  191. sendGeneratedMessages();
  192. return;
  193. } else if (e instanceof InvocationTargetException) {
  194. sendGeneratedMessages();
  195. exception = (Exception)e.getCause();
  196. break;
  197. } else {
  198. continue;
  199. }
  200. }
  201. sendGeneratedMessages();
  202. currentMessage.resolveContinuations(returnValue);
  203. return;
  204. }
  205. }
  206. System.err.println("Message processing exception:");
  207. if (message.getSource() != null) {
  208. System.err.println("\tSent by: " + message.getSource().toString());
  209. } else System.err.println("\tSent by: unknown");
  210. System.err.println("\tReceived by actor: " + toString());
  211. System.err.println("\tMessage: " + message.toString());
  212. if (exception == null) {
  213. if (matches == null) {
  214. System.err.println("\tNo methods with the same name found.");
  215. return;
  216. }
  217. System.err.println("\tDid not match any of the following: ");
  218. for (int i = 0; i < matches.length; i++) {
  219. System.err.print("\t\tMethod: " + matches[i].getName() + "( ");
  220. Class[] parTypes = matches[i].getParameterTypes();
  221. for (int j = 0; j < parTypes.length; j++) {
  222. System.err.print(parTypes[j].getName() + " ");
  223. }
  224. System.err.println(")");
  225. }
  226. } else {
  227. System.err.println("\tThrew exception: " + exception);
  228. exception.printStackTrace();
  229. }
  230. }
  231. public void hello() {
  232. {
  233. // standardOutput<-print("Hello ")
  234. {
  235. Object _arguments[] = { "Hello " };
  236. Message message = new Message( self, standardOutput, "print", _arguments, null, null );
  237. __messages.add( message );
  238. }
  239. }
  240. }
  241. public void world() {
  242. {
  243. // standardOutput<-println("World!")
  244. {
  245. Object _arguments[] = { "World!" };
  246. Message message = new Message( self, standardOutput, "println", _arguments, null, null );
  247. __messages.add( message );
  248. }
  249. }
  250. }
  251. public void act(String arguments[]) {
  252. {
  253. // standardOutput<-println("Hello World!")
  254. {
  255. Object _arguments[] = { "Hello World!" };
  256. Message message = new Message( self, standardOutput, "println", _arguments, null, null );
  257. __messages.add( message );
  258. }
  259. }
  260. {
  261. // hello()
  262. {
  263. Object _arguments[] = { };
  264. Message message = new Message( self, self, "hello", _arguments, null, null );
  265. __messages.add( message );
  266. }
  267. }
  268. {
  269. // world()
  270. {
  271. Object _arguments[] = { };
  272. Message message = new Message( self, self, "world", _arguments, null, null );
  273. __messages.add( message );
  274. }
  275. }
  276. }
  277. }
  278. }