Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
N
network-communication
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
joubran.mansour
network-communication
Commits
e3906db1
Commit
e3906db1
authored
Oct 24, 2026
by
joubran.mansour
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement WebClient and Aggregator
parent
355a464e
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
52 additions
and
20 deletions
+52
-20
.gitignore
.gitignore
+5
-0
Aggregator.java
httpclient/src/main/java/Aggregator.java
+23
-9
WebClient.java
httpclient/src/main/java/WebClient.java
+24
-11
WebServer.class
httpserver/target/classes/WebServer.class
+0
-0
No files found.
.gitignore
0 → 100644
View file @
e3906db1
target/
.idea/
*.iml
logs/
out/
\ No newline at end of file
httpclient/src/main/java/Aggregator.java
View file @
e3906db1
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.concurrent.CompletableFuture
;
...
...
@@ -11,17 +12,30 @@ public class Aggregator {
/*send task to list of workers*/
public
List
<
String
>
sendTasksToWorkers
(
List
<
String
>
workersAddresses
,
List
<
String
>
tasks
)
{
/*
Add your code here
*/
throw
new
UnsupportedOperationException
();
return
sendTasksToWorkers
(
workersAddresses
,
tasks
,
null
);
}
/*send task to list of workers*/
public
List
<
String
>
sendTasksToWorkers
(
List
<
String
>
workersAddresses
,
List
<
String
>
tasks
,
String
headers
)
{
/*
Add your code here
*/
throw
new
UnsupportedOperationException
();
if
(
workersAddresses
.
size
()
!=
tasks
.
size
())
{
throw
new
IllegalArgumentException
(
"Number of workers ("
+
workersAddresses
.
size
()
+
") must equal number of tasks ("
+
tasks
.
size
()
+
")"
);
}
List
<
CompletableFuture
<
String
>>
futures
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
workersAddresses
.
size
();
i
++)
{
byte
[]
payload
=
tasks
.
get
(
i
).
getBytes
();
CompletableFuture
<
String
>
future
=
(
headers
==
null
)
?
webClient
.
sendTask
(
workersAddresses
.
get
(
i
),
payload
)
:
webClient
.
sendTask
(
workersAddresses
.
get
(
i
),
payload
,
headers
);
futures
.
add
(
future
);
}
List
<
String
>
results
=
new
ArrayList
<>();
for
(
CompletableFuture
<
String
>
future
:
futures
)
{
results
.
add
(
future
.
join
());
}
return
results
;
}
}
\ No newline at end of file
httpclient/src/main/java/WebClient.java
View file @
e3906db1
import
java.net.URI
;
import
java.net.http.HttpClient
;
import
java.
util.ArrayLi
st
;
import
java.
util.List
;
import
java.
net.http.HttpReque
st
;
import
java.
net.http.HttpResponse
;
import
java.util.concurrent.CompletableFuture
;
public
class
WebClient
{
...
...
@@ -15,17 +16,29 @@ public class WebClient {
/* send task (post http request) asynchronously */
public
CompletableFuture
<
String
>
sendTask
(
String
url
,
byte
[]
requestPayload
)
{
/*
Add your code here
*/
throw
new
UnsupportedOperationException
();
return
sendTask
(
url
,
requestPayload
,
null
);
}
/* send task (post http request) asynchronously with custom headers*/
public
CompletableFuture
<
String
>
sendTask
(
String
url
,
byte
[]
requestPayload
,
String
headers
)
{
/*
Add your code here
*/
throw
new
UnsupportedOperationException
();
HttpRequest
.
Builder
builder
=
HttpRequest
.
newBuilder
()
.
uri
(
URI
.
create
(
url
))
.
POST
(
HttpRequest
.
BodyPublishers
.
ofByteArray
(
requestPayload
));
if
(
headers
!=
null
&&
!
headers
.
isBlank
())
{
int
idx
=
headers
.
indexOf
(
':'
);
if
(
idx
<=
0
)
{
throw
new
IllegalArgumentException
(
"Invalid header format, expected 'Name: value' but got: "
+
headers
);
}
String
name
=
headers
.
substring
(
0
,
idx
).
trim
();
String
value
=
headers
.
substring
(
idx
+
1
).
trim
();
builder
.
header
(
name
,
value
);
}
HttpRequest
request
=
builder
.
build
();
return
client
.
sendAsync
(
request
,
HttpResponse
.
BodyHandlers
.
ofString
())
.
thenApply
(
HttpResponse:
:
body
);
}
}
\ No newline at end of file
httpserver/target/classes/WebServer.class
deleted
100644 → 0
View file @
355a464e
File deleted
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment