Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package replicatedstackjgroups; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.util.ArrayList; import java.util.Scanner; import org.jgroups.JChannel; import org.jgroups.Message; import org.jgroups.ReceiverAdapter; import org.jgroups.View; import org.jgroups.util.Util; import org.json.simple.JSONObject; /** * * @author Andarias Silvanus & Rita Sarah */ public class ReplSet<T> extends ReceiverAdapter { private ArrayList<T> container; JChannel channel; String user_name = System.getProperty("user.name", "n/a"); public ReplSet() throws Exception { container = new ArrayList<T>(); start(); } public boolean add(T obj) { if (contains(obj)) return false; else { container.add(obj); return true; } } public boolean contains(T obj) { return container.contains(obj); } public boolean remove(T obj) { if (contains(obj)) { container.remove(obj); return true; } else return false; } public int getLength() { return container.size(); } public boolean isEmpty() { return container.isEmpty(); } public T getObject(int idx) { return (T) container.get(idx); } public ArrayList getContainer() { return container; } private void start() throws Exception { channel = new JChannel(); // use the default config, udp.xml channel.setReceiver(this); channel.connect("ChatCluster"); channel.getState(null, 10000); eventLoop(); channel.close(); } public void viewAccepted(View new_view) { // System.out.println("** view: " + new_view); } public void receive(Message msg) { JSONObject json_obj = (JSONObject) msg.getObject(); String mode = json_obj.get("mode").toString(); T param = (T) json_obj.get("body"); if (mode.equals("set_add")) { boolean res = add(param); if (res) System.out.println("set_add berhasil dilakukan"); else System.out.println("set_add tidak berhasil dilakukan karena SET sudah mengandung object tersebut"); } else if (mode.equals("set_contains")) { boolean res = contains(param); if (res) System.out.println("object tersebut exist di SET"); else System.out.println("object tersebut tidak exist di SET"); } else if (mode.equals("set_remove")) { boolean res = remove(param); if (res) System.out.println("set_remove berhasil dilakukan"); else System.out .println("set_remove tidak berhasil dilakukan karena SET tidak mengandung object tersebut"); } } private void eventLoop() { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); Scanner input = new Scanner(System.in); JSONObject json_obj = new JSONObject(); while (true) { try { System.out.print("> "); System.out.flush(); // String line = in.readLine().toLowerCase(); String line = input.next().toLowerCase(); if (line.startsWith("quit") || line.startsWith("exit")) break; else if (line.startsWith("set_add")) { T packet = (T) input.nextLine(); json_obj.put("mode", "set_add"); json_obj.put("body", packet); } else if (line.startsWith("set_contains")) { T packet = (T) input.nextLine(); json_obj.put("mode", "set_contains"); json_obj.put("body", packet); } else if (line.startsWith("set_remove")) { T packet = (T) input.nextLine(); json_obj.put("mode", "set_remove"); json_obj.put("body", packet); } else if (line.startsWith("set_print")) { printSet(); json_obj.put("mode", "print"); json_obj.put("body", ""); } Message msg = new Message(null, null, json_obj); channel.send(msg); json_obj.clear(); } catch (Exception e) { } } } private void printSet() { if (container.isEmpty()) System.out.println("SET kosong"); else { for (int i = 0; i < container.size(); i++) { System.out.println(getObject(i)); } } } public void getState(OutputStream output) throws Exception { synchronized (container) { Util.objectToStream(container, new DataOutputStream(output)); } } @SuppressWarnings("unchecked") public void setState(InputStream input) throws Exception { ArrayList<T> remoteSet = (ArrayList<T>) Util.objectFromStream(new DataInputStream(input)); synchronized (container) { container.clear(); container.addAll(remoteSet); } // System.out.println(remoteSet.size() + " messages in chat history):"); // for(int i = 0; i<remoteSet.size(); i++) // System.out.println(remoteSet.get(i)); } public static void main(String[] args) throws Exception { ReplSet RS = new ReplSet<String>(); } }