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

Second Commit : All Working Fine - Still No Logging

parent 7afa65cc
<component name="ArtifactManager">
<artifact type="jar" name="Client:jar">
<output-path>$PROJECT_DIR$/out/artifacts/Client_jar</output-path>
<root id="archive" name="httpclient.jar">
<element id="directory" name="META-INF">
<element id="file-copy" path="$PROJECT_DIR$/META-INF/MANIFEST.MF" />
</element>
<element id="module-output" name="httpclient" />
</root>
</artifact>
</component>
\ No newline at end of file
<component name="ArtifactManager">
<artifact type="jar" name="Server:jar">
<output-path>$PROJECT_DIR$/out/artifacts/Server_jar</output-path>
<root id="archive" name="httpserver.jar">
<element id="directory" name="META-INF">
<element id="file-copy" path="$PROJECT_DIR$/META-INF/MANIFEST.MF" />
</element>
<element id="module-output" name="httpserver" />
</root>
</artifact>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
File added
File added
Manifest-Version: 1.0
Main-Class: WebServer
......@@ -14,18 +14,32 @@ public class Aggregator {
/*send task to list of workers*/
public List<String> sendTasksToWorkers(List<String> workersAddresses, List<String> tasks) throws ExecutionException, InterruptedException {
List<String> responses = new ArrayList<>();
List<CompletableFuture<String>> temp = new ArrayList<>();
//List<CompletableFuture<String>> temp = new ArrayList<>();
for (String workerAddr : workersAddresses)
{
for (String task : tasks)
{
temp.add(webClient.sendTask(workerAddr , task.getBytes()));
try
{
responses.add(webClient.sendTask(workerAddr, task.getBytes()).get()+ " via " + workerAddr);
}
catch (Exception e)
{
System.out.println("Cannot Connect To Server " + workerAddr);
break;
}
}
}
for (CompletableFuture<String> resp : temp)
/*for (CompletableFuture<String> resp : temp)
{
responses.add(resp.get());
}
try {
responses.add(resp.get());
}
catch (Exception e)
{
System.out.println("Connection Problem");
}
}*/
//throw new UnsupportedOperationException();
return responses;
}
......@@ -33,18 +47,33 @@ public class Aggregator {
/*send task to list of workers*/
public List<String> sendTasksToWorkers(List<String> workersAddresses, List<String> tasks, String headers) throws ExecutionException, InterruptedException {
List<String> responses = new ArrayList<>();
List<CompletableFuture<String>> temp = new ArrayList<>();
//List<CompletableFuture<String>> temp = new ArrayList<>();
for (String workerAddr : workersAddresses)
{
for (String task : tasks)
{
temp.add(webClient.sendTask(workerAddr , task.getBytes() , headers));
try
{
responses.add(webClient.sendTask(workerAddr , task.getBytes() , headers).get()+" via "+workerAddr);
}
catch (Exception e)
{
System.out.println("Cannot Connect To Server " + workerAddr);
break;
}
}
}
for (CompletableFuture<String> resp : temp)
/*for (CompletableFuture<String> resp : temp)
{
responses.add(resp.get());
}
try {
responses.add(resp.get());
}
catch (Exception e)
{
System.out.println("Connection Problem");
}
}*/
return responses;
}
}
......@@ -4,7 +4,7 @@ import java.util.concurrent.ExecutionException;
public class Application {
private static final String WORKER_ADDRESS_1 = "http://localhost:8080/task";
private static final String WORKER_ADDRESS_2 = "http://localhost:8080/task";
private static final String WORKER_ADDRESS_2 = "http://localhost:8081/task";
public static void main(String[] args) throws ExecutionException, InterruptedException {
......@@ -21,7 +21,7 @@ public class Application {
System.out.println(result);
}
List<String> resultsWithHeaders = aggregator.sendTasksToWorkers(Arrays.asList(WORKER_ADDRESS_1, WORKER_ADDRESS_2),
List<String> resultsWithHeaders = aggregator.sendTasksToWorkers(Arrays.asList(WORKER_ADDRESS_1/*, WORKER_ADDRESS_2*/),
Arrays.asList(task1, task2), headers);
for (String result : resultsWithHeaders) {
......
......@@ -10,6 +10,7 @@ import java.util.concurrent.CompletableFuture;
public class WebClient {
private HttpClient client;
private String DEBUG_RESPONSE_KEY = "X-Debug-Info";
/* instantiate web client */
/* Read more about Builder pattern https://en.wikipedia.org/wiki/Builder_pattern*/
......@@ -38,7 +39,7 @@ public class WebClient {
/* send task (post http request) asynchronously with custom headers*/
public CompletableFuture<String> sendTask(String url, byte[] requestPayload, String headers)
{
CompletableFuture<String> response = new CompletableFuture<>();
CompletableFuture<String> reply = new CompletableFuture<>();
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
.uri(URI.create(url))
.POST(HttpRequest.BodyPublishers.ofByteArray(requestPayload));
......@@ -56,12 +57,13 @@ public class WebClient {
}
HttpRequest request = requestBuilder.build();
response = client.sendAsync
reply = client.sendAsync
(request,
HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8))
.thenApply(HttpResponse::body);
.thenApply(response ->
response.headers().map().get(DEBUG_RESPONSE_KEY).get(0) + response.body());
return response;
return reply;
}
}
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