mirror of
https://github.com/autistic-symposium/web3-starter-py.git
synced 2025-05-18 14:40:25 -04:00
Add some private projects
This commit is contained in:
parent
c8cd5cdbc4
commit
07aa882d51
80 changed files with 5216 additions and 41 deletions
26
soundcloud/soundcloud_problem_statement/java/build.gradle
Normal file
26
soundcloud/soundcloud_problem_statement/java/build.gradle
Normal file
|
@ -0,0 +1,26 @@
|
|||
buildscript {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
jcenter()
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'application'
|
||||
|
||||
mainClassName = "com.soundcloud.maze.Main"
|
||||
|
||||
tasks.withType(JavaCompile) {
|
||||
sourceCompatibility = 1.8
|
||||
targetCompatibility = 1.8
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testCompile "junit:junit:4.12"
|
||||
testCompile "org.assertj:assertj-core:1.7.1"
|
||||
}
|
84
soundcloud/soundcloud_problem_statement/java/gradlew.bat
vendored
Normal file
84
soundcloud/soundcloud_problem_statement/java/gradlew.bat
vendored
Normal file
|
@ -0,0 +1,84 @@
|
|||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%" == "" set DIRNAME=.
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS=
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:init
|
||||
@rem Get command-line arguments, handling Windows variants
|
||||
|
||||
if not "%OS%" == "Windows_NT" goto win9xME_args
|
||||
|
||||
:win9xME_args
|
||||
@rem Slurp the command line arguments.
|
||||
set CMD_LINE_ARGS=
|
||||
set _SKIP=2
|
||||
|
||||
:win9xME_args_slurp
|
||||
if "x%~1" == "x" goto execute
|
||||
|
||||
set CMD_LINE_ARGS=%*
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||
exit /b 1
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
|
@ -0,0 +1,153 @@
|
|||
package com.soundcloud.maze;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static java.util.Collections.emptySet;
|
||||
|
||||
public class Main {
|
||||
|
||||
private final static int EVENT_PORT = 9090;
|
||||
private final static int CLIENT_PORT = 9099;
|
||||
|
||||
private static long lastSeqNo = 0L;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Map<Long, Socket> clientPool = new ConcurrentHashMap<>();
|
||||
Map<Long, List<String>> seqNoToMessage = new HashMap<>();
|
||||
|
||||
Map<Long, Set<Long>> followRegistry = new HashMap<>();
|
||||
|
||||
new Thread(() -> {
|
||||
System.out.println("Listening for events on " + EVENT_PORT);
|
||||
try (Socket eventSocket = new ServerSocket(EVENT_PORT).accept()) {
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(eventSocket.getInputStream()))) {
|
||||
reader.lines().forEach(payload -> {
|
||||
System.out.println("Message received: " + payload);
|
||||
|
||||
List<String> payloadParts = Arrays.asList(payload.split("\\|"));
|
||||
seqNoToMessage.put(Long.parseLong(payloadParts.get(0)), payloadParts);
|
||||
|
||||
while (seqNoToMessage.containsKey(lastSeqNo + 1)) {
|
||||
List<String> nextMessage = seqNoToMessage.get(lastSeqNo + 1);
|
||||
String nextPayload = String.join("|", nextMessage);
|
||||
|
||||
long seqNo = Long.parseLong(nextMessage.get(0));
|
||||
String kind = nextMessage.get(1);
|
||||
|
||||
switch (kind) {
|
||||
case "F": {
|
||||
long fromUserId = Long.parseLong(nextMessage.get(2));
|
||||
long toUserId = Long.parseLong(nextMessage.get(3));
|
||||
|
||||
Set<Long> followers = followRegistry.getOrDefault(toUserId, new HashSet<>());
|
||||
followers.add(fromUserId);
|
||||
followRegistry.put(toUserId, followers);
|
||||
|
||||
try {
|
||||
Socket socket = clientPool.get(toUserId);
|
||||
if (socket != null) {
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
writer.write(nextPayload + "\n");
|
||||
writer.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case "U": {
|
||||
long fromUserId = Long.parseLong(nextMessage.get(2));
|
||||
long toUserId = Long.parseLong(nextMessage.get(3));
|
||||
|
||||
Set<Long> followers = followRegistry.getOrDefault(toUserId, new HashSet<>());
|
||||
followers.remove(fromUserId);
|
||||
followRegistry.put(toUserId, followers);
|
||||
}
|
||||
break;
|
||||
|
||||
case "P": {
|
||||
long toUserId = Long.parseLong(nextMessage.get(3));
|
||||
|
||||
try {
|
||||
Socket socket = clientPool.get(toUserId);
|
||||
if (socket != null) {
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
writer.write(nextPayload + "\n");
|
||||
writer.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case "B": {
|
||||
clientPool.values().forEach(socket -> {
|
||||
try {
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
writer.write(nextPayload + "\n");
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case "S": {
|
||||
long fromUserId = Long.parseLong(nextMessage.get(2));
|
||||
|
||||
Set<Long> followers = followRegistry.getOrDefault(fromUserId, emptySet());
|
||||
|
||||
followers.forEach(follower -> {
|
||||
try {
|
||||
Socket socket = clientPool.get(follower);
|
||||
if (socket != null) {
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
writer.write(nextPayload + "\n");
|
||||
writer.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
lastSeqNo = seqNo;
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}).start();
|
||||
|
||||
new Thread(() -> {
|
||||
System.out.println("Listening for client requests on " + CLIENT_PORT);
|
||||
try {
|
||||
ServerSocket serverSocket = new ServerSocket(CLIENT_PORT);
|
||||
Socket clientSocket = serverSocket.accept();
|
||||
while (clientSocket != null) {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||
String userId = reader.readLine();
|
||||
if (userId != null) {
|
||||
clientPool.put(Long.parseLong(userId), clientSocket);
|
||||
System.out.println("User connected: " + userId + " (" + clientPool.size() + " total)");
|
||||
}
|
||||
clientSocket = serverSocket.accept();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}).start();
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue