Commit 4a919357 authored by mohammad.salama's avatar mohammad.salama

GRPC with Web Not Working - GRPC Client is a problem

parent 15ff5138
......@@ -10,26 +10,24 @@
</component>
<component name="ChangeListManager">
<list default="true" id="b8bcd35c-99f3-44c8-a866-59f81c3c8bd0" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/src/main/java/GRPCConnection/GRPCServiceStart.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/resources/application.properties" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/WebSide/ClientApplication.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/AutoHealerAndClusterSearch/ObjectExchangeInCluster/ValueComparatorForFileScore.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/logs/app.log" beforeDir="false" afterPath="$PROJECT_DIR$/logs/app.log" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/GRPCConnection/GRPCClient/GRPCClient.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/GRPCConnection/GRPCClient/GRPCClient.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/GRPCConnection/GRPCServer/GRPCServer.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/GRPCConnection/GRPCServer/GRPCServer.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/GRPCConnection/Main.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/WebSide/WebClient.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/WebSide/WebClient.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/WebSide/WebServer.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/WebSide/WebServer.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/Application.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/Application.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/Coordinator.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/Coordinator.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/ServiceRegistry.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/ServiceRegistry.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/AutoHealerAndClusterSearch/generalTesting/test.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/AutoHealerAndClusterSearch/generalTesting/test.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/resources/application.properties" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/application.properties" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/GRPCConnection/GRPCClient/GRPCClient.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/GRPCConnection/GRPCClient/GRPCClient.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/GRPCConnection/GRPCServer/GRPCServer.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/GRPCConnection/GRPCServer/GRPCServer.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/GRPCConnection/Main.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/Application.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/Application.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/WebSide/WebClient.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/WebSide/WebClient.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/WebSide/WebServer.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/WebSide/WebServer.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/application.properties" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/application.properties" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/Coordinator$1.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/Coordinator$1.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/Coordinator.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/Coordinator.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/ServiceRegistry$1.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/ServiceRegistry$1.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/ServiceRegistry.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/ServiceRegistry.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/org/AutoHealerAndClusterSearch/generalTesting/test.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/org/AutoHealerAndClusterSearch/generalTesting/test.class" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -166,16 +164,26 @@
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/GRPCConnection/GRPCServer/GRPCServer.java</url>
<line>41</line>
<option name="timeStamp" value="1" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/ServiceRegistry.java</url>
<line>122</line>
<option name="timeStamp" value="10" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/WebSide/WebClient.java</url>
<line>28</line>
<option name="timeStamp" value="11" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/WebSide/WebServer.java</url>
<line>33</line>
<option name="timeStamp" value="14" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/WebSide/WebServer.java</url>
<line>79</line>
<option name="timeStamp" value="18" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>
......
the calculation of idf is not done yet!
we have now each word in query and its appearnce frequency in each file.
some more calculation should be done to get idf and other things
to run Distributed-Search:
java -jar Distributed-Search.jar <ip:port>
note:
the jar file must be within the same directory as the SearchFiles directory
example: directory x must contain: Distributed-Search.jar and SearchFiles/
\ No newline at end of file
......@@ -604,3 +604,45 @@ java.lang.NullPointerException: Cannot invoke "java.util.List.isEmpty()" because
2024-01-29 19:59:01,104 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Coordinator connected: /127.0.0.1
2024-01-29 19:59:14,721 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main-EventThread] The cluster addresses are: []
2024-01-29 19:59:16,911 WARN org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Coordinator [grpc-default-executor-0] No Nodes Are Working , Search Cannot Be Done
2024-01-31 15:06:54,908 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main-EventThread] Successfully connected to Zookeeper
2024-01-31 15:06:54,933 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.LeaderElection [main] My Node under Election is/election/c_0000000198
2024-01-31 15:06:54,940 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.LeaderElection [main] I am LEADER
2024-01-31 15:06:54,943 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main] The cluster addresses are: []
2024-01-31 15:06:54,946 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main] Registered to be Coordinator and I am Master !
2024-01-31 15:06:55,586 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main] Starting Application using Java 17.0.5 on M-Salameh with PID 800 (D:\HIAST\FIY\FS\Distributed Systems\Lab\8\Distributed-Search\target\classes started by M_Salameh in D:\HIAST\FIY\FS\Distributed Systems\Lab\8\Distributed-Search)
2024-01-31 15:06:55,586 DEBUG org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main] Running with Spring Boot v2.6.8, Spring v5.3.20
2024-01-31 15:06:55,587 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main] No active profile set, falling back to 1 default profile: "default"
2024-01-31 15:06:57,221 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main] Started Application in 2.079 seconds (JVM running for 6.945)
2024-01-31 15:07:03,324 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main-EventThread] Successfully connected to Zookeeper
2024-01-31 15:07:03,345 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.LeaderElection [main] My Node under Election is/election/c_0000000199
2024-01-31 15:07:03,349 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.LeaderElection [main] I am NOT LEADER
2024-01-31 15:07:03,356 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main] Registered to service registry
2024-01-31 15:07:03,363 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Server started on port 55
2024-01-31 15:07:03,377 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main-EventThread] The cluster addresses are: [127.0.0.1:55]
2024-01-31 15:07:10,241 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main-EventThread] Successfully connected to Zookeeper
2024-01-31 15:07:10,264 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.LeaderElection [main] My Node under Election is/election/c_0000000200
2024-01-31 15:07:10,268 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.LeaderElection [main] I am NOT LEADER
2024-01-31 15:07:10,275 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main] Registered to service registry
2024-01-31 15:07:10,280 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Server started on port 66
2024-01-31 15:07:10,286 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main-EventThread] The cluster addresses are: [127.0.0.1:55, 127.0.0.1:66]
2024-01-31 15:23:41,805 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main-EventThread] Successfully connected to Zookeeper
2024-01-31 15:23:41,822 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.LeaderElection [main] My Node under Election is/election/c_0000000201
2024-01-31 15:23:41,826 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.LeaderElection [main] I am LEADER
2024-01-31 15:23:41,828 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main] The cluster addresses are: []
2024-01-31 15:23:41,830 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main] Registered to be Coordinator and I am Master !
2024-01-31 15:23:42,353 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main] Starting Application using Java 17.0.5 on M-Salameh with PID 11784 (D:\HIAST\FIY\FS\Distributed Systems\Lab\8\Distributed-Search\target\classes started by M_Salameh in D:\HIAST\FIY\FS\Distributed Systems\Lab\8\Distributed-Search)
2024-01-31 15:23:42,354 DEBUG org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main] Running with Spring Boot v2.6.8, Spring v5.3.20
2024-01-31 15:23:42,354 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main] No active profile set, falling back to 1 default profile: "default"
2024-01-31 15:23:43,731 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main] Started Application in 1.761 seconds (JVM running for 7.215)
2024-01-31 15:23:51,791 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main-EventThread] Successfully connected to Zookeeper
2024-01-31 15:23:51,811 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.LeaderElection [main] My Node under Election is/election/c_0000000202
2024-01-31 15:23:51,814 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.LeaderElection [main] I am NOT LEADER
2024-01-31 15:23:51,818 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main] Registered to service registry
2024-01-31 15:23:51,823 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Server started on port 65
2024-01-31 15:23:51,829 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main-EventThread] The cluster addresses are: [127.0.0.1:65]
2024-01-31 15:23:59,146 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main-EventThread] Successfully connected to Zookeeper
2024-01-31 15:23:59,163 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.LeaderElection [main] My Node under Election is/election/c_0000000203
2024-01-31 15:23:59,166 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.LeaderElection [main] I am NOT LEADER
2024-01-31 15:23:59,172 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main] Registered to service registry
2024-01-31 15:23:59,177 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Server started on port 98
2024-01-31 15:23:59,181 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main-EventThread] The cluster addresses are: [127.0.0.1:65, 127.0.0.1:98]
......@@ -43,6 +43,11 @@
<artifactId>zookeeper</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.1</version>
</dependency>
</dependencies>
<build>
<extensions>
......
......@@ -7,27 +7,97 @@ import io.grpc.ManagedChannelBuilder;
import org.AutoHealerAndClusterSearch.SearchReply;
import org.AutoHealerAndClusterSearch.SearchRequest;
import org.AutoHealerAndClusterSearch.SearchServiceGrpc;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.util.List;
/**
* the client here will be the web server
* configure it later
* */
public class GRPCClient
public class GRPCClient implements Watcher
{
public static void main(String[] args)
private ZooKeeper zooKeeper;
private final String address = "192.168.184.10:2181";
private final int SESSION_TIMEOUT = 3000; //dead client
private final String COORDINATOR_ZNODE_PATH = "/coordinator_node";
private String COORDINATOR_ADDRESS;
private String COORDINATOR_ZNODE;
public GRPCClient () throws IOException, InterruptedException
{
connectToZookeeper();
run();
close();
}
public List<String> search(String query) throws InterruptedException, KeeperException
{
getAddress();
ManagedChannel channel = ManagedChannelBuilder.
forAddress("localhost" , 6565).
forAddress(COORDINATOR_ADDRESS , 6565).
usePlaintext().build();
SearchServiceGrpc.SearchServiceBlockingStub stub = SearchServiceGrpc.newBlockingStub(channel);
SearchRequest request = SearchRequest.newBuilder().setQuery("There is a file to be read be full of kindness").build();
SearchRequest request = SearchRequest.newBuilder().setQuery(query).build();
SearchReply reply = stub.search(request);
List<String> stringList = reply.getFilesList();
for (String s: stringList)
//SearchRequest request = SearchRequest.newBuilder().setQuery("There is a file to be read be full of kindness").build();
return stringList;
}
private synchronized void getAddress() throws InterruptedException, KeeperException
{
List<String> temp = zooKeeper.getChildren(COORDINATOR_ZNODE_PATH , this);
if (temp.isEmpty())
{
return;
}
COORDINATOR_ZNODE = temp.get(0);
String pth = COORDINATOR_ZNODE_PATH + "/"+ COORDINATOR_ZNODE;
Stat stat = zooKeeper.exists(pth , false);
if (stat == null) return;
COORDINATOR_ADDRESS = new String(zooKeeper.getData( pth, false , stat));
COORDINATOR_ADDRESS = COORDINATOR_ADDRESS.split(":")[0];
}
private void run() throws InterruptedException {
synchronized (zooKeeper) {
zooKeeper.wait();
}
}
private void close() throws InterruptedException {
this.zooKeeper.close();
}
private void connectToZookeeper() throws IOException
{
this.zooKeeper = new ZooKeeper(address, SESSION_TIMEOUT, this);
}
@Override
public void process(WatchedEvent watchedEvent)
{
switch (watchedEvent.getType())
{
System.out.println(s);
case NodeChildrenChanged :
{
try
{
getAddress();
}
catch (InterruptedException | KeeperException e)
{
throw new RuntimeException(e);
}
}
}
}
}
package WebSide;public class ClientApplication {
package WebSide;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
public class ClientApplication
{
private static final String SearchEngine = "http://localhost:5566/search";
public static void main(String[] args) throws ExecutionException, InterruptedException
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Search Query : ");
String query = scanner.nextLine();
WebClient webClient = new WebClient();
String ans = webClient.sendQuery(SearchEngine , query.getBytes());
System.out.println(ans);
}
}
package WebSide;
public class WebClient
{
import com.google.gson.JsonElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
}
\ No newline at end of file
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class WebClient {
private HttpClient client;
//static final Logger logger = LoggerFactory.getLogger(WebClient.class);
public WebClient()
{
this.client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_2).build();
}
/* send task (post http request) asynchronously */
public String sendQuery(String url, byte[] queryPayload) throws ExecutionException, InterruptedException {
CompletableFuture<String> response = new CompletableFuture<>();
HttpRequest httpRequest = HttpRequest.newBuilder()
.uri(URI.create(url))
.POST(HttpRequest.BodyPublishers.ofByteArray(queryPayload))
.build();
response = client.sendAsync
(httpRequest ,
HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)).
thenApply(HttpResponse::body);
/*response = client.sendAsync
(httpRequest,
HttpResponse.BodyHandlers.
);*/
String tem = response.join();
return tem;
}
}
package WebSide;
import GRPCConnection.GRPCClient.GRPCClient;
import com.google.gson.Gson;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import org.apache.zookeeper.KeeperException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
public class WebServer
{
private final String GRPC_SERVER = "localhost";
private final String Search_End_Point = "/search";
private final String Search_EndPoint = "/search";
private GRPCClient grpcClient;
private static final Logger logger = LoggerFactory.getLogger(WebServer.class);
private final int port;
private HttpServer server;
public WebServer(int port) throws IOException, InterruptedException
{
this.port = port;
}
public static void main(String[] args) throws IOException, InterruptedException {
int serverPort = 5566;
if (args.length == 1) {
serverPort = Integer.parseInt(args[0]);
}
WebServer webServer = new WebServer(serverPort);
webServer.startServer();
System.out.println("Server is listening on port " + serverPort);
logger.info("Server is listening on port " + serverPort);
}
public void startServer() {
try
{
this.server = HttpServer.create(new InetSocketAddress(port), 0);
logger.info("Server Started at : " + System.nanoTime());
System.out.println("Server Started");
}
catch (IOException e) {
logger.error("Server failed to Start at : " + System.nanoTime());
System.out.println("Server failed");
throw new RuntimeException(e);
}
HttpContext searchContext = server.createContext(Search_EndPoint);
searchContext.setHandler(this::handleSearchRequest);
server.setExecutor(Executors.newFixedThreadPool(8));
server.start();
}
private void handleSearchRequest(HttpExchange exchange) throws IOException {
logger.info("Search Request at " + System.nanoTime());
if (!exchange.getRequestMethod().equalsIgnoreCase("post")) {
exchange.close();
return;
}
byte[] requestBytes = exchange.getRequestBody().readAllBytes();
String query = new String(requestBytes);
List<String> files = null;
try
{
files = grpcClient.search(query);
}
catch (InterruptedException | KeeperException e)
{
files.add("Some Thing Went Wrong !!");
}
sendResponse(files, exchange);
}
private void sendResponse(List<String> files, HttpExchange exchange) throws IOException {
String s ="";
for(String file : files)
{
s += file;
s+="\n";
}
exchange.sendResponseHeaders(200, s.getBytes().length);
OutputStream outputStream = exchange.getResponseBody();
outputStream.write(s.getBytes());
outputStream.flush();
outputStream.close();
exchange.close();
}
private void sendResponseJson(List<String> responseList, HttpExchange exchange) throws IOException
{
Gson gson = new Gson();
String jsonResponse = gson.toJson(responseList);
exchange.getResponseHeaders().set("Content-Type", "application/json");
exchange.sendResponseHeaders(200, jsonResponse.getBytes().length);
OutputStream outputStream = exchange.getResponseBody();
outputStream.write(jsonResponse.getBytes());
outputStream.flush();
outputStream.close();
exchange.close();
}
}
......@@ -144,7 +144,9 @@ public class Coordinator
private static List<String> getFilesInOrder(Map<String , Double> filesScore)
{
List<String> files = new ArrayList<>();
if(filesScore == null)
{
files.add("No Answer !");
......
package org.AutoHealerAndClusterSearch.ObjectExchangeInCluster;public class ValueComparatorForFileScore {
package org.AutoHealerAndClusterSearch.ObjectExchangeInCluster;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
public class ValueComparatorForFileScore implements Comparator<Map.Entry<String,Double>>
{
/*Map<String, Double> base;
public ValueComparatorForFileScore() {
this.base = new TreeMap<>();
}
public ValueComparatorForFileScore(TreeMap<String, Double> base) {
this.base = base;
}*/
@Override
public int compare(Map.Entry<String,Double> a, Map.Entry<String,Double> b) {
return Double.compare(a.getValue() , b.getValue());
}
}
package org.AutoHealerAndClusterSearch.generalTesting;
import org.AutoHealerAndClusterSearch.ObjectExchangeInCluster.FileWordPair;
import org.AutoHealerAndClusterSearch.ObjectExchangeInCluster.ValueComparatorForFileScore;
import org.AutoHealerAndClusterSearch.WordsCountingInFiles.WordsCountingInFiles;
import java.io.*;
......@@ -137,12 +138,28 @@ public class test
}
public static void testTreeMap()
{
TreeMap<String , Double> treeMap = new TreeMap<>();
treeMap.put("Key1", 10.5);
treeMap.put("Key2", 5.2);
treeMap.put("Key3", 15.7);
treeMap.put("Key4", 7.8);
for (Map.Entry<String,Double> entry : treeMap.entrySet())
{
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
public static void main(String[] args)
{
String path = System.getProperty("user.dir") + "/SearchFiles/";
///listFilesForFolder(System.getProperty("user.dir") + "/SearchFiles");
///countWordsInFiles();
//System.out.println(countFilesInDirectory());
testTreeMap();
}
}
grpc.server.port=5678
\ No newline at end of file
server.port=6565
\ No newline at end of file
grpc.server.port=5678
\ No newline at end of file
server.port=6565
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment