From 1c2b26268043da54eff2bd106188d8feab563d3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Mon, 27 Oct 2014 22:04:11 +0100 Subject: [PATCH] added Snmp client thread (untested) --- .../.idea/copyright/profiles_settings.xml | 4 +- healthdisplay/.idea/misc.xml | 2 +- .../src/main/java/de/psychose/Main.java | 1 + .../src/main/java/de/psychose/MainForm.java | 4 ++ .../main/java/de/psychose/SnmpStatClient.java | 46 +++++++++++++++++-- 5 files changed, 48 insertions(+), 9 deletions(-) diff --git a/healthdisplay/.idea/copyright/profiles_settings.xml b/healthdisplay/.idea/copyright/profiles_settings.xml index 3572571..e7bedf3 100644 --- a/healthdisplay/.idea/copyright/profiles_settings.xml +++ b/healthdisplay/.idea/copyright/profiles_settings.xml @@ -1,5 +1,3 @@ - - - + \ No newline at end of file diff --git a/healthdisplay/.idea/misc.xml b/healthdisplay/.idea/misc.xml index 8a80acb..4c4c1de 100644 --- a/healthdisplay/.idea/misc.xml +++ b/healthdisplay/.idea/misc.xml @@ -10,7 +10,7 @@ - + diff --git a/healthdisplay/src/main/java/de/psychose/Main.java b/healthdisplay/src/main/java/de/psychose/Main.java index 0a14f32..b5a43b6 100644 --- a/healthdisplay/src/main/java/de/psychose/Main.java +++ b/healthdisplay/src/main/java/de/psychose/Main.java @@ -41,6 +41,7 @@ public class Main { @Override public void windowClosing(WindowEvent e) { chaOSCclient.stopReceiver(); + snmp.stopRunning(); super.windowClosing(e); } }); diff --git a/healthdisplay/src/main/java/de/psychose/MainForm.java b/healthdisplay/src/main/java/de/psychose/MainForm.java index 24bc772..6f46532 100644 --- a/healthdisplay/src/main/java/de/psychose/MainForm.java +++ b/healthdisplay/src/main/java/de/psychose/MainForm.java @@ -63,6 +63,7 @@ public class MainForm { } }); snmpTimer.setRepeats(true); + snmpStatClient.start(); if(showErrors) { @@ -76,6 +77,8 @@ public class MainForm { pulse3.hide(); statDisplay.hide(); } + + } @@ -146,4 +149,5 @@ public class MainForm { + } diff --git a/healthdisplay/src/main/java/de/psychose/SnmpStatClient.java b/healthdisplay/src/main/java/de/psychose/SnmpStatClient.java index ee84098..a76ba09 100644 --- a/healthdisplay/src/main/java/de/psychose/SnmpStatClient.java +++ b/healthdisplay/src/main/java/de/psychose/SnmpStatClient.java @@ -21,19 +21,22 @@ import java.util.List; * @author: lucas * @date: 18.04.14 12:10 */ -public class SnmpStatClient { +public class SnmpStatClient extends Thread { + private final int napTime = 5000; public static final String OID_COUNTER = "1.3.6.1.2.1.2.2.1.10"; private HashMap lastPorts = new HashMap<>(); private HashMap sumPorts = new HashMap<>(); private Snmp snmp; private CommunityTarget communityTarget; + private long currentTrafficSum = 0; + private Boolean doRun = true; 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(100); + communityTarget.setTimeout(300); return communityTarget; } @@ -46,15 +49,44 @@ public class SnmpStatClient { this.snmp = new Snmp(transportMapping); } catch (IOException e) { + e.printStackTrace(); System.out.println("error: cannot get traffic from snmp target"); } } - public long getTrafficSum() { + public void stopRunning() { + doRun = false; + } + @Override + public void run() { if (snmp == null || this.communityTarget == null) { System.out.println("snmp error"); - return 0; + doRun = false; + } + + while (doRun && !Thread.interrupted()) { + long sleepTill = System.currentTimeMillis() + napTime; + + getSNMPValues(); + + try { + long remainingTime = sleepTill - System.currentTimeMillis(); + if (remainingTime > 0) + Thread.sleep(remainingTime); + } catch (InterruptedException e) { + return; + } + + } + + } + + private void getSNMPValues() { + if (snmp == null || this.communityTarget == null) { + System.out.println("snmp error"); + doRun = false; + return; } long sum = 0; @@ -87,7 +119,11 @@ public class SnmpStatClient { sum += port; } - return sum; + currentTrafficSum = sum; + } + + public long getTrafficSum() { + return currentTrafficSum; } }