Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 94e3ffbe358486b906884167408d82b721c5dd52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*******************************************************************************
 * Copyright (c) 2012 tieto deutschland gmbh (http://www.tieto.com)
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Thomas Jung (initial contribution)
 *    Thomas Schuetz
 *******************************************************************************/

package org.eclipse.etrice.tutorials.simulators.trafficlight;

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.TextField;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class PedastrianLightWnd extends Frame {
	private static final long serialVersionUID = 1L;

	private TrafficLight3 carLights = null;
	private TrafficLight2 pedLights = null;

	ButtonActionListener buttonListener = null;

	Button requestButton = null;
	TextField statusLine = null;


	public PedastrianLightWnd(int ipPort) {
		super("PedestrianLightsGUI");
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints gbc;
		carLights = new TrafficLight3(4);
		pedLights = new TrafficLight2(2);
		statusLine = new TextField();
		requestButton = new Button("REQUEST");
		requestButton.setEnabled(false);
		addWindowListener(new WindowClosingAdapter(true));

		setLayout(layout);

		gbc = makeGbc(0, 1, 1, 1);
		layout.setConstraints(carLights, gbc);
		add(carLights);

		gbc = makeGbc(1, 1, 1, 1);
		layout.setConstraints(pedLights, gbc);
		add(pedLights);

		statusLine.setText("listening on TCP Port " + ipPort);
		statusLine.setEditable(false);
		statusLine.setFocusable(false);
		gbc = makeGbc(0, 0, 2, 1);
		gbc.fill = GridBagConstraints.BOTH;
		layout.setConstraints(statusLine, gbc);
		add(statusLine);

		gbc = makeGbc(0, 2, 2, 1);
		gbc.fill = GridBagConstraints.BOTH;
		layout.setConstraints(requestButton, gbc);
		add(requestButton);

		pack();
		setVisible(true);

		loopSocket(ipPort); // blocking loop
	}

	private void loopSocket(int ipPort) {
		while (true) {
			// open and close socket endlessly
			try {
				// open Socket
				ServerSocket socketServer = new ServerSocket(ipPort);

				// wait blocking for client to connect
				Socket socket = socketServer.accept();
				statusLine.setText("socket connected !");

				// prepare input and output streams
				BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				OutputStream out = socket.getOutputStream();

				// add listener for outgoing command
				buttonListener = new ButtonActionListener(out);
				requestButton.addActionListener(buttonListener);
				requestButton.setEnabled(true);

				resetLights();

				// read blocking until socket is disconnected 
				String cmd;
				while ((cmd = in.readLine()) != null) {
					dispatchCommand(requestButton, cmd);
				}
				statusLine.setText("socket disconnected !");

				// deactivate button
				requestButton.removeActionListener(buttonListener);
				requestButton.setEnabled(false);

				resetLights();

				// clean up socket
				socket.close();
				socketServer.close();
				
			} catch (IOException e) {
				System.err.println(e.toString());
				System.exit(1);
			}
		}
	}

	private void dispatchCommand(Button requestButton, String cmd) {
		// check carLights
		if (cmd.equals("carLights=red")) {
			carLights.setState(carLights.RED);
		}
		if (cmd.equals("carLights=yellowRed")) {
			carLights.setState(carLights.YELLOWRED);
		}
		if (cmd.equals("carLights=green")) {
			carLights.setState(carLights.GREEN);
		}
		if (cmd.equals("carLights=yellow")) {
			carLights.setState(carLights.YELLOW);
		}
		if (cmd.equals("carLights=off")) {
			carLights.setState(carLights.OFF);
		}
		
		// check pedLights
		if (cmd.equals("pedLights=red")) {
			pedLights.setState(pedLights.RED);
		}
		if (cmd.equals("pedLights=green")) {
			pedLights.setState(pedLights.GREEN);
		}
		if (cmd.equals("pedLights=off")) {
			pedLights.setState(pedLights.OFF);
		}

		// check button
		if (cmd.equals("button=on")) {
			requestButton.setEnabled(true);
		}
		if (cmd.equals("button=off")) {
			requestButton.setEnabled(false);
		}
	}

	private void resetLights() {
		carLights.setState(carLights.OFF);
		pedLights.setState(pedLights.OFF);
	}

	private GridBagConstraints makeGbc(int x, int y, int width, int height) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = width;
		gbc.gridheight = height;
		gbc.weightx = 1;
		gbc.weighty = 1;
		// gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.insets = new Insets(1, 1, 1, 1);
		return gbc;
	}
}

Back to the top