Finished porting, successfully got data

This commit is contained in:
Norbi Peti 2017-04-01 11:27:34 +02:00
parent 3df935b7bc
commit c31399155a
5 changed files with 81 additions and 63 deletions

View file

@ -22,5 +22,10 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>
</classpath> </classpath>

View file

@ -2,3 +2,4 @@ eclipse.preferences.version=1
encoding//src/main/java=UTF-8 encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8 encoding//src/test/java=UTF-8
encoding/<project>=UTF-8 encoding/<project>=UTF-8
encoding/src=UTF-8

View file

@ -55,6 +55,10 @@
<id>jitpack.io</id> <id>jitpack.io</id>
<url>https://jitpack.io/</url> <url>https://jitpack.io/</url>
</repository> </repository>
<repository>
<id>clojars.org</id>
<url>http://clojars.org/repo</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
@ -70,5 +74,10 @@
<version>1.11.2-R0.1-SNAPSHOT</version> <version>1.11.2-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>com.github.TooTallNate</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>Java-WebSocket-1.3.1</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View file

@ -1,13 +1,29 @@
package buttondevteam.PlaceMinecraft2; package buttondevteam.PlaceMinecraft2;
import java.net.URISyntaxException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
/** /**
* Hello world! * Hello world!
* *
*/ */
public class App public class App {
{ public static void main(String[] args) throws URISyntaxException {
public static void main( String[] args ) PlaceWebSocket placews = new PlaceWebSocket(
{ "wss://...");
System.out.println( "Hello World!" ); SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, null); // will use java's default key and trust store which is sufficient unless you deal with self-signed certificates
SSLSocketFactory factory = sslContext.getSocketFactory();// (SSLSocketFactory) SSLSocketFactory.getDefault();
placews.setSocket(factory.createSocket());
if (!placews.connectBlocking())
System.out.println("Failed to connect.");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Finished");
} }
} }

View file

@ -1,65 +1,52 @@
package buttondevteam.PlaceMinecraft2; package buttondevteam.PlaceMinecraft2;
import com.google.gson.Gson; import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class PlaceWebSocket { //Ported from https://gist.github.com/teaearlgraycold/76877c8f262de24becc081ad96759730 import java.net.URI;
private Gson json; import java.net.URISyntaxException;
public JsonObject recv_frame()
{ public class PlaceWebSocket extends WebSocketClient { // Ported from https://gist.github.com/teaearlgraycold/76877c8f262de24becc081ad96759730
//frame = super().recv_frame() public PlaceWebSocket(String wslink) throws URISyntaxException {
return json.fromJson(frame.data.decode('utf-8')); super(new URI(wslink));
/*
* insert_queue = 0 inserted_count = 0 max_queue_size = 100 save_frame_per = 20000
*/
} }
public void main() @Override
{ url = "wss://..."; public void onOpen(ServerHandshake handshakedata) {
ws = create_connection(url, class_=PlaceWebSocket); System.out.println("Status: " + handshakedata.getHttpStatusMessage());
insert_queue = 0
inserted_count = 0
max_queue_size = 100
save_frame_per = 20000
while(true)
{ try
{
frame = ws.recv_frame();
print(frame);
if(frame['type'] == 'place')
{
c.execute('''INSERT INTO placements VALUES (?, ?, ?, ?, ?)''', [
int(time.time()),
frame['payload']['x'],
frame['payload']['y'],
frame['payload']['color'],
frame['payload']['author']
])
insert_queue += 1
inserted_count += 1
} }
else if(frame['type'] == 'activity')
{
c.execute('''INSERT INTO activity VALUES (?, ?)''', [
int(time.time()),
frame['payload']['count']
])
insert_queue += 1
inserted_count += 1
if insert_queue >= max_queue_size: @Override
conn.commit() public void onMessage(String message) {
insert_queue = 0 JsonObject frame = new JsonParser().parse(message).getAsJsonObject();
if inserted_count % save_frame_per == 0: String type = frame.get("type").getAsString();
save_bitmap(c, conn) JsonObject payload = frame.get("payload").getAsJsonObject();
except KeyboardInterrupt: System.out.println("Type: " + type);
print('Exiting safely...') if (type.equals("place")) {
conn.commit() int x = payload.get("x").getAsInt();
conn.close() int y = payload.get("y").getAsInt();
sys.exit() String color = payload.get("color").getAsString();
except Exception as e: String author = payload.get("author").getAsString();
print('Error occured: {}'.format(str(e))) System.out.println("X: " + x + " - Y: " + y + " - Color: " + color + " - Author: " + author);
} else if (type.equals("activity")) {
String count = payload.get("count").getAsString();
if __name__ == '__main__': System.out.println("Count: " + count);
main() }
}
@Override
public void onClose(int code, String reason, boolean remote) {
System.out.println("Code: " + code + " - Reason: " + reason + " - Remote: " + remote);
}
@Override
public void onError(Exception ex) {
System.out.println(ex);
}
} }