public override void handlePacket(IPAddress address, JSONPacket inPacket, bool updateUI) { base.handlePacket(address, inPacket, updateUI); if (!inPacket.getParameter("type").StartsWith("pantau/")) { foreach (IPAddress watcher in watchers) { parent.writeLog(LogLevel.Info, "Kirim ke pemantau " + watcher + " pesan " + inPacket); communication.send(watcher, inPacket, UDPCommunication.IN_PORT); } } }
private void listenWatchConfirmation() { UdpClient client = null; try { client = new UdpClient(IN_PORT); client.Client.ReceiveTimeout = 1000; IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); while (true) { try { byte[] receivedBytes = client.Receive(ref endPoint); parent.writeLog(LogLevel.Info, "Terima dari " + endPoint + ": " + Encoding.ASCII.GetString(receivedBytes)); JSONPacket inPacket = JSONPacket.createFromJSONBytes(receivedBytes); if (inPacket.getParameter("type").Equals("pantau/confirm")) { if (inPacket.getParameter("status").Equals("ok") && inPacket.getParameter("gameid").Equals(controller.gameId)) { String gameId = inPacket.getParameter("gameid"); int ammo = Int32.Parse(inPacket.getParameter("ammo")); client.Close(); this.controller.startRegistration(gameId, ammo); } else { client.Close(); parent.showError("Pantau ditolak: " + inPacket.getParameter("status")); parent.setWatchingEnabled(false); } return; } else { parent.writeLog(LogLevel.Warn, "Paket diabaikan karena belum memantau: " + inPacket); } } catch (SocketException) { // void } catch (ThreadAbortException) { client.Close(); return; } } } catch (ThreadAbortException) { } catch (Exception e) { parent.writeLog(LogLevel.Error, "Error: " + e); } }
public virtual void handlePacket(IPAddress address, JSONPacket inPacket, bool updateUI) { try { this.recorder.record(address, inPacket.ToString()); String type = inPacket.getParameter("type"); if (type.Equals("register")) { if (inPacket.getParameter("gameid").Equals(gameId)) { if (state == State.REGISTRATION) { int nomerUrut = Prajurit.findPrajuritIndexByNomerInduk(prajurits, inPacket.getParameter("nomerInduk")); if (nomerUrut == -1) { // Register nomerUrut = prajurits.Count + 1; Prajurit newPrajurit = new Prajurit(nomerUrut, inPacket.getParameter("nomerInduk"), address, "A", null); prajuritDatabase.retrieveNameFromDatabase(newPrajurit); prajurits.Add(newPrajurit); if (updateUI) { parent.refreshTable(); } } else { nomerUrut = nomerUrut + 1; } // Confirm JSONPacket outPacket = new JSONPacket("confirm"); outPacket.setParameter("androidId", "" + nomerUrut); communication.send(address, outPacket); } else { parent.writeLog(LogLevel.Warn, "Registration from " + address + " is ignored as we are not in registration phase"); } } else { parent.writeLog(LogLevel.Warn, "Registration from " + address + " with game id " + inPacket.getParameter("gameid") + " is ignored"); } } else if (type.Equals("event/update")) { if (inPacket.getParameter("gameid").Equals(gameId)) { if (state != State.IDLE) { int index = Int32.Parse(inPacket.getParameter("androidId")) - 1; Prajurit prajurit = prajurits[index]; prajurit.setLocation(inPacket.getParameter("location")); prajurit.heading = Double.Parse(inPacket.getParameter("heading")); prajurit.accuracy = Int32.Parse(inPacket.getParameter("accuracy")); string[] prajuritState = inPacket.getParameter("state").Split('/'); prajurit.state = (prajuritState[0].Equals("alive") ? Prajurit.State.NORMAL : Prajurit.State.DEAD); prajurit.posture = (prajuritState[1].Equals("stand") ? Prajurit.Posture.STAND : Prajurit.Posture.CRAWL); if (inPacket.getParameter("action").Equals("hit")) { int idSenjata = Int32.Parse(inPacket.getParameter("idsenjata")); int counter = Int32.Parse(inPacket.getParameter("counter")); if (state == State.REGISTRATION) { // Registration phase, senjata assign to prajurit. Senjata newSenjata = new Senjata(idSenjata, prajurit, counter, this.initialAmmo); prajurit.senjata = newSenjata; senjatas.Add(newSenjata.idSenjata, newSenjata); } else if (state == State.EXERCISE) { Senjata senjataPenembak = senjatas[idSenjata]; senjataPenembak.currentCounter = counter; if (senjataPenembak.getRemainingAmmo() > 0) { prajurit.state = Prajurit.State.HIT; communication.send(prajurit.ipAddress, new JSONPacket("killed")); } } parent.refreshTable(); } else if (inPacket.getParameter("action").Equals("shoot")) { prajurit.state = Prajurit.State.SHOOT; } if (updateUI) { parent.mapDrawer.updateMap(prajurit); parent.refreshTable(); } } else { parent.writeLog(LogLevel.Warn, "Update event is ignored when state is idle."); } } else { parent.writeLog(LogLevel.Warn, "Event update from " + address + " with game id " + inPacket.getParameter("gameid") + " is ignored"); } } else if (type.Equals("ping")) { // Reply with pong, and let the rest of parameters be the same. inPacket.setParameter("type", "pong"); communication.send(address, inPacket); } else if (type.Equals("pantau/register")) { if (inPacket.getParameter("gameid").Equals(gameId)) { JSONPacket outPacket = new JSONPacket("pantau/confirm"); if (state == State.REGISTRATION && prajurits.Count == 0) { watchers.Add(address); outPacket.setParameter("status", "ok"); outPacket.setParameter("gameid", gameId); outPacket.setParameter("ammo", "" + initialAmmo); } else { outPacket.setParameter("status", "Tidak bisa pantau karena sudah ada prajurit yang bergabung atau latihan sudah dimulai."); } communication.send(address, outPacket, UDPCommunication.IN_PORT); parent.writeLog(LogLevel.Info, address + " joined as watcher"); } else { parent.writeLog(LogLevel.Warn, "Watch request from " + address + " is ignored, as the game id " + inPacket.getParameter("gameid") + " mismatched"); } } else if (type.Equals("pantau/state")) { if (inPacket.getParameter("state").StartsWith("START")) { string[] tokens = inPacket.getParameter("state").Split('/'); for (int i = 0; i < tokens[1].Length; i++) { prajurits[i].group = "" + tokens[1][i]; } startExercise(); } else if (inPacket.getParameter("state").Equals("STOP")) { stopExercise(false); } else { parent.writeLog(LogLevel.Error, "Unknown state set: " + inPacket.getParameter("state")); } } else { parent.writeLog(LogLevel.Error, "Unknown type: " + type); } } catch (Exception e) { parent.writeLog(LogLevel.Error, "Unhandled exception: " + e); } }