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 MatrixClockUpdateImpl extends AbstractComponent 38 implements 39 MatrixClock, 40 MatrixClockAttributeController 41 { 42 43 private final class MatrixElt 44 { 45 46 int stamp; 47 48 int node; 49 50 int status; 51 } 52 53 private int status[]; 54 private short size = -1; 55 private MatrixElt matrix[][]; 56 57 60 public synchronized int testRecvMatrix(Object stamp, short from, short to) 61 { 62 63 Update ptr = (Update) stamp; 64 if ((matrix[from][to].stamp + 1) < ptr.getStamp()) 65 { 66 return WAIT_TO_DELIVER; 68 } 69 else if ((matrix[from][to].stamp + 1) == ptr.getStamp()) 70 { 71 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 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 118 public synchronized Object 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 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 sb = new StringBuffer ("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 167 170 public short getNumberOfProcess() 171 { 172 return size; 173 } 174 175 178 public void setNumberOfProcess(short numberOfProcess) 179 { 180 if (matrix != null && numberOfProcess != size) 181 { 182 throw new IllegalStateException ( 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 201 204 public String [] listFc() 205 { 206 return EmptyStringArray.EMPTY_STRING_ARRAY; 207 } 208 209 213 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 |