Skip to content
Open
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
21 changes: 21 additions & 0 deletions HW3_HttpProxy/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
plugins {
id 'java'
}

group 'ru.sbp.hse'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// https://mvnrepository.com/artifact/org.jetbrains/annotations
compile group: 'org.jetbrains', name: 'annotations', version: '13.0'
}

apply plugin: 'application'
applicationDefaultJvmArgs = ["-Djava.net.preferIPv4Stack=true"]
Binary file added HW3_HttpProxy/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions HW3_HttpProxy/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Tue Mar 26 16:33:18 MSK 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip
172 changes: 172 additions & 0 deletions HW3_HttpProxy/gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions HW3_HttpProxy/gradlew.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions HW3_HttpProxy/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'http-proxy'

69 changes: 69 additions & 0 deletions HW3_HttpProxy/src/main/java/ru/spb/hse/httpproxy/Cache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ru.spb.hse.httpproxy;

import org.jetbrains.annotations.NotNull;

import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Logger;

public class Cache {
private static final Logger LOGGER = Logger.getLogger("Cache");
private Duration expirationTime;
private Map<Integer, CachedObject> cache;

public Cache(Duration expirationTime, int cacheSize) {
this.expirationTime = expirationTime;
this.cache = Collections.synchronizedMap(new LinkedHashMap<Integer,
CachedObject>() {
@Override
public boolean removeEldestEntry(Map.Entry eldest) {
return size() > cacheSize;
}
});
}

public synchronized byte[] getResponse(@NotNull byte[] request) {
if (!contains(request)) {
return null;
}
return cache.get(Arrays.hashCode(request)).response;
}

public synchronized boolean contains(@NotNull byte[] request) {
int hash = Arrays.hashCode(request);
if (!cache.containsKey(hash)) {
LOGGER.info("The request is not in " +
"cache");
return false;
}
if (Duration.between(cache.get(hash).wasCreated, Instant.now())
.compareTo(expirationTime) > 0) {
LOGGER.info("The request " + new String(request) + " is outdated");
cache.remove(hash);
return false;
}
return true;
}

public synchronized void add(@NotNull byte[] request, @NotNull byte[] response) {
if (contains(request)) {
return;
}
int hash = Arrays.hashCode(request);
cache.put(hash, new CachedObject(response));
}

private static class CachedObject {
private byte[] response;
private Instant wasCreated;

CachedObject(@NotNull byte[] response) {
this.response = response;
this.wasCreated = Instant.now();
}
}
}
19 changes: 19 additions & 0 deletions HW3_HttpProxy/src/main/java/ru/spb/hse/httpproxy/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.spb.hse.httpproxy;

import ru.spb.hse.httpproxy.parser.HTTPMessage;

import java.io.IOException;
import java.util.Arrays;

public class Main {
public static void main(String[] args) {
int port = Integer.parseInt(args[0]);
int time = Integer.parseInt(args[1]);
int size = Integer.parseInt(args[2]);
try(ProxyServer server = new ProxyServer(port, time, size)) {
server.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Loading