PacketExportManager.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.Collection;
  7. import java.util.LinkedList;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.PacketComparator;
  9. /**
  10. * Manager which handles the exports of packets during the simulation
  11. *
  12. * @author Andreas T. Meyer-Berg
  13. */
  14. public class PacketExportManager {
  15. /**
  16. * Model
  17. */
  18. private Model model;
  19. /**
  20. * True if packets should be printed
  21. */
  22. private boolean printPackets = false;
  23. /**
  24. * Writer to write the packets to a file
  25. */
  26. private BufferedWriter writer;
  27. /**
  28. * Base File for the exported Packages
  29. */
  30. private File exportFile = new File("testPackets.log");
  31. /**
  32. * Split Links into differentFiles
  33. */
  34. private boolean splitLinkExportFiles = false;
  35. /**
  36. * Creates a new PacketExportManager
  37. * @param model main model, which links should be exported
  38. */
  39. public PacketExportManager(Model model) {
  40. this.model = model;
  41. }
  42. /**
  43. * Exports the packets which were sent during the last time step according
  44. * to the settings (whether packages should be printed, specified export
  45. * File, splitLinks setting etc.)
  46. */
  47. public void exportPacketsOfLastTimeStep() {
  48. if (printPackets) {
  49. if (!splitLinkExportFiles) {
  50. /**
  51. * Packets of all links, merged together
  52. */
  53. LinkedList<Packet> packets = new LinkedList<Packet>();
  54. model.getConnectionNetworks().forEach(a->packets.addAll(a.getPackets()));
  55. packets.sort(new PacketComparator());
  56. writePacketsToFile(packets, exportFile);
  57. }else{
  58. for(Link link:model.getConnectionNetworks()){
  59. /**
  60. * File name:
  61. * test.log
  62. * to
  63. * test_linkName.log
  64. */
  65. String fileName = exportFile.getName();
  66. //Add linkName
  67. if(fileName.contains(".")){
  68. String baseName = fileName.substring(0, fileName.lastIndexOf('.'));
  69. String ending = fileName.substring(fileName.lastIndexOf('.')+1, fileName.length());
  70. fileName = baseName +"_"+link.getName()+"."+ending;
  71. }else{
  72. fileName = fileName +"_"+link.getName();
  73. }
  74. /**
  75. * Path to parent directory
  76. */
  77. String parent = exportFile.getParent();
  78. /**
  79. * File with the fileName
  80. */
  81. File linkExportFile = new File((parent == null?"":parent)+fileName);
  82. /**
  83. * Write Packets
  84. */
  85. writePacketsToFile(link.getPackets(), linkExportFile);
  86. }
  87. }
  88. }
  89. }
  90. /**
  91. * Writes all packets of the given Collection to the specified file
  92. * @param packets packets which should be written
  93. * @param file file the packets should be written to
  94. */
  95. private void writePacketsToFile(Collection<Packet> packets, File file){
  96. if(file == null || packets == null)
  97. return;
  98. try {
  99. //Check if file exists, if not create
  100. file.createNewFile();
  101. //Start Writing
  102. writer = new BufferedWriter(new FileWriter(file.getAbsolutePath(), true));
  103. //Write all packets
  104. packets.forEach(p -> {
  105. try {
  106. writer.append(p.getTextualRepresentation() + "\n");
  107. } catch (Exception e) {
  108. System.err.println("Warning: Error on exporting packets: "+e.toString());
  109. }
  110. });
  111. } catch (Exception e) {
  112. System.err.println("Warning: Writing failed on file "+file.getPath()+"\n"+e.toString());
  113. } finally {
  114. //Close open writer
  115. if (writer != null)
  116. try {
  117. writer.close();
  118. } catch (IOException e) {}
  119. }
  120. }
  121. /**
  122. * Returns true if the simulations will print the packets
  123. *
  124. * @return if the packets are printed
  125. */
  126. public boolean getPrintPackets() {
  127. return printPackets;
  128. }
  129. /**
  130. * Sets Print Packets, if true, the simulation will print the packets
  131. *
  132. * @param printPackets
  133. * true if simulation should print the packets
  134. */
  135. public void setPrintPackets(boolean printPackets) {
  136. this.printPackets = printPackets;
  137. }
  138. /**
  139. * Get BaseFile of packet exports
  140. *
  141. * @return ExportFile File, where the packets are written to
  142. */
  143. public File getExportFile() {
  144. return exportFile;
  145. }
  146. /**
  147. * Set BaseFile for the packet export
  148. *
  149. * @param exportFile
  150. * File, where the packets are written to
  151. */
  152. public void setExportFile(File exportFile) {
  153. this.exportFile = exportFile;
  154. }
  155. /**
  156. * True if each links uses a separated File: e.g. exportFile_link.log
  157. *
  158. * @return true, if each links uses a single file for export
  159. */
  160. public boolean isSplitLinkExportFiles() {
  161. return splitLinkExportFiles;
  162. }
  163. /**
  164. * Set whether packets should be split into different files for each link
  165. *
  166. * @param splitLinkExportFiles
  167. * true if each link should export to a single file
  168. */
  169. public void setSplitLinkExportFiles(boolean splitLinkExportFiles) {
  170. this.splitLinkExportFiles = splitLinkExportFiles;
  171. }
  172. }