📄 simplenode.java
字号:
/** * simulator/SimpleNode.java */package implementations;import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator;import simulator.Contact;import simulator.Message;import simulator.Node;import simulator.Stats;import util.CommandStatus;import util.Verbose;/** * */public class SimpleNode extends Node{ private int copies = 1; private int msgDataLength = 10; private int bufferSize = 1; private int usedBufferSize = 0; private ArrayList<SimpleMessage> buffer = new ArrayList<SimpleMessage>(); private HashSet<Contact> idleContacts = new HashSet<Contact>(); public SimpleNode() { } public SimpleNode(SimpleNode org) { super(org); copies = org.copies; bufferSize = org.bufferSize; } public void setNumberOfCopies(int c) { copies = c; } public void setMaxBufferSize(int m) { bufferSize = m; } public void contactIdle(Contact contact) { super.contactIdle(contact); if (buffer.size() < 1) return; Iterator<SimpleMessage> it = buffer.iterator(); Node dN = contact.getDest(); while (it.hasNext()) { SimpleMessage msg = it.next(); // We don't send messages to Nodes which we know that already had // this message, // unless it is destination node, in which case we send it anyway. if (!msg.hasCopy(dN) && (msg.getCopiesToMake() > 1 || msg.getDestNode() == dN)) { SimpleMessage sm = new SimpleMessage(msg); if (network.vShouldLog(Verbose.DEBUG4)) network.vprint("SENDING: " + sm + " using " + contact + " From " + this + " with # of copies: " + sm.getCopiesToMake()); contact.sendMessage(sm); it.remove(); placeInBuffer(msg); return; } } idleContacts.add(contact); if (network.vShouldLog(Verbose.DEBUG4)) network.vprint("NOP - Nothing left to send in " + this); } public void contactDown(Contact contact) { super.contactDown(contact); idleContacts.remove(contact); } private void checkIdleContacts() { Iterator<Contact> it = idleContacts.iterator(); while (it.hasNext()) { Contact c = it.next(); it.remove(); contactIdle(c); } } private void placeInBuffer(SimpleMessage sm) { int s = buffer.size(); if (s < 1) { buffer.add(sm); return; } int i, c = sm.getCopiesToMake(); if (buffer.get(0).getCopiesToMake() < c) { buffer.add(0, sm); return; } for (i = 0; i < s; ++i) { if (buffer.get(i).getCopiesToMake() < c) { buffer.add(i, sm); return; } } buffer.add(sm); } protected boolean acceptMessage(Message msg, Contact contact, int size) { if (msg.getLength() > bufferSize || msg.getLength() < size) { return false; } SimpleMessage sm = (SimpleMessage) msg; Iterator<SimpleMessage> it = buffer.iterator(); while (it.hasNext()) { SimpleMessage sTmp = it.next(); if (sm.getId() == sTmp.getId()) { int oldCop = sTmp.getCopiesToMake(); // If we find the same message in our buffer, we add number of // copies from // received message sTmp.setCopiesToMake(sm.getCopiesToMake() + oldCop); // We add the Node which sent us the message to the list of // nodes which we // know to have this message sTmp.addNodeWithCopy(contact.getSource()); if (network.vShouldLog(Verbose.DEBUG3)) network.vprint("ALREADY PRESENT " + sm + " in " + this + "; # of copies: " + oldCop + " increased to " + sTmp.getCopiesToMake()); it.remove(); placeInBuffer(sTmp); return true; } } // We check that after trying to find that message in our own buffer - // we do not need to send this message to Node which sent us this // message, and above loop should mark that. if (sm.getCopiesToMake() < 1) { if (network.vShouldLog(Verbose.WARN)) network.vprint("DROP: " + sm + " Number of copies < 1 in " + this + " from " + contact); return false; } // New message, but we know that the node which sent us this message // already has it! sm.addNodeWithCopy(contact.getSource()); while (usedBufferSize + sm.getLength() > bufferSize) { int oldS = usedBufferSize; SimpleMessage sTmp = buffer.remove(buffer.size() - 1); usedBufferSize -= sTmp.getLength(); if (network.vShouldLog(Verbose.DEBUG2)) network.vprint("REMOVE: " + sTmp + " with # of copies: " + sTmp.getCopiesToMake() + " in " + this + "; Buffer: " + oldS + " -> " + usedBufferSize + "/" + bufferSize); } placeInBuffer(sm); usedBufferSize += sm.getLength(); if (network.vShouldLog(Verbose.DEBUG2)) network.vprint("BUFFERED: " + sm + " with # of copies: " + sm.getCopiesToMake() + " in " + this + "; buffer_size: " + usedBufferSize + "/" + bufferSize); return true; } public void messageDelivered(Message m, Contact c, int size) { // size is incorrect - other node doesn't realy have a copy of // this message (as we marked in messageSent) - so we have to // correct this. We have to increase number of copies as well! if (size < m.getLength()) { Iterator<SimpleMessage> it = buffer.iterator(); while (it.hasNext()) { SimpleMessage sTmp = it.next(); SimpleMessage sm = (SimpleMessage) m; if (sm.getId() == sTmp.getId()) { sTmp.removeNodeWithCopy(c.getDest()); sTmp.setCopiesToMake(sTmp.getCopiesToMake() + sm.getCopiesToMake()); checkIdleContacts(); return; } } } } public void messageSent(Message m, Contact c, int size) { if (size < m.getLength()) return; if (network.vShouldLog(Verbose.DEBUG3)) network.vprint(m + " sent through " + c + " from " + this); Iterator<SimpleMessage> it = buffer.iterator(); while (it.hasNext()) { SimpleMessage sTmp = it.next(); if (m.getId() == sTmp.getId()) { // We managed to send this message to the destination node // - we can remove it from our buffer! if (sTmp.getDestNode() == c.getDest()) { int oldS = usedBufferSize; usedBufferSize -= sTmp.getLength(); if (network.vShouldLog(Verbose.DEBUG2)) network.vprint("DELIVERED " + sTmp + " TO FINAL destination. Removing from buffer in " + this + "; buffer_size: " + oldS + " -> " + usedBufferSize + "/" + bufferSize); it.remove(); return; } // That was not final destination, although we want to mark, // that the Node already received this message. sTmp.addNodeWithCopy(c.getDest()); return; } } } public boolean sendNewMessage(int dataLength, Node destNode) { SimpleMessage msg = new SimpleMessage(network.getNextMessageId(), network.getCurrentTime(), this, destNode, dataLength); msg.setCopiesToMake(copies); if (msg.getLength() > bufferSize) { if (network.vShouldLog(Verbose.ERR)) network.vprint("CREATE_ABORTED: " + msg + "; Size: " + msg.getLength() + " > max_buffer_size: " + bufferSize + " in " + this); return false; } statMsgEntry.setup(msg, network.getCurrentTime(), Stats.MSG_CREATED); network.stats().notify(statMsgEntry); while (usedBufferSize + msg.getLength() > bufferSize) { int oldS = usedBufferSize; SimpleMessage sTmp = buffer.remove(buffer.size() - 1); usedBufferSize -= sTmp.getLength(); if (network.vShouldLog(Verbose.DEBUG2)) network.vprint("REMOVE: " + sTmp + " with # of copies: " + sTmp.getCopiesToMake() + " in " + this + "; Buffer: " + oldS + " -> " + usedBufferSize + "/" + bufferSize); } placeInBuffer(msg); usedBufferSize += msg.getLength(); if (network.vShouldLog(Verbose.DEBUG2)) network.vprint("BUFFERED: " + msg + " with # of copies: " + msg.getCopiesToMake() + " in " + this + "; buffer_size: " + usedBufferSize + "/" + bufferSize); return true; } public boolean willAcceptMessageSize(int len) { if (len > bufferSize) return false; return true; } public CommandStatus parseCommandPart(ArrayList<String> part, String path) { String param = part.get(0); CommandStatus ok = super.parseCommandPart(part, path); if (ok != null) return ok; ok = new CommandStatus(CommandStatus.COMMAND_OK); if (part.size() != 2) return null; if (param.equals("send")) { sendNewMessage(msgDataLength, network.getNode(part.get(1))); return ok; } int val; try { if (param.equals("copies")) { val = Integer.parseInt(part.get(1)); if (val < 1) { return new CommandStatus("Number of max message hops can not be < 1!"); } copies = val; return ok; } else if (param.equals("buffer_size")) { val = Integer.parseInt(part.get(1)); if (val < 1) { return new CommandStatus("Max buffer size can not be < 1!"); } bufferSize = val; return ok; } else if (param.equals("msg_size")) { val = Integer.parseInt(part.get(1)); if (val < 1) { return new CommandStatus("Message Size can not be < 1!"); } msgDataLength = val; return ok; } } catch (NumberFormatException e) { return new CommandStatus("Error parsing value of parameter '" + param + "': " + e); } return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -