KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Basic Implementation of a matrix clock. Stamps used by this component are
34  * integer arrays. This implementation is not efficient if the number of process
35  * involved in the causality order is important.
36  */

37 public class MatrixClockImpl extends AbstractComponent
38     implements
39       MatrixClock,
40       MatrixClockAttributeController
41 {
42
43   private int matrix[][] = null;
44   private short size = -1;
45
46   // ---------------------------------------------------------------------------
47
// Implementation of MatrixClock interface
48
// ---------------------------------------------------------------------------
49

50   /**
51    * @see MatrixClock#testRecvMatrix(Object, short, short)
52    */

53   public synchronized int testRecvMatrix(Object JavaDoc stamp, short from, short to)
54   {
55     int[][] stampTab = (int[][]) stamp;
56     if (logger.isLoggable(BasicLevel.DEBUG))
57     {
58       logger.log(BasicLevel.DEBUG, "testRecvMatrix from=" + from + " to=" + to
59           + " stamp=");
60       logMatrix(stampTab);
61       logger.log(BasicLevel.DEBUG, "matrix state :");
62       logMatrix(matrix);
63     }
64     if (matrix[from][to] + 1 < stampTab[from][to])
65     {
66       // There is other messages from the same node to deliver before this one.
67
logger.log(BasicLevel.DEBUG, "return WAIT_TO_DELIVER");
68       return WAIT_TO_DELIVER;
69     }
70     else if (matrix[from][to] + 1 == stampTab[from][to])
71     {
72       // Verify that all messages to be delivred to this node and known by this
73
// message have already been delivred.
74
for (int k = 0; k < matrix.length; k++)
75       {
76         if ((k != from) && (stampTab[k][to] > matrix[k][to]))
77         {
78           logger.log(BasicLevel.DEBUG, "return WAIT_TO_DELIVER");
79           return WAIT_TO_DELIVER;
80         }
81       }
82     }
83     else
84     {
85       logger.log(BasicLevel.DEBUG, "return ALREADY_DELIVERED");
86       return ALREADY_DELIVERED;
87     }
88     // the message is ready to be delivred, so updates the matrix clock
89
for (int i = 0; i < stampTab.length; i++)
90     {
91       for (int j = 0; j < stampTab.length; j++)
92       {
93         if (stampTab[i][j] > matrix[i][j])
94         {
95           matrix[i][j] = stampTab[i][j];
96         }
97       }
98     }
99     logger.log(BasicLevel.DEBUG, "return DELIVER");
100     return DELIVER;
101   }
102
103   /**
104    * @see MatrixClock#getStamp(short, short)
105    */

106   public synchronized Object JavaDoc getStamp(short from, short to)
107   {
108     // increment matrix clock
109
matrix[from][to]++;
110     // copy the matrix and set the copy as stamp.
111
int[][] stamp = new int[matrix.length][matrix.length];
112     for (int i = 0; i < stamp.length; i++)
113     {
114       System.arraycopy(matrix[i], 0, stamp[i], 0, matrix.length);
115     }
116     if (logger.isLoggable(BasicLevel.DEBUG))
117     {
118       logger.log(BasicLevel.DEBUG, "getStamp from=" + from + " to=" + to
119           + " returned stamp=");
120       logMatrix(stamp);
121     }
122     return stamp;
123   }
124
125   // ---------------------------------------------------------------------------
126
// Implementation of MatrixClockAttributeController interface
127
// ---------------------------------------------------------------------------
128

129   /**
130    * @see org.objectweb.dream.protocol.causality.MatrixClockAttributeController#getNumberOfProcess()
131    */

132   public short getNumberOfProcess()
133   {
134     return size;
135   }
136
137   /**
138    * @see org.objectweb.dream.protocol.causality.MatrixClockAttributeController#setNumberOfProcess(short)
139    */

140   public void setNumberOfProcess(short numberOfProcess)
141   {
142     if (matrix != null && numberOfProcess != size)
143     {
144       throw new IllegalStateException JavaDoc(
145           "Matrix clock is already initialized, cannot change its size");
146     }
147     size = numberOfProcess;
148     matrix = new int[size][size];
149   }
150
151   // ---------------------------------------------------------------------------
152
// Implementation of BindingController interface
153
// ---------------------------------------------------------------------------
154

155   /**
156    * @see org.objectweb.fractal.api.control.BindingController#listFc()
157    */

158   public String JavaDoc[] listFc()
159   {
160     return EmptyStringArray.EMPTY_STRING_ARRAY;
161   }
162
163   // ---------------------------------------------------------------------------
164
// Overriding of LifeCycleController methods
165
// ---------------------------------------------------------------------------
166

167   /**
168    * @see org.objectweb.fractal.api.control.LifeCycleController#startFc()
169    */

170   public void startFc() throws IllegalLifeCycleException
171   {
172     if (matrix == null)
173     {
174       throw new IllegalLifeCycleException("Matrix size has not been set");
175     }
176     super.startFc();
177   }
178
179   // ---------------------------------------------------------------------------
180
// Utility methods
181
// ---------------------------------------------------------------------------
182

183   private void logMatrix(final int[][] matrix)
184   {
185     if (!logger.isLoggable(BasicLevel.DEBUG))
186     {
187       return;
188     }
189     for (int i = 0; i < matrix.length; i++)
190     {
191       StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc("[");
192       for (int j = 0; j < matrix.length; j++)
193       {
194         stringBuffer.append(matrix[i][j]);
195         if (j < matrix.length - 1)
196         {
197           stringBuffer.append(", ");
198         }
199       }
200       stringBuffer.append("]");
201       logger.log(BasicLevel.DEBUG, stringBuffer);
202     }
203   }
204 }
Popular Tags