Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: scaryghost/KFSXTrackingServer
base: d9140d6412
...
head fork: scaryghost/KFSXTrackingServer
compare: 5cdd85e652
  • 6 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
2  src/com/github/etsai/kfsxtrackingserver/Data.java
View
@@ -184,6 +184,7 @@ public void accumulateAggregateStat(String stat, int offset, String category) {
}
tempAggregate.addValue(offset);
aggregate.put(id, tempAggregate);
+ writer.addAggregate(stat, category);
}
public Iterable<Death> getDeaths() {
@@ -200,5 +201,6 @@ public void accumulateDeath(String stat, int offset) {
}
tempDeath.addValue(offset);
deaths.put(id, tempDeath);
+ writer.addDeath(stat);
}
}
41 src/com/github/etsai/kfsxtrackingserver/HTTPListener.java
View
@@ -1,7 +1,14 @@
package com.github.etsai.kfsxtrackingserver;
+import static com.github.etsai.kfsxtrackingserver.Common.logger;
+import com.github.etsai.kfsxtrackingserver.web.Page;
+import java.io.BufferedReader;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
+import java.util.logging.Level;
public class HTTPListener implements Runnable {
private final int port;
@@ -13,10 +20,17 @@ public HTTPListener(int port) {
@Override
public void run() {
- httpSocket= new ServerSocket(port);
-
- while(true) {
- Socket connection= webSocket.accept();
+ try {
+ httpSocket= new ServerSocket(port);
+
+ while(true) {
+ Socket connection= httpSocket.accept();
+ Thread th= new Thread(new Handler(connection));
+
+ th.start();
+ }
+ } catch (IOException ex) {
+ logger.log(Level.SEVERE, null, ex);
}
}
@@ -29,15 +43,16 @@ public Handler(Socket connection) {
@Override
public void run() {
- BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- DataOutputSream output= new DataOutputStream(connection.getOutputStream());
-
- String[] request= input.readLine().tokenize(" ");
- String[] fileSplit= request[1].tokenize("?=");
- String filepath= fileSplit[0] == "/" ? "/index.xml" : fileSplit[0];
- String extension= filepath.substring(filepath.lastIndexOf(".")+1, filepath.length());
-
- output.close();
+ try {
+ BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+ try (DataOutputStream output = new DataOutputStream(connection.getOutputStream())) {
+ String[] request= input.readLine().split(" ");
+
+ Page.generate(output, request);
+ }
+ } catch (IOException ex) {
+ logger.log(Level.SEVERE, null, ex);
+ }
}
}
}
4 src/com/github/etsai/kfsxtrackingserver/Main.java
View
@@ -44,7 +44,8 @@ public static void main(String[] args) throws ClassNotFoundException, SQLExcepti
UDPListener listener= new UDPListener(Integer.valueOf(properties.getProperty(propUdpPort)));
Thread udpTh= new Thread(listener);
-
+ Thread httpTh= new Thread(new HTTPListener(8080));
+
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
@@ -66,6 +67,7 @@ public void run() {
Common.statsData= Data.load(conn);
udpTh.start();
+ httpTh.start();
}
public static void initLogging() {
1  src/com/github/etsai/kfsxtrackingserver/Time.java
View
@@ -45,7 +45,6 @@ public Time(String timeStr) throws RuntimeException {
hours= Integer.valueOf(matcher.group(2));
minutes= Integer.valueOf(matcher.group(3));
seconds= Integer.valueOf(matcher.group(4));
- System.out.format("%s-%s-%s-%s%n", days, hours, minutes, seconds);
}
}
/**
1  src/com/github/etsai/kfsxtrackingserver/impl/AccumulatorImpl.groovy
View
@@ -10,6 +10,7 @@ import static com.github.etsai.kfsxtrackingserver.Common.logger
import com.github.etsai.kfsxtrackingserver.Accumulator
import com.github.etsai.kfsxtrackingserver.Packet
import com.github.etsai.kfsxtrackingserver.Packet.Type
+import java.util.logging.Level
/**
*
11 src/com/github/etsai/kfsxtrackingserver/impl/MatchPacket.groovy
View
@@ -5,9 +5,11 @@
package com.github.etsai.kfsxtrackingserver.impl
+import static com.github.etsai.kfsxtrackingserver.Common.logger
import com.github.etsai.kfsxtrackingserver.Packet
import com.github.etsai.kfsxtrackingserver.Packet.Type
import com.github.etsai.kfsxtrackingserver.Time
+import java.util.logging.Level
/**
*
@@ -27,9 +29,11 @@ public class MatchPacket extends Packet {
try {
def deaths= [:]
- parts[7].split(",").each {death ->
- def keyVal= death.split("=")
- deaths[keyVal[0]]= keyVal[1]
+ if (parts[7] != "") {
+ parts[7].split(",").each {death ->
+ def keyVal= death.split("=")
+ deaths[keyVal[0]]= keyVal[1]
+ }
}
data= [:]
@@ -43,6 +47,7 @@ public class MatchPacket extends Packet {
valid= true
} catch (Exception ex) {
+ logger.log(Level.SEVERE, null, ex)
valid= false
}
3  src/com/github/etsai/kfsxtrackingserver/impl/PlayerPacket.groovy
View
@@ -5,8 +5,10 @@
package com.github.etsai.kfsxtrackingserver.impl
+import static com.github.etsai.kfsxtrackingserver.Common.logger
import com.github.etsai.kfsxtrackingserver.Packet
import com.github.etsai.kfsxtrackingserver.Packet.Type
+import java.util.logging.Level
/**
*
@@ -58,6 +60,7 @@ public class PlayerPacket extends Packet {
data[keyStats]= playerStats
valid= true
} catch (Exception e) {
+ logger.log(Level.SEVERE, null, ex)
valid= false
}
}
44 src/com/github/etsai/kfsxtrackingserver/web/Index.groovy
View
@@ -4,26 +4,58 @@ import static com.github.etsai.kfsxtrackingserver.Common.statsData
import groovy.xml.MarkupBuilder
public class Index extends Page {
- public String fillBody(def xmlBuilde) {
+ public String fillBody(def xmlBuilder) {
xmlBuilder.kfstatsx() {
'difficulties'() {
- def attr= [:]
statsData.getDifficulties().each {diff ->
+ def attr= [:]
attr["name"]= diff.getName()
attr["length"]= diff.getLength()
attr["wins"]= diff.getWins()
attr["losses"]= diff.getLosses()
attr["wave"]= diff.getWave()
attr["time"]= diff.getTime().toString()
+ 'difficulty'(attr)
}
- 'difficulty'(attr)
+
}
-
'levels'() {
- def attr= [:]
statsData.getLevels().each {level ->
-
+ def attr= [:]
+ attr["name"]= level.getName()
+ attr["wins"]= level.getWins()
+ attr["losses"]= level.getLosses()
+ attr["time"]= level.getTime().toString()
+ xmlBuilder.'level'(attr)
+ }
+ }
+ 'deaths'() {
+ statsData.getDeaths().each {death ->
+ def attr= [:]
+ attr["source"]= death.getStat()
+ attr["count"]= death.getValue()
+ xmlBuilder.'death'(attr)
+ }
+ }
+ 'aggregate'() {
+ def categories= [:]
+ statsData.getAggregateStats().each {stat ->
+ def cat= stat.getCategory()
+ if (categories[cat] == null) {
+ categories[cat]= []
+ }
+ categories[cat] << stat
+ }
+ categories.each {cat, stats ->
+ xmlBuilder.'stats'(category: cat) {
+ stats.each {stat ->
+ def attrs= [:]
+ attrs["name"]= stat.getStat()
+ attrs["value"]= stat.getValue()
+ xmlBuilder.'stat'(attrs)
+ }
+ }
}
}
}
94 src/com/github/etsai/kfsxtrackingserver/web/Page.groovy
View
@@ -1,65 +1,61 @@
package com.github.etsai.kfsxtrackingserver.web;
+import com.github.etsai.kfsxtrackingserver.Common
+import java.util.logging.Level
+import java.io.DataOutputStream
+import groovy.xml.MarkupBuilder
+
public abstract class Page {
- private static def pages= ["index.xml", "playerinfo.xml", "players.xml"].collect {"/${it}"}
+ private static def pages= ["index.xml", "playerinfo.xml", "players.xml"].collect {"/${it}" as String}
private static def methods= ["GET", "HEAD"]
private static def returnCodes= [200: "OK", 400: "Bad Request", 403: "Forbidden",
404: "Not Found", 500: "Internal Server Error", 501: "Not Implemented"]
private static def extensions= ["html":"Content-Type: text/html", "xml":"Content-Type: application/xml" ,
"xsl":"Content-Type: application/xslt+xml","css":"Content-Type: text/css","":""]
- public static String generate(String filename) {
+ public static String generate(DataOutputStream output, String[] request) {
+ def writer= new StringWriter()
+ def xml= new MarkupBuilder(writer)
+
+ def code, body
+ def fileSplit= request[1].tokenize("\\?=");
+ def filename= fileSplit[0]
def extension= filename.substring(filename.lastIndexOf(".")+1, filename.length());
-
- if (!pages.contains(filename)) {
-
- }
-/*
- def header= "HTTP/1.0 "
-
-
- try {
- request= input.readLine().tokenize(" ")
- String client= String.format("%s:%s", socket.getInetAddress().getHostAddress(), socket.getPort());
- logger.info("${client}-${request}")
-
- def fileSplit= request[1].tokenize("?=")
- def filepath= fileSplit[0] == "/" ? "/index.xml" : fileSplit[0]
- def mid= filepath.lastIndexOf(".")
-
- extension= filepath.substring(mid+1, filepath.length())
- if(!methods.contains(request[0])) {
- code= 501
+
+ try {
+ if(!methods.contains(request[0])) {
+ code= 501
+ body= "${code} ${returnCodes[code]}"
+ } else {
+ if(extension == "xsl" || extension == "css"){
+ body= new File(".${filename}").readLines().join("\n")
+ } else if (!pages.contains(filename)) {
+ code= 404
body= "${code} ${returnCodes[code]}"
+ extension= "html"
} else {
- if(filepath == "/web/Weapons.xml" || extension == "xsl" || extension == "css"){
- body= new File("."+filepath).readLines().join("\n")
- }
- else if (!pages.contains(filepath)) {
- code= 404
- body= "${code} ${returnCodes[code]}"
- extension= "html"
- } else {
- body= pageActions[filepath](fileSplit)
- }
-
- }
- } catch (Exception ex) {
- def sw= new StringWriter()
- def pw= new PrintWriter(sw)
-
- extension= "html"
- code= 500
- body= "<pre>"
- body+= "${code} ${returnCodes[code]}\n\n"
- ex.printStackTrace(pw)
- body+= sw.toString() + "</pre>"
- logger.log(Level.SEVERE, "Error generating webpage", ex);
+ new Index().fillBody(xml)
+ body= writer.toString()
+ //body= pageActions[filepath](fileSplit)
+ }
}
-
- def content= extensions[extension]
- header+= "${code} ${returnCodes[code]}\r\nConnection: close\r\nServer KFStats\r\n${content}\r\n\r\n"
-*/
+ } catch (Exception ex) {
+ def sw= new StringWriter()
+ def pw= new PrintWriter(sw)
+
+ extension= "html"
+ code= 500
+ body= "<pre>${code} ${returnCodes[code]}\n\n"
+ ex.printStackTrace(pw)
+ body+= sw.toString() + "</pre>"
+ Common.logger.log(Level.SEVERE, "Error generating webpage", ex);
+ }
+
+ def content= extensions[extension]
+ def header= "HTTP/1.0 ${code} ${returnCodes[code]}\r\nConnection: close\r\nServer KFStats\r\n${content}\r\n\r\n"
+
+ output.writeBytes(header)
+ if (request != "HEAD") output.writeBytes(body)
}
public abstract String fillBody(def xmlBuilder);

No commit comments for this range

Something went wrong with that request. Please try again.