Implemented node service

This commit is contained in:
Andrey Terentev 2023-11-16 10:27:52 +07:00
parent 6b841e80c9
commit f25ab87b7b
6 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1,12 @@
package ru.dragonestia.loadbalancer.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LoadBalancerWebApplication {
public static void main(String[] args) {
SpringApplication.run(LoadBalancerWebApplication.class, args);
}
}

View File

@ -0,0 +1,17 @@
package ru.dragonestia.loadbalancer.web.page;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.html.Paragraph;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
@Route("/")
public class HomePage extends VerticalLayout {
public HomePage() {
super();
add(new H1("Hello world!"));
add(new Paragraph("Hello world!"));
}
}

View File

@ -0,0 +1 @@
server.port=8090

View File

@ -0,0 +1,13 @@
package ru.dragonestia.loadbalancer.web;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class LoadBalancerWebApplicationTests {
@Test
void contextLoads() {
}
}

View File

@ -3,6 +3,7 @@ package ru.dragonestia.loadbalancer.service;
import ru.dragonestia.loadbalancer.model.Node; import ru.dragonestia.loadbalancer.model.Node;
import java.util.List; import java.util.List;
import java.util.Optional;
public interface NodeService { public interface NodeService {
@ -11,4 +12,6 @@ public interface NodeService {
void removeNode(Node node); void removeNode(Node node);
List<Node> allNodes(); List<Node> allNodes();
Optional<Node> findNode(String identifier);
} }

View File

@ -0,0 +1,41 @@
package ru.dragonestia.loadbalancer.service.impl;
import org.springframework.stereotype.Service;
import ru.dragonestia.loadbalancer.model.Node;
import ru.dragonestia.loadbalancer.service.NodeService;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class NodeServiceImpl implements NodeService {
private final Map<String, Node> nodes = new ConcurrentHashMap<>();
@Override
public void createNode(Node node) {
if (nodes.containsKey(node.identifier())) {
throw new IllegalArgumentException("Node with id '" + node.identifier() + "' already exists");
}
nodes.put(node.identifier(), node);
}
@Override
public void removeNode(Node node) {
nodes.remove(node.identifier());
}
@Override
public List<Node> allNodes() {
return nodes.values().stream().toList();
}
@Override
public Optional<Node> findNode(String identifier) {
return nodes.containsKey(identifier)? Optional.of(nodes.get(identifier)) : Optional.empty();
}
}