[Megameknet-cvs] MegaMekNET/src/server/campaign/visitor SXMLSaveVisitor.java,1.12,1.13
Status: Inactive
Brought to you by:
mcwizard
From: Immanuel S. <im...@us...> - 2005-04-11 20:02:10
|
Update of /cvsroot/megameknet/MegaMekNET/src/server/campaign/visitor In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15726/src/server/campaign/visitor Modified Files: SXMLSaveVisitor.java Log Message: fixed xml saving, now featuring asynchron saves. Index: SXMLSaveVisitor.java =================================================================== RCS file: /cvsroot/megameknet/MegaMekNET/src/server/campaign/visitor/SXMLSaveVisitor.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** SXMLSaveVisitor.java 26 Mar 2005 17:44:26 -0000 1.12 --- SXMLSaveVisitor.java 11 Apr 2005 20:01:28 -0000 1.13 *************** *** 14,17 **** --- 14,19 ---- package server.campaign.visitor; + import java.util.ArrayList; + import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; *************** *** 19,22 **** --- 21,26 ---- import java.util.Map; import org.jdom.Element; + import server.MMServ; + import server.campaign.CampaignMain; import server.campaign.NewbieHouse; import server.campaign.NewbiePlanet; *************** *** 36,41 **** --- 40,49 ---- import server.campaign.pilot.SPilot; import server.campaign.util.TimeZoneData; + import common.BlackBoard; + import common.CampaignData; + import common.House; import common.Unit; import common.visitor.Visitor; + import common.visitor.Visitorable; import common.visitor.XMLSaveVisitor; *************** *** 47,50 **** --- 55,113 ---- public class SXMLSaveVisitor extends XMLSaveVisitor implements ServerVisitor, Visitor { + private static Object lock = new Object(); + + public SXMLSaveVisitor(Collection units_, CampaignData data, + BlackBoard blackboard_, Market market_, final String userToInform) { + final long starttime = System.currentTimeMillis(); + // make a defensive copy of all data structures to write them asynchron + final Collection units = new ArrayList(units_); + final Collection planets = new ArrayList(data.getAllPlanets()); + final Collection planetEnvironment = new ArrayList(data.getAllTerrains()); + final Collection houses = new ArrayList(data.getAllHouses()); + final BlackBoard blackboard = new BlackBoard(blackboard_.allMessages()); + final Market market = new Market(market_); + + new Thread(){ + public void run() { + synchronized(lock) { + // Units + toXml("campaign/unit.xml", "units", units); + // Houses (one file for each house because memory concerns) + for (Iterator it = houses.iterator(); it.hasNext();) { + SHouse house = (SHouse)it.next(); + toXml("campaign/"+house.getName()+".xml", house); + } + // Write all house names (on the fly). + toXml("campaign/houses.xml", new Visitorable(){ + public Object visit(Visitor visitor) { + Element e = new Element("houses"); + for (Iterator it = houses.iterator(); it.hasNext();) { + House house = (House)it.next(); + Element h = new Element("house"); + h.setAttribute("class", house.getClass().getName()); + h.setAttribute("file", "campaign/"+house.getName()+".xml"); + e.addContent(h); + } + return e; + } + }); + // Blackboard + toXml("campaign/blackboard.xml", blackboard); + // Market + toXml("campaign/blackmarket.xml", market); + // Planet environment + toXml("campaign/planetEnvironment.xml", "planetEnvironments", planetEnvironment); + // Planets + toXml("campaign/planet.xml", "planets", planets); + String msg = "XML save finished after "+((System.currentTimeMillis()-starttime)/1000)+"s"; + if (userToInform != null) + CampaignMain.cm.toUser(msg, userToInform, true); + else + MMServ.mmlog.infoLog(msg); + } + } + }.start(); + } + public Object visitSPlayer(SPlayer data) { Element e = (Element)visitPlayer(data); |