1 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 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 50 53 public synchronized int testRecvMatrix(Object 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 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 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 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 106 public synchronized Object getStamp(short from, short to) 107 { 108 matrix[from][to]++; 110 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 129 132 public short getNumberOfProcess() 133 { 134 return size; 135 } 136 137 140 public void setNumberOfProcess(short numberOfProcess) 141 { 142 if (matrix != null && numberOfProcess != size) 143 { 144 throw new IllegalStateException ( 145 "Matrix clock is already initialized, cannot change its size"); 146 } 147 size = numberOfProcess; 148 matrix = new int[size][size]; 149 } 150 151 155 158 public String [] listFc() 159 { 160 return EmptyStringArray.EMPTY_STRING_ARRAY; 161 } 162 163 167 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 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 stringBuffer = new StringBuffer ("["); 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 |