diff --git a/src/navigators/smart/tom/demo/counter/CounterClient.java b/src/navigators/smart/tom/demo/counter/CounterClient.java new file mode 100644 index 0000000000000000000000000000000000000000..8151c9b1e306000df7ec969c82cb3e325af309d4 --- /dev/null +++ b/src/navigators/smart/tom/demo/counter/CounterClient.java @@ -0,0 +1,99 @@ +/** + * 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.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import java.io.InputStreamReader; +import navigators.smart.tom.ServiceProxy; +import navigators.smart.tom.util.Logger; + +/** + * Example client that updates a BFT replicated service (a counter). + * + */ +public class CounterClient { + + @SuppressWarnings("static-access") + public static void main(String[] args) throws IOException { + if (args.length < 2) { + System.out.println("Usage: java ...CounterClient []"); + System.out.println(" if equals 0 the request will be read-only"); + System.out.println(" default equals 1000"); + System.exit(-1); + } + + ServiceProxy counterProxy = new ServiceProxy(Integer.parseInt(args[0])); + + //counterProxy.setInvokeTimeout(1); + + int result = 0; + + try { + + int inc = Integer.parseInt(args[1]); + int numberOfOps = (args.length > 2) ? Integer.parseInt(args[2]) : 1000; + + boolean wait = false; + Logger.debug = true; + BufferedReader inReader = new BufferedReader(new InputStreamReader(System.in)); + + for (int i = 0; i < numberOfOps; i++) { + + if (wait) { + System.out.println("Iteration " + i); + System.out.println("Press Enter for next iteration, type 'exit' to exit or type 'go' to run all remaining iterations"); + + String lido = inReader.readLine(); + + if (lido.equals("exit")) { + break; + } else if (lido.equals("go")) { + wait = false; + } + } + + ByteArrayOutputStream out = new ByteArrayOutputStream(4); + new DataOutputStream(out).writeInt(inc); + + System.out.println("Counter sending: " + i); + byte[] reply = counterProxy.invoke(out.toByteArray(), (inc == 0)); + if(reply != null) { + int newValue = new DataInputStream(new ByteArrayInputStream(reply)).readInt(); + System.out.println("Counter value: " + newValue); + result = 0; + } else { + result = 1; + break; + } + } + + } catch(Exception e){ + e.printStackTrace(); + result = 1; + } finally { + counterProxy.close(); + System.exit(result); + } + } +} diff --git a/src/navigators/smart/tom/demo/counter/CounterServer.java b/src/navigators/smart/tom/demo/counter/CounterServer.java new file mode 100644 index 0000000000000000000000000000000000000000..a965b0fd5323bed10149312098809d0e0e2efa64 --- /dev/null +++ b/src/navigators/smart/tom/demo/counter/CounterServer.java @@ -0,0 +1,117 @@ +/** + * 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); + } + /********************************************************/ +} diff --git a/src/navigators/smart/tom/demo/counter/MultiCounterClient.java b/src/navigators/smart/tom/demo/counter/MultiCounterClient.java new file mode 100644 index 0000000000000000000000000000000000000000..0550c5767d2c42919f290e7a2d24c3637a25ab0f --- /dev/null +++ b/src/navigators/smart/tom/demo/counter/MultiCounterClient.java @@ -0,0 +1,45 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package navigators.smart.tom.demo.counter; + +/** + * + * @author alysson + */ +public class MultiCounterClient { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) throws Exception { + int numOfClients = (args.length > 0)?Integer.parseInt(args[0]):2; + int initialProcess = (args.length > 1)?Integer.parseInt(args[1]):4; + + Process[] p = new Process[numOfClients]; + + for (int i = 0; i < numOfClients; i++) { + int id = initialProcess+i; + int inc = 1; + + System.out.println("Starting client "+id); + + //UNIX (not tested yet!) + //p[i] = Runtime.getRuntime().exec("/bin/sh -e java -cp dist/SMaRt.jar " + // + "navigators.smart.tom.demo.counter.CounterClient " + id + " " + inc + // + " > output-" + id + "-" + inc + ".txt 2>&1"); + + //Windows + p[i] = Runtime.getRuntime().exec("cmd /c java -cp dist/SMaRt.jar " + + "navigators.smart.tom.demo.counter.CounterClient " + id + " " + inc + + " > output-" + id + "-" + inc + ".txt 2>&1"); + } + + for (int i = 0; i < numOfClients; i++) { + int r = p[i].waitFor(); + System.out.println("Client "+(i+initialProcess)+" finished with "+r); + } + + } +}