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.Map; 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 ReplStack<T> extends ReceiverAdapter { JChannel channel; ArrayList container; String user_name = System.getProperty("user.name", "n/a"); public ReplStack() throws Exception { container = new ArrayList<T>(); start(); } public void push(T obj) { container.add(obj); } public T pop() { if (container.isEmpty()) { return null; } else { T x = (T) container.get(container.size() - 1); container.remove(container.size() - 1); return x; } } public T top() { if (container.isEmpty()) { return null; } else { T x = (T) container.get(container.size() - 1); return x; } } 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(); } @Override public void viewAccepted(View new_view) { // System.out.println("** view: " + new_view); } @Override public void receive(Message msg) { JSONObject obj = (JSONObject) msg.getObject(); String mode = obj.get("mode").toString(); if (mode.equals("stack_push")) { T param = (T) obj.get("body"); push(param); System.out.println(param + " pushed"); } else if (mode.equals("stack_pop")) { T x = pop(); System.out.println(x + " popped"); } else if (mode.equals("stack_top")) { T x = top(); System.out.println(x + " is top"); } } 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("stack_push")) { T object = (T) input.nextLine(); json_obj.put("mode", "stack_push"); json_obj.put("body", object); } else if (line.startsWith("stack_pop")) { json_obj.put("mode", "stack_pop"); json_obj.put("body", ""); } else if (line.startsWith("stack_top")) { json_obj.put("mode", "stack_top"); json_obj.put("body", ""); } Message msg = new Message(null, null, json_obj); channel.send(msg); json_obj.clear(); } catch (Exception e) { } } } 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 { ReplStack RS = new ReplStack<String>(); } }