Files
Aufgabe7_bandit/src/bandit/Model/Zahlengenerator.java
T

112 lines
3.0 KiB
Java

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package bandit.Model;
//import bandit.Model.Generator.Generator2;
//import bandit.Model.Generator.Generator3;
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;
import java.util.ArrayList;
import java.util.concurrent.Flow;
/**
*
* @author le
*/
public class Zahlengenerator implements Subscriber<WuerfelData> // Callable
{
private static Logger lg = OhmLogger.getLogger(); //Logger sollten immer static sein da sie für alle instanzen loggs ausführen
private ArrayList<Flow.Subscription> subscriptions;
//private Flow.Subscription subscription;
private SubmissionPublisher<WuerfelData> wertPublisher;
private Wuerfel wuerfel_1;
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){
wuerfel_1.addWertSubscription(this);
wuerfel_2.addWertSubscription(this);
wuerfel_3.addWertSubscription(this);
}
}
public void addWertSubscription(Subscriber<WuerfelData> subscriber)
{
wertPublisher.subscribe(subscriber);
}
public void start()
{
lg.info("Generator gestartet");
wuerfel_1.start();
wuerfel_2.start();
wuerfel_3.start();
}
public void stop()
{
lg.info("Generator gestopt");
wuerfel_1.stop();
wuerfel_2.stop();
wuerfel_3.stop();
int w1_value = wuerfel_1.getValue();
int w2_value = wuerfel_2.getValue();
int w3_value = wuerfel_3.getValue();
if (w1_value == w2_value && w2_value == w3_value){
lg.warning("This user has to much luck, he/she should go to a casino");
}
else
lg.warning("unlucky -> " + w1_value + w2_value + w3_value);
}
@Override
public void onSubscribe(Flow.Subscription subscription) {
lg.config("on subscribe");
this.subscriptions.add(subscription);
subscription.request(1);
}
@Override
public void onNext(WuerfelData item) {
lg.config("on Next");
this.wertPublisher.submit(item);
this.subscriptions.get(item.getId() - 1).request(1);
}
@Override
public void onError(Throwable throwable) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void onComplete() {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
}