KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > protocol > causality > MatrixClockUpdateImpl


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact : dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.protocol.causality;
26
27 import org.objectweb.dream.AbstractComponent;
28 import org.objectweb.dream.util.EmptyStringArray;
29 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
30 import org.objectweb.util.monolog.api.BasicLevel;
31
32 /**
33  * Matric clock implementation using
34  * {@link org.objectweb.dream.protocol.causality.Update }as stamp. More
35  * efficient for large matrix.
36  */

37 public class MatrixClockUpdateImpl extends AbstractComponent
38     implements
39       MatrixClock,
40       MatrixClockAttributeController
41 {
42
43   private final class MatrixElt
44   {
45     /** Element value. */
46     int stamp;
47     /** Source node of last modification. */
48     int node;
49     /** State value when last modified. */
50     int status;
51   }
52
53   private int status[];
54   private short size = -1;
55   private MatrixElt matrix[][];
56
57   /**
58    * @see MatrixClock#testRecvMatrix(Object, short, short)
59    */

60   public synchronized int testRecvMatrix(Object JavaDoc stamp, short from, short to)
61   {
62
63     Update ptr = (Update) stamp;
64     if ((matrix[from][to].stamp + 1) < ptr.getStamp())
65     {
66       // There is other messages from the same node to deliver before this one.
67
return WAIT_TO_DELIVER;
68     }
69     else if ((matrix[from][to].stamp + 1) == ptr.getStamp())
70     {
71       // Verify that all messages to be delivred to this node and known by this
72
// message have already been delivred.
73
ptr = ptr.getNext();
74       while (ptr != null)
75       {
76         if ((ptr.getColumn() == to)
77             && ptr.getStamp() > matrix[ptr.getLine()][ptr.getColumn()].stamp)
78         {
79           break;
80         }
81         ptr = ptr.getNext();
82       }
83     }
84     else
85     {
86       return ALREADY_DELIVERED;
87     }
88
89     if (ptr != null)
90     {
91       return WAIT_TO_DELIVER;
92     }
93     else
94     {
95       // the message is ready to be delivred, so updates the matrix clock
96
ptr = (Update) stamp;
97       do
98       {
99         MatrixElt elt = matrix[ptr.getLine()][ptr.getColumn()];
100         if (elt.stamp < ptr.getStamp())
101         {
102           elt.stamp = ptr.getStamp();
103           elt.node = from;
104           elt.status = status[to];
105         }
106         Update nextUpdate = ptr.getNext();
107         Update.unalloc(ptr);
108         ptr = nextUpdate;
109       }
110       while (ptr != null);
111       return DELIVER;
112     }
113   }
114
115   /**
116    * @see MatrixClock#getStamp(short, short)
117    */

118   public synchronized Object JavaDoc getStamp(short from, short to)
119   {
120     MatrixElt elt = matrix[from][to];
121     elt.stamp++;
122     elt.status = status[from];
123     elt.node = to;
124
125     Update update = Update.alloc(from, to, elt.stamp, null);
126
127     if (to != from)
128     {
129       // if the message is remote there is need of matrix clock update.
130
for (short i = 0; i < matrix.length; i++)
131       {
132         for (short j = 0; j < matrix.length; j++)
133         {
134           MatrixElt elt1 = matrix[i][j];
135           if ((elt1.status > status[to]) && (elt1.node != to)
136               && ((i != from) || (j != to)))
137           {
138             Update.alloc(i, j, elt1.stamp, update);
139           }
140         }
141       }
142       status[to] = status[from];
143       status[from]++;
144     }
145
146     if (logger.isLoggable(BasicLevel.DEBUG))
147     {
148       StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Returned Stamp : ");
149       Update u = update;
150       while (u != null)
151       {
152         sb.append("{l=").append(u.getLine());
153         sb.append(", c=").append(u.getColumn());
154         sb.append(", s=").append(u.getStamp());
155         sb.append("} ");
156         u = u.getNext();
157       }
158       logger.log(BasicLevel.DEBUG, sb);
159     }
160     return update;
161   }
162
163   // ---------------------------------------------------------------------------
164
// Implementation of MatrixClockAttributeController interface
165
// ---------------------------------------------------------------------------
166

167   /**
168    * @see MatrixClockAttributeController#getNumberOfProcess()
169    */

170   public short getNumberOfProcess()
171   {
172     return size;
173   }
174
175   /**
176    * @see MatrixClockAttributeController#setNumberOfProcess(short)
177    */

178   public void setNumberOfProcess(short numberOfProcess)
179   {
180     if (matrix != null && numberOfProcess != size)
181     {
182       throw new IllegalStateException JavaDoc(
183           "Matrix clock is already initialized, cannot change its size");
184     }
185     size = numberOfProcess;
186     matrix = new MatrixElt[size][size];
187     status = new int[size];
188     for (short i = 0; i < matrix.length; i++)
189     {
190       for (short j = 0; j < matrix[i].length; j++)
191       {
192         matrix[i][j] = new MatrixElt();
193       }
194     }
195   }
196
197   // ---------------------------------------------------------------------------
198
// Implementation of BindingController interface
199
// ---------------------------------------------------------------------------
200

201   /**
202    * @see org.objectweb.fractal.api.control.BindingController#listFc()
203    */

204   public String JavaDoc[] listFc()
205   {
206     return EmptyStringArray.EMPTY_STRING_ARRAY;
207   }
208
209   // ---------------------------------------------------------------------------
210
// Overriding of LifeCycleController methods
211
// ---------------------------------------------------------------------------
212

213   /**
214    * @see org.objectweb.fractal.api.control.LifeCycleController#startFc()
215    */

216   public void startFc() throws IllegalLifeCycleException
217   {
218     if (matrix == null)
219     {
220       throw new IllegalLifeCycleException("Matrix size has not been set");
221     }
222     super.startFc();
223   }
224 }
Popular Tags