package bandit.Model; | package bandit.Model; | ||||
import bandit.Model.Zahlengenerator; | |||||
import java.util.concurrent.ExecutorService; | import java.util.concurrent.ExecutorService; | ||||
import java.util.concurrent.Executors; | import java.util.concurrent.Executors; | ||||
import java.util.concurrent.Flow.Subscriber; | import java.util.concurrent.Flow.Subscriber; | ||||
import java.util.concurrent.SubmissionPublisher; | import java.util.concurrent.SubmissionPublisher; | ||||
import java.util.concurrent.atomic.AtomicBoolean; | |||||
import java.util.logging.Logger; | import java.util.logging.Logger; | ||||
import bandit.util.OhmLogger; | import bandit.util.OhmLogger; | ||||
public class Wuerfel implements Runnable // Callable | 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 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 WuerfelData data; | ||||
private volatile boolean laufend; | private volatile boolean laufend; | ||||
private ExecutorService eService; | private ExecutorService eService; | ||||
private final Object LOCK; | private final Object LOCK; | ||||
private SubmissionPublisher<WuerfelData> wertPublisher; | private SubmissionPublisher<WuerfelData> wertPublisher; | ||||
public Wuerfel(int id) | public Wuerfel(int id) | ||||
{ | { | ||||
data = new WuerfelData(id, 0); | data = new WuerfelData(id, 0); | ||||
laufend = false; | laufend = false; | ||||
eService = null; | eService = null; | ||||
wertPublisher = new SubmissionPublisher<>(); | wertPublisher = new SubmissionPublisher<>(); | ||||
@Override | @Override | ||||
public void run() | public void run() | ||||
{ | { | ||||
lg.info("Generator1 run"); | |||||
lg.fine("Run "+ data.getId()); | |||||
while (true) | while (true) | ||||
{ | { | ||||
while (!laufend) | while (!laufend) | ||||
{ | { | ||||
try | try | ||||
{ | { | ||||
lg.info("WAIT_Gen1"); | |||||
lg.fine("Stop " + data.getId()); | |||||
LOCK.wait(); | LOCK.wait(); | ||||
} | } | ||||
catch (InterruptedException ex) | catch (InterruptedException ex) | ||||
System.err.println(ex); | System.err.println(ex); | ||||
} | } | ||||
this.berechneWert(); | this.berechneWert(); | ||||
// lg.info("submit " + data.getValue()); | |||||
wertPublisher.submit(data); | wertPublisher.submit(data); | ||||
} | } | ||||
} | } | ||||
data.setValue((int) (1 + 6*Math.random())); | data.setValue((int) (1 + 6*Math.random())); | ||||
} | } | ||||
public synchronized int getValue(){ | |||||
return data.getValue(); | |||||
} | |||||
} | } | ||||
private Wuerfel wuerfel_2; | private Wuerfel wuerfel_2; | ||||
private Wuerfel wuerfel_3; | private Wuerfel wuerfel_3; | ||||
public Zahlengenerator() | public Zahlengenerator() | ||||
{ | { | ||||
subscriptions = new ArrayList<>(); | subscriptions = new ArrayList<>(); | ||||
wertPublisher = new SubmissionPublisher<>(); | wertPublisher = new SubmissionPublisher<>(); | ||||
wuerfel_1 = new Wuerfel(1); | wuerfel_1 = new Wuerfel(1); | ||||
wuerfel_2 = new Wuerfel(2); | wuerfel_2 = new Wuerfel(2); | ||||
wuerfel_3 = new Wuerfel(3); | wuerfel_3 = new Wuerfel(3); | ||||
synchronized(this){ | synchronized(this){ | ||||
wuerfel_2.addWertSubscription(this); | wuerfel_2.addWertSubscription(this); | ||||
wuerfel_3.addWertSubscription(this); | wuerfel_3.addWertSubscription(this); | ||||
} | } | ||||
} | } | ||||
public void addWertSubscription(Subscriber<WuerfelData> subscriber) | public void addWertSubscription(Subscriber<WuerfelData> subscriber) | ||||
wuerfel_1.start(); | wuerfel_1.start(); | ||||
wuerfel_2.start(); | wuerfel_2.start(); | ||||
wuerfel_3.start(); | wuerfel_3.start(); | ||||
} | } | ||||
public void stop() | public void stop() | ||||
wuerfel_1.stop(); | wuerfel_1.stop(); | ||||
wuerfel_2.stop(); | wuerfel_2.stop(); | ||||
wuerfel_3.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 | @Override | ||||
public void onSubscribe(Flow.Subscription subscription) { | public void onSubscribe(Flow.Subscription subscription) { | ||||
lg.info("on subscribe"); | |||||
lg.config("on subscribe"); | |||||
this.subscriptions.add(subscription); | this.subscriptions.add(subscription); | ||||
subscription.request(1); | subscription.request(1); | ||||
} | } | ||||
@Override | @Override | ||||
public void onNext(WuerfelData item) { | public void onNext(WuerfelData item) { | ||||
lg.info("on Next"); | |||||
lg.config("on Next"); | |||||
this.wertPublisher.submit(item); | this.wertPublisher.submit(item); | ||||
this.subscriptions.get(item.getId() - 1).request(1); | this.subscriptions.get(item.getId() - 1).request(1); | ||||
} | } |
{ | { | ||||
if (evt.getSource() == view.getBtnStart()) | if (evt.getSource() == view.getBtnStart()) | ||||
{ | { | ||||
lg.info("Start"); | |||||
lg.info("Start gedruekt"); | |||||
model.start(); | model.start(); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
lg.info("Stop"); | |||||
lg.info("Stop gedrueckt"); | |||||
model.stop(); | model.stop(); | ||||
} | } | ||||
} | } |
import bandit.Model.WuerfelData; | import bandit.Model.WuerfelData; | ||||
import bandit.Model.Zahlengenerator; | import bandit.Model.Zahlengenerator; | ||||
import bandit.View.ZahlenView; | import bandit.View.ZahlenView; | ||||
import java.util.ArrayList; | |||||
import java.util.concurrent.Flow; | import java.util.concurrent.Flow; | ||||
import java.util.concurrent.Flow.Subscriber; | import java.util.concurrent.Flow.Subscriber; | ||||
import java.util.concurrent.Flow.Subscription; | import java.util.concurrent.Flow.Subscription; | ||||
import javax.swing.Icon; | |||||
/** | /** | ||||
* | * |
* regenerated by the Form Editor. | * regenerated by the Form Editor. | ||||
*/ | */ | ||||
@SuppressWarnings("unchecked") | @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 | * @param args the command line arguments | ||||
}); | }); | ||||
} | } | ||||
// 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 | |||||
} | } |
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStream; | import java.io.InputStream; | ||||
import java.time.LocalDateTime; | import java.time.LocalDateTime; | ||||
import java.util.Map; | |||||
import java.util.Properties; | |||||
import java.util.Set; | |||||
//import java.util.Properties; | //import java.util.Properties; | ||||
import java.util.logging.*; | import java.util.logging.*; | ||||
lg.getHandlers(); | lg.getHandlers(); | ||||
lg.addHandler(ch); | 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 { | } else { | ||||
System.err.println("Unable to find config.properties file. OhmLogger will use default settings."); | System.err.println("Unable to find config.properties file. OhmLogger will use default settings."); |
log.level=INFO | |||||
# Beispiel f\u00fcr verschiedene Level f\u00fcr unterschiedliche Pakete | |||||
# log.level.bandit.Zahlengenerator=FINE | |||||
# log.level.bandit.Model.Wuerfel=WARNING |