001package jmri.jmrix.maple.simulator.configurexml; 002 003import java.util.List; 004import jmri.jmrix.configurexml.AbstractSerialConnectionConfigXml; 005import jmri.jmrix.maple.InputBits; 006import jmri.jmrix.maple.MapleSystemConnectionMemo; 007import jmri.jmrix.maple.OutputBits; 008import jmri.jmrix.maple.SerialNode; 009import jmri.jmrix.maple.SerialTrafficController; 010import jmri.jmrix.maple.simulator.ConnectionConfig; 011import jmri.jmrix.maple.simulator.SimulatorAdapter; 012import org.jdom2.Element; 013 014/** 015 * Handle XML persistence of layout connections by persisting the 016 * SerialDriverAdapter (and connections). 017 * <p> 018 * Note this is named as the XML version of a ConnectionConfig object, 019 * but it's actually persisting the SerialDriverAdapter. 020 * <p> 021 * This class is invoked from jmrix.JmrixConfigPaneXml on write, as that class 022 * is the one actually registered. Reads are brought here directly via the class 023 * attribute in the XML. 024 * 025 * @author Bob Jacobsen Copyright (c) 2003 copied from NCE/Tams code 026 * @author kcameron Copyright (c) 2014 027 */ 028public class ConnectionConfigXml extends AbstractSerialConnectionConfigXml { 029 030 public ConnectionConfigXml() { 031 super(); 032 } 033 034 /** 035 * Write out the SerialNode objects too. 036 * 037 * @param e Element being extended 038 */ 039 @Override 040 protected void extendElement(Element e) { 041 SerialNode node = (SerialNode) ((MapleSystemConnectionMemo)adapter.getSystemConnectionMemo()).getTrafficController().getNode(0); 042 int index = 1; 043 while (node != null) { 044 // add node as an element 045 Element n = new Element("node"); 046 n.setAttribute("name", "" + node.getNodeAddress()); 047 e.addContent(n); 048 // add parameters to the node as needed 049 n.addContent(makeParameter("transmissiondelay", "" + InputBits.getTimeoutTime())); 050 n.addContent(makeParameter("inputbits", "" + InputBits.getNumInputBits())); 051 n.addContent(makeParameter("senddelay", "" + OutputBits.getSendDelay())); 052 n.addContent(makeParameter("outputbits", "" + OutputBits.getNumOutputBits())); 053 054 // look for the next node 055 node = (SerialNode) ((MapleSystemConnectionMemo)adapter.getSystemConnectionMemo()).getTrafficController().getNode(index); 056 index++; 057 } 058 } 059 060 protected Element makeParameter(String name, String value) { 061 Element p = new Element("parameter"); 062 p.setAttribute("name", name); 063 p.addContent(value); 064 return p; 065 } 066 067 @Override 068 protected void getInstance(Object object) { 069 adapter = ((ConnectionConfig) object).getAdapter(); 070 } 071 072 @Override 073 protected void getInstance() { 074 if (adapter == null) { 075 adapter = new SimulatorAdapter(); 076 } 077 } 078 079 @Override 080 protected void unpackElement(Element shared, Element perNode) { 081 List<Element> l = shared.getChildren("node"); 082 for (int i = 0; i < l.size(); i++) { 083 Element n = l.get(i); 084 int addr = Integer.parseInt(n.getAttributeValue("name")); 085 int delay = Integer.parseInt(findParmValue(n, "transmissiondelay")); 086 int senddelay = Integer.parseInt(findParmValue(n, "senddelay")); 087 int numinput = Integer.parseInt(findParmValue(n, "inputbits")); 088 int numoutput = Integer.parseInt(findParmValue(n, "outputbits")); 089 090 SerialTrafficController tc = ((MapleSystemConnectionMemo) adapter.getSystemConnectionMemo()).getTrafficController(); 091 // create node (they register themselves) 092 SerialNode node = new SerialNode(addr, 0, tc); 093 InputBits.setTimeoutTime(delay); 094 InputBits.setNumInputBits(numinput); 095 OutputBits.setSendDelay(senddelay); 096 OutputBits.setNumOutputBits(numoutput); 097 098 // Trigger initialization of this Node to reflect these parameters 099 tc.initializeSerialNode(node); 100 } 101 } 102 103 @Override 104 protected void register() { 105 this.register(new ConnectionConfig(adapter)); 106 } 107 108}