/** * Copyright (c) 2007-2009 Alysson Bessani, Eduardo Alchieri, Paulo Sousa, and the authors indicated in the @author tags * * This file is part of SMaRt. * * SMaRt is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * SMaRt is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with SMaRt. If not, see . */ package navigators.smart.tom.demo.counter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import navigators.smart.tom.ServiceReplica; import navigators.smart.tom.util.DebugInfo; /** * Example replica that implements a BFT replicated service (a counter). * */ public class CounterServer extends ServiceReplica { private int counter = 0; private int iterations = 0; public CounterServer(int id) { super(id); } //******* EDUARDO BEGIN **************// public CounterServer(int id, boolean join) { super(id,join); } //******* EDUARDO END **************// @Override public byte[] executeCommand(int clientId, long timestamp, byte[] nonces, byte[] command, boolean readOnly, DebugInfo info) { iterations++; try { int increment = new DataInputStream(new ByteArrayInputStream(command)).readInt(); System.out.println("read-only request: "+readOnly); counter += increment; if (info == null) System.out.println("[server] (" + iterations + ") Counter incremented: " + counter); else System.out.println("[server] (" + iterations + " / " + info.eid + ") Counter incremented: " + counter); ByteArrayOutputStream out = new ByteArrayOutputStream(4); new DataOutputStream(out).writeInt(counter); return out.toByteArray(); } catch (IOException ex) { Logger.getLogger(CounterServer.class.getName()).log(Level.SEVERE, null, ex); return null; } } public static void main(String[] args){ if(args.length < 1) { System.out.println("Use: java CounterServer "); System.exit(-1); } if(args.length > 1) { new CounterServer(Integer.parseInt(args[0]), Boolean.valueOf(args[1])); }else{ new CounterServer(Integer.parseInt(args[0])); } } /** ISTO E CODIGO DO JOAO, PARA TRATAR DOS CHECKPOINTS */ @Override protected byte[] serializeState() { //System.out.println("vai ler counter para: "+this.counter); byte[] b = new byte[4]; for (int i = 0; i < 4; i++) { int offset = (b.length - 1 - i) * 8; b[i] = (byte) ((counter >>> offset) & 0xFF); } return b; //throw new UnsupportedOperationException("Not supported yet."); } @Override protected void deserializeState(byte[] state) { int value = 0; for (int i = 0; i < 4; i++) { int shift = (4 - 1 - i) * 8; value += (state[i] & 0x000000FF) << shift; } //System.out.println("vai setar counter para: "+value); this.counter = value; // System.out.println("Valor de counter deserializeState "+this.counter); } /********************************************************/ }