Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MTelling committed May 1, 2017
1 parent 475d778 commit 776fa9f
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 71 deletions.
35 changes: 0 additions & 35 deletions Testing/Tests/Challenge_Five.json

This file was deleted.

1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ dependencies {
compile("org.webjars:jquery:3.1.0")
compile("org.webjars:requirejs:2.3.1")
compile("com.google.code.gson:gson:2.5")
compile group: 'com.mashape.unirest', name: 'unirest-java', version: '1.3.1'
}
23 changes: 21 additions & 2 deletions src/main/java/com/htg/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,33 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

@SpringBootApplication
public class Application {

public static String urlToRunner = "http://localhost:4567";

//public static String urlToRunner = "http://localhost:4567";
public static String urlToRunner = "http://192.168.0.105:4567";
public static Logger logger = Logger.getLogger("Logger");
private static FileHandler fileHandler;

public static void main(String[] args) {

try {

fileHandler = new FileHandler("Server.log");
logger.addHandler(fileHandler);
SimpleFormatter formatter = new SimpleFormatter();
fileHandler.setFormatter(formatter);
logger.info("com.htg.runner.Application started!");

} catch (IOException e) {
e.printStackTrace();
}

if (args.length > 0) urlToRunner = args[0];
SpringApplication.run(Application.class, args);
}
Expand Down
25 changes: 0 additions & 25 deletions src/main/java/com/htg/Challenge.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,5 @@ public void setExternalName(String externalName) {



public static void main(String[] args) {
int n = 25000;
int count = 0;
int current = 1;

while (count != n) {
current++;
if (isPrime(current)) {
count++;
System.out.println(current);
}
}

System.out.println(current);
}
private static boolean isPrime(int x) {
for (int i = 2; i < x / 2; i++) {
if (x % i == 0 && i != x) {
return false;
}
}

return true;
}

}

1 change: 0 additions & 1 deletion src/main/java/com/htg/ChallengeReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public ChallengeReader() {}

public ChallengeDescription getChallengeDescriptionFromPath(String path) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
System.out.println(path);
Files.lines(FileSystems.getDefault().getPath(path)).forEachOrdered(stringBuilder::append);
Gson gson = new Gson();
String jsonFile = stringBuilder.toString();
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/htg/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public class GameController {
@SendToUser("/queue/game")
public GameResponse gameResponse(GameRequest gameRequest, SimpMessageHeaderAccessor simpMessageHeaderAccessor) throws Exception {
String sessionID = simpMessageHeaderAccessor.getSessionAttributes().get("sessionID").toString();

System.out.println(state.getCurrentChallengeDescription().getName());
Application.logger.info(sessionID + " requested new game.");
return new GameResponse(state.getCurrentChallengeDescription());
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/htg/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public LoginResponse login(LoginRequest message, SimpMessageHeaderAccessor simpM
String sessionID = simpMessageHeaderAccessor.getSessionAttributes().get("sessionID").toString();
User user = new User(message.getUsername());
if (serverState.addUser(sessionID, user)) {
Application.logger.info(user.getName() + " sucessfully logged in!");
return new LoginResponse("success", user.getName(), user.getScore(), serverState.getLeaderboard());
} else {
return new LoginResponse("exists", user.getName(), user.getScore(), serverState.getLeaderboard());
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/com/htg/PMController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.http.conn.HttpHostConnectException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
Expand Down Expand Up @@ -32,12 +33,21 @@ public PMResponse codeCheck(PMRequest pmRequest, SimpMessageHeaderAccessor simpM
String sessionID = simpMessageHeaderAccessor.getSessionAttributes().get("sessionID").toString();
String challengeID = state.getCurrentChallengeDescription().getFilename();

if (sessionID == null) return new PMResponse("You need to be logged in!!!");
if (sessionID == null || state.getUser(sessionID) == null) {
Application.logger.info("Someone submitted code without being logged in!");
return new PMResponse("You need to be logged in!!! Please refresh the page.");
}

String code = pmRequest.getCode();
String challenge = state.getCurrentChallengeDescription().getFilename();

String resultFromCall = callRunManager(code, sessionID, challenge);
String resultFromCall = "";
try {
resultFromCall = callRunManager(code, sessionID, challenge);
} catch (Exception e) {
Application.logger.severe("Error connecting to code runner. Service down?");
return new PMResponse("Error connecting to the code runner. Please try again!");
}

// Check if all tests are completed
try {
Expand Down Expand Up @@ -82,12 +92,12 @@ public PMResponse codeCheck(PMRequest pmRequest, SimpMessageHeaderAccessor simpM
}


private String callRunManager(String code, String sessionID, String challenge) throws UnirestException {
private String callRunManager(String code, String sessionID, String challenge) throws Exception {
RunRequest runRequest = new RunRequest(sessionID, code, challenge);

String url = Application.urlToRunner;

HttpResponse jsonResponse = Unirest.post(url)
HttpResponse jsonResponse = Unirest.post(url)
.header("accept", "application/json")
.body(new Gson().toJson(runRequest)).asString();

Expand Down Expand Up @@ -120,7 +130,7 @@ private String createOutputString(RunnerResult runnerResult) {


public void announceWin(String winnerUsername) {
System.out.println("Announcing new winner");
Application.logger.info(winnerUsername + " won the current game.");
simpMessagingTemplate.convertAndSend("/topic/news", new NewsResponse("win", winnerUsername, state.getLeaderboard()));
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/htg/SubscribeEventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof SessionSubscribeEvent) {
SessionSubscribeEvent sessionSubscribeEvent = (SessionSubscribeEvent) event;
StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(sessionSubscribeEvent.getMessage());
System.out.println("Socket request from: " + headerAccessor.getSessionAttributes().get("sessionID"));
}
}
}

0 comments on commit 776fa9f

Please sign in to comment.