📄 basemessagemanager.java
字号:
package MRL.Utilities.MessageManagement;import yab.agent.Condition;import yab.agent.DisasterSpace;import yab.agent.object.*;import yab.io.RCRSSProtocolSocket;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.*;/** * Message Manager for SBCe_Saviour 2006 * by Maryam Kalantary * **/public abstract class BaseMessageManager implements MessageConstants{ private int pointer; private RCRSSProtocolSocket socket; private int selfId; private int messageCount; protected DisasterSpace world; public ArrayList reportedVictims; public ArrayList reportedFires; public ArrayList reportedClears; public ArrayList reportedPositions; public ArrayList reportedUnClears; public ArrayList reportedBlocks; public ArrayList reportedEmptyBuildings; public ArrayList reportedHeardCivilians; public ArrayList reportedLockedByBlockades; public ArrayList reportedRequestPartitionChangings; public ArrayList reportedPartitionFinisheds; public ArrayList reportedSetPartitions; public ArrayList reportedClearedPaths; public ArrayList reportedTargets; public ArrayList reportedRequestTargets; public ArrayList reportedTasks; public ArrayList reportedFinishedTasks; public ArrayList centerReportedVictims; public ArrayList centerReportedFires; public ArrayList centerReportedClears; public ArrayList centerReportedPositions; public ArrayList centerReportedUnClears; public ArrayList centerReportedBlocks; public ArrayList centerReportedEmptyBuidings; public ArrayList centerReportedHeardCivilians; public ArrayList centerReportedLockedByBlockades; public ArrayList centerReportedRequestPartitionChangings; public ArrayList centerReportedPartitionFinisheds; public ArrayList centerReportedSetPartitions; public ArrayList centerReportedClearedPaths; public ArrayList centerReportedTargets; public ArrayList centerReportedRequestTargets; public ArrayList centerReportedTasks; public ArrayList centerReportedFinishedTasks; protected Map idToVirtualId; protected Map virtualIdToId; protected HashSet messagesToCenters; protected HashSet messagesToAgents; protected FileWriter fw; protected BaseMessageManager(RCRSSProtocolSocket socket, DisasterSpace world) { try { File file = new File("/logs/mrl/message"); file.mkdir(); File f = new File("/logs/mrl/message/" + world.self.id + ".msm"); if (f.exists()) { f.delete(); } fw = new FileWriter("/logs/mrl/message/" + world.self.id + ".msm"); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } this.socket = socket; this.selfId = world.self.id; this.messageCount = world.get(selfId) instanceof MovingObject ? AGENT_MSG_COUNT : STATION_MSG_COUNT; this.world = world; idToVirtualId = new HashMap(); virtualIdToId = new HashMap(); int virtualId = 0;// Collections.sort(world.motionlessObjects, yab.agent.Util.idComparator); for (Iterator i = world.motionlessObjects.iterator(); i.hasNext(); virtualId++) { MotionlessObject realObject = (MotionlessObject) i.next(); idToVirtualId.put(new Integer(realObject.id), new Integer(virtualId)); virtualIdToId.put(new Integer(virtualId), new Integer(realObject.id)); } reportedVictims = new ArrayList(); reportedClears = new ArrayList(); reportedFires = new ArrayList(); reportedPositions = new ArrayList(); reportedUnClears = new ArrayList(); reportedBlocks = new ArrayList(); reportedEmptyBuildings = new ArrayList(); reportedHeardCivilians = new ArrayList(); reportedLockedByBlockades = new ArrayList(); reportedRequestPartitionChangings = new ArrayList(); reportedPartitionFinisheds = new ArrayList(); reportedSetPartitions = new ArrayList(); reportedClearedPaths = new ArrayList(); reportedTargets = new ArrayList(); reportedRequestTargets = new ArrayList(); reportedTasks = new ArrayList(); reportedFinishedTasks = new ArrayList(); centerReportedVictims = new ArrayList(); centerReportedClears = new ArrayList(); centerReportedFires = new ArrayList(); centerReportedPositions = new ArrayList(); centerReportedUnClears = new ArrayList(); centerReportedBlocks = new ArrayList(); centerReportedEmptyBuidings = new ArrayList(); centerReportedHeardCivilians = new ArrayList(); centerReportedLockedByBlockades = new ArrayList(); centerReportedRequestPartitionChangings = new ArrayList(); centerReportedPartitionFinisheds = new ArrayList(); centerReportedSetPartitions = new ArrayList(); centerReportedClearedPaths = new ArrayList(); centerReportedTargets = new ArrayList(); centerReportedRequestTargets = new ArrayList(); centerReportedTasks = new ArrayList(); centerReportedFinishedTasks = new ArrayList(); messagesToCenters = new HashSet(); messagesToAgents = new HashSet(); } /** * This method can be called only once per cycle! * otherwise consult your psychiretrist */ protected void clearForNextCycle() { reportedVictims.clear(); reportedClears.clear(); reportedFires.clear(); reportedPositions.clear(); reportedUnClears.clear(); reportedBlocks.clear(); reportedEmptyBuildings.clear(); reportedHeardCivilians.clear(); reportedLockedByBlockades.clear(); reportedRequestPartitionChangings.clear(); reportedPartitionFinisheds.clear(); reportedSetPartitions.clear(); reportedClearedPaths.clear(); reportedTargets.clear(); reportedRequestTargets.clear(); reportedTasks.clear(); reportedFinishedTasks.clear(); centerReportedVictims.clear(); centerReportedClears.clear(); centerReportedFires.clear(); centerReportedPositions.clear(); centerReportedUnClears.clear(); centerReportedBlocks.clear(); centerReportedEmptyBuidings.clear(); centerReportedHeardCivilians.clear(); centerReportedLockedByBlockades.clear(); centerReportedRequestPartitionChangings.clear(); centerReportedPartitionFinisheds.clear(); centerReportedSetPartitions.clear(); centerReportedClearedPaths.clear(); centerReportedTargets.clear(); centerReportedRequestTargets.clear(); centerReportedTasks.clear(); centerReportedFinishedTasks.clear(); } protected void sendMessages(int tempCount, Collection messages) { clearForNextCycle(); if (tempCount == MAX_MESSAGE_COUNT) { log("\nEXCEEDED MAX_MESSAGE_COUNT in send messages\n"); return; } log("\n ----- TIME = " + world.self.time() + " send message " + tempCount + " messages.size = " + messages.size() + " id = " + selfId + " "); char[] message = new char[MAX_LENGTH * 8]; pointer = 0; //where to write in char [] message //if (messages.isEmpty()) return; addMessage(Type.POSITION_REPORT, message, messages); addMessage(Type.LOCKEDBYBLOCKADE_REPORT, message, messages); addMessage(Type.CIVILIAN_REPORT, message, messages); addMessage(Type.TARGET_REPORT, message, messages); addMessage(Type.TASK_REPORT, message, messages); addMessage(Type.REQUESTTARGET_REPORT,message,messages); addMessage(Type.TASK_FINISHED_REPORT,message,messages); addMessage(Type.FIRE_REPORT, message, messages); addMessage(Type.BLOCK_REPORT, message, messages); addMessage(Type.UNCLEAR_REPORT, message, messages); addMessage(Type.CLEAR_REPORT, message, messages); addMessage(Type.CLEAREDPATH_REPORT, message, messages); addMessage(Type.SETPARTITION_REPORT, message, messages); addMessage(Type.PARTITIONFINISHED_REPORT, message, messages); addMessage(Type.REQUESTPARTITIONCHANGING_REPORT, message, messages); addMessage(Type.EMPTYBUILDING_REPORT, message, messages); addMessage(Type.HEARDCIVILIAN_REPORT, message, messages); send(message); if (messages.size() > 0 && ++tempCount < messageCount) { sendMessages(tempCount, messages); } //log (" end " + tempCount + " messages.size = " + messages.size ()); } private void addMessage(Type type, char[] message, Collection messages) { final Type t = type; Condition c = new Condition() { public boolean eval(Object obj) { if (((Message) obj).type == t) return true; return false; } }; ArrayList temp = c.extractMAR(messages); int count = 0; //tedade message hayi ke khahad ferestad. if (!temp.isEmpty()) { int headerPos = pointer; //jayi ke bayad header va length ezafe beshan ro nigah midare. pointer += HEADER_SIZE + LENGTH_SIZE; // baraye inke badan biad header va length ro benevise ja mizare. for (Iterator i = temp.iterator(); i.hasNext();) { Message m = (Message) i.next(); if (MAX_LENGTH * 8 - pointer - HEADER_SIZE >= m.length()) //ja baraye m hast { count++; i.remove();//msg e ferestade shode ro hazf mikone Util.copyArray(m.toCharArray(idToVirtualId), message, 0, pointer, m.length()); pointer += m.length(); } else { //msg haye ferestade nashode hanooz too list hastan. messages.addAll(temp); log("\n::MM- Message Full MM::\n "); break; } } if (count > 0)//hade aghal ye msg zade { Util.copyArray(Util.intToCharArray(type.intValue(), HEADER_SIZE), message, 0, headerPos, HEADER_SIZE); Util.copyArray(Util.intToCharArray(count, LENGTH_SIZE), message, 0, headerPos + HEADER_SIZE, LENGTH_SIZE); } else { pointer = headerPos; } } log("(" + count + " " + t.toString() + ")"); } private void send(char[] messageArray) { // log("\n" + new String(messageArray) + "\n"); Util.copyArray(Util.intToCharArray(HEADER_NULL, HEADER_SIZE), messageArray, 0, pointer, HEADER_SIZE); byte[] messageBytes = new byte[MAX_LENGTH]; for (int i = 0; i < MAX_LENGTH; i++) { byte b = 0; for (int j = 0; j < 8; j++) { b = (byte) (b << 1); if (messageArray[i * 8 + j] == '1') //b += (int) Math.pow (2, 7 - j); { b++; } } messageBytes[i] = b; } socket.akTell(selfId, messageBytes); } private char[] byteArrayToCharArray(byte[] messageBytes) { char[] messageArray = new char[MAX_LENGTH * 8]; for (int i = 0; i < 8 * MAX_LENGTH; i++) { messageArray[i] = '0'; } for (int i = 0; i < messageBytes.length; i++) { byte b = messageBytes[i]; for (int j = 7; j >= 0; j--) { messageArray[i * 8 + j] = (b & 1) == 1 ? '1' : '0'; b = (byte) (b >> 1); } } // log("\n" + new String(messageArray) + "\n"); return messageArray; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -