Skip to content
Snippets Groups Projects
Commit 120a3083 authored by Ludovic Apvrille's avatar Ludovic Apvrille
Browse files

Update on TTool demo

parent db665b1d
No related branches found
No related tags found
No related merge requests found
import java.io.*;
import java.net.*;
import java.util.*;
public class DatagramServer implements Runnable {
private static final int PORT = 8374;
private static final int SENDING_PORT = 8373;
private static final int PORT = 8374;
private static final int SENDING_PORT = 8373;
protected DatagramSocket socket;
protected BufferedReader in = null;
protected DatagramPacket dgp;
protected Feeder feed;
protected int port, sendingPort;
protected boolean go;
protected boolean notReceived = true;
private Thread t;
public DatagramServer() {
port = PORT;
sendingPort = SENDING_PORT;
port = PORT;
sendingPort = SENDING_PORT;
}
public void setPort(int _port) {
port = _port;
port = _port;
}
public void setFeeder(Feeder _feed) {
feed = _feed;
feed = _feed;
}
public void runServer() {
go = true;
t = new Thread(this);
t.start();
go = true;
t = new Thread(this);
t.start();
}
public void stopServer() {
go = false;
if (t != null) {
t.interrupt();
}
go = false;
if (t != null) {
t.interrupt();
}
}
public static void main(String[] args) {
DatagramServer ds = new DatagramServer();
ds.runServer();
}
public void run() {
byte[] buf = new byte[1000];
try {
socket = new DatagramSocket(port);
//sendingSocket = new DatagramSocket(sendingPort);
dgp = new DatagramPacket(buf, buf.length);
System.out.println("Server started on port:" + port);
while (go) {
socket.receive(dgp);
notReceived = false;
String rec = new String(dgp.getData(), 0, dgp.getLength());
String rcvd = rec + ", length=" + dgp.getLength() + ", from address: "
+ dgp.getAddress() + ", port: " + dgp.getPort();
//System.out.println("Received:" + rcvd);
if (feed != null) {
feed.setMessage(rec);
}
//BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
//String outMessage = stdin.readLine();
//buf = ("Server say: " + outMessage).getBytes();
//DatagramPacket out = new DatagramPacket(buf, buf.length, dgp.getAddress(), dgp.getPort());
//sk.send(out);
}
} catch (Exception e) {
System.out.println("Exception e:" + e.getMessage());
}
}
public boolean sendTo(String s) {
if ((socket == null) || (dgp == null)) {
return false;
}
try {
byte[] buf = s.getBytes();
DatagramPacket out = new DatagramPacket(buf, buf.length, dgp.getAddress(), dgp.getPort());
socket.send(out);
} catch (Exception e) {
System.out.println("Exception e:" + e.getMessage());
return false;
}
return true;
}
public boolean sendDatagramTo(String s, int port) {
if (socket == null) {
return false;
}
System.out.println("Datagram sending 1");
try {
DatagramSocket sendingSocket = new DatagramSocket(port+100);
InetAddress addr = InetAddress.getByName("localhost");;
/*if (notReceived) {
// We assume "localhost" for the address
System.out.println("Datagram sending 1.1");
addr = InetAddress.getByName("localhost");
} else {
System.out.println("Datagram sending 1.2");
addr = dgp.getAddress();
}*/
//System.out.println("Datagram sending 2");
byte[] buf = s.getBytes();
System.out.println("Datagram sending 3 on port=" + port);
DatagramPacket out = new DatagramPacket(buf, buf.length, addr, port);
System.out.println("Datagram sending 4");
sendingSocket.send(out);
System.out.println("Datagram sending 5");
sendingSocket.close();
System.out.println("Datagram sending 5.1");
} catch (Exception e) {
System.out.println("Exception e:" + e.getMessage());
return false;
}
System.out.println("Datagram sending 6");
return true;
}
}
\ No newline at end of file
DatagramServer ds = new DatagramServer();
ds.runServer();
}
public void run() {
byte[] buf = new byte[1000];
try {
socket = new DatagramSocket(port);
//sendingSocket = new DatagramSocket(sendingPort);
dgp = new DatagramPacket(buf, buf.length);
System.out.println("Server started on port:" + port);
while (go) {
socket.receive(dgp);
notReceived = false;
String rec = new String(dgp.getData(), 0, dgp.getLength());
String rcvd = rec + ", length=" + dgp.getLength() + ", from address: "
+ dgp.getAddress() + ", port: " + dgp.getPort();
//System.out.println("Received:" + rcvd);
if (feed != null) {
feed.setMessage(rec);
}
//BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
//String outMessage = stdin.readLine();
//buf = ("Server say: " + outMessage).getBytes();
//DatagramPacket out = new DatagramPacket(buf, buf.length, dgp.getAddress(), dgp.getPort());
//sk.send(out);
}
} catch (Exception e) {
System.out.println("Exception e:" + e.getMessage());
}
}
public boolean sendTo(String s) {
if ((socket == null) || (dgp == null)) {
return false;
}
try {
byte[] buf = s.getBytes();
DatagramPacket out = new DatagramPacket(buf, buf.length, dgp.getAddress(), dgp.getPort());
socket.send(out);
} catch (Exception e) {
System.out.println("Exception e:" + e.getMessage());
return false;
}
return true;
}
public boolean sendDatagramTo(String s, int port) {
if (socket == null) {
return false;
}
System.out.println("Datagram sending 1");
try {
DatagramSocket sendingSocket = new DatagramSocket(port+100);
InetAddress addr = InetAddress.getByName("localhost");;
/*if (notReceived) {
// We assume "localhost" for the address
System.out.println("Datagram sending 1.1");
addr = InetAddress.getByName("localhost");
} else {
System.out.println("Datagram sending 1.2");
addr = dgp.getAddress();
}*/
//System.out.println("Datagram sending 2");
byte[] buf = s.getBytes();
System.out.println("Datagram sending 3 on port=" + port);
DatagramPacket out = new DatagramPacket(buf, buf.length, addr, port);
System.out.println("Datagram sending 4");
sendingSocket.send(out);
System.out.println("Datagram sending 5");
sendingSocket.close();
System.out.println("Datagram sending 5.1");
} catch (Exception e) {
System.out.println("Exception e:" + e.getMessage());
return false;
}
System.out.println("Datagram sending 6");
return true;
}
}
......@@ -7,108 +7,108 @@ import java.awt.image.*;
import java.io.*;
import java.util.*;
public class MainMicrowave extends JFrame implements Feeder, MouseListener {
private MicrowavePanel mp;
private DatagramServer ds;
public MainMicrowave() {
super("Microwave demonstration");
setSize(800, 600);
setVisible(true);
ds = new DatagramServer();
ds.setFeeder(this);
initComponents();
ds.runServer();
super("Microwave demonstration");
setSize(800, 600);
setVisible(true);
ds = new DatagramServer();
ds.setFeeder(this);
initComponents();
ds.runServer();
}
public void initComponents() {
setLayout(new BorderLayout());
mp = new MicrowavePanel();
mp.addMouseListener(this);
mp.setPreferredSize(new Dimension(800,600));
add(mp, BorderLayout.CENTER);
mp.revalidate();
setLayout(new BorderLayout());
mp = new MicrowavePanel();
mp.addMouseListener(this);
mp.setPreferredSize(new Dimension(800,600));
add(mp, BorderLayout.CENTER);
mp.revalidate();
}
public void setMessage(String msg) {
if (mp == null) {
return;
}
int index;
String s;
int duration;
System.out.println("Got message:" + msg);
try {
if (msg.startsWith("Duration ")) {
s = msg.substring(9, msg.length());
duration = Integer.decode(s.trim()).intValue();
mp.setDuration(duration);
mp.setStart(false);
//System.out.println("Setting new duration :" + duration);
mp.setCookingFinished(false);
} else if (msg.startsWith("Start ")) {
s = msg.substring(6, msg.length());
duration = Integer.decode(s.trim()).intValue();
mp.setDuration(duration);
mp.setStart(true);
mp.setCookingFinished(false);
//System.out.println("Setting new duration (start): " + duration);
} else if (msg.startsWith("Magnetron_ON")) {
mp.setMagnetronON(true);
mp.setCookingFinished(false);
} else if (msg.startsWith("Magnetron_OFF")) {
mp.setMagnetronON(false);
mp.setCookingFinished(false);
} else if (msg.startsWith("Open Door")) {
mp.setDoorOpened(true);
mp.setCookingFinished(false);
} else if (msg.startsWith("Close Door")) {
mp.setDoorOpened(false);
mp.setCookingFinished(false);
} else if (msg.startsWith("Dring")) {
mp.setCookingFinished(true);
}
} catch (Exception e) {
System.out.println("Exception when computing message: " + e.getMessage());
}
mp.repaint();
if (mp == null) {
return;
}
int index;
String s;
int duration;
System.out.println("Got message:" + msg);
try {
if (msg.startsWith("Duration ")) {
s = msg.substring(9, msg.length());
duration = Integer.decode(s.trim()).intValue();
mp.setDuration(duration);
mp.setStart(false);
//System.out.println("Setting new duration :" + duration);
mp.setCookingFinished(false);
} else if (msg.startsWith("Start ")) {
s = msg.substring(6, msg.length());
duration = Integer.decode(s.trim()).intValue();
mp.setDuration(duration);
mp.setStart(true);
mp.setCookingFinished(false);
//System.out.println("Setting new duration (start): " + duration);
} else if (msg.startsWith("Magnetron_ON")) {
mp.setMagnetronON(true);
mp.setCookingFinished(false);
} else if (msg.startsWith("Magnetron_OFF")) {
mp.setMagnetronON(false);
mp.setCookingFinished(false);
} else if (msg.startsWith("Open Door")) {
mp.setDoorOpened(true);
mp.setCookingFinished(false);
} else if (msg.startsWith("Close Door")) {
mp.setDoorOpened(false);
mp.setCookingFinished(false);
} else if (msg.startsWith("Dring")) {
mp.setCookingFinished(true);
}
} catch (Exception e) {
System.out.println("Exception when computing message: " + e.getMessage());
}
mp.repaint();
}
public void mouseClicked(MouseEvent e){
int x = e.getX();
int y = e.getY();
System.out.println("Mouse clicked!!!");
// START?
if ((x>630)&&(x<720)&&(y>335)&&(y<365)) {
System.out.println("Mouse clicked on start");
if (ds != null) {
ds.sendDatagramTo(" ", 8375);
}
System.out.println("Action on start sent");
}
int x = e.getX();
int y = e.getY();
System.out.println("Mouse clicked!!!");
// START?
if ((x>630)&&(x<720)&&(y>335)&&(y<365)) {
System.out.println("Mouse clicked on start");
if (ds != null) {
ds.sendDatagramTo(" ", 8375);
}
System.out.println("Action on start sent");
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public static void main(String[] args) {
MainMicrowave mmw = new MainMicrowave();
}
}
\ No newline at end of file
MainMicrowave mmw = new MainMicrowave();
}
}
......@@ -7,162 +7,162 @@ import java.awt.image.*;
import java.io.*;
import java.util.*;
public class MicrowavePanel extends JPanel {
private int duration = 0;
private boolean magnetronON =false;
private int duration = 0;
private boolean magnetronON =false;
private boolean start = false;
private boolean doorOpened = false;
private boolean cookingFinished = false;
public MicrowavePanel() {
setBackground(Color.white);
}
public void setCookingFinished(boolean finished) {
cookingFinished = finished;
}
public void setDoorOpened(boolean opened) {
doorOpened = opened;
}
public void setStart(boolean on) {
start = on;
}
public void setMagnetronON(boolean isOn) {
magnetronON = isOn;
}
public void setDuration(int _duration) {
duration = _duration;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//System.out.println("Salut .. repainting");
// Foot
g.setColor(Color.black);
g.fillRect(100, 500, 100, 30);
g.fillRect(600, 500, 100, 30);
// Main oven
g.setColor(Color.gray);
g.fillRect(50, 50, 700, 450);
g.setColor(Color.black);
g.drawRect(50, 50, 700, 450);
g.setColor(Color.white);
g.fillRoundRect(75, 75, 500, 400, 20, 20);
// Inside of the oven
g.setColor(Color.black);
g.fillRoundRect(100, 100, 450, 350, 20, 20);
if (doorOpened) {
// Inside the oven
int dec = -5;
g.setColor(Color.white);
g.fillRoundRect(140, 140, 370, 270, 20, 20);
g.setColor(Color.black);
g.drawRect(140, 140, 370, 270);
g.drawRect(200, 190, 250, 170);
g.drawLine(140, 140, 200, 190);
g.drawLine(510, 140, 450, 190);
g.drawLine(510, 410, 450, 360);
g.drawLine(140, 410, 200, 360);
//Door
g.setColor(Color.white);
g.fillRect(95+dec, 110, 35, 330);
g.setColor(Color.black);
g.drawRect(95+dec, 110, 35, 330);
g.fillRect(95+dec, 110, 10, 330);
g.setColor(Color.gray);
g.fillRect(65+dec, 100, 25, 350);
g.fillRect(65+dec, 150, 35, 30);
g.fillRect(65+dec, 370, 35, 30);
g.setColor(Color.black);
g.drawRect(65+dec, 100, 25, 350);
} else {
}
if (magnetronON) {
g.setColor(Color.red);
g.fillRoundRect(150, 150, 350, 250, 20, 20);
}
if (doorOpened) {
// Handle of the door
} else {
// Door closed
g.setColor(Color.gray);
g.fillRect(460, 100, 50, 350);
g.setColor(Color.white);
g.drawRect(460, 100, 50, 350);
}
// Panel to print duration & information
g.setColor(Color.white);
g.fillRect(625, 100, 100, 50);
g.setColor(Color.black);
g.fillRect(630, 105, 90, 40);
g.setColor(Color.white);
g.fillRect(625, 160, 100, 150);
g.setColor(Color.black);
g.fillRect(630, 165, 90, 140);
// Panel for start button
g.setColor(Color.white);
g.fillRect(625, 330, 100, 40);
g.setColor(Color.black);
g.fillRect(630, 335, 90, 30);
g.setColor(Color.white);
g.drawString("START", 655, 355);
g.setColor(Color.black);
g.setColor(Color.green);
//System.out.println("Duration=" + duration);
Font fold = g.getFont();
Font f = fold.deriveFont(30);
g.setFont(f);
g.drawString(""+duration, 690, 130);
if (magnetronON) {
g.drawString("Cooking", 650, 220);
}
if (start) {
g.drawString("Start", 650, 190);
}
if (cookingFinished) {
g.drawString("Finished", 650, 250);
}
if (doorOpened) {
g.drawString("Door opened", 634, 280);
}
}
}
\ No newline at end of file
setBackground(Color.white);
}
public void setCookingFinished(boolean finished) {
cookingFinished = finished;
}
public void setDoorOpened(boolean opened) {
doorOpened = opened;
}
public void setStart(boolean on) {
start = on;
}
public void setMagnetronON(boolean isOn) {
magnetronON = isOn;
}
public void setDuration(int _duration) {
duration = _duration;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//System.out.println("Salut .. repainting");
// Foot
g.setColor(Color.black);
g.fillRect(100, 500, 100, 30);
g.fillRect(600, 500, 100, 30);
// Main oven
g.setColor(Color.gray);
g.fillRect(50, 50, 700, 450);
g.setColor(Color.black);
g.drawRect(50, 50, 700, 450);
g.setColor(Color.white);
g.fillRoundRect(75, 75, 500, 400, 20, 20);
// Inside of the oven
g.setColor(Color.black);
g.fillRoundRect(100, 100, 450, 350, 20, 20);
if (doorOpened) {
// Inside the oven
int dec = -5;
g.setColor(Color.white);
g.fillRoundRect(140, 140, 370, 270, 20, 20);
g.setColor(Color.black);
g.drawRect(140, 140, 370, 270);
g.drawRect(200, 190, 250, 170);
g.drawLine(140, 140, 200, 190);
g.drawLine(510, 140, 450, 190);
g.drawLine(510, 410, 450, 360);
g.drawLine(140, 410, 200, 360);
//Door
g.setColor(Color.white);
g.fillRect(95+dec, 110, 35, 330);
g.setColor(Color.black);
g.drawRect(95+dec, 110, 35, 330);
g.fillRect(95+dec, 110, 10, 330);
g.setColor(Color.gray);
g.fillRect(65+dec, 100, 25, 350);
g.fillRect(65+dec, 150, 35, 30);
g.fillRect(65+dec, 370, 35, 30);
g.setColor(Color.black);
g.drawRect(65+dec, 100, 25, 350);
} else {
}
if (magnetronON) {
g.setColor(Color.red);
g.fillRoundRect(150, 150, 350, 250, 20, 20);
}
if (doorOpened) {
// Handle of the door
} else {
// Door closed
g.setColor(Color.gray);
g.fillRect(460, 100, 50, 350);
g.setColor(Color.white);
g.drawRect(460, 100, 50, 350);
}
// Panel to print duration & information
g.setColor(Color.white);
g.fillRect(625, 100, 100, 50);
g.setColor(Color.black);
g.fillRect(630, 105, 90, 40);
g.setColor(Color.white);
g.fillRect(625, 160, 100, 150);
g.setColor(Color.black);
g.fillRect(630, 165, 90, 140);
// Panel for start button
g.setColor(Color.white);
g.fillRect(625, 330, 100, 40);
g.setColor(Color.black);
g.fillRect(630, 335, 90, 30);
g.setColor(Color.white);
g.drawString("START", 655, 355);
g.setColor(Color.black);
g.setColor(Color.green);
//System.out.println("Duration=" + duration);
Font fold = g.getFont();
Font f = fold.deriveFont(30);
g.setFont(f);
g.drawString(""+duration, 690, 130);
if (magnetronON) {
g.drawString("Cooking", 650, 220);
}
if (start) {
g.drawString("Start", 650, 190);
}
if (cookingFinished) {
g.drawString("Finished", 650, 250);
}
if (doorOpened) {
g.drawString("Door opened", 634, 280);
}
}
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment