Merge branch 'master' of repos.ctdo.de:psychose

This commit is contained in:
Stefan Kögl 2014-05-10 14:52:39 +02:00
commit 620c7bc3c3
12 changed files with 358 additions and 82 deletions

View File

@ -125,6 +125,9 @@
<item class="de.psychose.StatsDisplay" icon="" removable="true" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="0" />
</item>
<item class="de.psychose.PulseControl" icon="" removable="true" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="0" />
</item>
</group>
</component>
</project>

View File

@ -1,7 +1,7 @@
<project>
<modelVersion>4.0.0</modelVersion>
<name>Maven Default Project</name>
<version>1.0</version>
<version>1.1</version>
<artifactId>psychose</artifactId>
<groupId>psy</groupId>
@ -63,7 +63,7 @@
<configuration>
<archive>
<manifest>
<mainClass>de.psychose.MainForm</mainClass>
<mainClass>de.psychose.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>

View File

@ -9,6 +9,7 @@ import java.awt.event.ActionEvent;
* @date: 14.04.14 21:44
*/
public class ActorDisplay {
private final Timer timer;
private final static Color onColor = Color.WHITE;
private final static Color offColor = Color.RED;
private final static String offText = "no data";
@ -73,7 +74,7 @@ public class ActorDisplay {
}
public ActorDisplay() {
final Timer timer = new Timer(100, new AbstractAction() {
this.timer = new Timer(100, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
@ -127,8 +128,11 @@ public class ActorDisplay {
}
}
});
timer.setRepeats(true);
timer.start();
this.timer.setRepeats(true);
}
public void startErrorTimer() {
this.timer.start();
}
}

View File

@ -40,6 +40,22 @@ public class ChaOSCclient {
return changeChaoscSubscription(true);
}
public void sendPulse(String actor, int heartbeat, int pulse, int oxygen) {
try {
OSCMessage subscribeMessage = new OSCMessage("/" + actor + "/heartbeat");
subscribeMessage.addArgument(heartbeat);
subscribeMessage.addArgument(pulse);
subscribeMessage.addArgument(oxygen);
portOut.send(subscribeMessage);
} catch (IOException e) {
System.out.println("could not send pulse OSC Message");
e.printStackTrace();
}
}
private boolean changeChaoscSubscription(boolean subscribe) {
try {
OSCMessage subscribeMessage = new OSCMessage("/" + (subscribe ? "subscribe" : "unsubscribe"));
@ -50,8 +66,6 @@ public class ChaOSCclient {
portOut.send(subscribeMessage);
return true;
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
@ -70,6 +84,7 @@ public class ChaOSCclient {
InetAddress addr = a.nextElement();
if (addr.isSiteLocalAddress()) {
System.out.println("found address: " + addr.getHostAddress());
return addr;
}
}

View File

@ -0,0 +1,59 @@
package de.psychose;
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.SocketException;
import java.net.UnknownHostException;
/**
* @author: lucas
* @date: 25.04.14 00:23
*/
public class Main {
public static void main(String[] args) {
final boolean showErrors = args.length > 0;
try
{
//UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
UIManager.setLookAndFeel( "com.sun.java.swing.plaf.gtk.GTKLookAndFeel" );
}
catch ( Exception e )
{
e.printStackTrace();
}
try {
final ChaOSCclient chaOSCclient = new ChaOSCclient("chaosc", 7110);
final SnmpStatClient snmp = new SnmpStatClient("switch/161");
final MainForm mainForm = new MainForm(showErrors, chaOSCclient, snmp);
final JFrame frame = new JFrame("MainForm");
frame.setContentPane(mainForm.getMainPanel());
frame.setResizable(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
chaOSCclient.stopReceiver();
super.windowClosing(e);
}
});
frame.setVisible(true);
new Streamer(8888, mainForm.getMainPanel()).run();
chaOSCclient.startReceiver();
} catch (UnknownHostException | SocketException e) {
e.printStackTrace();
}
}
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="de.psychose.MainForm">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="5" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="5" left="5" bottom="5" right="5"/>
<constraints>
<xy x="20" y="20" width="720" height="576"/>
@ -16,27 +16,27 @@
<children>
<hspacer id="f912c">
<constraints>
<grid row="1" column="5" row-span="2" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="2" column="5" row-span="2" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="6a2b8">
<constraints>
<grid row="1" column="1" row-span="2" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="2" column="1" row-span="2" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="67192">
<constraints>
<grid row="1" column="3" row-span="2" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="2" column="3" row-span="2" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<nested-form id="5519b" form-file="de/psychose/StatsDisplay.form" binding="statDisplay">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
</nested-form>
<vspacer id="ad2ab">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<nested-form id="736bf" form-file="de/psychose/ActorDisplay.form" binding="actor2">
@ -54,6 +54,21 @@
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
</nested-form>
<nested-form id="5b53" form-file="de/psychose/PulseControl.form" binding="pulse1">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
</nested-form>
<nested-form id="4c934" form-file="de/psychose/PulseControl.form" binding="pulse2">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
</nested-form>
<nested-form id="f9eb3" form-file="de/psychose/PulseControl.form" binding="pulse3">
<constraints>
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
</nested-form>
</children>
</grid>
</form>

View File

@ -5,25 +5,24 @@ import com.illposed.osc.OSCMessage;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;
/**
* @author: lucas
* @date: 14.04.14 21:43
*/
public class MainForm {
private ChaOSCclient osCclient;
private SnmpStatClient snmpStatClient;
private final ChaOSCclient osCclient;
private JPanel mainPanel;
private ActorDisplay actor1;
private ActorDisplay actor2;
private ActorDisplay actor3;
private StatsDisplay statDisplay;
private PulseControl pulse1;
private PulseControl pulse2;
private PulseControl pulse3;
private int totalMessageCount = 0;
private int messagesTempCounter = 0;
@ -31,15 +30,17 @@ public class MainForm {
private long totalTraffic = 0;
private long lastTraffic = 0;
public MainForm(final ChaOSCclient chaOSCclient, final SnmpStatClient snmpStatClient) {
public JPanel getMainPanel() {
return mainPanel;
}
public MainForm(final boolean showErrors, final ChaOSCclient chaOSCclient, final SnmpStatClient snmpStatClient) {
this.osCclient = chaOSCclient;
this.snmpStatClient = snmpStatClient;
addActor("merle", "Proband 1", actor1);
addActor("uwe", "Proband 2", actor2);
addActor("bjoern", "Proband 3", actor3);
addActor("merle", "Körper 1", actor1, pulse1);
addActor("uwe", "Körper 2", actor2, pulse2);
addActor("bjoern", "Körper 3", actor3, pulse3);
osCclient.startReceiver();
final Timer timer = new Timer(1000, new AbstractAction() {
@Override
@ -55,18 +56,30 @@ public class MainForm {
final Timer snmpTimer = new Timer(5000, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
totalTraffic = snmpStatClient.getTrafficSum();
statDisplay.setTotalTraffic(String.valueOf(totalTraffic / 1024));
statDisplay.setBandwidth(String.valueOf((totalTraffic - lastTraffic) / 1024 / 5));
totalTraffic = snmpStatClient.getTrafficSum(); // in kB
statDisplay.setTotalTraffic(String.valueOf(totalTraffic));
statDisplay.setBandwidth(String.valueOf((totalTraffic - lastTraffic) / 5));
lastTraffic = totalTraffic;
}
});
snmpTimer.setRepeats(true);
if(showErrors) {
actor1.startErrorTimer();
actor2.startErrorTimer();
actor3.startErrorTimer();
snmpTimer.start();
} else {
pulse1.hide();
pulse2.hide();
pulse3.hide();
statDisplay.hide();
}
}
private void addActor(final String actor, final String label, final ActorDisplay actorDisplay) {
private void addActor(final String actor, final String label, final ActorDisplay actorDisplay, final PulseControl pulse) {
actorDisplay.setCaption(label);
osCclient.addListener("/" + actor.toLowerCase() + "/heartbeat", new OSCListener() {
@Override
@ -119,38 +132,18 @@ public class MainForm {
}
}
});
}
public static void main(String[] args) {
final String host = args.length > 0 ? args[0] : "chaosc";
final int port = args.length > 1 ? Integer.parseInt(args[1]) : 7110;
try {
final ChaOSCclient chaOSCclient = new ChaOSCclient(host, port);
final SnmpStatClient snmp = new SnmpStatClient("switch/161");
final MainForm mainForm = new MainForm(chaOSCclient, snmp);
final JFrame frame = new JFrame("MainForm");
frame.setContentPane(mainForm.mainPanel);
frame.setResizable(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.addWindowListener(new WindowAdapter() {
pulse.addObserver(new Observer() {
@Override
public void windowClosing(WindowEvent e) {
chaOSCclient.stopReceiver();
super.windowClosing(e);
public void update(Observable o, Object arg) {
if(arg instanceof PulseData) {
final PulseData data = (PulseData)arg;
osCclient.sendPulse(actor, data.getHeartbeat(), data.getPulse(), data.getOxygen());
}
}
});
frame.setVisible(true);
new Streamer(8888, mainForm.mainPanel).run();
} catch (UnknownHostException | SocketException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="de.psychose.PulseControl">
<grid id="27dc6" binding="pulsePanel" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="261" height="38"/>
</constraints>
<properties>
<background color="-16777216"/>
<foreground color="-1"/>
</properties>
<border type="none"/>
<children>
<component id="a9565" class="javax.swing.JCheckBox" binding="enableCheckBox" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<foreground color="-1"/>
<selected value="false"/>
<text value="Enable"/>
</properties>
</component>
<component id="dadb" class="javax.swing.JSpinner" binding="spinner1" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="50" height="-1"/>
<preferred-size width="50" height="-1"/>
</grid>
</constraints>
<properties>
<background color="-16711423"/>
</properties>
</component>
<hspacer id="dec01">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
</grid>
</form>

View File

@ -0,0 +1,76 @@
package de.psychose;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Observable;
import java.util.Random;
/**
* @author: lucas
* @date: 03.05.14 10:10
*/
public class PulseControl extends Observable {
private final int PULSE_WOBBLE_WIDTH = 10;
private JCheckBox enableCheckBox;
private JSpinner spinner1;
private JPanel pulsePanel;
private Timer timer;
private Random random = new Random();
private int heartbeat = 0;
public PulseControl() {
enableCheckBox.setFocusable(false);
spinner1.setFocusable(false);
spinner1.setValue(110);
timer = new Timer(100, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
heartbeat = (heartbeat+1) % 2;
final int pulseWobbleCenter = (int)spinner1.getValue();
int pulse = pulseWobbleCenter - PULSE_WOBBLE_WIDTH / 2 + random.nextInt(PULSE_WOBBLE_WIDTH);
if(pulse < 60) pulse = 60;
if(pulse > 180) pulse = 180;
final PulseData data = new PulseData(heartbeat, pulse, 95 + random.nextInt(4));
setChanged();
notifyObservers(data);
final int delay = 60000 / pulse;
timer.setDelay(delay);
}
});
timer.setRepeats(true);
enableCheckBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
System.out.println("item state changed");
JCheckBox checkBox = (JCheckBox)e.getSource();
if(checkBox.isSelected()) {
if(!timer.isRunning()) {
System.out.println("starting pulsecontrol " + this);
timer.start();
}
} else {
if(timer.isRunning()) {
System.out.println("stopping pulsecontrol " + this);
timer.stop();
}
}
}
});
}
public void hide() {
this.pulsePanel.setVisible(false);
}
}

View File

@ -0,0 +1,55 @@
package de.psychose;
/**
* @author: lucas
* @date: 03.05.14 10:58
*/
public class PulseData {
private int heartbeat;
private int pulse;
private int oxygen;
public PulseData() {
}
public PulseData(int heartbeat, int pulse, int oxygen) {
this.heartbeat = heartbeat;
this.pulse = pulse;
this.oxygen = oxygen;
}
public int getHeartbeat() {
return heartbeat;
}
public void setHeartbeat(int heartbeat) {
this.heartbeat = heartbeat;
}
public int getPulse() {
return pulse;
}
public void setPulse(int pulse) {
this.pulse = pulse;
}
public int getOxygen() {
return oxygen;
}
public void setOxygen(int oxygen) {
this.oxygen = oxygen;
}
@Override
public String toString() {
return "PulseData{" +
"heartbeat=" + heartbeat +
", pulse=" + pulse +
", oxygen=" + oxygen +
"} ";
}
}

View File

@ -23,58 +23,68 @@ import java.util.List;
*/
public class SnmpStatClient {
public static final String OID_COUNTER = "1.3.6.1.2.1.2.2.1.10";
private final String host;
private HashMap<Integer, Long> lastPorts = new HashMap<>();
private HashMap<Integer, Long> sumPorts = new HashMap<>();
private Snmp snmp;
private CommunityTarget communityTarget;
private CommunityTarget getCommunityTarget() {
private CommunityTarget getCommunityTarget(String host) {
CommunityTarget communityTarget = new CommunityTarget();
communityTarget.setCommunity(new OctetString("public"));
communityTarget.setVersion(SnmpConstants.version2c);
communityTarget.setAddress(new UdpAddress(host));
communityTarget.setTimeout(500);
communityTarget.setTimeout(100);
return communityTarget;
}
public SnmpStatClient(String host) {
this.host = host;
}
public long getTrafficSum() {
long sum = 0;
try {
final TransportMapping transportMapping = new DefaultUdpTransportMapping();
transportMapping.listen();
final Snmp snmp = new Snmp(transportMapping);
this.communityTarget = getCommunityTarget(host);
this.snmp = new Snmp(transportMapping);
} catch (IOException e) {
System.out.println("error: cannot get traffic from snmp target");
}
}
public long getTrafficSum() {
if (snmp == null || this.communityTarget == null) {
System.out.println("snmp error");
return 0;
}
long sum = 0;
try {
final TreeUtils treeUtils = new TreeUtils(snmp, new DefaultPDUFactory());
final List<TreeEvent> treeEventList = treeUtils.getSubtree(getCommunityTarget(), new OID(OID_COUNTER));
final List<TreeEvent> treeEventList = treeUtils.getSubtree(this.communityTarget, new OID(OID_COUNTER));
for (TreeEvent treeEvent : treeEventList) {
if (treeEvent.getStatus() == TreeEvent.STATUS_OK) {
for (VariableBinding binding : treeEvent.getVariableBindings()) {
int oid = binding.getOid().last();
long value = binding.getVariable().toLong();
long value = binding.getVariable().toLong() / 1024; // convert bytes down to kilobytes
long lastValue = 0;
if(lastPorts.containsKey(oid)) lastValue = lastPorts.get(oid);
if (lastPorts.containsKey(oid)) lastValue = lastPorts.get(oid);
long diff = value - lastValue;
if(diff > 0) {
if (diff > 0) {
sumPorts.put(oid, lastValue + diff);
}
}
}
}
snmp.close();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
System.out.println("error: could not resolve address from snmp target");
}
for(long port: sumPorts.values()) {
sum+=port;
for (long port : sumPorts.values()) {
sum += port;
}
return sum;

View File

@ -30,4 +30,8 @@ public class StatsDisplay {
lblBandwidth.setText(bandwidth);
}
public void hide() {
this.statPanel.setVisible(false);
}
}