private void handleResquest(String s) { Logger.info("@ClientMediator.handleResquest id=" + _id + " :s=" + s); if (s.indexOf("sync") > -1) { tellClient("sync " + s.split(" ")[1], 0); } else if (s.indexOf("get-status") > -1) { Integer[] ts = (Integer[]) tankStateProxy().getTanks(); Logger.debug("ClientMediator.handleResquest: ts.len=" + ts.length); for (int i = 0; i < ts.length; i++) { ClientMediator cm = (ClientMediator) appFacade().retrieveMediator(Const.CLIENT_MEDIATOR + ts[i].intValue()); TankState t = cm.getState(); if (t == null || cm.destroyd) continue; tellClient( "create tank " + t.tankID + " " + t.tankName + " 0 0 " + t.life + " 500 " + t.x + " " + t.y + " " + cm.getTankAngle() + " " + cm.getGunAngle(), 0); } } else if (s.indexOf("born") > -1) { if (_state == null) Arena.addReq(_id, s, null); } else if (s.indexOf("move-to") > -1) { if (_state != null) { Arena.addReq(_id, s, getState()); } } else if (s.indexOf("rotate-to") > -1) { // rotate gun if (_state != null) { Arena.addReq(_id, s, getState()); } } else if (s.indexOf("shoot") > -1) { if (_state != null) { Arena.addReq(_id, s, getState()); } } else if (s.indexOf("talk") > -1) { if (_state != null) s = "talk " + _state.tankName + " say: " + s.substring(5); this.sendNotification(Const.BROAD_CAST, s, null); } }
public void stop() { _thread.stop(); try { _socket.close(); } catch (Exception e) { Logger.error("@ClientMediator.stop error"); } // kill tank }
public void toberun() { if (_writer == null || _reader == null) { Logger.error("@ClientMediator.toberun: _writer or _reader null"); return; } String s; while (true) { s = null; try { s = _reader.readLine(); } catch (Exception e) { Logger.error("@ClientMediator.toberun: _read error, excep is " + e.toString()); break; } if (s != null && s.length() > 0) { handleResquest(new String(s)); } } Arena.addReq(_id, Const.LOSE_CONNECTION, null); }
public void handleNotification(INotification notification) { // Logger.debug("@ClientMediator.handleNotification:noti="+notification.getBody()); String noname = notification.getName(); if (noname == Const.TICK) { if (destroyd) return; long t = TimerCmd.timeStamp; String[] ary; int st, ed; if (_lastRotTank != null) { ary = _lastRotTank.split(" "); st = Integer.parseInt(ary[0]); ed = st + Integer.parseInt(ary[6]); if (t <= ed && t >= st) { int ist = Integer.parseInt(ary[0]); if (_move.size() > 0) { st = Integer.parseInt(_move.peek().split(" ")[0]); while (st < ist && _move.size() > 0) { _move.poll(); if (_move.size() == 0) break; st = Integer.parseInt(_move.peek().split(" ")[0]); } } } } if (_move.size() > 0) { ary = _move.peek().split(" "); st = Integer.parseInt(ary[0]); ed = st + Integer.parseInt(ary[8]); while ((t > ed) && _move.size() > 0) { // Logger.debug("@TankMediator.handleNotification: t = " + t + " st = " + st + " // ed="+ed); String p = (String) _move.poll(); Logger.debug("ClientMediator.TICK _move.poll=" + p); if (_move.size() == 0) break; ary = _move.peek().split(" "); st = Integer.parseInt(ary[0]); ed = st + Integer.parseInt(ary[8]); } if (_move.size() > 0) { String[] li = (String[]) _move.toArray(new String[0]); for (int i = li.length - 1; i >= 0; i--) { String s = li[i]; ary = s.split(" "); st = Integer.parseInt(ary[0]); ed = st + Integer.parseInt(ary[8]); if (t <= ed && t >= st) { if (t == st) { // ed==st in this situation _state.x = (int) Float.parseFloat(ary[4]); _state.y = (int) Float.parseFloat(ary[5]); _nextX = _state.x; _nextY = _state.y; } else { float sx = Float.parseFloat(ary[4]); float ex = Float.parseFloat(ary[6]); float sy = Float.parseFloat(ary[5]); float ey = Float.parseFloat(ary[7]); double eco = 1.0 * (t - st) / (ed - st); _state.x = (int) (sx + (ex - sx) * eco); _state.y = (int) (sy + (ey - sy) * eco); double neco = 1.0 * (t + 1 - st) / (ed - st); _nextX = (sx + (ex - sx) * neco); _nextY = (sy + (ey - sy) * neco); } while (_move.size() > 1) _move.poll(); break; } } } } if (_state != null) { ConcurrentHashMap<Integer, BulletState> bs = ((ServerBulletMediator) (appFacade().retrieveMediator(Const.SERVER_BULLET_MEDIATOR))) .getBullets(); for (Enumeration enu = bs.keys(); enu.hasMoreElements(); ) { int id = (Integer) enu.nextElement(); BulletState blt = bs.get(id); if (Texter.distance(blt.x, blt.y, _state.x, _state.y) < 16) { this.sendNotification(Const.BROAD_CAST, "destroy bullet " + id, null); _state.life -= 100; if (_state.life <= 0) { this.sendNotification(Const.BROAD_CAST, "destroy tank " + _state.tankID, null); } else { this.sendNotification( Const.BROAD_CAST, "set-life tank " + _state.tankID + " " + _state.life, null); } } } } // barrier and tank if (_state != null) { // colision detection // Logger.only("ClientMediator.handleNotification: x="+_state.x+" y="+_state.y+" // nx="+_nextX+" ny="+_nextY+" "+Texter.sqrareDis(_nextX, _nextY, 470, 200)+" // "+Texter.sqrareDis(_state.x, _state.y, 470, 200)); if (!Arena.canGo(_state.x, _state.y, _nextX, _nextY)) { Arena.addReq(_id, Const.STOP_MOVE, getState()); } for (Enumeration enu = AcceptCmd.clients.elements(); enu.hasMoreElements(); ) { ClientMediator cmm = (ClientMediator) enu.nextElement(); if (cmm.destroyd) return; int cmid = cmm.getID(); if (cmid == _id) continue; TankState ts = cmm.getState(); if (ts == null) continue; double curdis = Texter.distance(ts.x, ts.y, _state.x, _state.y); double netdis = Texter.distance(ts.x, ts.y, _nextX, _nextY); if (curdis < 40 && netdis < curdis) { Arena.addReq(_id, Const.STOP_MOVE, getState()); Arena.addReq(cmid, Const.STOP_MOVE, ts); } } } // handle delayed first // ary=_delayOrder.toArray(new String[0]); // _delayOrder.clear(); // for(int i=0;i<ary.length;i++){ // Logger.only("delayArray: "+ary[i]); // int ttt=Integer.parseInt(ary[i].split(" ")[0]); // if(ttt==t){ // tellClientWithTime(ary[i]); // }else if(ttt>t){ // _delayOrder.add(ary[i]); // } // } } else if (noname == Const.BROAD_CAST) { String s = (String) notification.getBody(); String ary[] = s.split(" "); if (notification.getType() == Const.NO_PREFIX) { tellClientWithTime(s); return; } else if (s.indexOf("talk") > -1) { tellClient(s, 0); return; } if (_id == Integer.parseInt(ary[2])) { if (s.indexOf("rotate-gun") > -1) { _lastRotateGun = TimerCmd.timeStamp + " " + s; if (_state != null) _state.gunAngle = Float.parseFloat(_lastRotateGun.split(" ")[5]); } else if (s.indexOf("move tank") > -1) { Logger.debug("ClientMediator: move add s=" + s); int offset = Integer.parseInt(notification.getType()); if (offset == 0) _move.clear(); _move.add((TimerCmd.timeStamp + offset) + " " + s); } else if (s.indexOf("create tank") > -1) { _state = new TankState( _id, ary[3], Integer.parseInt(ary[6]), (int) Float.parseFloat(ary[8]), (int) Float.parseFloat(ary[9]), Float.parseFloat(ary[10]), Float.parseFloat(ary[11])); _state.printState(); } else if (s.indexOf("rotate-tank") > -1) { _lastRotTank = TimerCmd.timeStamp + " " + s; if (_state != null) _state.tankAngle = Float.parseFloat(_lastRotTank.split(" ")[5]); } else if (s.indexOf("destroy tank") > -1) { this.destroyd = true; } } if (notification.getType() != null) tellClient(s, Integer.parseInt(notification.getType())); else tellClient(s, 0); } }
public class JungGraphObserver implements Control, HyphaDataListener, HyphaLinkListener { private static final String PAR_HYPHADATA_PROTO = "network.node.hyphadata_proto"; private static final String PAR_HYPHALINK_PROTO = "network.node.hyphalink_proto"; private static final String PAR_MYCOCAST_PROTO = "network.node.mycocast_proto"; private static final String PAR_WALK_DELAY = "walk_delay"; private static Logger log = Logger.getLogger(JungGraphObserver.class.getName()); private static final String PAR_PERIOD = "period"; private static int period; private static int walkDelay; private final String name; private final int hyphadataPid; private final int hyphalinkPid; private final int mycocastPid; // private static Lock = new ReentrantLock(); private static MycoGraph graph = new MycoGraph(); private static VisualizationViewer<MycoNode, MycoEdge> visualizer; private static Set<ChangeListener> changeListeners; // private class TypePredicate extends Predicate<{ // } public static void addChangeListener(ChangeListener cl) { changeListeners.add(cl); } public static void removeChangeListener(ChangeListener cl) { if (changeListeners.contains(cl)) { changeListeners.remove(cl); } } private static Thread mainThread; public static boolean stepBlocked = false; public static boolean noBlock = true; public static boolean walking = false; public JungGraphObserver(String name) { this.name = name; this.hyphadataPid = Configuration.getPid(PAR_HYPHADATA_PROTO); this.hyphalinkPid = Configuration.getPid(PAR_HYPHALINK_PROTO); this.mycocastPid = Configuration.getPid(PAR_MYCOCAST_PROTO); this.period = Configuration.getInt(name + "." + PAR_PERIOD); this.walkDelay = Configuration.getInt(name + "." + PAR_WALK_DELAY); mainThread = Thread.currentThread(); this.changeListeners = new HashSet<ChangeListener>(); visualizer = null; // HyphaData.addHyphaDataListener(this); // HyphaLink.addHyphaLinkListener(this); } public static void setVisualizer(VisualizationViewer<MycoNode, MycoEdge> visualizer) { JungGraphObserver.visualizer = visualizer; addChangeListener(visualizer); } public static MycoGraph getGraph() { return graph; } public void nodeStateChanged(MycoNode n, HyphaType t, HyphaType oldState) { if (t != HyphaType.DEAD) { if (!graph.containsVertex(n)) { graph.addVertex(n); } } else { if (graph.containsVertex(n)) { graph.removeVertex(n); } } } public void linkAdded(MycoNode a, MycoNode b) { if (graph.findEdge(a, b) == null) { MycoEdge edge = new MycoEdge(); graph.addEdge(edge, a, b, EdgeType.DIRECTED); } } public void linkRemoved(MycoNode a, MycoNode b) { MycoEdge edge = graph.findEdge(a, b); while (edge != null) { graph.removeEdge(edge); edge = graph.findEdge(a, b); } } public boolean execute() { if (CDState.getCycle() % period != 0) return false; MycoCast mycocast = (MycoCast) Network.get(0).getProtocol(mycocastPid); int bio = mycocast.countBiomass(); int ext = mycocast.countExtending(); int bra = mycocast.countBranching(); int imm = mycocast.countImmobile(); // Update vertices Set<MycoNode> activeNodes = new HashSet<MycoNode>(); for (int i = 0; i < Network.size(); i++) { MycoNode n = (MycoNode) Network.get(i); activeNodes.add(n); HyphaData data = n.getHyphaData(); // if (data.isBiomass()) { continue; } if (graph.containsVertex(n)) { graph.removeVertex(n); } if (!graph.containsVertex(n)) { graph.addVertex(n); } } Set<MycoNode> jungNodes = new HashSet<MycoNode>(graph.getVertices()); jungNodes.removeAll(activeNodes); for (MycoNode n : jungNodes) { graph.removeVertex(n); } // Update edges for (int i = 0; i < Network.size(); i++) { MycoNode n = (MycoNode) Network.get(i); HyphaData data = n.getHyphaData(); HyphaLink link = n.getHyphaLink(); synchronized (graph) { // We now add in all links and tune out display in Visualizer java.util.List<MycoNode> neighbors = (java.util.List<MycoNode>) link.getNeighbors(); //// Adding only links to hypha thins out links to biomass // (java.util.List<MycoNode>) link.getHyphae(); Collection<MycoNode> jungNeighbors = graph.getNeighbors(n); // Remove edges from Jung graph that are not in peersim graph for (MycoNode o : jungNeighbors) { if (!neighbors.contains(o)) { MycoEdge edge = graph.findEdge(n, o); while (edge != null) { graph.removeEdge(edge); edge = graph.findEdge(n, o); } } } // Add missing edges to Jung graph that are in peersim graph for (MycoNode o : neighbors) { if (graph.findEdge(n, o) == null) { MycoEdge edge = new MycoEdge(); graph.addEdge(edge, n, o, EdgeType.DIRECTED); } } } // log.finest("VERTICES: " + graph.getVertices()); // log.finest("EDGES: " + graph.getEdges()); } for (ChangeListener cl : changeListeners) { cl.stateChanged(new ChangeEvent(graph)); } if (walking) { try { Thread.sleep(walkDelay); } catch (InterruptedException e) { } stepBlocked = false; } try { while (stepBlocked && !noBlock) { synchronized (JungGraphObserver.class) { JungGraphObserver.class.wait(); } } } catch (InterruptedException e) { stepBlocked = true; } stepBlocked = true; // System.out.println(graph.toString()); return false; } public static synchronized void stepAction() { walking = false; stepBlocked = false; noBlock = false; JungGraphObserver.class.notifyAll(); } public static synchronized void walkAction() { walking = true; stepBlocked = false; noBlock = false; JungGraphObserver.class.notifyAll(); } public static synchronized void pauseAction() { walking = false; stepBlocked = true; noBlock = false; } public static synchronized void runAction() { walking = false; stepBlocked = false; noBlock = true; JungGraphObserver.class.notifyAll(); } }