Example #1
0
 /* (non-Javadoc)
  * @see model.Model#calcMazeSize(java.lang.String)
  */
 @Override
 public void calcMazeSize(String name) {
   double size = -5;
   if (getMazeCollection().containsKey(name)) {
     Maze3d myMaze = new Maze3d(getMazeCollection().get(name));
     size = myMaze.toByteArray().length;
     commandData.put("maze", name);
     changeAndNotify("calcedMazeSize", size);
   } else {
     changeAndNotify("notify", "Bad Maze Name (m.calcmazesize)");
   }
 }
Example #2
0
 /* (non-Javadoc)
  * @see model.Model#officialExit()
  */
 @Override
 public void officialExit() {
   threadPool.shutdown();
   try {
     saveToZip();
     changeAndNotify("saveZip", "File has been saved");
     threadPool.awaitTermination(59, TimeUnit.SECONDS);
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
   threadPool.shutdownNow();
   changeAndNotify("quit", "Official Exit");
 }
Example #3
0
 /* (non-Javadoc)
  * @see model.Model#getFilesInDirectory(java.lang.String)
  */
 @Override
 public void getFilesInDirectory(String path) {
   File f = new File(path);
   File[] fList = f.listFiles();
   String[] fileNames = new String[1];
   if (fList == null) {
     fileNames[0] = "No Files in these directory";
     changeAndNotify("dir", fileNames);
     return;
   } else {
     fileNames = new String[fList.length];
     for (int i = 0; i < fList.length; i++) {
       fileNames[i] = fList[i].getName();
     }
     changeAndNotify("dir", fileNames);
   }
 }
Example #4
0
 /* (non-Javadoc)
  * @see model.Model#calcFileSize(java.lang.String)
  */
 @Override
 public void calcFileSize(String name) {
   File f = new File(name);
   if (f.length() == 0L) {
     this.saveMaze(name, "tempFileName");
     f = new File("tempFileName");
   }
   commandData.put("maze", name);
   changeAndNotify("calcedFileSize", f.length());
 }
Example #5
0
 /**
  * Instantiates a new my model.
  *
  * @param _prop the _prop
  */
 public MyModel(Properties _prop) {
   super();
   this.setProp(_prop);
   threadPool = Executors.newFixedThreadPool(this.getProp().getNumOfThreads());
   try {
     loadFromZip();
     changeAndNotify("loadZip", "Mazes has been loaded from file");
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Example #6
0
  /* (non-Javadoc)
   * @see model.Model#solve(java.lang.String)
   */
  @Override
  public void solve(String name) {
    String algorithm = prop.getSolveAlgo();

    String mazeName = name;

    try {
      Socket myServer = new Socket(this.prop.getIp(), this.prop.getPort());
      ObjectOutputStream output = new ObjectOutputStream(myServer.getOutputStream());

      ArrayList<Object> messageToServer = new ArrayList<Object>();
      messageToServer.add("solve");
      messageToServer.add(algorithm);
      messageToServer.add(mazeCollection.get(mazeName));
      output.writeObject(messageToServer);
      output.flush();

      ObjectInputStream input = new ObjectInputStream(myServer.getInputStream());
      Solution messageFromServer = (Solution) input.readObject();

      if (messageFromServer == null) {
        changeAndNotify("notify", "Bad Maze Name (m.solve)");
        output.close();
        input.close();
        myServer.close();
        return;
      }
      solutionCollection.put(mazeCollection.get(mazeName), messageFromServer);

      changeAndNotify("solved", name);

      myServer.getInputStream().close();
      myServer.getOutputStream().close();
      output.close();
      input.close();
      myServer.close();

    } catch (Exception e) {
      changeAndNotify("notify", "Server might be closed");
    }
  }
Example #7
0
 /* (non-Javadoc)
  * @see model.Model#saveMaze(java.lang.String, java.lang.String)
  */
 @Override
 public void saveMaze(String mazeName, String fileName) {
   Maze3d myMaze = new Maze3d(getMazeCollection().get(mazeName));
   try {
     OutputStream out = new MyCompressorOutputStream(new FileOutputStream(fileName));
     out.write(myMaze.toByteArray());
     out.flush();
     out.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
   changeAndNotify("saved", mazeName);
 }
Example #8
0
 /* (non-Javadoc)
  * @see model.Model#loadMaze(java.lang.String, java.lang.String)
  */
 @Override
 public void loadMaze(String mazeName, String fileName) {
   InputStream in;
   try {
     in = new MyDecompressorInputStream(new FileInputStream(fileName));
     byte[] b = new byte[((MyDecompressorInputStream) in).getLength()];
     in.read(b);
     in.close();
     this.mazeCollection.put(mazeName, new Maze3d(b));
     changeAndNotify("loaded", mazeName);
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Example #9
0
 /* (non-Javadoc)
  * @see model.Model#getCrossSection(char, int, java.lang.String)
  */
 @Override
 public void getCrossSection(char xyz, int index, String name) {
   if (getMazeCollection().containsKey(name)) {
     Maze3d myMaze = new Maze3d(getMazeCollection().get(name));
     if (xyz == 'x' || xyz == 'X') {
       changeAndNotify("crossed", myMaze.getCrossSectionByX(index));
       return;
     }
     if (xyz == 'y' || xyz == 'Y') {
       changeAndNotify("crossed", myMaze.getCrossSectionByY(index));
       return;
     }
     if (xyz == 'z' || xyz == 'Z') {
       changeAndNotify("crossed", myMaze.getCrossSectionByZ(index));
       return;
     } else {
       changeAndNotify("notify", "bad X/Y/Z cord");
     }
   } else {
     changeAndNotify("notify", "Bad Maze Name (m.getcross)");
     return;
   }
 }
Example #10
0
  /* (non-Javadoc)
   * @see model.Model#generate3dMaze(java.lang.String, int)
   */
  @Override
  public void generate3dMaze(String name, int size) {
    Future<Maze3d> myMaze =
        threadPool.submit(
            new Callable<Maze3d>() {
              @Override
              public Maze3d call() throws Exception {
                Maze3dGenerator mg = new MyMaze3dGenerator();
                if (getProp().getGenerationAlgo().equalsIgnoreCase("simple")) {
                  mg = new SimpleMaze3dGenerator();
                }
                Maze3d myMaze = mg.generate(size, size, size);
                return myMaze;
              }
            });
    try {
      getMazeCollection().put(name, myMaze.get());
    } catch (InterruptedException | ExecutionException e) {

      e.printStackTrace();
    }
    changeAndNotify("generated", name);
  }