Browse Source

Demo done

Tom 5 years ago
parent
commit
1ed63f53e9
3 changed files with 356 additions and 31 deletions
  1. 113 7
      src/connection/ConnectHandheld.java
  2. 242 23
      src/connection/socket/Server.java
  3. 1 1
      src/ui/view/GUI.java

+ 113 - 7
src/connection/ConnectHandheld.java

@@ -2,17 +2,32 @@ package connection;
 
 import java.awt.BorderLayout;
 import java.awt.Dimension;
+import java.awt.image.BufferedImage;
 import java.io.IOException;
+import java.net.BindException;
+import java.text.NumberFormat;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
 
+import javax.swing.ImageIcon;
 import javax.swing.JButton;
+import javax.swing.JFormattedTextField;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
+import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JSplitPane;
 import javax.swing.JTextArea;
-import javax.swing.JTextField;
+import javax.swing.text.NumberFormatter;
+
 import api.Algorithm;
+import classes.AbstractCpsObject;
+import classes.CpsUpperNode;
+import classes.HolonObject;
+import connection.ConnectPhysical.HolonObjectStatus;
+import connection.ConnectPhysical.PhysicalLinkWrapper;
 import connection.socket.Server;
 import ui.controller.Control;
 import ui.view.Console;
@@ -21,16 +36,20 @@ public class ConnectHandheld implements Algorithm{
 	
 	//Holeg
 	Control control;
+	private HolonObject observed;
 	
 	//Gui
 	private JPanel content = new JPanel();
 	private JTextArea textArea;
 	
 
-	//RMI
+	//TCP
 	int port = 4242;
-
+	Server server;
+	
 	private Console console;
+	private boolean holonObjectConnected;
+	private JLabel houseLabel;
 	
 
 
@@ -70,16 +89,46 @@ public class ConnectHandheld implements Algorithm{
 		JPanel settingsPanel = new JPanel(null);
 		settingsPanel.setPreferredSize(new Dimension(400, 400));
 		settingsPanel.setMinimumSize(new Dimension(400, 225));
+		
+		NumberFormat format = NumberFormat.getIntegerInstance();
+		format.setGroupingUsed(false);
+		format.setParseIntegerOnly(true);
+		NumberFormatter integerFormatter = new NumberFormatter(format);
+		integerFormatter.setMinimum(0);
+		integerFormatter.setMaximum(65535);
+		integerFormatter.setCommitsOnValidEdit(true);
+		
+		
+		
 		JLabel portLabel = new JLabel("Port:");
 		portLabel.setBounds(10, 20, 35, 30);
 		settingsPanel.add(portLabel);
-		JTextField portTF = new JTextField(""+port);
+		JFormattedTextField portTF = new JFormattedTextField(integerFormatter);
+		portTF.setText(""+port);
 		portTF.setBounds(55 ,20, 80, 30);
+		portTF.addPropertyChangeListener(propertyChange ->{
+			String text = portTF.getValue().toString();
+			text = text.replaceAll("\\s", "");
+			port = Integer.parseInt((text));
+		});
 		settingsPanel.add(portTF);
+		
+		
+		houseLabel = new JLabel("House Status: " + (holonObjectConnected?"Connected":"Not selected"));
+		houseLabel.setBounds(10, 90, 220, 30);
+		settingsPanel.add(houseLabel);
+		
+		JButton selectRoom1Button = new JButton("Select");
+		selectRoom1Button.setBounds(230,95, 90, 20);
+		selectRoom1Button.addActionListener(actionEvent -> this.selectHolonObject());
+		settingsPanel.add(selectRoom1Button);
+		
+		
 		JButton connectButton = new JButton("Start Server");
 		connectButton.setBounds(100 ,175, 200, 50);
 		connectButton.addActionListener(actionEvent -> connect());
 		settingsPanel.add(connectButton);
+
 		return settingsPanel;
 	}
 
@@ -87,11 +136,27 @@ public class ConnectHandheld implements Algorithm{
 
 
 
+
+
+
+
+
+
 	private void connect() {
-		console.println("Start Server on Port:" + port);
+		if(!holonObjectConnected) {
+			console.println("Select a HolonObject");
+			return;
+		}
 		try {
-			Server server = new Server(port, console);
-		} catch (IOException e) {
+			if(server != null) {
+				server.stopServer();
+			}
+			console.println("Start Server on Port:" + port);
+			server = new Server(port, console, observed, control);
+		}catch(BindException e){
+			console.println(e.getMessage());
+		}
+		catch (IOException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
@@ -108,6 +173,47 @@ public class ConnectHandheld implements Algorithm{
 		this.control = control;
 	}
 
+	
+		private void selectHolonObject() {
+			List<HolonObject> holonObjectList = new ArrayList<HolonObject>();
+			addObjectToList(control.getModel().getObjectsOnCanvas(),holonObjectList);
+			Object[] possibilities = holonObjectList.stream().map(aCps -> new Handle<HolonObject>(aCps)).toArray();
+			@SuppressWarnings("unchecked")
+			Handle<HolonObject> selected = (Handle<HolonObject>) JOptionPane.showInputDialog(content, "Select HolonObject:", "HolonObject?",  JOptionPane.OK_OPTION,new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)) , possibilities, "");
+			if(selected != null) {
+				console.println("Selected: " + selected);
+				observed = selected.object;
+				holonObjectConnected = true;
+				updateSelectionLabel();
+			}
+		}
+		
+		private void updateSelectionLabel() {
+			houseLabel.setText("House Status: " + (holonObjectConnected?"Connected":"Not selected"));
+		}
+
+
+
+
 
+		private void addObjectToList(List<AbstractCpsObject> listToSearch, List<HolonObject> listToAdd){
+			for (AbstractCpsObject aCps : listToSearch) {
+				if (aCps instanceof HolonObject) listToAdd.add((HolonObject) aCps);
+				else if(aCps instanceof CpsUpperNode) {
+					addObjectToList(((CpsUpperNode)aCps).getNodes(),listToAdd);
+				}
+			}
+		}
+		
+		
+		private   class  Handle<T>{
+			public T object;
+			Handle(T object){
+				this.object = object;
+			}
+			public String toString() {
+				return object.toString();
+			}
+		}
 	
 }

+ 242 - 23
src/connection/socket/Server.java

@@ -1,35 +1,54 @@
 package connection.socket;
 import java.net.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import classes.HolonElement;
+import classes.HolonObject;
+import ui.controller.Control;
+
 import java.io.*;
 
 public class Server implements Runnable{
 	private ServerSocket serverSocket;
     private Socket clientSocket;
-    private PrintWriter out;
-    private BufferedReader in;
-    private boolean stopped = false;
+    private DataOutputStream out;
+    private DataInputStream in;
+    private boolean stopped = false;		
+    private boolean connection = false;
     private ui.view.Console console;
-    public Server(int port, ui.view.Console console) throws IOException {
+    
+    
+    private HolonObject observed;
+    private HolonObjectModel model;
+	private Control control;
+    
+    public Server(int port, ui.view.Console console, HolonObject observed, Control control) throws IOException {
+    	this.observed = observed;
+    	this.console = console;
+    	this.control = control;
     	//Bind Port
     	serverSocket = new ServerSocket(port);
         //Wait for Connection
     	Thread serverThread = new Thread(this);
         serverThread.start();
-        this.console = console;
             
             
     }
  
     public void stopServer() throws IOException {
-    	stopped = false;
+    	stopped = true;
         stopConnection();
-        serverSocket.close();
+        if(serverSocket != null)serverSocket.close();
+		console.println("Server Closed");
     }
 
 	private void stopConnection() throws IOException {
-		in.close();
-        out.close();
-        clientSocket.close();
+		connection = false;
+		if(in != null)in.close();
+		if(out != null)out.close();
+		if(clientSocket != null)clientSocket.close();
 	}
 
 	@Override
@@ -37,25 +56,225 @@ public class Server implements Runnable{
 		while(!stopped) {
 			try {
 			//Wait for new Connection
+			console.println("Wait for Connection..");
 			clientSocket = serverSocket.accept();
-			console.println("A Connection from " + clientSocket.getInetAddress() + ":" + clientSocket.getPort());
-			out = new PrintWriter(clientSocket.getOutputStream(), true);
-			in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
-			String inputLine;
-			while ((inputLine = in.readLine()) != null) {
-				console.println("Res: " + inputLine);
-				if (".".equals(inputLine)) {
-					out.println("bye");
-					break;
-				}
-				out.println(inputLine);
-				console.println("Send: " + inputLine);
+			console.println("Connection from " + clientSocket.getInetAddress() + ":" + clientSocket.getPort());
+			connection = true;
+			out = new DataOutputStream(clientSocket.getOutputStream());
+			in = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
+			if(observed == null) stopConnection();	
+			createModel();
+			Thread updateThread = new Thread(new UpdateLoop());
+			updateThread.start();
+			//InputLoop
+			try {
+				inputLoop();
+			}catch(EOFException e){
+				console.println("Connection Closed");
 			}
 			stopConnection();	
-			}catch (IOException e) {
+			}
+			catch(SocketException e){
+				//ServerSocket is closed
+				stopped = true;
+			}
+			catch (IOException e) {
 				e.printStackTrace();
 			}
+			connection = false;
 		}
+	}
+
+	private void inputLoop() throws IOException {
+		while (connection) {
+			//WaitForInput
+			byte inputByte = in.readByte();
+			//UpdateHoleg
+			if (inputByte == Command.SetAmount) {
+				if(observed == null) stopConnection();
+				console.println("Res: [" + inputByte + "] -> SetAmount");
+				int index = in.readInt();
+				int amount = in.readInt();
+				observed.getElements().get(index).setAmount(amount);
+				
+			}else if (inputByte == Command.SetEnable) {
+				if(observed == null) stopConnection();
+				console.println("Res: [" + inputByte + "] -> SetEnable");
+				int index = in.readInt();
+				boolean enabled = in.readBoolean();
+				observed.getElements().get(index).setActive(enabled);
+			}else if (inputByte == Command.IncreaseAmount) {
+				if(observed == null) stopConnection();
+				console.println("Res: [" + inputByte + "] -> IncreaseAmount");
+				int index = in.readInt();
+				HolonElement ele = observed.getElements().get(index);
+				ele.setAmount(ele.getAmount()+1);
+				
+			}else if (inputByte == Command.DecreaseAmount) {
+				if(observed == null) stopConnection();
+				console.println("Res: [" + inputByte + "] -> DecreaseAmount");
+				int index = in.readInt();
+				HolonElement ele = observed.getElements().get(index);
+				ele.setAmount(ele.getAmount()-1);
+				
+			} else{
+				console.println("Res: [" + inputByte + "] -> unknown");
+			}
+			control.calculateStateAndVisualForCurrentTimeStep();
+			control.updateCanvas();
+			control.getGui().triggerUpdateController(null);
+		}
+	}
+	
 	
+	private void createModel() {
+		model = new HolonObjectModel();
+		int index = 0;
+		for(HolonElement ele :observed.getElements()) {
+			model.add(ele, index++);
+		}
 	}
+	
+	
+	
+	
+	
+	public class UpdateLoop implements Runnable{
+		public UpdateLoop(){
+		}
+		@Override
+		public void run() {
+			sendUpdate();
+			while(!stopped && connection) {
+				if(checkForUpdates()){
+					sendUpdate();
+				}else {
+					waitOneSecond();	
+				}
+			}
+			
+		}
+
+		private void waitOneSecond() {
+			//wait one second
+			try {
+				TimeUnit.SECONDS.sleep(5);
+			} catch (InterruptedException e) {
+			}
+		}
+
+		private boolean checkForUpdates() {
+			//TODO Delete And CheckforUpdatesReal
+			//-->Test
+			waitOneSecond();
+			createModel();
+			//<--Test
+			return true;
+		}
+
+		private void sendUpdate() {
+			try {
+				if(observed == null) stopConnection();
+				console.println("Send: [" + Command.Update + "] -> Update");
+				out.writeByte(Command.Update);
+				out.writeInt(model.size());
+				for(HolonElementWrapper wrapper : model.getElements()) {
+					out.writeUTF(wrapper.name);
+					out.writeInt(wrapper.amount);
+					out.writeFloat(wrapper.energy);
+					out.writeBoolean(wrapper.enabled);
+				}
+				
+				
+				
+			} catch (IOException e) {
+				if(connection)console.println(e.getMessage());
+			}
+		}
+	}
+	
+	
+	public class Command {
+		//InputCommands
+		public static final int SetAmount = 10;
+		public static final int SetEnable = 11;
+		public static final int IncreaseAmount = 12;
+		public static final int DecreaseAmount = 13;
+		//UpdateResiveCommands
+		public static final int Update = 20;
+	}
+	
+	
+	
+	public class HolonObjectModel {
+		//Field
+		private List<HolonElementWrapper> elements;
+		private String holonObjectName;
+		
+		//constructor
+		public HolonObjectModel(){
+			elements = new ArrayList<HolonElementWrapper>();
+		}
+		
+		
+		public void add(HolonElement ele, int index ) {
+			String name = ele.getEleName();
+			int amount =ele.getAmount();
+			float energy =  ele.getEnergyPerElement();
+			boolean enabled = ele.isActive();
+			elements.add(new HolonElementWrapper(name, amount, energy, enabled, index));
+		}
+		
+		public int size() {
+			return elements.size();
+		}
+
+		//Getter/Setter
+		public List<HolonElementWrapper> getElements() {
+			return elements;
+		}
+		
+		public String getHolonObjectName() {
+			return holonObjectName;
+		}
+		public void setHolonObjectName(String holonObjectName) {
+			this.holonObjectName = holonObjectName;
+		}
+
+	}
+	public class HolonElementWrapper{
+		public int index;
+		public String name;
+		public int amount;
+		public float energy;
+		public boolean enabled;
+		public HolonElementWrapper(){
+			
+		}
+		public HolonElementWrapper(String name, int amount, float energy, boolean enabled, int index){
+			this.name = name;
+			this.amount = amount;
+			this.energy = energy;
+			this.enabled = enabled;
+		}
+		@Override
+		public boolean equals(Object obj) {
+			if (obj == this) {
+				return true;
+			}
+			if (!(obj instanceof HolonElementWrapper)) {
+			   return false;
+			}
+			HolonElementWrapper element = (HolonElementWrapper) obj;
+			return this.name == element.name &&
+				   this.amount == element.amount &&
+				   this.energy == element.energy &&
+				   this.enabled == element.enabled;
+		}
+		
+	}
+	
+	
+	
+	
 }

+ 1 - 1
src/ui/view/GUI.java

@@ -3038,7 +3038,7 @@ public class GUI implements CategoryListener {
 				selectedValueBY, 2);
 	}
 
-	private void triggerUpdateController(AbstractCpsObject temp) {
+	public void triggerUpdateController(AbstractCpsObject temp) {
 		if (temp != null) {
 			updCon.paintProperties(temp);
 		}