From: <sno...@us...> - 2013-09-12 21:20:34
|
Revision: 93 http://sourceforge.net/p/openrpg/svn/93 Author: snowdog_ Date: 2013-09-12 21:20:30 +0000 (Thu, 12 Sep 2013) Log Message: ----------- Added extra explanation comments to the ExampleClientModule code and cleaned it up a bit. Converted the ExampleClientModule to access the data portion of the ORPGMessage structure also. Modified Paths: -------------- trunk/src/openrpg2/client/core/modules/ExampleClientModule.java trunk/src/openrpg2/common/core/network/NetworkClientModule.java Modified: trunk/src/openrpg2/client/core/modules/ExampleClientModule.java =================================================================== --- trunk/src/openrpg2/client/core/modules/ExampleClientModule.java 2013-09-12 21:02:35 UTC (rev 92) +++ trunk/src/openrpg2/client/core/modules/ExampleClientModule.java 2013-09-12 21:20:30 UTC (rev 93) @@ -37,11 +37,35 @@ import openrpg2.common.module.NetworkedModule; /** - * + * This is a simple example of a Networked module with a graphical interface. * @author Snowdog */ public class ExampleClientModule extends NetworkedModule implements ClientLoadable, ActionListener{ + /* + * MESSAGE_TYPE + * Always a good idea to include the ORPGMessage type identifier for + * a module as a public constant. This allows other modules easy access. + * Convention is to use the module class name but this is not strictly + * enforced. Modules sending larger volumes of messages should have a + * short identifier to reduce bandwidth consumption. + */ + public static final String MESSAGE_TYPE = "POPUP"; // convention would be "ExampleClientModule" instead + + + /** + * HEADER KEYS + * Its good practice to include any header key identifiers as + * public final static strings. This allows other modules to access them in + * order to send this module information via messages. + * Always a good idea to avoid hard coding things like this directly + * into the code ;) + */ + public static final String OPERATION = "OP"; + public static final String HEADER_KEY2 = "HK2"; + + public static final int OP_POPUP = 1; + JPanel panel = null; JButton sendButton = null; JLabel windowText = null; @@ -63,21 +87,58 @@ panel.add(sendButton); } + @Override public void actionPerformed( ActionEvent event ){ if (event.getSource() == this.sendButton){ sendPopupMessage(); } } + /** + * This method formats a new outbound ORPGMessage and sends it to the server + */ private void sendPopupMessage(){ - log.finer("sendPopupMessage() called"); - ORPGMessage msg = new ORPGMessage(); - msg.setHandlerId("Popup"); - msg.setHeader("PopText","ExampleClientModule is annoying you with a popup"); + log.finer("sendPopupMessage() called"); //log that the method was called to the DevConsole (fine level) + ORPGMessage msg = new ORPGMessage(); //create a new empty ORPGMessage + msg.setMessageType(ExampleClientModule.MESSAGE_TYPE); //tag the message with the id for this module + + /* + * Simple messages may only require small amounts of data to be sent. In these cases it is + * efficent to simply embed them into the header of the ORPGMessage. This is perfect for + * a small notification, perhaps a status change, or simple result of an operation. If your + * data is of any kind of significant size (say an XML fragment) then it should be placed + * into the message as data instead. The server (and client) will parse the header during + * message transport but the message data is passed through as a simple binary blob! + */ + int sampleInteger = 0; + msg.setHeader(ExampleClientModule.OPERATION, 1); //example of sending small data in the message header + msg.setHeader(ExampleClientModule.HEADER_KEY2, "sampleString");//example of sending small data in the message header + + /* + * MESSAGE DATA USAGE + * As mentioned above the DATA portion of the ORPGMessage passes through + * to the receiving module unparsed and unchanged. This allows for + * effecient transport of large datasets without causing delays (due to + * header parsing). If you need to move anything more than a couple simple + * data types shift your into the message data component. + */ + msg.setDataAsString("ExampleClientModule is annoying you with a popup"); + /* + * MessageAddress + * This is where you set where the message is headed to. + * Each messageAddress holds one or more AddressTokens + * Each AddressToken defines a specific client or set of clients to + * deliver the message to. + * In this case we are creating a message to send to all users in the + * 'Lobby' (default group) of the server and then adding the MessageAddress to + * the ORPGMessage itself (setDestination) + */ MessageAddress sendTo = new MessageAddress(); sendTo.append(new AddressToken(AddressToken.ALL,GroupManager.LOBBY_ID)); //send to all clients msg.setDestination(sendTo); + + /* now try sending the message to the network...*/ try{ this.modCom.sendToNetwork(msg); }catch(ModuleCommunicationException e){ @@ -87,17 +148,39 @@ } } + + /** + * This is where all incoming messages will arrive. + * First we check what type of message it is (is it ours), this is not strictly + * required but is a good sanity check + * In this case as ALL messages from this module are for the popup window + * we are ignoring all header info and converting the data back into a string + * for display. + * If we wanted to handle different types of messages we can assign a header + * variable (example above ExampleClientModule.OPERATION) when sending + * the message and inspect its value to figure out what kind of message + * the module is receiving (in example above the value would be "1"). + * Note: all header values are strings in inbound messages!!! + */ + @Override public void processMessage(ORPGMessage msg){ - if ( msg.getHandlerId().compareTo("Popup") == 0 ){ - String popupMessage = msg.getHeader("PopText"); + if ( msg.getMessageType().compareTo(ExampleClientModule.MESSAGE_TYPE) == 0 ){ + String popupMessage = msg.getDataAsString(); JOptionPane.showMessageDialog(panel,popupMessage,"ExacmpleClientModule Popup",JOptionPane.INFORMATION_MESSAGE); } } + /** + * The doRegistration method holds all the module binding calls. + * These calls will register with the various subsystems to notify various + * parts of the ORPG2 core as to what this module is interested in and/or + * provides to other modules. + */ + @Override public void doRegistration(){ log.finer("doRegistration() called"); - this.modCom.registerMessageType("Popup", this); - this.modCom.registerGUIComponent("Popup", panel); + this.modCom.registerMessageType(ExampleClientModule.MESSAGE_TYPE, this); + this.modCom.registerGUIComponent(ExampleClientModule.MESSAGE_TYPE, panel); } } Modified: trunk/src/openrpg2/common/core/network/NetworkClientModule.java =================================================================== --- trunk/src/openrpg2/common/core/network/NetworkClientModule.java 2013-09-12 21:02:35 UTC (rev 92) +++ trunk/src/openrpg2/common/core/network/NetworkClientModule.java 2013-09-12 21:20:30 UTC (rev 93) @@ -88,6 +88,7 @@ MessageAddress m = new MessageAddress(); m.append(new AddressToken());//default AddressToken directs to connected server! msg.setDestination(m); + msg.setMessageType(ORPGConstants.TYPE_NETWORK); msg.setHeader(HEADER_OP, OP_JOIN); try { this.modCom.sendToNetwork(msg); //try to send message to server (throws exception on error) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |