@@ -5,12 +5,10 @@ | |||
package bandit.Model; | |||
import bandit.Model.Zahlengenerator; | |||
import java.util.concurrent.ExecutorService; | |||
import java.util.concurrent.Executors; | |||
import java.util.concurrent.Flow.Subscriber; | |||
import java.util.concurrent.SubmissionPublisher; | |||
import java.util.concurrent.atomic.AtomicBoolean; | |||
import java.util.logging.Logger; | |||
import bandit.util.OhmLogger; | |||
@@ -21,21 +19,15 @@ import bandit.util.OhmLogger; | |||
public class Wuerfel implements Runnable // Callable | |||
{ | |||
private static Logger lg = OhmLogger.getLogger(); //Logger sollten immer static sein da sie für alle instanzen loggs ausführen | |||
//private int wert; | |||
private WuerfelData data; | |||
private volatile boolean laufend; | |||
private ExecutorService eService; | |||
private final Object LOCK; | |||
private SubmissionPublisher<WuerfelData> wertPublisher; | |||
public Wuerfel(int id) | |||
{ | |||
data = new WuerfelData(id, 0); | |||
laufend = false; | |||
eService = null; | |||
wertPublisher = new SubmissionPublisher<>(); | |||
@@ -74,7 +66,7 @@ public class Wuerfel implements Runnable // Callable | |||
@Override | |||
public void run() | |||
{ | |||
lg.info("Generator1 run"); | |||
lg.fine("Run "+ data.getId()); | |||
while (true) | |||
{ | |||
while (!laufend) | |||
@@ -83,7 +75,7 @@ public class Wuerfel implements Runnable // Callable | |||
{ | |||
try | |||
{ | |||
lg.info("WAIT_Gen1"); | |||
lg.fine("Stop " + data.getId()); | |||
LOCK.wait(); | |||
} | |||
catch (InterruptedException ex) | |||
@@ -102,7 +94,6 @@ public class Wuerfel implements Runnable // Callable | |||
System.err.println(ex); | |||
} | |||
this.berechneWert(); | |||
// lg.info("submit " + data.getValue()); | |||
wertPublisher.submit(data); | |||
} | |||
} | |||
@@ -112,6 +103,9 @@ public class Wuerfel implements Runnable // Callable | |||
data.setValue((int) (1 + 6*Math.random())); | |||
} | |||
public synchronized int getValue(){ | |||
return data.getValue(); | |||
} | |||
} | |||
@@ -33,19 +33,13 @@ public class Zahlengenerator implements Subscriber<WuerfelData> // Callable | |||
private Wuerfel wuerfel_2; | |||
private Wuerfel wuerfel_3; | |||
public Zahlengenerator() | |||
{ | |||
subscriptions = new ArrayList<>(); | |||
wertPublisher = new SubmissionPublisher<>(); | |||
wuerfel_1 = new Wuerfel(1); | |||
wuerfel_2 = new Wuerfel(2); | |||
wuerfel_3 = new Wuerfel(3); | |||
synchronized(this){ | |||
@@ -53,9 +47,6 @@ public class Zahlengenerator implements Subscriber<WuerfelData> // Callable | |||
wuerfel_2.addWertSubscription(this); | |||
wuerfel_3.addWertSubscription(this); | |||
} | |||
} | |||
public void addWertSubscription(Subscriber<WuerfelData> subscriber) | |||
@@ -70,9 +61,6 @@ public class Zahlengenerator implements Subscriber<WuerfelData> // Callable | |||
wuerfel_1.start(); | |||
wuerfel_2.start(); | |||
wuerfel_3.start(); | |||
} | |||
public void stop() | |||
@@ -82,22 +70,24 @@ public class Zahlengenerator implements Subscriber<WuerfelData> // Callable | |||
wuerfel_1.stop(); | |||
wuerfel_2.stop(); | |||
wuerfel_3.stop(); | |||
if (wuerfel_1.getValue() == wuerfel_2.getValue() && wuerfel_2.getValue() == wuerfel_3.getValue()){ | |||
lg.warning("This user has to much luck, he/she should go to a casino"); | |||
} | |||
else | |||
lg.warning("unlucky" + wuerfel_1.getValue() + wuerfel_2.getValue() + wuerfel_3.getValue()); | |||
} | |||
@Override | |||
public void onSubscribe(Flow.Subscription subscription) { | |||
lg.info("on subscribe"); | |||
lg.config("on subscribe"); | |||
this.subscriptions.add(subscription); | |||
subscription.request(1); | |||
} | |||
@Override | |||
public void onNext(WuerfelData item) { | |||
lg.info("on Next"); | |||
lg.config("on Next"); | |||
this.wertPublisher.submit(item); | |||
this.subscriptions.get(item.getId() - 1).request(1); | |||
} |
@@ -39,12 +39,12 @@ public class StartStopController implements ActionListener | |||
{ | |||
if (evt.getSource() == view.getBtnStart()) | |||
{ | |||
lg.info("Start"); | |||
lg.info("Start gedruekt"); | |||
model.start(); | |||
} | |||
else | |||
{ | |||
lg.info("Stop"); | |||
lg.info("Stop gedrueckt"); | |||
model.stop(); | |||
} | |||
} |
@@ -8,9 +8,11 @@ package bandit.Schnittstelle; | |||
import bandit.Model.WuerfelData; | |||
import bandit.Model.Zahlengenerator; | |||
import bandit.View.ZahlenView; | |||
import java.util.ArrayList; | |||
import java.util.concurrent.Flow; | |||
import java.util.concurrent.Flow.Subscriber; | |||
import java.util.concurrent.Flow.Subscription; | |||
import javax.swing.Icon; | |||
/** | |||
* |
@@ -69,49 +69,50 @@ public class ZahlenView extends javax.swing.JFrame | |||
* regenerated by the Form Editor. | |||
*/ | |||
@SuppressWarnings("unchecked") | |||
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents | |||
private void initComponents() { | |||
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents | |||
private void initComponents() | |||
{ | |||
jPanel1 = new javax.swing.JPanel(); | |||
btnStart = new javax.swing.JButton(); | |||
btnStop = new javax.swing.JButton(); | |||
jPanel2 = new javax.swing.JPanel(); | |||
lblZahl1 = new javax.swing.JLabel(); | |||
lblZahl2 = new javax.swing.JLabel(); | |||
lblZahl3 = new javax.swing.JLabel(); | |||
jPanel1 = new javax.swing.JPanel(); | |||
btnStart = new javax.swing.JButton(); | |||
btnStop = new javax.swing.JButton(); | |||
jPanel2 = new javax.swing.JPanel(); | |||
lblZahl1 = new javax.swing.JLabel(); | |||
lblZahl2 = new javax.swing.JLabel(); | |||
lblZahl3 = new javax.swing.JLabel(); | |||
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |||
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |||
btnStart.setText("Start"); | |||
jPanel1.add(btnStart); | |||
btnStart.setText("Start"); | |||
jPanel1.add(btnStart); | |||
btnStop.setText("Stop"); | |||
jPanel1.add(btnStop); | |||
btnStop.setText("Stop"); | |||
jPanel1.add(btnStop); | |||
getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END); | |||
getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END); | |||
lblZahl1.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N | |||
lblZahl1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); | |||
lblZahl1.setText("?"); | |||
lblZahl1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); | |||
jPanel2.add(lblZahl1); | |||
lblZahl1.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N | |||
lblZahl1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); | |||
lblZahl1.setText("?"); | |||
lblZahl1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); | |||
jPanel2.add(lblZahl1); | |||
lblZahl2.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N | |||
lblZahl2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); | |||
lblZahl2.setText("?"); | |||
lblZahl2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); | |||
jPanel2.add(lblZahl2); | |||
lblZahl2.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N | |||
lblZahl2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); | |||
lblZahl2.setText("?"); | |||
lblZahl2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); | |||
jPanel2.add(lblZahl2); | |||
lblZahl3.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N | |||
lblZahl3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); | |||
lblZahl3.setText("?"); | |||
lblZahl3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); | |||
jPanel2.add(lblZahl3); | |||
lblZahl3.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N | |||
lblZahl3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); | |||
lblZahl3.setText("?"); | |||
lblZahl3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); | |||
jPanel2.add(lblZahl3); | |||
getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER); | |||
getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER); | |||
pack(); | |||
}// </editor-fold>//GEN-END:initComponents | |||
pack(); | |||
}// </editor-fold>//GEN-END:initComponents | |||
/** | |||
* @param args the command line arguments | |||
@@ -162,13 +163,13 @@ public class ZahlenView extends javax.swing.JFrame | |||
}); | |||
} | |||
// Variables declaration - do not modify//GEN-BEGIN:variables | |||
private javax.swing.JButton btnStart; | |||
private javax.swing.JButton btnStop; | |||
private javax.swing.JPanel jPanel1; | |||
private javax.swing.JPanel jPanel2; | |||
private javax.swing.JLabel lblZahl1; | |||
private javax.swing.JLabel lblZahl2; | |||
private javax.swing.JLabel lblZahl3; | |||
// End of variables declaration//GEN-END:variables | |||
// Variables declaration - do not modify//GEN-BEGIN:variables | |||
private javax.swing.JButton btnStart; | |||
private javax.swing.JButton btnStop; | |||
private javax.swing.JPanel jPanel1; | |||
private javax.swing.JPanel jPanel2; | |||
private javax.swing.JLabel lblZahl1; | |||
private javax.swing.JLabel lblZahl2; | |||
private javax.swing.JLabel lblZahl3; | |||
// End of variables declaration//GEN-END:variables | |||
} |
@@ -8,6 +8,9 @@ package bandit.util; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.time.LocalDateTime; | |||
import java.util.Map; | |||
import java.util.Properties; | |||
import java.util.Set; | |||
//import java.util.Properties; | |||
import java.util.logging.*; | |||
@@ -57,20 +60,21 @@ public class OhmLogger | |||
lg.getHandlers(); | |||
lg.addHandler(ch); | |||
lg.setLevel(Level.parse(System.getProperty("log.level", "WARNING"))); | |||
Properties properties = new Properties(); | |||
properties.load(configFile); | |||
java.util.logging.Level classLogLevel = java.util.logging.Level.parse(properties.getProperty("log.level")); | |||
lg.setLevel(classLogLevel); | |||
// Lese spezifische Level für einzelne Klassen aus der Properties-Datei | |||
// Properties properties = new Properties(); | |||
// properties.load(configFile); | |||
// | |||
// for (String key : properties.stringPropertyNames()) { | |||
// if (key.startsWith("log.level.")) { | |||
// String className = key.substring("log.level.".length()); | |||
// Logger classLogger = Logger.getLogger(className); | |||
// java.util.logging.Level classLogLevel = java.util.logging.Level.parse(properties.getProperty(key)); | |||
// classLogger.setLevel(classLogLevel); | |||
// } | |||
// } | |||
//// Lese spezifische Level für einzelne Klassen aus der Properties-Datei | |||
// for (String key : properties.stringPropertyNames()) { | |||
// if (key.startsWith("log.level.")) { | |||
// String className = key.substring("log.level.".length()); | |||
// Logger classLogger = Logger.getLogger(className); | |||
// java.util.logging.Level classLogLevel = java.util.logging.Level.parse(properties.getProperty(key)); | |||
// classLogger.setLevel(classLogLevel); | |||
// } | |||
// } | |||
} else { | |||
System.err.println("Unable to find config.properties file. OhmLogger will use default settings."); |
@@ -0,0 +1,4 @@ | |||
log.level=INFO | |||
# Beispiel f\u00fcr verschiedene Level f\u00fcr unterschiedliche Pakete | |||
# log.level.bandit.Zahlengenerator=FINE | |||
# log.level.bandit.Model.Wuerfel=WARNING |