提交 f25b8e3a 编写于 作者: L liquidsnake@sapo.pt

Small changes to classes in package statemanagment. Added some test code to...

Small changes to classes in package statemanagment. Added some test code to methods that I created in tomlayer (currently commented). The client of the counter demo is changed to infinitely send requests.
上级 840d495c
......@@ -54,6 +54,9 @@ public class SMMessage extends SystemMessage implements Externalizable {
}
public SMMessage() {
}
/**
* Retrieves the state log
* @return The state Log
......
......@@ -94,6 +94,9 @@ public class StateLog {
messageBatches[position] = batch;
//System.out.println("posicao: " + position);
//System.out.println("execucoes: " + execCounter);
position++;
execCounter++;
......
......@@ -31,12 +31,14 @@ public class StateManager {
private StateLog log;
private HashSet<Message> messages = null;
private int f;
private int lastEid;
public StateManager(int k, int f) {
this.log = new StateLog(k);
messages = new HashSet<Message>();
this.f = f;
this.lastEid = -1;
}
public void addReplica(int sender, int eid) {
......@@ -47,6 +49,19 @@ public class StateManager {
messages.clear();
}
public void emptyReplicas(int eid) {
for (Message m : messages)
if (m.eid <= eid) messages.remove(m);
}
public void setLastEID(int eid) {
lastEid = eid;
}
public int getLastEID() {
return lastEid;
}
public boolean moreThenF(int eid) {
int count = 0;
......
......@@ -18,14 +18,16 @@
package navigators.smart.statemanagment;
import java.io.Serializable;
/**
* This classe represents a state tranfered from a replica to another. The state associated with the last
* checkpoint together with all the batches of messages received do far, comprises the sender's
* current state
*
* @author João Sousa
* @author Jo�o Sousa
*/
public class TransferableState {
public class TransferableState implements Serializable {
private byte[][] messageBatches; // batches received since the last checkpoint.
private int nextEid; // Execution ID for the last checkpoint
......@@ -45,6 +47,9 @@ public class TransferableState {
}
public TransferableState() {
}
/**
* Retrieves all batches of messages
* @return Batch of messages
......
......@@ -34,6 +34,7 @@ import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import navigators.smart.clientsmanagement.ClientsManager;
import navigators.smart.clientsmanagement.PendingRequests;
import navigators.smart.communication.ServerCommunicationSystem;
......@@ -900,6 +901,15 @@ public final class TOMLayer extends Thread implements RequestReceiver {
public void saveState(byte[] state) {
stateManager.getLog().newCheckpoint(state);
/************************* TESTE *************************
int value = 0;
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
value += (stateManager.getLog().getState()[i] & 0x000000FF) << shift;
}
System.out.println("Estado: " + value);
System.out.println("Checkpoint: " + stateManager.getLog().getCurrentCheckpointEid());
/************************* TESTE *************************/
}
public void saveBatch(byte[] batch) {
stateManager.getLog().addMessageBatch(batch);
......@@ -908,8 +918,12 @@ public final class TOMLayer extends Thread implements RequestReceiver {
/** ISTO E CODIGO DO JOAO, PARA TRATAR DA TRANSFERENCIA DE ESTADO */
public void requestState(int me, int[] otherAcceptors, int sender, int eid) {
stateManager.addReplica(sender, eid);
if (stateManager.moreThenF(eid)) {
if (stateManager.moreThenF(eid) && stateManager.getLastEID() < eid) {
stateManager.setLastEID(eid);
//stateManager.emptyReplicas(eid);// isto causa uma excepcao
SMMessage smsg = new SMMessage(me, eid, TOMUtil.SM_REQUEST, null);
communication.send(otherAcceptors, smsg);
}
......@@ -920,16 +934,44 @@ public final class TOMLayer extends Thread implements RequestReceiver {
}
public void SMRequestDeliver(SMMessage msg) {
/************************* TESTE *************************
System.out.println("Recebi um pedido de estado!");
System.out.println("Quem enviou: " + msg.getSender());
System.out.println("Que tipo: " + msg.getType());
System.out.println("Que EID: " + msg.getEid());
System.exit(0);
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
java.util.logging.Logger.getLogger(TOMLayer.class.getName()).log(Level.SEVERE, null, ex);
}
/************************* TESTE *************************/
TransferableState state = getTransferableState(msg.getEid());
if (state != null) {
int[] targets = { msg.getSender() };
SMMessage smsg = new SMMessage(execManager.getProcessId(), -1, TOMUtil.SM_REPLY, state);
SMMessage smsg = new SMMessage(execManager.getProcessId(), msg.getEid(), TOMUtil.SM_REPLY, state);
communication.send(targets, smsg);
}
}
public void SMReplyDeliver(SMMessage msg) {
/************************* TESTE *************************
System.out.println("Recebi a minha resposta!");
System.out.println("Quem enviou: " + msg.getSender());
System.out.println("Que tipo: " + msg.getType());
System.out.println("Que EID: " + msg.getEid());
System.exit(0);
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
java.util.logging.Logger.getLogger(TOMLayer.class.getName()).log(Level.SEVERE, null, ex);
}
/************************* TESTE *************************/
}
/********************************************************/
}
......@@ -44,7 +44,7 @@ public class CounterClient {
int i=0;
//sends 1000 requests to replicas and then terminates
while(i<1000){
while(/**i<1000*/true){
int inc = Integer.parseInt(args[1]);
ByteArrayOutputStream out = new ByteArrayOutputStream(4);
new DataOutputStream(out).writeInt(inc);
......@@ -54,7 +54,7 @@ public class CounterClient {
System.out.println("Counter value: "+newValue);
i++;
}
System.exit(0);
//System.exit(0);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册