diff --git a/.idea/libraries/jerklib.xml b/.idea/libraries/jerklib.xml new file mode 100644 index 0000000..2090b94 --- /dev/null +++ b/.idea/libraries/jerklib.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/crashteststeuerung.iml b/crashteststeuerung.iml index 2ab6dea..5b4384e 100644 --- a/crashteststeuerung.iml +++ b/crashteststeuerung.iml @@ -9,6 +9,7 @@ + diff --git a/libs/jerklib.jar b/libs/jerklib.jar new file mode 100644 index 0000000..ac29b29 Binary files /dev/null and b/libs/jerklib.jar differ diff --git a/src/de/ctdo/crashtest/BuntiClient.java b/src/de/ctdo/crashtest/BuntiClient.java index 33aaf42..e0b75c5 100644 --- a/src/de/ctdo/crashtest/BuntiClient.java +++ b/src/de/ctdo/crashtest/BuntiClient.java @@ -25,6 +25,7 @@ public class BuntiClient { } public void setPar56(int id, int red, int green, int blue) { + if(true) return; try { HttpPost post = new HttpPost(baseAddress + "/control/devices"); post.addHeader("Content-Type", "application/json"); @@ -53,6 +54,8 @@ public class BuntiClient { public void setLampel(boolean red, boolean yellow, boolean green) { + if(true) return; + int value = 0; if( green ) value |= 0x01; diff --git a/src/de/ctdo/crashtest/Communication.java b/src/de/ctdo/crashtest/Communication.java new file mode 100644 index 0000000..5e503a8 --- /dev/null +++ b/src/de/ctdo/crashtest/Communication.java @@ -0,0 +1,73 @@ +package de.ctdo.crashtest; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +public class Communication implements Runnable { + private final static int LISTEN_PORT = 2342; + private Boolean isStopped = false; + private ServerSocket serverSocket; + private Thread runningThread; + + private ExecutorService threadPool = Executors.newFixedThreadPool(10); + + private GuiControl control; + + public Communication(GuiControl control) { + this.control = control; + } + + + private synchronized boolean isStopped() { + return this.isStopped; + } + + @Override + public void run() { + + synchronized(this){ + this.runningThread = Thread.currentThread(); + } + + openServerSocket(); + + while(!isStopped()){ + Socket clientSocket = null; + try { + clientSocket = this.serverSocket.accept(); + } catch (IOException e) { + if(isStopped()) { + System.out.println("Server Stopped.") ; + return; + } + throw new RuntimeException("Error accepting client connection", e); + } + this.threadPool.execute(new CommunicationRunner(clientSocket, this.control)); + } + + this.threadPool.shutdown(); + System.out.println("Server Stopped.") ; + + } + + + public synchronized void stop() { + this.isStopped = true; + try { + this.serverSocket.close(); + } catch (IOException e) { + throw new RuntimeException("Error closing server", e); + } + } + + private void openServerSocket() { + try { + this.serverSocket = new ServerSocket(LISTEN_PORT); + } catch (IOException e) { + throw new RuntimeException("Cannot open port 8080", e); + } + } +} diff --git a/src/de/ctdo/crashtest/CommunicationRunner.java b/src/de/ctdo/crashtest/CommunicationRunner.java new file mode 100644 index 0000000..e0afb61 --- /dev/null +++ b/src/de/ctdo/crashtest/CommunicationRunner.java @@ -0,0 +1,76 @@ +package de.ctdo.crashtest; + +import java.io.*; +import java.net.Socket; +import java.net.SocketTimeoutException; + +public class CommunicationRunner implements Runnable { + private Socket clientSocket = null; + private GuiControl control; + + public CommunicationRunner(Socket clientSocket, GuiControl control) { + this.clientSocket = clientSocket; + this.control = control; + } + + @Override + public void run() { + try { + clientSocket.setSoTimeout(5000); + InputStream input = clientSocket.getInputStream(); + OutputStream output = clientSocket.getOutputStream(); + long time = System.currentTimeMillis(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(input)); + + String line = reader.readLine(); + + System.out.println("got line: " + line); + + runCommand(line); + + output.close(); + input.close(); + System.out.println("Request processed: " + time + clientSocket.getRemoteSocketAddress()); + } catch (SocketTimeoutException e) { + System.out.println("Socket Timeout, closing " + clientSocket.getRemoteSocketAddress()); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + clientSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + private void runCommand(String line) { + + // must contain a ":" + // then split and switch on the command + int pos = line.indexOf(":"); + if(pos > 0) { + String command = line.substring(0, pos).toLowerCase().trim(); + String parameter = line.substring(pos+1, line.length()).trim(); + + if(command.equals("wall")) { + control.setWall(parameter); + } else if(command.equals("timerstart")) { + control.startTimer(Integer.parseInt(parameter)); + } else if(command.equals("timerstop")) { + control.stopTimer(); + } else if(command.equals("timerpause")) { + control.pauseTimer(Boolean.parseBoolean(parameter)); + } else if(command.equals("setextra")) { + control.setExtra(parameter); + } else if(command == "reset") { + control.resetGame(); + } + + + } + + } +} diff --git a/src/de/ctdo/crashtest/GuiControl.java b/src/de/ctdo/crashtest/GuiControl.java new file mode 100644 index 0000000..1a8eb50 --- /dev/null +++ b/src/de/ctdo/crashtest/GuiControl.java @@ -0,0 +1,18 @@ +package de.ctdo.crashtest; + +public interface GuiControl { + + void startTimer(int seconds); + void stopTimer(); // and hide + void pauseTimer(Boolean pause); + + void setWall(String message); + void setExtra(String text); + + int getTimerSeconds(); + int getTimerSecondsLast(); + Boolean getTimerRunning(); + + void resetGame(); + +} diff --git a/src/de/ctdo/crashtest/LampelClient.java b/src/de/ctdo/crashtest/LampelClient.java index 941aa8c..f055d32 100644 --- a/src/de/ctdo/crashtest/LampelClient.java +++ b/src/de/ctdo/crashtest/LampelClient.java @@ -16,7 +16,7 @@ public class LampelClient { try { Socket client = new Socket(LAMPEL_HOST, ECMD_TCP_PORT); - client.setSoTimeout(2000); + client.setSoTimeout(800); DataOutputStream outToServer = new DataOutputStream(client.getOutputStream()); diff --git a/src/de/ctdo/crashtest/Steuerung.java b/src/de/ctdo/crashtest/Steuerung.java index b2c3aa3..67c9517 100644 --- a/src/de/ctdo/crashtest/Steuerung.java +++ b/src/de/ctdo/crashtest/Steuerung.java @@ -1,9 +1,6 @@ package de.ctdo.crashtest; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.CharBuffer; + public class Steuerung { diff --git a/src/de/ctdo/crashtest/SteuerungFrame.java b/src/de/ctdo/crashtest/SteuerungFrame.java index 452ddec..e67932a 100644 --- a/src/de/ctdo/crashtest/SteuerungFrame.java +++ b/src/de/ctdo/crashtest/SteuerungFrame.java @@ -1,62 +1,73 @@ package de.ctdo.crashtest; -import org.bff.javampd.MPD; -import org.bff.javampd.MPDPlayer; +import jerklib.*; +import jerklib.events.IRCEvent; +import jerklib.events.JoinCompleteEvent; +import jerklib.events.listeners.IRCEventListener; +import org.bff.javampd.*; import org.bff.javampd.exception.MPDConnectionException; +import org.bff.javampd.exception.MPDDatabaseException; +import org.bff.javampd.exception.MPDPlayerException; import org.bff.javampd.exception.MPDResponseException; +import org.bff.javampd.objects.MPDSong; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Collection; import java.util.Random; -public class SteuerungFrame extends JFrame implements StateChangeListener { - private JPanel pnlRoot; - private JLabel lblState; +public class SteuerungFrame extends JFrame implements StateChangeListener, GuiControl, IRCEventListener { + private JTextArea textWall; + private JLabel countDown; + private JLabel extraField; + private JPanel lowerPanel; private char lastKey = ' '; private MPD mpd; private MPDPlayer player; + private int timerSeconds = 0; + private int timerSecondsLast = 0; + private int timeSpentTableGame = 0; + private int timeSpentRokets = 0; + private Timer timer; + private Statemachine machine = new Statemachine(); + private BuntiClient bunti = new BuntiClient("bunti.ctdo.de", 8080); + private Communication server; + private ConnectionManager irc; + private Session ircsession; - Statemachine machine = new Statemachine(); - BuntiClient bunti = new BuntiClient("bunti.ctdo.de", 8080); public SteuerungFrame() { - //setType(Type.UTILITY); - setBackground(Color.black); - setBounds(200,200, 400, 200); - - machine.addStateChangedListener(this); - initGui(); - setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); + setBounds(200,200, 400, 200); + //server = new Communication(this); + + irc = new ConnectionManager(new ProfileImpl("crashtest","crashtest", "crashtest2", "crashtest3")); + ircsession = irc.requestConnection("irc.chaostreff-dortmund.de"); + ircsession.addIRCEventListener(this); + + //initTimer(); + + machine.addStateChangedListener(this); machine.reset(); - try { - mpd = new MPD("dampfradio.raum.chaostreff-dortmund.de", 6600); - player = mpd.getMPDPlayer(); + //initMPD(); - } catch (UnknownHostException e) { - e.printStackTrace(); - return; - } catch (MPDConnectionException e) { - e.printStackTrace(); - return; - } - - addKeyListener(new KeyAdapter() { + this.addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent e) { lastKey = e.getKeyChar(); - if (lastKey == KeyEvent.VK_F) { + /*if (lastKey == KeyEvent.VK_F) { Random r = new Random(); setLEDs(r.nextInt(255), r.nextInt(255),r.nextInt(255)); - } + } */ if (lastKey == KeyEvent.VK_1) { machine.reset(); @@ -67,12 +78,20 @@ public class SteuerungFrame extends JFrame implements StateChangeListener { updateGui(); } }); - - addWindowStateListener(new WindowAdapter() { + + this.addWindowStateListener(new WindowAdapter() { + @Override + public void windowClosed(WindowEvent e) { + + System.out.print("oihdf"); + } + @Override public void windowClosing(WindowEvent e) { try { - if(mpd != null) mpd.close(); + if (mpd != null) mpd.close(); + + if (server != null) server.stop(); } catch (MPDConnectionException e1) { e1.printStackTrace(); } catch (MPDResponseException e1) { @@ -80,20 +99,70 @@ public class SteuerungFrame extends JFrame implements StateChangeListener { } } }); + + //new Thread(server).start(); + } + + private void initMPD() { + try { + mpd = new MPD("dampfradio.raum.chaostreff-dortmund.de", 6600); + player = mpd.getMPDPlayer(); + + MPDDatabase database = mpd.getMPDDatabase(); + Collection bla = database.findTitle(""); + + } catch (UnknownHostException e) { + e.printStackTrace(); + return; + } catch (MPDConnectionException e) { + e.printStackTrace(); + return; + } catch (MPDDatabaseException e) { + e.printStackTrace(); + } + } + + private void initTimer() { + timer = new Timer(100, new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + timerSecondsLast--; + setTimerText(); + + if(timerSecondsLast <= 0) { + timerSecondsLast = 0; + timer.stop(); + } + } + }); + } + + private void setTimerText() { + int mins = timerSecondsLast / 600; + int secs = timerSecondsLast % 600 / 10 ; + int tsecs = timerSecondsLast % 9; + countDown.setText(" " + mins + ":" + secs + "." + tsecs); } @Override public void stateChanged(Statemachine.state newState) { updateGui(); + + if(ircsession != null) { + for(Channel chan: ircsession.getChannels()) { + chan.say("New State: " + newState); + } + } switch (newState) { case IDLE: - setLEDs(0x40,0,0xff); + //setLEDs(0x40,0,0xff); + setLEDs(0,0,0); bunti.setLampel(false,false,false); break; case ENTERED_ROOM: bunti.setLampel(false,false,false); - setLEDs(255,0,100); + setLEDs(20,0,100); // start von Mo Do - Eins, Zwei Polizei @@ -135,8 +204,8 @@ public class SteuerungFrame extends JFrame implements StateChangeListener { setLEDs(0, 255, 0); break; case ROKET_DONE: - - + bunti.setLampel(true,true,true); + setLEDs(255, 196, 0); break; } @@ -151,21 +220,140 @@ public class SteuerungFrame extends JFrame implements StateChangeListener { } private void initGui() { - Container pane = getContentPane(); + textWall = new JTextArea(); + countDown = new JLabel(); + extraField = new JLabel(); + lowerPanel = new JPanel(); - pnlRoot = new JPanel(new FlowLayout()); - lblState = new JLabel("", JLabel.LEFT); - pnlRoot.add(lblState); - pane.add(pnlRoot); + Container container = getContentPane(); + container.setLayout(new BorderLayout()); + container.add(textWall, BorderLayout.NORTH); + container.add(lowerPanel, BorderLayout.SOUTH); + + lowerPanel.setLayout(new BorderLayout()); + lowerPanel.add(countDown, BorderLayout.WEST); + lowerPanel.add(extraField, BorderLayout.EAST); + + Font wallFont = new Font("Arcade Interlaced", Font.PLAIN, 66); + Font lowerFont = new Font("Arcade Interlaced", Font.PLAIN, 80); + Color fontColor = new Color(0x00, 190, 100); + + textWall.setFont(wallFont); + textWall.setForeground(fontColor); + textWall.setBackground(Color.BLACK); + textWall.setText("follow the white rabbit..."); + textWall.setLineWrap(true); + textWall.setWrapStyleWord(true); + textWall.setFocusable(false); + + countDown.setFont(lowerFont); + countDown.setHorizontalAlignment(SwingConstants.LEFT); + countDown.setForeground(fontColor); + //countDown.setBackground(Color.BLACK); + + extraField.setFont(lowerFont); + extraField.setHorizontalAlignment(SwingConstants.RIGHT); + extraField.setForeground(fontColor); + //extraField.setBackground(Color.BLACK); + + lowerPanel.setBackground(Color.BLACK); + container.setBackground(Color.BLACK); + + + + + countDown.setText(" 15:00.0"); + extraField.setText("B "); + updateGui(); } private void updateGui() { - lblState.setText("LastKey: " + lastKey + "
CurrentState: " + + /*lblState.setText("LastKey: " + lastKey + "
CurrentState: " + machine.getCurrentState() + "
ChangeCounter: " + - machine.getStateChangeCounter() + ""); + machine.getStateChangeCounter() + ""); */ + + } + + @Override + public void startTimer(int seconds) { + timerSeconds = seconds*10; + timerSecondsLast = seconds*10; + timer.start(); + } + + @Override + public void stopTimer() { + timer.stop(); + timerSeconds = 0; + timerSecondsLast = 0; + } + + @Override + public void pauseTimer(Boolean pause) { + if(pause) { + timer.stop(); + } else { + timer.start(); + } + } + + @Override + public void setWall(final String message) { + Runnable runnable = new Runnable() { + @Override + public void run() { + textWall.setText(message); + } + }; + + SwingUtilities.invokeLater(runnable); + } + + @Override + public void setExtra(final String text) { + Runnable runnable = new Runnable() { + @Override + public void run() { + extraField.setText(text + " " + lastKey + " "); + } + }; + + SwingUtilities.invokeLater(runnable); + } + + @Override + public int getTimerSeconds() { + return timerSeconds; + } + + @Override + public int getTimerSecondsLast() { + return timerSecondsLast; + } + + @Override + public Boolean getTimerRunning() { + return null; + } + + @Override + public void resetGame() { + + } + + @Override + public void recieveEvent(IRCEvent e) { + + if (e.getType() == IRCEvent.Type.CONNECT_COMPLETE) { + e.getSession().joinChannel("#crashtest"); + + } else if (e.getType() == IRCEvent.Type.JOIN_COMPLETE) { + JoinCompleteEvent jce = (JoinCompleteEvent) e; + jce.getChannel().say("hello master. what's your order?"); + } } } diff --git a/src/de/ctdo/crashtest/TheGame.java b/src/de/ctdo/crashtest/TheGame.java new file mode 100644 index 0000000..dac5ff7 --- /dev/null +++ b/src/de/ctdo/crashtest/TheGame.java @@ -0,0 +1,16 @@ +package de.ctdo.crashtest; + +public class TheGame { + + // hier müssen alle Verbindungen nach Aussen rein + // MPD + // Lampel + // Bunti + // IRC + + // Die GUI muss als Interface bekannt sein + // fuer Updates der GUI und den Keypress Event + + // Logik des Spiels muss hier programmiert werden + +}