Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

Message.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file Message.d
00004         
00005         Copyright (c) 2004 Kris Bell
00006         
00007         This software is provided 'as-is', without any express or implied
00008         warranty. In no event will the authors be held liable for damages
00009         of any kind arising from the use of this software.
00010         
00011         Permission is hereby granted to anyone to use this software for any 
00012         purpose, including commercial applications, and to alter it and/or 
00013         redistribute it freely, subject to the following restrictions:
00014         
00015         1. The origin of this software must not be misrepresented; you must 
00016            not claim that you wrote the original software. If you use this 
00017            software in a product, an acknowledgment within documentation of 
00018            said product would be appreciated but is not required.
00019 
00020         2. Altered source versions must be plainly marked as such, and must 
00021            not be misrepresented as being the original software.
00022 
00023         3. This notice may not be removed or altered from any distribution
00024            of the source.
00025 
00026         4. Derivative works are permitted, but they must carry this notice
00027            in full and credit the original source.
00028 
00029 
00030                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00031 
00032 
00033         @version        Initial version, July 2004      
00034         @author         Kris
00035 
00036 
00037 *******************************************************************************/
00038 
00039 module mango.cluster.Message;
00040 
00041 private import  mango.cache.Payload;
00042 
00043 private import  mango.io.Exception,    
00044                 mango.io.PickleRegistry;        
00045 
00046 public  import  mango.cluster.model.ICluster;        
00047 
00048 /*******************************************************************************
00049 
00050         A cluster-based messaging class. You should implement the various
00051         abstract methods, and optionally override the read() and write() 
00052         methods to transport any non-transient content along with the task.
00053         Note that when using read() and write(), you should invoke the 
00054         superclass first. That is, read() and write() should look something
00055         like this:
00056 
00057         @code
00058         void read (IReader reader)
00059         {       
00060                 super.read (reader);
00061                 reader.get (myAttribute1);
00062                 reader.get (myAttribute2);
00063                 reader.get (myAttribute3);
00064         }
00065 
00066         void write (IWriter writer)
00067         {
00068                 super.write (writer);
00069                 writer.put  (myAttribute1);
00070                 writer.put  (myAttribute2);
00071                 writer.put  (myAttribute3);
00072         }
00073         @endcode
00074 
00075         You should do it this way so that your Message can be deserialized
00076         as a superclass instance (if ever necessary).
00077 
00078 *******************************************************************************/
00079 
00080 class Message : Payload, IMessage
00081 {
00082         private char[] reply;
00083 
00084         /**********************************************************************
00085 
00086                 Overridable create method that simply instantiates a 
00087                 new instance. May be used to allocate subclassses from 
00088                 a freelist
00089 
00090         **********************************************************************/
00091 
00092         abstract Object create ();
00093 
00094         /**********************************************************************
00095 
00096                 Return the guid for this payload. This should be unique
00097                 per payload class, if said class is used in conjunction
00098                 with the clustering facilities. Inspected by the Pickle
00099                 utilitiy classes.
00100                 
00101         **********************************************************************/
00102 
00103         abstract char[] getGuid ();
00104 
00105         /***********************************************************************
00106 
00107         ***********************************************************************/
00108         
00109         void setReply (char[] channel)
00110         {
00111                 reply = channel;
00112         }
00113 
00114         /***********************************************************************
00115 
00116         ***********************************************************************/
00117         
00118         char[] getReply ()
00119         {
00120                 return reply;
00121         }
00122 
00123         /***********************************************************************
00124 
00125         ***********************************************************************/
00126         
00127         bool isReplyExpected ()
00128         {
00129                 return reply.length > 0;
00130         }
00131 
00132         /**********************************************************************
00133         
00134                 Recover the reply-channel from the provided reader
00135 
00136         **********************************************************************/
00137 
00138         override void read (IReader reader)
00139         {       
00140                 super.read (reader);
00141                 reader.get (reply);
00142         }
00143 
00144         /**********************************************************************
00145 
00146                 Emit our reply-channel to the provided writer
00147 
00148         **********************************************************************/
00149 
00150         override void write (IWriter writer)
00151         {
00152                 super.write (writer);
00153                 writer.put  (reply);
00154         }
00155 
00156         /***********************************************************************
00157 
00158                 Create a new instance of a payload, and populate it via
00159                 read() using the specified reader
00160 
00161         ***********************************************************************/
00162 
00163         override Object create (IReader reader)
00164         {
00165                 return super.create (reader);
00166         }
00167 }
00168 
00169 
00170 /*******************************************************************************
00171 
00172         An empty Message that can be used for network signaling, or simply 
00173         as an example.
00174 
00175 *******************************************************************************/
00176 
00177 class NullMessage : Message
00178 {
00179         /**********************************************************************
00180         
00181                 Register ourselves with the pickle factory
00182 
00183         **********************************************************************/
00184 
00185         static this ()
00186         {
00187                 PickleRegistry.enroll (new NullMessage);
00188         }
00189 
00190         /**********************************************************************
00191 
00192                 Overridable create method that simply instantiates a 
00193                 new instance. May be used to allocate subclassses from 
00194                 a freelist
00195 
00196         **********************************************************************/
00197 
00198         override Object create ()
00199         {
00200                 return new NullMessage;
00201         }
00202 
00203         /**********************************************************************
00204 
00205                 Return the guid for this payload. This should be unique
00206                 per payload class, if said class is used in conjunction
00207                 with the clustering facilities. Inspected by the Pickle
00208                 utility classes.
00209                 
00210         **********************************************************************/
00211 
00212         override char[] getGuid ()
00213         {
00214                 return this.classinfo.name;
00215         }
00216 }
00217 
00218 
00219 /*******************************************************************************
00220 
00221         A cluster-based executable class. You should implement the various
00222         abstract methods, and optionally override the read() and write() 
00223         methods to transport any non-transient content along with the task.
00224         Note that when using read() and write(), you should invoke the 
00225         superclass first. That is, read() and write() should look something
00226         like this:
00227 
00228         @code
00229         void read (IReader reader)
00230         {       
00231                 super.read (reader);
00232                 reader.get (myAttribute1);
00233                 reader.get (myAttribute2);
00234                 reader.get (myAttribute3);
00235         }
00236 
00237         void write (IWriter writer)
00238         {
00239                 super.write (writer);
00240                 writer.put  (myAttribute1);
00241                 writer.put  (myAttribute2);
00242                 writer.put  (myAttribute3);
00243         }
00244         @endcode
00245 
00246         You should do it this way so that your Task can be deserialized as
00247         a superclass instance (if ever necessary).
00248 
00249 *******************************************************************************/
00250 
00251 class Task : Message, ITask
00252 {
00253         /***********************************************************************
00254 
00255         ***********************************************************************/
00256         
00257         abstract void execute ();
00258 
00259         /**********************************************************************
00260 
00261                 Overridable create method that simply instantiates a 
00262                 new instance. May be used to allocate subclassses from 
00263                 a freelist
00264 
00265         **********************************************************************/
00266 
00267         abstract Object create ();
00268 
00269         /**********************************************************************
00270 
00271                 Return the guid for this payload. This should be unique
00272                 per payload class, if said class is used in conjunction
00273                 with the clustering facilities. Inspected by the Pickle
00274                 utilitiy classes.
00275                 
00276         **********************************************************************/
00277 
00278         abstract char[] getGuid ();
00279 
00280         /**********************************************************************
00281         
00282                 Recover attributes from the provided reader
00283 
00284         **********************************************************************/
00285 
00286         override void read (IReader reader)
00287         {
00288                 super.read (reader);
00289         }
00290 
00291         /**********************************************************************
00292 
00293                 Emit attributes to the provided writer
00294 
00295         **********************************************************************/
00296 
00297         override void write (IWriter writer)
00298         {
00299                 super.write (writer);
00300         }
00301 
00302         /***********************************************************************
00303 
00304                 Create a new instance of a payload, and populate it via
00305                 read() using the specified reader
00306 
00307         ***********************************************************************/
00308 
00309         override Object create (IReader reader)
00310         {
00311                 return super.create (reader);
00312         }
00313 }
00314 

Generated on Fri May 27 18:11:56 2005 for Mango by  doxygen 1.4.0