Skip to content

Commit

Permalink
Move most command handlers in distributor to http handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Jul 4, 2019
1 parent d18d60a commit f17525c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@

package org.openqa.selenium.grid.distributor;

import static org.openqa.selenium.remote.http.Contents.string;

import org.openqa.selenium.grid.data.NodeStatus;
import org.openqa.selenium.grid.node.Node;
import org.openqa.selenium.grid.node.remote.RemoteNode;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpHandler;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.tracing.DistributedTracer;

import java.util.Objects;

public class AddNode implements CommandHandler {
import static org.openqa.selenium.remote.http.Contents.string;

public class AddNode implements HttpHandler {

private final DistributedTracer tracer;
private final Distributor distributor;
Expand All @@ -50,7 +50,7 @@ public AddNode(
}

@Override
public void execute(HttpRequest req, HttpResponse resp) {
public HttpResponse execute(HttpRequest req) {
NodeStatus status = json.toType(string(req), NodeStatus.class);

Node node = new RemoteNode(
Expand All @@ -61,5 +61,7 @@ public void execute(HttpRequest req, HttpResponse resp) {
status.getStereotypes().keySet());

distributor.add(node);

return new HttpResponse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@

package org.openqa.selenium.grid.distributor;

import static org.openqa.selenium.remote.http.Contents.utf8String;

import com.google.common.collect.ImmutableMap;

import org.openqa.selenium.grid.data.CreateSessionResponse;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.http.HttpHandler;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

import java.util.Objects;

class CreateSession implements CommandHandler {
import static org.openqa.selenium.remote.http.Contents.utf8String;

class CreateSession implements HttpHandler {

private final Json json;
private final Distributor distributor;
Expand All @@ -40,8 +39,8 @@ public CreateSession(Json json, Distributor distributor) {
}

@Override
public void execute(HttpRequest req, HttpResponse resp) {
public HttpResponse execute(HttpRequest req) {
CreateSessionResponse sessionResponse = distributor.newSession(req);
resp.setContent(utf8String(json.toJson(ImmutableMap.of("value", sessionResponse))));
return new HttpResponse().setContent(utf8String(json.toJson(ImmutableMap.of("value", sessionResponse))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
import org.openqa.selenium.grid.data.DistributorStatus;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.http.HttpHandler;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

import java.util.Objects;

class GetDistributorStatus implements CommandHandler {
class GetDistributorStatus implements HttpHandler {

private final Json json;
private final Distributor distributor;
Expand All @@ -40,9 +41,9 @@ public GetDistributorStatus(Json json, Distributor distributor) {
}

@Override
public void execute(HttpRequest req, HttpResponse resp) {
public HttpResponse execute(HttpRequest req) {
DistributorStatus status = distributor.getStatus();

resp.setContent(utf8String(json.toJson(ImmutableMap.of("value", status))));
return new HttpResponse().setContent(utf8String(json.toJson(ImmutableMap.of("value", status))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

package org.openqa.selenium.grid.distributor;

import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.remote.http.HttpHandler;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

import java.util.Objects;
import java.util.UUID;

class RemoveNode implements CommandHandler {
class RemoveNode implements HttpHandler {

private final Distributor distributor;
private final UUID nodeId;
Expand All @@ -35,7 +35,8 @@ public RemoveNode(Distributor distributor, UUID nodeId) {
}

@Override
public void execute(HttpRequest req, HttpResponse resp) {
public HttpResponse execute(HttpRequest req) {
distributor.remove(nodeId);
return new HttpResponse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@

package org.openqa.selenium.grid.distributor;

import static org.openqa.selenium.remote.http.Contents.utf8String;

import com.google.common.collect.ImmutableMap;

import org.openqa.selenium.grid.data.DistributorStatus;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.http.HttpHandler;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

import java.util.Objects;

class StatusHandler implements CommandHandler {
import static org.openqa.selenium.remote.http.Contents.utf8String;

class StatusHandler implements HttpHandler {

private final Distributor distributor;
private final Json json;
Expand All @@ -40,7 +39,7 @@ public StatusHandler(Distributor distributor, Json json) {
}

@Override
public void execute(HttpRequest req, HttpResponse resp) {
public HttpResponse execute(HttpRequest req) {
DistributorStatus status = distributor.getStatus();

ImmutableMap<String, Object> report = ImmutableMap.of(
Expand All @@ -49,6 +48,6 @@ public void execute(HttpRequest req, HttpResponse resp) {
"message", status.hasCapacity() ? "Ready" : "No free slots available",
"grid", status));

resp.setContent(utf8String(json.toJson(report)));
return new HttpResponse().setContent(utf8String(json.toJson(report)));
}
}

0 comments on commit f17525c

Please sign in to comment.