Search in sources :

Example 1 with ArchiveFileSource

use of org.terasology.gestalt.module.resources.ArchiveFileSource in project Terasology by MovingBlocks.

the class ServerConnectionHandler method sendModules.

private void sendModules(List<NetData.ModuleRequest> moduleRequestList) {
    for (NetData.ModuleRequest request : moduleRequestList) {
        NetData.ModuleDataHeader.Builder result = NetData.ModuleDataHeader.newBuilder();
        result.setId(request.getModuleId());
        Module module = moduleManager.getEnvironment().get(new Name(request.getModuleId()));
        if (!(module.getResources() instanceof ArchiveFileSource)) {
            // TODO: gestaltv7 restore module downloading for maximum possibles
            result.setError("Module not available for download");
        } else {
            FileReference fileReference = module.getResources().getFiles().iterator().next();
            try (InputStream stream = fileReference.open()) {
                ByteString byteString = ByteString.readFrom(stream, 1024);
                channelHandlerContext.channel().write(NetData.NetMessage.newBuilder().setModuleData(NetData.ModuleData.newBuilder().setModule(byteString)).build());
                result.setVersion(module.getVersion().toString());
                result.setSize(byteString.size());
                channelHandlerContext.channel().write(NetData.NetMessage.newBuilder().setModuleDataHeader(result).build());
            } catch (IOException e) {
                logger.error("Error sending module", e);
                channelHandlerContext.channel().close();
                break;
            }
        }
    }
}
Also used : NetData(org.terasology.protobuf.NetData) InputStream(java.io.InputStream) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) Module(org.terasology.gestalt.module.Module) FileReference(org.terasology.gestalt.module.resources.FileReference) ArchiveFileSource(org.terasology.gestalt.module.resources.ArchiveFileSource) Name(org.terasology.gestalt.naming.Name)

Aggregations

ByteString (com.google.protobuf.ByteString)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Module (org.terasology.gestalt.module.Module)1 ArchiveFileSource (org.terasology.gestalt.module.resources.ArchiveFileSource)1 FileReference (org.terasology.gestalt.module.resources.FileReference)1 Name (org.terasology.gestalt.naming.Name)1 NetData (org.terasology.protobuf.NetData)1