/* Xholon Runtime Framework - executes event-driven & dynamic applications * Copyright (C) 2005, 2006 Ken Webb * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 * of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package org.primordion.xholon.tutorials; import org.primordion.xholon.base.Message; import org.primordion.xholon.base.XholonWithPorts; /** * My Hello World. A Xholon tutorial. * @author Ken Webb * @since 0.1 (Created on October 10, 2005) */ public class XhMyHello extends XholonWithPorts implements CeMyHello { // references to other Xholon instance; indices into the port array public static final int P_PARTNER = 0; // Signals, Events // signals for interface used between P_PARTNER ports public static final int SIGNAL_ONE = 100; // no data public int state = 0; public String roleName = null; /** Constructor. */ public XhMyHello() {} /* * @see org.primordion.xholon.base.ITreeNode#initialize() */ public void initialize() { super.initialize(); state = 0; roleName = null; } /* * @see org.primordion.xholon.base.ITreeNode#act() */ public void act() { switch(xhClass.getId()) { case HelloWorldSystemCE: processMessageQ(); break; case HelloCE: if (state == 0) { System.out.print("Hello "); port[P_PARTNER].sendMessage(SIGNAL_ONE, null, this); state = 1; } break; default: break; } super.act(); } /* * @see org.primordion.xholon.base.IXholon#processReceivedMessage(org.primordion.xholon.base.Message) */ public void processReceivedMessage(Message msg) { int event = msg.getSignal(); switch(xhClass.getId()) { case HelloCE: switch (state) { case 1: // Ready switch(event) { case SIGNAL_ONE: System.out.print("Hello "); port[P_PARTNER].sendMessage(SIGNAL_ONE, null, this); break; default: break; } break; default: break; } break; // end Hello case WorldCE: switch (state) { case 0: // Ready switch(event) { case SIGNAL_ONE: System.out.println("World !"); port[P_PARTNER].sendMessage(SIGNAL_ONE, null, this); break; default: break; } break; default: break; } break; // end World default: System.out.println("XhHelloWorld: message with no receiver " + msg); break; } } /* * @see java.lang.Object#toString() */ public String toString() { String outStr = getName(); if ((port != null) && (port.length > 0)) { outStr += " ["; for (int i = 0; i < port.length; i++) { if (port[i] != null) { outStr += " port:" + port[i].getName(); } } outStr += "]"; } outStr += " state=" + state; return outStr; } }