From: Andreas W. <an...@us...> - 2006-01-17 15:51:14
|
Update of /cvsroot/magicmap/magicmapserver/src/net/sf/magicmap/server/model/node In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23010/src/net/sf/magicmap/server/model/node Added Files: Tag: ServerCalculationBranch LocationNode.java AccessPointNode.java ClientNode.java AccessPointSeerNode.java Node.java AccessPointEdge.java NodeModel.java Log Message: First Version --- NEW FILE: LocationNode.java --- /* * Created on 25.11.2004 */ package net.sf.magicmap.server.model.node; import java.util.ArrayList; /** * Ein Ort bzw. Referenzpunkt ist eine Art eingefrorenes Objekt, welches mal * AccessPoints mit Signalstärken gesehen hat. * @see net.sf.magicmap.client.models#AccessPointSeerNode * @author thuebner */ public class LocationNode extends AccessPointSeerNode { public LocationNode(NodeModel model) { super(model); } /** * Liefert AccessPoints mit denen dieser Referenzpunkt * verbunden werden soll. */ /*public ArrayList getNeighbors() { return super.getNeighbors(); }*/ public ArrayList getLocations(){ ArrayList locations = getModel().getLocationsWithAtLeastOneAccessPoint(super.getNeighbors(), this); return locations; } public ArrayList getNeighbors(){ ArrayList result = super.getNeighbors(); // Alle Locations, die mind. einen AccessPoint gemeinsam haben mit // diesem Client System.out.println("Location sieht " + result.size() + " APs"); ArrayList locations = getModel().getLocationsWithAtLeastOneAccessPoint(result, this); System.out.println("Location: " + getName() + " soll verbunden werden mit " + locations.size() + " anderen Locations."); result.addAll(getLocations()); return result; } /* (non-Javadoc) * @see net.sf.magicmap.client.model.Node#getType() */ public int getType(){ // TODO Auto-generated method stub return NodeModel.NODETYPE_LOCATION; } } --- NEW FILE: Node.java --- /* * Created on 25.11.2004 */ package net.sf.magicmap.server.model.node; import java.util.ArrayList; /** * Einfacher Knoten in unserem Graphen aus Clients, AccessPoints, * und Referenzpunkten. * @author thuebner */ public abstract class Node { private static int counter = 0; private String name; // Beschreibung private String internal; protected boolean fix; // Fest oder beweglich protected boolean update = false; // wenn Referenzpunktupdate = true private int x; // Position (nur wenn fix = true) private int y; private long id; private NodeModel model; /** * Erzeugt leeren Knoten. Ein Knoten muß über sein * NodeModel bescheid wissen, damit er verschiedene * Dinge abfragen kann. Nur so können beliebige Knotentypen ohne * Änderungen an NodeModel hinzugefügt werden. Siehe Methode * getNeighbors, welche vom Knotentyp abhängig mit Hilfe von NodeModel * potenzielle Nachbarknoten des Knoten sucht. * @param model */ public Node(NodeModel model) { this.model = model; counter++; // Eindeutigen Defaultnamen setzen this.internal = this.getClass().getName(); this.internal = internal.substring(this.internal.lastIndexOf('.') + 1) + counter; this.name = this.internal; this.id = -1; } public String getDisplayName(){ return this.name; } public boolean isUpdate(){ return update; } public void setUpdate(boolean newupdate){ this.update = newupdate; this.model.updateNode(this,NodeModel.UPDATE_RESCAN,null); } public boolean isFix(){ return fix; } public void setFix(boolean fix){ this.fix = fix; this.model.updateNode(this, NodeModel.UPDATE_FIXSTATE, null); } public String getName(){ return name; } public final void setName(String name){ String oldname = this.name; this.name = name; if (model.nodeExists(oldname)) model.rehashNode(this, oldname); } public int getX(){ return x; } public int getY(){ return y; } public void setPositionSilent(int x, int y){ this.x = x; this.y = y; } public void setPosition(int x, int y){ setPositionSilent(x, y); model.updateNode(this, NodeModel.UPDATE_POSITION, null); } public abstract ArrayList getNeighbors(); public NodeModel getModel(){ return model; } public abstract int getType(); public int hashCode(){ return internal.hashCode(); } public String toString(){ return internal; } public long getId(){ return id; } public void setId(long id){ this.id = id; } } --- NEW FILE: NodeModel.java --- package net.sf.magicmap.server.model.node; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import net.sf.magicmap.server.interfaces.NodeModelListener; /** * Model zur Beschreibung des aktuellen Zustands in der * Welt der Clients und AccessPoints. Es können Anfragen * gestellt und Berechnungen durchgeführt werden auf * dem Modell * * @author thuebner */ public class NodeModel { public static final int UPDATE_SEESACCESSPOINT = 0; public static final int UPDATE_NOTSEESACCESSPOINT = 1; public static final int UPDATE_LABELCHANGED = 2; public static final int UPDATE_FIXSTATE = 3; public static final int UPDATE_CLEAR = 4; public static final int UPDATE_POSITION = 5; public static final int UPDATE_RESCAN = 6; public static final int NODETYPE_ACCESSPOINT = 1; public static final int NODETYPE_CLIENT = 2; public static final int NODETYPE_LOCATION = 3; private HashMap nodes; private HashMap accesspoints; private ArrayList listeners; /** * Erzeugt neues leeres Model * */ public NodeModel() { this.nodes = new HashMap(); this.accesspoints = new HashMap(); this.listeners = new ArrayList(); } public void addNode(Node node){ this.nodes.put(node.getName(), node); if (node.getType() == NODETYPE_ACCESSPOINT){ // Wenn AccessPoint, dann nach MAC-Adresse extra hashen this.accesspoints.put(((AccessPointNode) node).getMacAddress(), node); } Iterator it = listeners.iterator(); while (it.hasNext()){ NodeModelListener l = (NodeModelListener) it.next(); l.nodeAddedEvent(node); } } public void removeNode(Node node){ this.nodes.remove(node.getName()); if (node.getType() == NODETYPE_ACCESSPOINT){ // Wenn AccessPoint, dann nach MAC-Adresse extra hashen this.accesspoints.remove(((AccessPointNode) node).getMacAddress()); } Iterator it = listeners.iterator(); while (it.hasNext()){ NodeModelListener l = (NodeModelListener) it.next(); l.nodeRemovedEvent(node); } } public void fireSeesAccessPoint(Node node, AccessPointNode ap){ updateNode(node, UPDATE_SEESACCESSPOINT, ap); } public void fireNotSeesAccessPoint(Node node, AccessPointNode ap){ updateNode(node, UPDATE_NOTSEESACCESSPOINT, ap); } public void updateNode(Node node, int type, Object data){ Iterator it = listeners.iterator(); while (it.hasNext()){ NodeModelListener l = (NodeModelListener) it.next(); l.nodeUpdatedEvent(node, type, data); } } /** * Aktualisiert den Hash-Schlüssel für einen Knoten. * Unter Node.name wird der neue Hashwert erwartet. * * @param node Knoten der aktualisiert werden soll * @param name Alter Hash-Wert */ public void rehashNode(Node node, String name){ nodes.remove(name); nodes.put(node.getName(), node); updateNode(node, UPDATE_LABELCHANGED, name); } public ArrayList findNeighbors(Node node){ ArrayList result = new ArrayList(); if (node instanceof ClientNode){ ArrayList aes = ((ClientNode) node).getApEdges(); } return result; } public ArrayList findNonNeighbors(Node node){ ArrayList result = new ArrayList(); return result; } public void addNodeModelListener(NodeModelListener l){ if (!listeners.contains(l)){ this.listeners.add(l); Iterator it = nodes.values().iterator(); while (it.hasNext()){ Node node = (Node) it.next(); l.nodeAddedEvent(node); } } } public void removeNodeModelListener(NodeModelListener l){ this.listeners.remove(l); } public NodeModelListener[] nodeModelListeners(){ return (NodeModelListener[]) listeners.toArray(new NodeModelListener[0]); } public ArrayList getLocationsWithAtLeastOneAccessPoint(ArrayList accesspoints){ return getLocationsWithAtLeastOneAccessPoint(accesspoints, null); } /** * Gibt alle Orte/Referenzpunkte zurück, die mindestens einen * AccessPoint mit der übergebenen gemeinsam haben. * @param result * @return */ public ArrayList getLocationsWithAtLeastOneAccessPoint(ArrayList accesspoints, Node exclude){ ArrayList result = new ArrayList(); HashSet accessPointsHash = new HashSet(accesspoints); Iterator it = nodes.values().iterator(); while (it.hasNext()){ Node o = (Node) it.next(); if (o.getType() == NodeModel.NODETYPE_LOCATION && exclude != o){ LocationNode location = (LocationNode) o; ArrayList seenAccessPoints = location.getSeenAccessPoints(); for (int j = 0; j < seenAccessPoints.size(); j++){ AccessPointNode p = (AccessPointNode) seenAccessPoints.get(j); if (accessPointsHash.contains(p)){ result.add(location); break; } } } } return result; } public Node findNode(String name){ Node node = (Node) nodes.get(name); return node; } public boolean nodeExists(String name){ return (findNode(name) != null); } public AccessPointNode findAccessPoint(String mac){ AccessPointNode node = (AccessPointNode) accesspoints.get(mac); return node; } public boolean accessPointExists(String mac){ return (findAccessPoint(mac) != null); } /** * @return */ public Collection getNodes(){ return Collections.unmodifiableCollection(nodes.values()); } /** * Entfernt alles aus dem Model * */ public void clear(){ this.nodes.clear(); this.accesspoints.clear(); updateNode(null, UPDATE_CLEAR, null); } } --- NEW FILE: AccessPointNode.java --- /* * Created on 25.11.2004 */ package net.sf.magicmap.server.model.node; import java.util.ArrayList; /** * * @author thuebner */ public class AccessPointNode extends Node { /** * @param model */ public AccessPointNode(NodeModel model) { super(model); } private String macAddress; public String getMacAddress(){ return macAddress; } public void setMacAddress(String macAddress){ this.macAddress = macAddress; } public String getDisplayName(){ return this.macAddress; } // Ein AccessPoint hat von sich aus keine // verbindungen irgendwohin public ArrayList getNeighbors(){ return new ArrayList(); } /* (non-Javadoc) * @see net.sf.magicmap.client.model.Node#getType() */ public int getType(){ // TODO Auto-generated method stub return NodeModel.NODETYPE_ACCESSPOINT; } } --- NEW FILE: AccessPointEdge.java --- /* * Created on 25.11.2004 */ package net.sf.magicmap.server.model.node; /** * @author thuebner */ public class AccessPointEdge { public AccessPointNode accessPoint; public double signalLevel; public AccessPointNode getAccessPoint(){ return accessPoint; } public void setAccessPoint(AccessPointNode accessPoint){ this.accessPoint = accessPoint; } public double getSignalLevel(){ return signalLevel; } public void setSignalLevel(double signalStrength){ this.signalLevel = signalStrength; } } --- NEW FILE: ClientNode.java --- /* * Created on 25.11.2004 */ package net.sf.magicmap.server.model.node; import java.util.ArrayList; /** * Ein Client sieht Access Points und ist dabei in der Regel * frei beweglich. * @see net.sf.magicmap.client.models#AccessPointSeerNode * @author thuebner */ public class ClientNode extends AccessPointSeerNode { public String macAddress; /** * @param model */ public ClientNode(NodeModel model) { super(model); this.macAddress = "00-00-00-00-00-00"; } public ArrayList getLocations(){ ArrayList locations = getModel().getLocationsWithAtLeastOneAccessPoint(super.getNeighbors()); return locations; } public ArrayList getNeighbors(){ ArrayList result = super.getNeighbors(); // Alle Locations, die mind. einen AccessPoint gemeinsam haben mit // diesem Client ArrayList locations = getModel().getLocationsWithAtLeastOneAccessPoint(result); result.addAll(getLocations()); return result; } /* (non-Javadoc) * @see net.sf.magicmap.client.model.Node#getType() */ public int getType(){ // TODO Auto-generated method stub return NodeModel.NODETYPE_CLIENT; } public String getMacAddress(){ return macAddress; } public void setMacAddress(String macAddress){ this.macAddress = macAddress; } } --- NEW FILE: AccessPointSeerNode.java --- /* * Created on 25.11.2004 */ package net.sf.magicmap.server.model.node; import java.util.ArrayList; import java.util.Iterator; import net.sf.magicmap.server.measurement.Constants; /** * Ein Objekt was Access Points in einer Umgebung mit Signalstärken * sehen kann. * @author thuebner */ public abstract class AccessPointSeerNode extends Node { /* * Alle Kanten zu AccessPoints (AccessPointEdge) mit * entsprechenden Informationen über die Signalstärke * */ private ArrayList apEdges; public AccessPointSeerNode(NodeModel model) { super(model); this.apEdges = new ArrayList(); } public ArrayList getApEdges(){ return apEdges; } public ArrayList getSeenAccessPoints(){ ArrayList result = new ArrayList(); // Alle AccessPoints, die gesehen werden Iterator it = apEdges.iterator(); while (it.hasNext()){ AccessPointEdge ae = (AccessPointEdge) it.next(); result.add(ae.getAccessPoint()); } return result; } /** * Liefert Signalstärke eines AccessPoints zurück, die * vom AccessPointer-Seher gemessen wurde oder * net.sf.magicmap.client.measurement.Constants.MIN_SIGNALLEVEL wenn * AccessPoint nicht gesehen wird. * @param ap * @return */ public double getSignalLevelForAccessPoint(AccessPointNode ap){ Iterator it = apEdges.iterator(); while (it.hasNext()){ AccessPointEdge ae = (AccessPointEdge) it.next(); if (ae.getAccessPoint() == ap){ return ae.getSignalLevel(); } } return Constants.MIN_SIGNALLEVEL; } /** * Setzt / Aktualisiert die Signalstärke zu einem Access Point * @param ap * @param level */ public void setSignalLevelForAcessPoint(AccessPointNode ap, double level){ Iterator it = apEdges.iterator(); while (it.hasNext()){ AccessPointEdge ae = (AccessPointEdge) it.next(); if (ae.getAccessPoint() == ap){ ae.setSignalLevel(level); // Wichtig! this.getModel().fireSeesAccessPoint(this, ap); return; } } // Gibt es noch nicht AccessPointEdge ae = new AccessPointEdge(); ae.setAccessPoint(ap); ae.setSignalLevel(level); this.apEdges.add(ae); this.getModel().fireSeesAccessPoint(this, ap); } public void seesAccessPoint(AccessPointNode ap, double signalStrength){ if (!getSeenAccessPoints().contains(ap)){ AccessPointEdge ae = new AccessPointEdge(); ae.setAccessPoint(ap); ae.setSignalLevel(signalStrength); this.apEdges.add(ae); getModel().fireSeesAccessPoint(this, ap); } } public void notSeesAccessPoint(AccessPointNode ap){ Iterator it = apEdges.iterator(); while (it.hasNext()){ AccessPointEdge ae = (AccessPointEdge) it.next(); if (ae.getAccessPoint() == ap){ it.remove(); break; } } getModel().fireNotSeesAccessPoint(this, ap); } /** * Liefert alle Knoten zurück, zu denen eine gerichtete Kante * gezeichnet werden muß. Dies sind bei einen AccessPoint-Seher nur * die AccessPoints selbst. */ public ArrayList getNeighbors(){ return getSeenAccessPoints(); } } |