package swift.dc;

import static sys.net.api.Networking.Networking;
import swift.client.SwiftImpl;
import swift.crdt.CRDTIdentifier;
import swift.crdt.IntegerTxnLocal;
import swift.crdt.interfaces.CachePolicy;
import swift.crdt.interfaces.IsolationLevel;
import swift.crdt.interfaces.TxnHandle;
import sys.Sys;
import sys.net.api.Endpoint;
import sys.net.api.rpc.RpcEndpoint;

public class TestClient {
    public static void main(String[] args) {
        try {
            Sys.init();

            SwiftImpl server = SwiftImpl.newInstance(0, "localhost", DCConstants.SURROGATE_PORT);
            TxnHandle handle = server.beginTxn(IsolationLevel.SNAPSHOT_ISOLATION, CachePolicy.STRICTLY_MOST_RECENT,
                    false);
            IntegerTxnLocal i1 = handle.get(new CRDTIdentifier("e", "1"), false, swift.crdt.IntegerVersioned.class);
            System.out.println("(e,1) = " + i1.getValue());
            IntegerTxnLocal i2 = handle.get(new CRDTIdentifier("e", "2"), false, swift.crdt.IntegerVersioned.class);
            System.out.println("(e,2) = " + i2.getValue());
            i1.add(1);
            System.out.println("(e,1).add(1)");
            System.out.println("(e,1) = " + i1.getValue());
            handle.commit();
            System.out.println("commit");

            handle = server.beginTxn(IsolationLevel.SNAPSHOT_ISOLATION, CachePolicy.STRICTLY_MOST_RECENT, false);
            i1 = handle.get(new CRDTIdentifier("e", "1"), false, swift.crdt.IntegerVersioned.class);
            System.out.println("(e,1) = " + i1.getValue());
            i2 = handle.get(new CRDTIdentifier("e", "2"), false, swift.crdt.IntegerVersioned.class);
            System.out.println("(e,2) = " + i2.getValue());
            i1.add(1);
            System.out.println("(e,1).add(1)");
            System.out.println("(e,1) = " + i1.getValue());
            handle.commit();
            System.out.println("commit");

            handle = server.beginTxn(IsolationLevel.SNAPSHOT_ISOLATION, CachePolicy.STRICTLY_MOST_RECENT, false);
            i1 = handle.get(new CRDTIdentifier("t", "1"), true, swift.crdt.IntegerVersioned.class);
            System.out.println("(t,1) = " + i1.getValue());
            i2 = handle.get(new CRDTIdentifier("t", "2"), true, swift.crdt.IntegerVersioned.class);
            System.out.println("(t,2) = " + i2.getValue());
            i1.add(1);
            System.out.println("(t,1).add(1)");
            System.out.println("(t,1) = " + i1.getValue());
            handle.commit();
            System.out.println("commit");

            handle = server.beginTxn(IsolationLevel.SNAPSHOT_ISOLATION, CachePolicy.STRICTLY_MOST_RECENT, false);
            i1 = handle.get(new CRDTIdentifier("t", "1"), false, swift.crdt.IntegerVersioned.class);
            System.out.println("(t,1) = " + i1.getValue());
            i2 = handle.get(new CRDTIdentifier("t", "2"), false, swift.crdt.IntegerVersioned.class);
            System.out.println("(t,2) = " + i2.getValue());
            i1.add(1);
            System.out.println("(t,1).add(1)");
            System.out.println("(t,1) = " + i1.getValue());
            handle.commit();
            System.out.println("commit");

            handle = server.beginTxn(IsolationLevel.SNAPSHOT_ISOLATION, CachePolicy.STRICTLY_MOST_RECENT, false);
            i1 = handle.get(new CRDTIdentifier("t", "1"), false, swift.crdt.IntegerVersioned.class);
            System.out.println("(t,1) = " + i1.getValue());
            i2 = handle.get(new CRDTIdentifier("t", "2"), false, swift.crdt.IntegerVersioned.class);
            System.out.println("(t,2) = " + i2.getValue());
            i1.add(1);
            System.out.println("(t,1).add(1)");
            System.out.println("(t,1) = " + i1.getValue());
            handle.commit();
            System.out.println("commit");

            System.out.println("TetsClient ended with success");

            server.stop(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.exit(0);
    }
}

@@ -248,16 +248,68 @@
 nLevel;%0A
+import swift.crdt.interfaces.ObjectUpdatesListener;%0A
 import s
@@ -880,32 +880,86 @@
 rVersioned.class
+,%0A                    new DummyObjectUpdatesListener()
 );%0A            S
@@ -1111,32 +1111,86 @@
 rVersioned.class
+,%0A                    new DummyObjectUpdatesListener()
 );%0A            S
@@ -1643,32 +1643,86 @@
 rVersioned.class
+,%0A                    new DummyObjectUpdatesListener()
 );%0A            S
@@ -1858,32 +1858,86 @@
 rVersioned.class
+,%0A                    new DummyObjectUpdatesListener()
 );%0A            S
@@ -2389,32 +2389,86 @@
 rVersioned.class
+,%0A                    new DummyObjectUpdatesListener()
 );%0A            S
@@ -2603,32 +2603,86 @@
 rVersioned.class
+,%0A                    new DummyObjectUpdatesListener()
 );%0A            S
@@ -3135,32 +3135,86 @@
 rVersioned.class
+,%0A                    new DummyObjectUpdatesListener()
 );%0A            S
@@ -3350,32 +3350,86 @@
 rVersioned.class
+,%0A                    new DummyObjectUpdatesListener()
 );%0A            S
@@ -3882,32 +3882,86 @@
 rVersioned.class
+,%0A                    new DummyObjectUpdatesListener()
 );%0A            S
@@ -4105,16 +4105,70 @@
 ed.class
+,%0A                    new DummyObjectUpdatesListener()
 );%0A     
@@ -4626,11 +4626,263 @@
 );%0A    %7D
+%0A%0A    static class DummyObjectUpdatesListener implements ObjectUpdatesListener %7B%0A        @Override%0A        public void onObjectUpdate(TxnHandle txn, CRDTIdentifier id) %7B%0A            System.out.println(%22Yoohoo, the object has changed!%22);%0A        %7D%0A    %7D
 %0A%7D%0A

use dummy updates listener