<commit_before>package ca.sfu.server;
import java.io.IOException;
import java.util.ArrayList;

import ca.sfu.cmpt431.facility.Board;
import ca.sfu.cmpt431.facility.BoardOperation;
import ca.sfu.cmpt431.facility.Comrade;
import ca.sfu.cmpt431.facility.Outfits;
import ca.sfu.cmpt431.message.Message;
import ca.sfu.cmpt431.message.MessageCodeDictionary;
import ca.sfu.cmpt431.message.join.JoinRequestMsg;
import ca.sfu.cmpt431.message.join.JoinSplitMsg;
import ca.sfu.cmpt431.message.leave.LeaveReceiverMsg;
import ca.sfu.cmpt431.message.merge.MergeLastMsg;
import ca.sfu.cmpt431.message.regular.RegularBoardReturnMsg;
import ca.sfu.cmpt431.message.regular.RegularNextClockMsg;
import ca.sfu.cmpt431.message.regular.RegularOutfitMsg;
import ca.sfu.network.MessageReceiver;
import ca.sfu.network.MessageSender;
import ca.sfu.network.SynchronizedMsgQueue.MessageWithIp;


public class Server{
	
	protected static final int LISTEN_PORT = 6560;
	private MessageReceiver Receiver;

	private ArrayList<MessageSender> newClientSender = new ArrayList<MessageSender>();
	private ArrayList<Comrade>  regedClientSender = new ArrayList<Comrade>();
	
	private ArrayList<Integer> toLeave = new ArrayList<Integer>();
	
	private int waiting4confirm = 0;
	private int nextClock = 0;
	
	private int status;
	
	private int phase;
	private static final int COMPUTE = 0;
	private static final int ADD = 1;
	private static final int LEAVE = 2;
	
	/* UI widgets */
	MainFrame frame = null;
	InformationPanel infoPanel = null;
	
	public Server() throws IOException {
		Receiver = new MessageReceiver(LISTEN_PORT);
		status = 0;
	}
	
	protected void startServer() throws IOException, ClassNotFoundException, InterruptedException
	{
		// UI
		Board b = BoardOperation.LoadFile("Patterns/HerschelLoop.lg");
		
		System.out.println("UI");
		frame = new MainFrame(b, 800, 800);
		infoPanel = new InformationPanel();
		
		MessageWithIp m;
		int result = -1;
		
		while(true) {
			if(!Receiver.isEmpty()) {
				m = Receiver.getNextMessageWithIp();
				
				switch(status) {
					//waiting for first client
					case 0:
						handleNewAddingLeaving(m,1);
						handlePending();
						//send it the outfit
						regedClientSender.get(0).sender.sendMsg(new RegularOutfitMsg(-1, -1, new Outfits(0,nextClock,0,0,b)));
						waiting4confirm++;
						status = 2;
						break;
					
					//wait for the confirm
					//start a cycle
					case 2:
						if(handleNewAddingLeaving(m,2))
							break;
										
						handleConfirm(m,3); //expect only one message responding for JoinOutfitsMsg
						
						if(!newClientSender.isEmpty()){
							handlePending();
							status = 2;
							break;
						}
						
						if(waiting4confirm == 0){
							//send you a start
							System.out.println("sending start");
							infoPanel.setCycleNum(frame.automataPanel.getCycle());
							for (Comrade var : regedClientSender) {
								var.sender.sendMsg(new RegularNextClockMsg(nextClock));
								waiting4confirm++;
							}
							status = 3;
							phase = COMPUTE;
						}
						
						break;
						
					//waiting for the client to send the result back
					//handle new adding or
					//restart next cycle
					case 3:
						if(handleNewAddingLeaving(m,3))
							break;
						
						if(phase == COMPUTE){
							handleNewBoardInfo(m,b,3);
							if(waiting4confirm!=0){
								break;
							}
							
//							Thread.sleep(50);
							frame.repaint();
							infoPanel.setCellNum(frame.automataPanel.getCell());
							infoPanel.setLifeNum(frame.automataPanel.getAlive());
							
							infoPanel.setTargetNum("localhost");
							
							phase = LEAVE;
//							BoardOperation.Print(b);
							System.out.println("repaint");
						}
						
						if(phase == LEAVE){
							
							//deal with the confirm
							//manage the heap
							if(((Message)m.extracMessage()).getMessageCode()==MessageCodeDictionary.REGULAR_CONFIRM){
//								System.out.println("result:"+result);
								if(result == 1){
									int s = regedClientSender.size();
									
//									System.out.println(regedClientSender.get(s-2).id);
									
									regedClientSender.get(s-1).sender.sendMsg(new LeaveReceiverMsg(MessageCodeDictionary.ID_SERVER, 0, ""));
									regedClientSender.get(s-1).sender.close();
									regedClientSender.remove(s-1);
									
									Comrade c = regedClientSender.get(s-2);
									regedClientSender.remove(s-2);
									regedClientSender.add(0, c);<commit_msg>server1:44<commit_after>package ca.sfu.server;
import java.io.IOException;
import java.util.ArrayList;

import ca.sfu.cmpt431.facility.Board;
import ca.sfu.cmpt431.facility.BoardOperation;
import ca.sfu.cmpt431.facility.Comrade;
import ca.sfu.cmpt431.facility.Outfits;
import ca.sfu.cmpt431.message.Message;
import ca.sfu.cmpt431.message.MessageCodeDictionary;
import ca.sfu.cmpt431.message.join.JoinRequestMsg;
import ca.sfu.cmpt431.message.join.JoinSplitMsg;
import ca.sfu.cmpt431.message.leave.LeaveReceiverMsg;
import ca.sfu.cmpt431.message.merge.MergeLastMsg;
import ca.sfu.cmpt431.message.regular.RegularBoardReturnMsg;
import ca.sfu.cmpt431.message.regular.RegularNextClockMsg;
import ca.sfu.cmpt431.message.regular.RegularOutfitMsg;
import ca.sfu.network.MessageReceiver;
import ca.sfu.network.MessageSender;
import ca.sfu.network.SynchronizedMsgQueue.MessageWithIp;


public class Server{
	
	protected static final int LISTEN_PORT = 6560;
	private MessageReceiver Receiver;

	private ArrayList<MessageSender> newClientSender = new ArrayList<MessageSender>();
	private ArrayList<Comrade>  regedClientSender = new ArrayList<Comrade>();
	
	private ArrayList<Integer> toLeave = new ArrayList<Integer>();
	
	private int waiting4confirm = 0;
	private int nextClock = 0;
	
	private int status;
	
	private int phase;
	private static final int COMPUTE = 0;
	private static final int ADD = 1;
	private static final int LEAVE = 2;
	
	/* UI widgets */
	MainFrame frame = null;
	InformationPanel infoPanel = null;
	
	public Server() throws IOException {
		Receiver = new MessageReceiver(LISTEN_PORT);
		status = 0;
	}
	
	protected void startServer() throws IOException, ClassNotFoundException, InterruptedException
	{
		// UI
		Board b = BoardOperation.LoadFile("Patterns/HerschelLoop.lg");
		
		System.out.println("UI");
		frame = new MainFrame(b, 800, 800);
		infoPanel = new InformationPanel();
		
		MessageWithIp m;
		int result = -1;
		
		while(true) {
			if(!Receiver.isEmpty()) {
				m = Receiver.getNextMessageWithIp();
				
				switch(status) {
					//waiting for first client
					case 0:
						handleNewAddingLeaving(m,1);
						handlePending();
						//send it the outfit
						regedClientSender.get(0).sender.sendMsg(new RegularOutfitMsg(-1, -1, new Outfits(0,nextClock,0,0,b)));
						waiting4confirm++;
						status = 2;
						break;
					
					//wait for the confirm
					//start a cycle
					case 2:
						if(handleNewAddingLeaving(m,2))
							break;
										
						handleConfirm(m,3); //expect only one message responding for JoinOutfitsMsg
						
						if(!newClientSender.isEmpty()){
							handlePending();
							status = 2;
							break;
						}
						
						if(waiting4confirm == 0){
							//send you a start
							System.out.println("sending start");
							infoPanel.setCycleNum(frame.automataPanel.getCycle());
							for (Comrade var : regedClientSender) {
								var.sender.sendMsg(new RegularNextClockMsg(nextClock));
								waiting4confirm++;
							}
							status = 3;
							phase = COMPUTE;
						}
						
						break;
						
					//waiting for the client to send the result back
					//handle new adding or
					//restart next cycle
					case 3:
						if(handleNewAddingLeaving(m,3))
							break;
						
						if(phase == COMPUTE){
							handleNewBoardInfo(m,b,3);
							if(waiting4confirm!=0){
								break;
							}
							
//							Thread.sleep(50);
							frame.repaint();
							infoPanel.setCellNum(frame.automataPanel.getCell());
							infoPanel.setLifeNum(frame.automataPanel.getAlive());
							
							infoPanel.setTargetNum("localhost");
							
							phase = LEAVE;
//							BoardOperation.Print(b);
							System.out.println("repaint");
						}
						
						if(phase == LEAVE){
							
							//deal with the confirm
							//manage the heap
							if(((Message)m.extracMessage()).getMessageCode()==MessageCodeDictionary.REGULAR_CONFIRM){
								System.out.println("result:"+result);
								if(result == 1){
									int s = regedClientSender.size();
									
//									System.out.println(regedClientSender.get(s-2).id);
									
									regedClientSender.get(s-1).sender.sendMsg(new LeaveReceiverMsg(MessageCodeDictionary.ID_SERVER, 0, ""));
									regedClientSender.get(s-1).sender.close();
									regedClientSender.remove(s-1);
									
									Comrade c = regedClientSender.get(s-2);
									regedClientSender.remove(s-2);
									regedClientSender.add(0, c);
{'fraction_non_alphanumeric': 0.11346478873239436, 'fraction_numerical': 0.020845070422535212, 'mean_word_length': 2.8981115502854635, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2226754', 'n_tokens_mistral': 3896, 'n_tokens_neox': 2716, 'n_words': 543}