package node.company;

import java.util.HashSet;
import java.util.Set;

import node.FatalNodeException;
import node.data.RepositoryException;
import transaction.Vault;
import communication.protocols.HelloProtocol;
import communication.protocols.Protocol;
import communication.protocols.TransactionTerminationProtocol;
import communication.protocols.TransactionTerminationReplyProtocol;
import communication.protocols.TransactionExecutionProtocol;
import communication.protocols.TransactionExecutionReplyProtocol;
import data.trainnetwork.BookableSection;
import data.trainnetwork.Seat;

public class DistributedRepositorySlave extends DistributedRepository {

	public DistributedRepositorySlave() throws RepositoryException {
		super();
	}

	@Override
	protected Set<Protocol> getProtocols() {
		Set<Protocol> protocols = new HashSet<Protocol>();
		
		// Accept and handle `Hello` requests from other nodes
		protocols.add(new HelloProtocol(nodes));
		
		// Accept and handle distributed transactions
		protocols.add(new TransactionExecutionProtocol<String, Vault<BookableSection>, Set<Seat>>(transactions, communicationLock));
		protocols.add(new TransactionExecutionReplyProtocol<String, Vault<BookableSection>, Set<Seat>>(transactionCoordinators));
		protocols.add(new TransactionTerminationProtocol<String, Vault<BookableSection>, Set<Seat>>(transactions, communicationLock));
		protocols.add(new TransactionTerminationReplyProtocol<String, Vault<BookableSection>, Set<Seat>>(transactionCoordinators));
		
		// TODO add client handling
		
		return protocols;
	}

	public void test() {
		while (true) {
			//sections.debugPrint();
			System.out.println(nodes);
			try {
				sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		DistributedRepositorySlave s;
		try {
			if (args.length < 1 || !(args[0] instanceof String)) {
				throw new RepositoryException("Arg1 required to be the master node's location of the static data cluster");
			}
			
			DistributedRepositorySlave.DATA_STORE_LOCATION = args[0];
			s = new DistributedRepositorySlave();
			s.start();
			//s.test();
		} catch (RepositoryException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

@@ -131,24 +131,106 @@
 yException;%0A
+import transaction.TransactionContent;%0Aimport transaction.TransactionCoordinator;%0A
 import trans
@@ -1670,217 +1670,480 @@
 %7B%0A%09%09
-while (true) %7B%0A%09%09%09//sections.debugPrint();%0A%09%09%09System.out.println(nodes);%0A%09%09%09try %7B%0A%09%09%09%09sleep(2000);%0A%09%09%09%7D catch (InterruptedException e) %7B%0A%09%09%09%09// TODO Auto-generated catch bl
+try %7B%0A%09%09%09sleep(5000);%0A%09%09%7D catch (InterruptedException e1) %7B%0A%09%09%09e1.printStackTrace();%0A%09%09%7D%0A%09%09%0A%09%09TransactionContent%3CString, Vault%3CBookableSection%3E, Set%3CSeat%3E%3E c = TransactionContentGenerator.getTestContent();%0A%09%09%0A%09%09TransactionCoordinator%3CString, Vault%3CBookableSection%3E, Set%3CSeat%3E%3E tc%0A%09%09%09= new TransactionCoordinator%3CString, Vault%3CBookableSection%3E, Set%3CSeat%3E%3E(c, sections, nodes, communicationL
 ock
+);
 %0A%09%09
+%0A
 %09%09
-e.printStackTrace(
+transactionCoordinators.put(tc.getTransactionId(), tc
 );%0A%09%09
-%09
-%7D
 %0A%09%09
-%7D
+tc.start();
 %0A%09%7D%0A
@@ -2524,18 +2524,16 @@
 t();%0A%09%09%09
-//
 s.test()

Add transaction test - DistributedRepositorySlave