use of jmri.Block in project JMRI by JMRI.
the class LayoutBlock method notifyNeighboursOfRemoval.
void notifyNeighboursOfRemoval(ArrayList<Routes> routesToRemove, Block notifyingblk) {
String msgPrefix = "From " + this.getDisplayName() + " notify block " + notifyingblk.getDisplayName() + " ";
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " notifyNeighboursOfRemoval called for routes from " + notifyingblk.getDisplayName() + " ===");
}
boolean notifyvalid = false;
for (int i = neighbours.size() - 1; i > -1; i--) {
if (neighbours.get(i).getBlock() == notifyingblk) {
notifyvalid = true;
}
}
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " The notifying block is still valid? " + notifyvalid);
}
for (int j = routesToRemove.size() - 1; j > -1; j--) {
boolean stillexist = false;
Block destBlock = routesToRemove.get(j).getDestBlock();
Block sourceBlock = routesToRemove.get(j).getNextBlock();
RoutingPacket newUpdate = new RoutingPacket(REMOVAL, destBlock, -1, -1, -1, -1, getNextPacketID());
if (enableDeleteRouteLogging) {
log.info("From " + this.getDisplayName() + " notify block " + notifyingblk.getDisplayName() + " checking " + destBlock.getDisplayName() + " from " + sourceBlock.getDisplayName());
}
ArrayList<Routes> validroute = new ArrayList<Routes>();
ArrayList<Routes> destRoutes = getDestRoutes(destBlock);
for (Routes r : destRoutes) {
//We now know that we still have a valid route to the dest
if (r.getNextBlock() == this.getBlock()) {
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " The destBlock " + destBlock.getDisplayName() + " is our neighbour");
}
validroute.add(new Routes(r.getDestBlock(), r.getNextBlock(), 0, 0, 0, 0));
stillexist = true;
} else {
//At this stage do we need to check if the valid route comes from a neighbour?
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " we still have a route to " + destBlock.getDisplayName() + " via " + r.getNextBlock().getDisplayName() + " in our list");
}
validroute.add(new Routes(destBlock, r.getNextBlock(), 0, 0, 0, 0));
stillexist = true;
}
}
if (stillexist) {
if (enableDeleteRouteLogging) {
log.info(msgPrefix + "A Route still exists");
log.info(msgPrefix + " the number of routes installed to block " + destBlock.getDisplayName() + " is " + validroute.size());
}
if (validroute.size() == 1) {
//Specific routing update.
Block nextHop = validroute.get(0).getNextBlock();
LayoutBlock layoutBlock;
if (validroute.get(0).getNextBlock() != this.getBlock()) {
layoutBlock = InstanceManager.getDefault(jmri.jmrit.display.layoutEditor.LayoutBlockManager.class).getLayoutBlock(nextHop);
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " We only have a single valid route left to " + destBlock.getDisplayName() + " So will tell " + layoutBlock.getDisplayName() + " we no longer have it");
}
if (layoutBlock != null) {
layoutBlock.removeRouteFromNeighbour(this, newUpdate);
}
getAdjacency(nextHop).removeRouteAdvertisedToNeighbour(routesToRemove.get(j));
}
//At this point we could probably do with checking for other valid paths from the notifyingblock
//Have a feeling that this is pretty much the same as above!
ArrayList<Block> validNeighboursToNotify = new ArrayList<Block>();
//could still hold a valid through path.
for (int i = neighbours.size() - 1; i > -1; i--) {
//Need to ignore if the dest block is our neighour in this instance
if ((neighbours.get(i).getBlock() != destBlock) && (neighbours.get(i).getBlock() != nextHop)) {
if (validThroughPath(notifyingblk, neighbours.get(i).getBlock())) {
Block neighblock = neighbours.get(i).getBlock();
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " we could of potentially sent the route to " + neighblock.getDisplayName());
}
if (!validThroughPath(nextHop, neighblock)) {
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " there is no other valid path so will mark for removal");
}
validNeighboursToNotify.add(neighblock);
} else {
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " there is another valid path so will NOT mark for removal");
}
}
}
}
}
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " the next block is our selves so we won't remove!");
log.info(msgPrefix + " do we need to find out if we could of send the route to another neighbour such as?");
}
for (int i = 0; i < validNeighboursToNotify.size(); i++) {
//we will not notify the neighbour of our loss of route
if (!validThroughPath(validNeighboursToNotify.get(i), destBlock)) {
layoutBlock = InstanceManager.getDefault(jmri.jmrit.display.layoutEditor.LayoutBlockManager.class).getLayoutBlock(validNeighboursToNotify.get(i));
if (layoutBlock != null) {
layoutBlock.removeRouteFromNeighbour(this, newUpdate);
}
getAdjacency(validNeighboursToNotify.get(i)).removeRouteAdvertisedToNeighbour(routesToRemove.get(j));
} else {
if (enableDeleteRouteLogging) {
log.info(msgPrefix + validNeighboursToNotify.get(i).getDisplayName() + " has a valid path to " + destBlock.getDisplayName());
}
}
}
} else {
//Need to deal with having multiple routes left.
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " routes left to block " + destBlock.getDisplayName());
}
for (int i = 0; i < validroute.size(); i++) {
//We need to see if we have valid routes.
if (validThroughPath(notifyingblk, validroute.get(i).getNextBlock())) {
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " to " + validroute.get(i).getNextBlock().getDisplayName() + " Is a valid route");
}
//Will mark the route for potential removal
validroute.get(i).setMiscFlags(0x02);
} else {
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " to " + validroute.get(i).getNextBlock().getDisplayName() + " Is not a valid route");
}
//Mark the route to not be removed.
validroute.get(i).setMiscFlags(0x01);
//Given that the route to this is not valid, we do not want to
//be notifying this next block about the loss of route.
}
}
//Now need to get through the list and cross reference each one.
for (int i = 0; i < validroute.size(); i++) {
if (validroute.get(i).getMiscFlags() == 0x02) {
Block nextblk = validroute.get(i).getNextBlock();
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " route from " + nextblk.getDisplayName() + " has been flagged for removal");
}
//Need to cross reference it with the routes that are left.
boolean leaveroute = false;
for (int k = 0; k < validroute.size(); k++) {
if (validroute.get(k).getMiscFlags() == 0x01) {
if (validThroughPath(nextblk, validroute.get(k).getNextBlock())) {
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " we have a valid path from " + nextblk.getDisplayName() + " to " + validroute.get(k).getNextBlock());
}
leaveroute = true;
}
}
}
if (!leaveroute) {
LayoutBlock layoutBlock = InstanceManager.getDefault(jmri.jmrit.display.layoutEditor.LayoutBlockManager.class).getLayoutBlock(nextblk);
if (enableDeleteRouteLogging) {
log.info(msgPrefix + "############ We need to send notification to " + nextblk.getDisplayName() + " to remove route ########### haven't found an example of this yet!");
}
layoutBlock.removeRouteFromNeighbour(this, newUpdate);
getAdjacency(nextblk).removeRouteAdvertisedToNeighbour(routesToRemove.get(j));
} else {
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " a valid path through exists " + nextblk.getDisplayName() + " so we will not remove route.");
}
}
}
}
}
} else {
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " We have no other routes to " + destBlock.getDisplayName() + " Therefore we will broadast this to our neighbours");
}
for (Adjacencies adj : neighbours) {
adj.removeRouteAdvertisedToNeighbour(destBlock);
}
firePropertyChange("routing", null, newUpdate);
}
}
if (enableDeleteRouteLogging) {
log.info(msgPrefix + " finshed check and notifying of removed routes from " + notifyingblk.getDisplayName() + " ===");
}
}
use of jmri.Block in project JMRI by JMRI.
the class LayoutBlockConnectivityTools method discoverValidBeanPairs.
/**
* Discovers valid pairs of beans type T assigned to a layout editor. If no
* bean type is provided, then either SignalMasts or Sensors are discovered
* If no editor is provided, then all editors are considered
*
* @param pathMethod Determine whether or not we should reject pairs if
* there are other beans in the way. Constant values of
* NONE, ANY, MASTTOMAST, HEADTOHEAD
*/
public Hashtable<NamedBean, ArrayList<NamedBean>> discoverValidBeanPairs(LayoutEditor editor, Class<?> T, int pathMethod) {
LayoutBlockManager lbm = InstanceManager.getDefault(LayoutBlockManager.class);
Hashtable<NamedBean, ArrayList<NamedBean>> retPairs = new Hashtable<NamedBean, ArrayList<NamedBean>>();
ArrayList<FacingProtecting> beanList = generateBlocksWithBeans(editor, T);
for (FacingProtecting fp : beanList) {
for (Block block : fp.getProtectingBlocks()) {
if (log.isDebugEnabled()) {
try {
log.debug("\nSource " + fp.getBean().getDisplayName());
log.debug("facing " + fp.getFacing().getDisplayName());
log.debug("protecting " + block.getDisplayName());
} catch (java.lang.NullPointerException e) {
//Can be considered normal if the signalmast is assigned to an end bumper.
}
}
LayoutBlock lFacing = lbm.getLayoutBlock(fp.getFacing());
LayoutBlock lProtecting = lbm.getLayoutBlock(block);
NamedBean source = fp.getBean();
try {
retPairs.put(source, discoverPairDest(source, lProtecting, lFacing, beanList, pathMethod));
} catch (JmriException ex) {
log.error(ex.toString());
}
}
}
return retPairs;
}
use of jmri.Block in project JMRI by JMRI.
the class LayoutBlockManager method getLayoutBlocksOccupiedByRosterEntry.
/**
* Returns a list of layout blocks which this roster entry appears to
* be occupying. A layout block is assumed to contain this roster entry if the value
* of the underlying block is the RosterEntry itself, or a string with the entry's
* id or dcc address.
*
* @param re the roster entry
* @return list of layout block user names
*/
public List<LayoutBlock> getLayoutBlocksOccupiedByRosterEntry(RosterEntry re) {
BlockManager bm = jmri.InstanceManager.getDefault(jmri.BlockManager.class);
List<Block> blockList = bm.getBlocksOccupiedByRosterEntry(re);
List<LayoutBlock> layoutBlockList = new ArrayList<>();
for (Block block : blockList) {
if (block.getUserName() != null) {
LayoutBlock lb = getByUserName(block.getUserName());
if (lb != null) {
layoutBlockList.add(lb);
}
}
}
return layoutBlockList;
}
use of jmri.Block in project JMRI by JMRI.
the class LayoutBlock method getNextBlock.
/**
* @param destBlock - is the destination of the block we are following
* @param direction - is the direction of travel from the previous block
*/
public Block getNextBlock(Block destBlock, int direction) {
int bestMetric = 965000;
Block bestBlock = null;
for (Routes r : routes) {
if ((r.getDestBlock() == destBlock) && (r.getDirection() == direction)) {
if (r.getMetric() < bestMetric) {
bestMetric = r.getMetric();
bestBlock = r.getNextBlock();
//bestBlock=r.getDestBlock();
}
}
}
return bestBlock;
}
use of jmri.Block in project JMRI by JMRI.
the class LayoutBlock method updateActiveThroughPaths.
void updateActiveThroughPaths(ThroughPaths tp, boolean active) {
if (enableUpdateRouteLogging) {
log.info("We have been notified that a through path has changed state");
}
if (activePaths == null) {
activePaths = new ArrayList<ThroughPaths>();
}
if (active) {
activePaths.add(tp);
setRoutesValid(tp.getSourceBlock(), active);
setRoutesValid(tp.getDestinationBlock(), active);
} else {
//We need to check if either our source or des is in use by another path.
activePaths.remove(tp);
boolean SourceInUse = false;
boolean DestinationInUse = false;
for (int i = 0; i < activePaths.size(); i++) {
Block testSour = activePaths.get(i).getSourceBlock();
Block testDest = activePaths.get(i).getDestinationBlock();
if ((testSour == tp.getSourceBlock()) || (testDest == tp.getSourceBlock())) {
SourceInUse = true;
}
if ((testSour == tp.getDestinationBlock()) || (testDest == tp.getDestinationBlock())) {
DestinationInUse = true;
}
}
if (!SourceInUse) {
setRoutesValid(tp.getSourceBlock(), active);
}
if (!DestinationInUse) {
setRoutesValid(tp.getDestinationBlock(), active);
}
}
for (int i = 0; i < throughPaths.size(); i++) {
//This is processed simply for the throughpath table.
if (tp == throughPaths.get(i)) {
firePropertyChange("path", null, i);
}
}
}
Aggregations