Skip to content

Commit a5f4ae9

Browse files
authored
Merge pull request #13 from NF-itmo/main
Sync changes, that were made on lab
2 parents 301336d + 58e8bb8 commit a5f4ae9

9 files changed

Lines changed: 862 additions & 822 deletions

File tree

httpd/static/index.html

Lines changed: 789 additions & 784 deletions
Large diffs are not rendered by default.

server/src/main/java/org/web1/Main.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package org.web1;
22

3-
import com.fastcgi.*;
3+
import com.fastcgi.FCGIInterface;
44

55
import java.util.HashMap;
66
import java.util.function.Function;
7+
import java.util.logging.Logger;
78

89
import org.validator.ValidatedRecordFactory;
910
import org.validator.validation.exceptions.ValidationException;
1011
import org.web1.DTOs.RequestDTO;
1112
import org.web1.checkers.Checker;
1213
import org.web1.checkers.CheckerFunction;
14+
import org.web1.utils.SimpleLogger;
1315
import org.web1.utils.mappers.JsonBuilder;
1416
import org.web1.utils.mappers.QueryStringToHashmap;
15-
import org.web1.utils.responce.ResponseFactory;
17+
import org.web1.utils.responce.ResponseController;
1618
import org.web1.utils.responce.ResponseStatus;
1719
import org.web1.utils.timer.Timer;
1820

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

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

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

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

48-
String result = ResponseFactory.create(
53+
54+
responseController.send(
4955
new JsonBuilder()
5056
.add("result", checkResult)
5157
.add("elapsedTimeNs", timer.stop()),
5258
ResponseStatus.OK
5359
);
54-
55-
System.out.println(result);
5660
} catch (ValidationException e) {
57-
String result = ResponseFactory.create(
61+
responseController.send(
5862
new JsonBuilder()
5963
.add("error", '"'+e.getMessage()+'"'),
6064
ResponseStatus.BAD_REQUEST
6165
);
62-
System.out.println(result);
66+
} catch (Exception e) {
67+
logger.severe("Failed to process request: " + e.getMessage());
6368
}
6469
}
6570
}
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
package org.web1.checkers;
2-
import org.web1.checkers.utils.GraphQuarters;
3-
import org.web1.checkers.utils.GraphUtils;
4-
5-
import java.util.HashMap;
2+
import org.web1.checkers.utils.PlotQuarters;
3+
import org.web1.checkers.utils.PlotUtils;
64

75
public class Checker implements CheckerFunction{
8-
public boolean test(int x, float y, float r) {
9-
final GraphQuarters quarter = GraphUtils.getQuarter(x, y);
6+
public boolean test(final int x, final float y, final float r) {
7+
final PlotQuarters quarter = PlotUtils.getQuarter(x, y);
108

11-
if (quarter == GraphQuarters.FIRST_QUADRANT) return firstQuarterTester(x, y, r);
12-
else if (quarter == GraphQuarters.SECOND_QUADRANT) return secondQuarterTester(x, y, r);
13-
else if (quarter == GraphQuarters.THIRD_QUADRANT) return thirdQuarterTester(x, y, r);
9+
if (quarter == PlotQuarters.FIRST_QUADRANT) return firstQuarterTester(x, y, r);
10+
else if (quarter == PlotQuarters.SECOND_QUADRANT) return secondQuarterTester(x, y, r);
11+
else if (quarter == PlotQuarters.THIRD_QUADRANT) return thirdQuarterTester(x, y, r);
1412
return forthQuarterTester(x, y, r);
1513
}
1614

17-
private boolean firstQuarterTester(int x, float y, float r) {
18-
return x <= r/2 && y <= r;
15+
private boolean firstQuarterTester(final int x, final float y, final float r) {
16+
return x <= r / 2 && y <= r;
1917
}
2018

21-
private boolean secondQuarterTester(int x, float y, float r) {
22-
return y <= (r/2 + 0.5f*x);
19+
private boolean secondQuarterTester(final int x, final float y, final float r) {
20+
return y <= (r / 2 + 0.5f * x);
2321
}
2422

25-
private boolean thirdQuarterTester(int x, float y, float r) {
23+
private boolean thirdQuarterTester(final int x, final float y,final float r) {
2624
return false;
2725
}
2826

29-
private boolean forthQuarterTester(int x, float y, float r) {
30-
return Math.sqrt(x*x + y*y) <= r/2;
27+
private boolean forthQuarterTester(final int x, final float y, final float r) {
28+
return Math.sqrt(x * x + y * y) <= r / 2;
3129
}
3230
}

server/src/main/java/org/web1/checkers/utils/GraphQuarters.java renamed to server/src/main/java/org/web1/checkers/utils/PlotQuarters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.web1.checkers.utils;
22

3-
public enum GraphQuarters {
3+
public enum PlotQuarters {
44
FIRST_QUADRANT,
55
SECOND_QUADRANT,
66
THIRD_QUADRANT,

server/src/main/java/org/web1/checkers/utils/GraphUtils.java renamed to server/src/main/java/org/web1/checkers/utils/PlotUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package org.web1.checkers.utils;
22

3-
public class GraphUtils {
4-
public static GraphQuarters getQuarter(int x, float y) {
3+
public class PlotUtils {
4+
public static PlotQuarters getQuarter(final int x, final float y) {
55
boolean isXGraterOrEqualsZero = x >= 0;
66
boolean isYGraterOrEqualsZero = y >= 0;
77

8-
if (isXGraterOrEqualsZero && isYGraterOrEqualsZero) return GraphQuarters.FIRST_QUADRANT;
9-
else if (!isXGraterOrEqualsZero && !isYGraterOrEqualsZero) return GraphQuarters.THIRD_QUADRANT;
10-
else if (!isXGraterOrEqualsZero && isYGraterOrEqualsZero) return GraphQuarters.SECOND_QUADRANT; // читабельность важнее
11-
return GraphQuarters.FOURTH_QUADRANT;
8+
if (isXGraterOrEqualsZero && isYGraterOrEqualsZero) return PlotQuarters.FIRST_QUADRANT;
9+
else if (!isXGraterOrEqualsZero && !isYGraterOrEqualsZero) return PlotQuarters.THIRD_QUADRANT;
10+
else if (!isXGraterOrEqualsZero && isYGraterOrEqualsZero) return PlotQuarters.SECOND_QUADRANT; // читабельность важнее
11+
return PlotQuarters.FOURTH_QUADRANT;
1212
}
1313
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.web1.utils;
2+
3+
import java.io.IOException;
4+
import java.util.logging.*;
5+
6+
public class SimpleLogger {
7+
static Logger logger = Logger.getLogger(Logger.class.getName());
8+
9+
public static Logger create(){
10+
try {
11+
Handler fileHandler = new FileHandler("log.log");
12+
fileHandler.setFormatter(fileHandler.getFormatter());
13+
logger.addHandler(fileHandler);
14+
logger.setLevel(Level.INFO);
15+
16+
return logger;
17+
} catch (SecurityException | IOException e) {
18+
throw new RuntimeException("Can't init logger");
19+
}
20+
}
21+
}

server/src/main/java/org/web1/utils/mappers/QueryStringToHashmap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.function.Function;
55

66
public class QueryStringToHashmap implements Function<String, HashMap<String,String>> {
7-
public HashMap<String, String> apply(String jsonStr) {
7+
public HashMap<String, String> apply(final String jsonStr) {
88
HashMap<String, String> params = new HashMap<>();
99

1010
String[] pairs = jsonStr.split("&"); // get key+value pairs string

server/src/main/java/org/web1/utils/responce/ResponseFactory.java renamed to server/src/main/java/org/web1/utils/responce/ResponseController.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import org.web1.utils.mappers.JsonBuilder;
44

5+
import java.io.FileWriter;
6+
import java.io.IOException;
57
import java.nio.charset.StandardCharsets;
68
import java.util.HashMap;
9+
import java.util.logging.Level;
10+
import java.util.logging.Logger;
711

8-
public class ResponseFactory {
12+
public class ResponseController {
913
private static final String BASE_RESPONSE = """
10-
HTTP/2 %s\r
1114
Status: %s\r
1215
Access-Control-Allow-Origin: *\r
1316
Connection: keep-alive\r
@@ -17,21 +20,29 @@ public class ResponseFactory {
1720
%s""";
1821

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

2125
static {
2226
responseStatus.put(ResponseStatus.BAD_REQUEST, "400");
2327
responseStatus.put(ResponseStatus.OK, "200");
2428
}
2529

26-
public static String create(JsonBuilder jsonBuilder, ResponseStatus status) {
30+
public ResponseController(Logger logger) {
31+
this.logger = logger;
32+
}
33+
34+
public void send(JsonBuilder jsonBuilder, ResponseStatus status) {
2735
String response = jsonBuilder.build();
2836

29-
return String.format(
37+
String out = String.format(
3038
BASE_RESPONSE,
3139
responseStatus.get(status),
32-
responseStatus.get(status),
33-
response.getBytes(StandardCharsets.UTF_8).length + 1,
40+
response.getBytes(StandardCharsets.UTF_8).length,
3441
response
3542
);
43+
44+
logger.info("Sending response: " + out);
45+
46+
System.out.println(out);
3647
}
3748
}

server/src/main/java/org/web1/utils/timer/Timer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.web1.utils.timer;
22

33
public class Timer {
4-
long startTime;
4+
private long startTime;
55
public void start() {
66
this.startTime = System.nanoTime();
77
}

0 commit comments

Comments
 (0)