use of jmri.ThrottleListener in project JMRI by JMRI.
the class AbstractThrottleManager method notifyThrottleKnown.
/**
* Handle throttle information when it's finally available, e.g. when a new
* Throttle object has been created.
* <P>
* This method creates a throttle for all ThrottleListeners of that address
* and notifies them via the ThrottleListener.notifyThrottleFound method.
*/
public void notifyThrottleKnown(DccThrottle throttle, LocoAddress addr, boolean suppressUseIncrements) {
log.debug("notifyThrottleKnown for " + addr);
DccLocoAddress dla = (DccLocoAddress) addr;
Addresses ads = null;
if (!addressThrottles.containsKey(dla)) {
log.debug("Address " + addr + "doesn't already exists so will add");
ads = new Addresses(throttle);
addressThrottles.put(dla, ads);
} else {
addressThrottles.get(dla).setThrottle(throttle);
}
ArrayList<WaitingThrottle> a = throttleListeners.get(dla);
if (a == null) {
log.debug("notifyThrottleKnown with zero-length listeners: " + addr);
} else {
for (int i = 0; i < a.size(); i++) {
ThrottleListener l = a.get(i).getListener();
log.debug("Notify listener " + (i + 1) + " of " + a.size());
l.notifyThrottleFound(throttle);
if (suppressUseIncrements == false) {
// this is a new throttle
addressThrottles.get(dla).incrementUse();
} else {
// requestThrottle() found an existing throttle, we're re-using that one
log.debug("incrementUse suppressed");
}
addressThrottles.get(dla).addListener(l);
if (ads != null && a.get(i).getRosterEntry() != null && throttle.getRosterEntry() == null) {
throttle.setRosterEntry(a.get(i).getRosterEntry());
}
}
throttleListeners.remove(dla);
}
ArrayList<WaitingThrottle> p = listenerOnly.get(dla);
if (p == null) {
log.debug("notifyThrottleKnown with zero-length propertyChangeListeners: " + addr);
} else {
for (int i = 0; i < p.size(); i++) {
PropertyChangeListener l = p.get(i).getPropertyChangeListener();
log.debug("Notify propertyChangeListener");
l.propertyChange(new PropertyChangeEvent(this, "throttleAssigned", null, dla));
if (ads != null && p.get(i).getRosterEntry() != null && throttle.getRosterEntry() == null) {
throttle.setRosterEntry(p.get(i).getRosterEntry());
}
throttle.addPropertyChangeListener(l);
}
listenerOnly.remove(dla);
}
}
use of jmri.ThrottleListener in project JMRI by JMRI.
the class AbstractThrottleManager method failedThrottleRequest.
/**
* If the system-specific ThrottleManager has been unable to create the DCC
* throttle then it needs to be removed from the throttleListeners,
* otherwise any subsequent request for that address results in the address
* being reported as already in use, if singleUse is set. This also sends a
* notification message back to the requestor with a string reason as to why
* the request has failed.
*
* @param address The DCC Loco Address that the request failed on.
* @param reason A text string passed by the ThrottleManae as to why
*/
public void failedThrottleRequest(DccLocoAddress address, String reason) {
ArrayList<WaitingThrottle> a = throttleListeners.get(address);
if (a == null) {
log.warn("failedThrottleRequest with zero-length listeners: " + address);
} else {
for (int i = 0; i < a.size(); i++) {
ThrottleListener l = a.get(i).getListener();
l.notifyFailedThrottleRequest(address, reason);
}
}
throttleListeners.remove(address);
ArrayList<WaitingThrottle> p = listenerOnly.get(address);
if (p == null) {
log.debug("failedThrottleRequest with zero-length PropertyChange listeners: " + address);
} else {
for (int i = 0; i < p.size(); i++) {
PropertyChangeListener l = p.get(i).getPropertyChangeListener();
l.propertyChange(new PropertyChangeEvent(this, "attachFailed", address, null));
}
}
listenerOnly.remove(address);
}
use of jmri.ThrottleListener in project JMRI by JMRI.
the class AbstractAutomaton method getThrottle.
/**
* Obtains a DCC throttle, including waiting for the command station
* response.
*
* @param address Numeric address value
* @param longAddress true if this is a long address, false for a short
* address
* @param waitSecs number of seconds to wait for throttle to acquire
* before returning null
* @return A usable throttle, or null if error
*/
public DccThrottle getThrottle(int address, boolean longAddress, int waitSecs) {
log.debug("requesting DccThrottle for addr " + address);
if (!inThread) {
log.warn("getThrottle invoked from invalid context");
}
throttle = null;
ThrottleListener throttleListener = new ThrottleListener() {
@Override
public void notifyThrottleFound(DccThrottle t) {
throttle = t;
synchronized (self) {
// should be only one thread waiting, but just in case
self.notifyAll();
}
}
@Override
public void notifyFailedThrottleRequest(jmri.DccLocoAddress address, String reason) {
log.error("Throttle request failed for " + address + " because " + reason);
failedThrottleRequest = true;
synchronized (self) {
// should be only one thread waiting, but just in case
self.notifyAll();
}
}
};
boolean ok = InstanceManager.getDefault(ThrottleManager.class).requestThrottle(address, longAddress, throttleListener);
// check if reply is coming
if (!ok) {
log.info("Throttle for loco " + address + " not available");
//kill the pending request
InstanceManager.getDefault(ThrottleManager.class).cancelThrottleRequest(address, throttleListener);
return null;
}
// now wait for reply from identified throttle
int waited = 0;
while (throttle == null && failedThrottleRequest == false && waited <= waitSecs) {
log.debug("waiting for throttle");
// 1 seconds
wait(1000);
waited++;
if (throttle == null) {
log.warn("Still waiting for throttle " + address + "!");
}
}
if (throttle == null) {
log.debug("canceling request for Throttle " + address);
//kill the pending request
InstanceManager.getDefault(ThrottleManager.class).cancelThrottleRequest(address, throttleListener);
}
return throttle;
}
use of jmri.ThrottleListener in project JMRI by JMRI.
the class AbstractThrottleTest method testRelease_ThrottleListener.
/**
* Test of release method, of class AbstractThrottle.
*/
public void testRelease_ThrottleListener() {
ThrottleListener l = null;
AbstractThrottle instance = new AbstractThrottleImpl();
instance.release(l);
}
use of jmri.ThrottleListener in project JMRI by JMRI.
the class AbstractAutomaton method getThrottle.
/**
* Obtains a DCC throttle, including waiting for the command station
* response.
*
* @param re specifies the desired locomotive
* @param waitSecs number of seconds to wait for throttle to acquire before
* returning null
* @return A usable throttle, or null if error
*/
public DccThrottle getThrottle(BasicRosterEntry re, int waitSecs) {
log.debug("requesting DccThrottle for rosterEntry " + re.getId());
if (!inThread) {
log.warn("getThrottle invoked from invalid context");
}
throttle = null;
ThrottleListener throttleListener = new ThrottleListener() {
@Override
public void notifyThrottleFound(DccThrottle t) {
throttle = t;
synchronized (self) {
// should be only one thread waiting, but just in case
self.notifyAll();
}
}
@Override
public void notifyFailedThrottleRequest(jmri.DccLocoAddress address, String reason) {
log.error("Throttle request failed for " + address + " because " + reason);
failedThrottleRequest = true;
synchronized (self) {
// should be only one thread waiting, but just in case
self.notifyAll();
}
}
};
boolean ok = InstanceManager.throttleManagerInstance().requestThrottle(re, throttleListener);
// check if reply is coming
if (!ok) {
log.info("Throttle for loco " + re.getId() + " not available");
//kill the pending request
InstanceManager.getDefault(ThrottleManager.class).cancelThrottleRequest(re, throttleListener);
return null;
}
// now wait for reply from identified throttle
int waited = 0;
while (throttle == null && failedThrottleRequest == false && waited <= waitSecs) {
log.debug("waiting for throttle");
// 1 seconds
wait(1000);
waited++;
if (throttle == null) {
log.warn("Still waiting for throttle " + re.getId() + "!");
}
}
if (throttle == null) {
log.debug("canceling request for Throttle " + re.getId());
//kill the pending request
InstanceManager.getDefault(ThrottleManager.class).cancelThrottleRequest(re, throttleListener);
}
return throttle;
}
Aggregations