@@ -26,7 +26,7 @@ public class ServerStrategySolveSearchProblem implements IServerStrategy{
*/
@Override
public void serverStrategy(InputStream inFromClient, OutputStream outToClient) {
System.out.println("Server Strategy Solve maze!");

try {
//Creating the I/O streams
ObjectInputStream fromClient = new ObjectInputStream(inFromClient);
@@ -40,30 +40,33 @@ public void serverStrategy(InputStream inFromClient, OutputStream outToClient) {

//The Location of the file
String tempDirectoryPath = System.getProperty("java.io.tmpdir");
//The name of the file is a combination of the number of rows, number of columns, start Position,end Position, and index (we will add it later)
String filename=maze.getNumOfRows()+"_"+maze.getNumOfColumns()+"_"+maze.getStartPosition().GetRowIndex()+"_"+maze.getStartPosition().GetColumnIndex()+
"_"+maze.getGoalPosition().GetRowIndex()+"_"+maze.getGoalPosition().GetColumnIndex();
String filePath=tempDirectoryPath+filename;


int counter=0;
//The counter of the files
int index=0;
//This function compresses the maze into a string (the maze itself... without the start/goal positions or the size of the maze)
String mazeString=maze.convertMazeToString();
//Creating the file
File f;
//the file
File file;
boolean flag=true;//If The solution exists
while(flag)
{
counter++;
f = new File(filePath+"_"+counter+".txt");
//Creating the file (with the new name - including the counter)
index++;
file = new File(filePath+"_"+index+".txt");


System.out.println(filePath+"_"+counter+".txt");
//If there is such a file
if(f.exists() && !f.isDirectory())
if(file.exists() && !file.isDirectory())
{
//If this file is the solution for this maze
if(this.checkIfFileExist(f,mazeString))
if(this.checkIfFileExist(file,mazeString))
{
//Get the solution from the file and return it
solution= this.SolutionExists(f);
solution= this.SolutionExists(file);
break;
}
}
@@ -79,10 +82,19 @@ public void serverStrategy(InputStream inFromClient, OutputStream outToClient) {
if(!flag)
{
//Solve the maze and put it's solution in the file
solution=solutionDoesNotExists(maze,filePath+"_"+counter+".txt");
solution=solutionDoesNotExists(maze,filePath+"_"+index+".txt");
}

//Getting the arrayList of the solution (without the neighbors - The neighbors list of every state is null. avoids stackOverFlow)
ArrayList<AState>list=solution.getSolutionPath();
ArrayList<AState> mazeList=new ArrayList<>();
for(int i=0;i<list.size();i++)
{
mazeList.add(new MazeState((MazeState)list.get(i)));
}

//Creating the modified solution
solution=new Solution(mazeList);

//Sending the solution of the maze via the output stream
toClient.writeObject(solution);
@@ -93,13 +105,19 @@ public void serverStrategy(InputStream inFromClient, OutputStream outToClient) {
e.printStackTrace();
}
}

/**
* This function will extract the solution of the given maze from the right file and return it
* @param file - The file in which the solution is located
* @return - The solution
*/
private Solution SolutionExists(File file)
{

System.out.println("Exist");

//The list that will become the solution
ArrayList<AState> list=new ArrayList<>();
String str="";

//Reading the solution from the file
try {
Scanner sc = new Scanner(file);
sc.nextLine();
@@ -109,9 +127,12 @@ private Solution SolutionExists(File file)
} catch (FileNotFoundException e) {
e.printStackTrace();
}


int index=-1;
String row="";
String col="";
//Extracting the solution from the
while(index!=str.length()-1)
{
index=str.indexOf(",");
@@ -125,10 +146,15 @@ private Solution SolutionExists(File file)
str=str.substring(index+1);
}


//Creating and returning th solution
return new Solution(list);
}

/**
* This function will recieve a string that represents a number and will return the number in int
* @param str - The given string
* @return - The number that the string represents
*/
private int ConvertFromStringToInt(String str)
{
int num=0;
@@ -138,9 +164,17 @@ private int ConvertFromStringToInt(String str)
}
return num;
}

/**
* This function will check if a current file is has the solution of the current maze
* @param file - The given file
* @param maze - The given maze represented in string
* @return - True if the file is the file that holds the solution to the maze
*/
private boolean checkIfFileExist(File file,String maze)
{
String str="";
//Read the first line (has the representation of the maze in string)
try {
Scanner sc = new Scanner(file);
str=sc.nextLine();
@@ -149,12 +183,20 @@ private boolean checkIfFileExist(File file,String maze)
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//Compare between the strings
return str.equals(maze);

}

/**
* This function will create a file with the solution of the current maze
* @param maze - The maze
* @param filePath - The path of the file to be created
* @return - The solution of the maze
*/
private Solution solutionDoesNotExists(Maze maze,String filePath)
{
System.out.println("Dose not exist");

//Creating the tools to solve the maze
BestFirstSearch bestFirstSearch = new BestFirstSearch();
SearchableMaze searchableMaze = new SearchableMaze(maze);
@@ -171,14 +213,15 @@ private Solution solutionDoesNotExists(Maze maze,String filePath)
str2=list.get(i).toString();
str+=str2.substring(1,str2.length()-1)+",";
}
System.out.println("The solution "+str);




//Putting the solution in the file
PrintWriter writer = null;
try {
writer = new PrintWriter(filePath, "UTF-8");
//The representation of the maze in string - we will it inorder to compare between two mazes
writer.println(maze.convertMazeToString());
writer.println(str);
writer.close();
@@ -271,19 +271,28 @@ public byte[] toByteArray() {
return theMaze;

}

/**
* This function will convert the maze into string (not including the size and the start/goal positions)
* @return - The representation of the maze in string
*/
public String convertMazeToString()
{


//The optimal number for compression (in big mazes). The reason for it is complicated.. you are most welcome to contact me and i'll explain :)
int MAGIC_NUMBER=62;

//The size of the maze (number of cells)
int size=this.getNumOfRows()*this.getNumOfColumns();

//How many full times the MAGIC NUMBER is in the size of the array
int div=size/MAGIC_NUMBER;
int remain=size%MAGIC_NUMBER;

long num=0;
String str="";

//Transforming the array to it's representation in string
for(int i=0;i<div;i++)
{
for(int j=MAGIC_NUMBER*i;j<MAGIC_NUMBER*i+MAGIC_NUMBER;j++) {
@@ -28,6 +28,20 @@ protected AState(int priority) {
this.stateToString="";
}

/**
* The Copy constructor
*
* @param aState - The other astate
*/
protected AState(AState aState) {
visited = aState.visited;
this.priority = aState.priority;
possibleNextStates = new ArrayList<>();
predecessor=null;
this.stateToString=aState.stateToString;
}


/**
* This function will compare to AStates using the priority
* @param o - The object
@@ -17,6 +17,17 @@ public MazeState(Position curr)
current=curr;
this.stateToString="{"+curr.GetRowIndex()+","+curr.GetColumnIndex()+"}";

}
/**
* The copy constructor
* @param mazeState - The other MazeState
*/
public MazeState(MazeState mazeState)
{
super(mazeState);
current=mazeState.current;
this.stateToString=mazeState.stateToString;

}

/**
@@ -1,13 +1,10 @@
package test;

import Client.Client;
import Client.IClientStrategy;
import IO.MyDecompressorInputStream;

import Server.*;
import Client.*;
import algorithms.mazeGenerators.Maze;
import algorithms.mazeGenerators.MyMazeGenerator;
import algorithms.mazeGenerators.Position;
import algorithms.search.AState;
import algorithms.search.Solution;

@@ -16,6 +13,9 @@
import java.net.UnknownHostException;
import java.util.ArrayList;

/**
* Created by Aviadjo on 3/27/2017.
*/
public class RunCommunicateWithServers {
public static void main(String[] args) {
//Initializing servers
@@ -41,27 +41,23 @@ public static void main(String[] args) {

private static void CommunicateWithServer_MazeGenerating() {
try {

Client client = new Client(InetAddress.getLocalHost(), 5400, new IClientStrategy() {
@Override
public void clientStrategy(InputStream inFromServer, OutputStream outToServer) {
try {

ObjectOutputStream toServer = new ObjectOutputStream(outToServer);

ObjectInputStream fromServer = new ObjectInputStream(inFromServer);
toServer.flush();

int[] mazeDimensions = new int[]{10, 6};
int[] mazeDimensions = new int[]{50, 50};

toServer.writeObject(mazeDimensions); //send maze dimensions to server
toServer.flush();


toServer.flush();

ObjectInputStream fromServer = new ObjectInputStream(inFromServer);
byte[] compressedMaze = (byte[]) fromServer.readObject(); //read generated maze (compressed with MyCompressor) from server
InputStream is = new MyDecompressorInputStream(new ByteArrayInputStream(compressedMaze));
byte[] decompressedMaze = new byte[10000000 /*CHANGE SIZE ACCORDING TO YOU MAZE SIZE*/]; //allocating byte[] for the decompressed maze -
byte[] decompressedMaze = new byte[100000 /*CHANGE SIZE ACCORDING TO YOU MAZE SIZE*/]; //allocating byte[] for the decompressed maze -
is.read(decompressedMaze); //Fill decompressedMaze with bytes
Maze maze = new Maze(decompressedMaze);
maze.print();
@@ -70,9 +66,7 @@ public void clientStrategy(InputStream inFromServer, OutputStream outToServer) {
}
}
});

client.communicateWithServer();

} catch (UnknownHostException e) {
e.printStackTrace();
}
@@ -84,29 +78,14 @@ private static void CommunicateWithServer_SolveSearchProblem() {
@Override
public void clientStrategy(InputStream inFromServer, OutputStream outToServer) {
try {

ObjectOutputStream toServer = new ObjectOutputStream(outToServer);
ObjectInputStream fromServer = new ObjectInputStream(inFromServer);
toServer.flush();
MyMazeGenerator mg = new MyMazeGenerator();
Maze maze = mg.generate(25, 25);
System.out.println(maze.convertMazeToString());
/* Maze maze;
int [][] binMaze={{ 0, 0, 0, 1, 1, 1, 0 },// 0 0 0 1 1 1 0
{ 0, 1, 0, 0, 0, 1, 0 },// 0 1 0 0 0 1 0
{ 0, 0, 1, 0, 0, 0, 1 },// 0 0 1 E 0 0 1
{ 0, 1, 1, 1, 0, 0, 0 },// 0 1 1 1 0 0 0
{ 0, 0, 0, 0, 0, 0, 1 },// 0 0 0 0 S 0 1
{ 0, 0, 0, 0, 0, 0, 1 },// 0 0 0 0 0 0 1
{ 1, 0, 1, 0, 0, 0, 0}};// 1 0 1 0 0 0 0
Position start=new Position(4,4);
Position goal=new Position(2,3);
maze=new Maze(binMaze,start,goal);*/
Maze maze = mg.generate(50, 50);
maze.print();
toServer.writeObject(maze); //send maze to server
toServer.flush();

Solution mazeSolution = (Solution) fromServer.readObject(); //read generated maze (compressed with MyCompressor) from server

//Print Maze Solution retrieved from the server
@@ -126,5 +105,32 @@ public void clientStrategy(InputStream inFromServer, OutputStream outToServer) {
}
}

private static void CommunicateWithServer_StringReverser() {
try {
Client client = new Client(InetAddress.getLocalHost(), 5402, new IClientStrategy() {
@Override
public void clientStrategy(InputStream inFromServer, OutputStream outToServer) {
try {
BufferedReader fromServer = new BufferedReader(new InputStreamReader(inFromServer));
PrintWriter toServer = new PrintWriter(outToServer);

String message = "Client Message";
String serverResponse;
toServer.write(message + "\n");
toServer.flush();
serverResponse = fromServer.readLine();
System.out.println(String.format("Server response: %s", serverResponse));
toServer.flush();
fromServer.close();
toServer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
client.communicateWithServer();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}