Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,573 changes: 789 additions & 784 deletions httpd/static/index.html

Large diffs are not rendered by default.

19 changes: 12 additions & 7 deletions server/src/main/java/org/web1/Main.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package org.web1;

import com.fastcgi.*;
import com.fastcgi.FCGIInterface;

import java.util.HashMap;
import java.util.function.Function;
import java.util.logging.Logger;

import org.validator.ValidatedRecordFactory;
import org.validator.validation.exceptions.ValidationException;
import org.web1.DTOs.RequestDTO;
import org.web1.checkers.Checker;
import org.web1.checkers.CheckerFunction;
import org.web1.utils.SimpleLogger;
import org.web1.utils.mappers.JsonBuilder;
import org.web1.utils.mappers.QueryStringToHashmap;
import org.web1.utils.responce.ResponseFactory;
import org.web1.utils.responce.ResponseController;
import org.web1.utils.responce.ResponseStatus;
import org.web1.utils.timer.Timer;

Expand All @@ -23,13 +25,16 @@ public class Main {
public static void main(String[] args) {
FCGIInterface fastCGI = new FCGIInterface();
Timer timer = new Timer();
Logger logger = SimpleLogger.create();
ResponseController responseController = new ResponseController(logger);

while (fastCGI.FCGIaccept() >= 0) {
timer.start();

final HashMap<String, String> queryParams = parseQuery.apply(
FCGIInterface.request.params.getProperty("QUERY_STRING")
);
logger.info("Received request with query: " + queryParams);

try {
RequestDTO requestData = ValidatedRecordFactory.create(
Expand All @@ -45,21 +50,21 @@ public static void main(String[] args) {
Float.parseFloat(requestData.R())
);

String result = ResponseFactory.create(

responseController.send(
new JsonBuilder()
.add("result", checkResult)
.add("elapsedTimeNs", timer.stop()),
ResponseStatus.OK
);

System.out.println(result);
} catch (ValidationException e) {
String result = ResponseFactory.create(
responseController.send(
new JsonBuilder()
.add("error", '"'+e.getMessage()+'"'),
ResponseStatus.BAD_REQUEST
);
System.out.println(result);
} catch (Exception e) {
logger.severe("Failed to process request: " + e.getMessage());
}
}
}
Expand Down
30 changes: 14 additions & 16 deletions server/src/main/java/org/web1/checkers/Checker.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
package org.web1.checkers;
import org.web1.checkers.utils.GraphQuarters;
import org.web1.checkers.utils.GraphUtils;

import java.util.HashMap;
import org.web1.checkers.utils.PlotQuarters;
import org.web1.checkers.utils.PlotUtils;

public class Checker implements CheckerFunction{
public boolean test(int x, float y, float r) {
final GraphQuarters quarter = GraphUtils.getQuarter(x, y);
public boolean test(final int x, final float y, final float r) {
final PlotQuarters quarter = PlotUtils.getQuarter(x, y);

if (quarter == GraphQuarters.FIRST_QUADRANT) return firstQuarterTester(x, y, r);
else if (quarter == GraphQuarters.SECOND_QUADRANT) return secondQuarterTester(x, y, r);
else if (quarter == GraphQuarters.THIRD_QUADRANT) return thirdQuarterTester(x, y, r);
if (quarter == PlotQuarters.FIRST_QUADRANT) return firstQuarterTester(x, y, r);
else if (quarter == PlotQuarters.SECOND_QUADRANT) return secondQuarterTester(x, y, r);
else if (quarter == PlotQuarters.THIRD_QUADRANT) return thirdQuarterTester(x, y, r);
return forthQuarterTester(x, y, r);
}

private boolean firstQuarterTester(int x, float y, float r) {
return x <= r/2 && y <= r;
private boolean firstQuarterTester(final int x, final float y, final float r) {
return x <= r / 2 && y <= r;
}

private boolean secondQuarterTester(int x, float y, float r) {
return y <= (r/2 + 0.5f*x);
private boolean secondQuarterTester(final int x, final float y, final float r) {
return y <= (r / 2 + 0.5f * x);
}

private boolean thirdQuarterTester(int x, float y, float r) {
private boolean thirdQuarterTester(final int x, final float y,final float r) {
return false;
}

private boolean forthQuarterTester(int x, float y, float r) {
return Math.sqrt(x*x + y*y) <= r/2;
private boolean forthQuarterTester(final int x, final float y, final float r) {
return Math.sqrt(x * x + y * y) <= r / 2;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.web1.checkers.utils;

public enum GraphQuarters {
public enum PlotQuarters {
FIRST_QUADRANT,
SECOND_QUADRANT,
THIRD_QUADRANT,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.web1.checkers.utils;

public class GraphUtils {
public static GraphQuarters getQuarter(int x, float y) {
public class PlotUtils {
public static PlotQuarters getQuarter(final int x, final float y) {
boolean isXGraterOrEqualsZero = x >= 0;
boolean isYGraterOrEqualsZero = y >= 0;

if (isXGraterOrEqualsZero && isYGraterOrEqualsZero) return GraphQuarters.FIRST_QUADRANT;
else if (!isXGraterOrEqualsZero && !isYGraterOrEqualsZero) return GraphQuarters.THIRD_QUADRANT;
else if (!isXGraterOrEqualsZero && isYGraterOrEqualsZero) return GraphQuarters.SECOND_QUADRANT; // читабельность важнее
return GraphQuarters.FOURTH_QUADRANT;
if (isXGraterOrEqualsZero && isYGraterOrEqualsZero) return PlotQuarters.FIRST_QUADRANT;
else if (!isXGraterOrEqualsZero && !isYGraterOrEqualsZero) return PlotQuarters.THIRD_QUADRANT;
else if (!isXGraterOrEqualsZero && isYGraterOrEqualsZero) return PlotQuarters.SECOND_QUADRANT; // читабельность важнее
return PlotQuarters.FOURTH_QUADRANT;
}
}
21 changes: 21 additions & 0 deletions server/src/main/java/org/web1/utils/SimpleLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.web1.utils;

import java.io.IOException;
import java.util.logging.*;

public class SimpleLogger {
static Logger logger = Logger.getLogger(Logger.class.getName());

public static Logger create(){
try {
Handler fileHandler = new FileHandler("log.log");
fileHandler.setFormatter(fileHandler.getFormatter());
logger.addHandler(fileHandler);
logger.setLevel(Level.INFO);

return logger;
} catch (SecurityException | IOException e) {
throw new RuntimeException("Can't init logger");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.function.Function;

public class QueryStringToHashmap implements Function<String, HashMap<String,String>> {
public HashMap<String, String> apply(String jsonStr) {
public HashMap<String, String> apply(final String jsonStr) {
HashMap<String, String> params = new HashMap<>();

String[] pairs = jsonStr.split("&"); // get key+value pairs string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import org.web1.utils.mappers.JsonBuilder;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ResponseFactory {
public class ResponseController {
private static final String BASE_RESPONSE = """
HTTP/2 %s\r
Status: %s\r
Access-Control-Allow-Origin: *\r
Connection: keep-alive\r
Expand All @@ -17,21 +20,29 @@ public class ResponseFactory {
%s""";

private static final HashMap<ResponseStatus, String> responseStatus = new HashMap<>();
private final Logger logger;

static {
responseStatus.put(ResponseStatus.BAD_REQUEST, "400");
responseStatus.put(ResponseStatus.OK, "200");
}

public static String create(JsonBuilder jsonBuilder, ResponseStatus status) {
public ResponseController(Logger logger) {
this.logger = logger;
}

public void send(JsonBuilder jsonBuilder, ResponseStatus status) {
String response = jsonBuilder.build();

return String.format(
String out = String.format(
BASE_RESPONSE,
responseStatus.get(status),
responseStatus.get(status),
response.getBytes(StandardCharsets.UTF_8).length + 1,
response.getBytes(StandardCharsets.UTF_8).length,
response
);

logger.info("Sending response: " + out);

System.out.println(out);
}
}
2 changes: 1 addition & 1 deletion server/src/main/java/org/web1/utils/timer/Timer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.web1.utils.timer;

public class Timer {
long startTime;
private long startTime;
public void start() {
this.startTime = System.nanoTime();
}
Expand Down