public int propose(int seq, Operation op) throws RemoteException { if (!work) return seq; boolean decided = false; while (!decided) { seq++; ArrayList<KeyValueServerInterface> list = getServersList(); int count = 0; int max = -1; for (KeyValueServerInterface server : list) { Instance ins = server.prepare(op.op_num, seq); if (ins != null) { count++; if (ins.n_a > max) { max = ins.n_a; // content = ins.content; op.content = ins.content; } } } if (count > (list.size() / 2)) { if (sendAllAccept(op.op_num, seq, op.content, list)) { for (KeyValueServerInterface server : list) { server.decide(seq, op); } decided = true; } } } return seq; }
public ArrayList<KeyValueServerInterface> getServersList() throws RemoteException { // ArrayList<KeyValueServerInterface> res = new ArrayList(); try { Registry registry = LocateRegistry.getRegistry(); String[] name = registry.list(); for (String str : name) { // if( str.equals(server_name) ) continue; KeyValueServerInterface obj = (KeyValueServerInterface) registry.lookup(str); res.add(obj); } } catch (Exception e) { System.out.println(e.getMessage()); } return res; }
public boolean sendAllAccept( int ins_num, int income_seq, String content, ArrayList<KeyValueServerInterface> list) throws RemoteException { int count = 0; for (KeyValueServerInterface server : list) { if (server.accept(ins_num, income_seq, content) != -1) count++; } if (count > (list.size() / 2)) return true; return false; }