Search in sources :

Example 1 with PacketsContextCommon

use of io.xol.chunkstories.net.PacketsContextCommon in project chunkstories by Hugobros3.

the class PacketReceiveFile method process.

@Override
public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException, PacketProcessingException {
    if (!(processor instanceof ClientPacketsContext))
        return;
    // ClientPacketsContext cppi = (ClientPacketsContext)processor;
    String fileTag = in.readUTF();
    long fileLength = in.readLong();
    if (fileLength > 0) {
        PacketsContextCommon context = (PacketsContextCommon) processor;
        PendingDownload pendingDownload = context.getConnection().getLocationForExpectedFile(fileTag);
        if (pendingDownload == null)
            throw new IOException("Received unexpected PacketFile with tag: " + fileTag);
        // Class to report back the download status to whoever requested it via the onStart callback
        PacketFileDownloadStatus status = new PacketFileDownloadStatus((int) fileLength);
        if (pendingDownload.a != null)
            pendingDownload.a.onStart(status);
        long remaining = fileLength;
        FileOutputStream fos = new FileOutputStream(pendingDownload.f);
        byte[] buffer = new byte[4096];
        while (remaining > 0) {
            long toRead = Math.min(4096, remaining);
            System.out.println("Working ! ...");
            // cppi.getConnection().getCurrentlyDownloadedFileProgress().setStepText("Downloading "+fileTag+", "+(fileLength - remaining)/1024+"/"+fileLength/1024+"kb");
            int actuallyRead = in.read(buffer, 0, (int) toRead);
            fos.write(buffer, 0, (int) actuallyRead);
            remaining -= actuallyRead;
            status.downloaded += actuallyRead;
        }
        status.end.release();
        fos.close();
    }
}
Also used : PendingDownload(io.xol.chunkstories.net.Connection.PendingDownload) FileOutputStream(java.io.FileOutputStream) PacketsContextCommon(io.xol.chunkstories.net.PacketsContextCommon) IOException(java.io.IOException) ClientPacketsContext(io.xol.chunkstories.client.net.ClientPacketsContext)

Example 2 with PacketsContextCommon

use of io.xol.chunkstories.net.PacketsContextCommon in project chunkstories by Hugobros3.

the class PacketContentTranslator method process.

@Override
public void process(PacketSender sender, DataInputStream in, PacketReceptionContext context) throws IOException, PacketProcessingException {
    this.serializedText = in.readUTF();
    ByteArrayInputStream bais = new ByteArrayInputStream(serializedText.getBytes("UTF-8"));
    BufferedReader reader = new BufferedReader(new InputStreamReader(bais, "UTF-8"));
    try {
        OnlineContentTranslator translator = new LoadedContentTranslator(context.getContext().getContent(), reader);
        PacketsContextCommon cCommon = (PacketsContextCommon) context;
        cCommon.setContentTranslator(translator);
        context.logger().info("Successfully installed content translator");
        cCommon.getConnection().handleSystemRequest("world/translator_ok");
    } catch (IncompatibleContentException e) {
        e.printStackTrace();
    }
    reader.close();
}
Also used : IncompatibleContentException(io.xol.chunkstories.content.translator.IncompatibleContentException) OnlineContentTranslator(io.xol.chunkstories.api.content.OnlineContentTranslator) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) LoadedContentTranslator(io.xol.chunkstories.content.translator.LoadedContentTranslator) BufferedReader(java.io.BufferedReader) PacketsContextCommon(io.xol.chunkstories.net.PacketsContextCommon)

Aggregations

PacketsContextCommon (io.xol.chunkstories.net.PacketsContextCommon)2 OnlineContentTranslator (io.xol.chunkstories.api.content.OnlineContentTranslator)1 ClientPacketsContext (io.xol.chunkstories.client.net.ClientPacketsContext)1 IncompatibleContentException (io.xol.chunkstories.content.translator.IncompatibleContentException)1 LoadedContentTranslator (io.xol.chunkstories.content.translator.LoadedContentTranslator)1 PendingDownload (io.xol.chunkstories.net.Connection.PendingDownload)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1