Package net.plan99.payfile.utils

Examples of net.plan99.payfile.utils.Exceptions$CodeThatCanThrow


                sendError(e);
            } catch (IOException ignored) {}
        } catch (Throwable t) {
            // Internal server error.
            try {
                sendError(new ProtocolException(ProtocolException.Code.INTERNAL_ERROR, "Internal server error: " + t.toString()));
            } catch (IOException ignored) {}
        } finally {
            forceClose();
        }
    }
View Full Code Here


                break;
            case DOWNLOAD_CHUNK:
                downloadChunk(msg.getDownloadChunk());
                break;
            default:
                throw new ProtocolException("Unknown message");
        }
    }
View Full Code Here

    private void checkForNetworkMismatch(Payfile.QueryFiles queryFiles) throws ProtocolException {
        final String theirNetwork = queryFiles.getBitcoinNetwork();
        final String myNetwork = wallet.getParams().getId();
        if (!theirNetwork.equals(myNetwork)) {
            final String msg = String.format("Client is using '%s' and server is '%s'", theirNetwork, myNetwork);
            throw new ProtocolException(ProtocolException.Code.NETWORK_MISMATCH, msg);
        }
    }
View Full Code Here

                    file = f;
                    break;
                }
            }
            if (file == null)
                throw new ProtocolException("DOWNLOAD_CHUNK specified invalid file handle " + downloadChunk.getHandle());
            if (downloadChunk.getNumChunks() <= 0)
                throw new ProtocolException("DOWNLOAD_CHUNK: num_chunks must be >= 1");
            if (file.getPricePerChunk() > 0) {
                // How many chunks can the client afford with their current balance?
                PaymentChannelServerState state = payments == null ? null : payments.state();
                if (state == null)
                    throw new ProtocolException("Payment channel not initiated but this file is not free");
                long balance = state.getBestValueToMe().longValue();
                long affordableChunks = balance / file.getPricePerChunk();
                if (affordableChunks < downloadChunk.getNumChunks())
                    throw new ProtocolException("Insufficient payment received for requested amount of data: got " + balance);
                balance -= downloadChunk.getNumChunks();
            }
            for (int i = 0; i < downloadChunk.getNumChunks(); i++) {
                long chunkId = downloadChunk.getChunkId() + i;
                if (chunkId == 0)
                    log.info("{}: Starting download of {}", peerName, file.getFileName());
                // This is super inefficient.
                File diskFile = new File(directoryToServe, file.getFileName());
                FileInputStream fis = new FileInputStream(diskFile);
                final long offset = chunkId * CHUNK_SIZE;
                if (fis.skip(offset) != offset)
                    throw new IOException("Bogus seek");
                byte[] chunk = new byte[CHUNK_SIZE];
                final int bytesActuallyRead = fis.read(chunk);
                if (bytesActuallyRead < 0) {
                    log.debug("Reached EOF");
                } else if (bytesActuallyRead > 0 && bytesActuallyRead < chunk.length) {
                    chunk = Arrays.copyOf(chunk, bytesActuallyRead);
                }
                Payfile.PayFileMessage msg = Payfile.PayFileMessage.newBuilder()
                        .setType(Payfile.PayFileMessage.Type.DATA)
                        .setData(Payfile.Data.newBuilder()
                                .setChunkId(downloadChunk.getChunkId())
                                .setHandle(file.getHandle())
                                .setData(ByteString.copyFrom(chunk))
                                .build()
                        ).build();
                writeMessage(msg);
            }
        } catch (IOException e) {
            throw new ProtocolException("Error reading from disk: " + e.getMessage());
        }
    }
View Full Code Here

                break;
            case PAYMENT:
                handlePayment(msg.getPayment());
                break;
            default:
                throw new ProtocolException("Unhandled message");
        }
    }
View Full Code Here

            code = ProtocolException.Code.valueOf(error.getCode());
        } catch (IllegalArgumentException e) {
            log.error("{}: Unknown error code: {}", socket, error.getCode());
            code = ProtocolException.Code.GENERIC;
        }
        throw new ProtocolException(code, error.getExplanation());
    }
View Full Code Here

    private void handlePayment(ByteString payment) throws ProtocolException {
        try {
            Protos.TwoWayChannelMessage paymentMessage = Protos.TwoWayChannelMessage.parseFrom(payment);
            paymentChannelClient.receiveMessage(paymentMessage);
        } catch (InvalidProtocolBufferException e) {
            throw new ProtocolException("Could not parse payment message: " + e.getMessage());
        } catch (InsufficientMoneyException e) {
            // This shouldn't happen as we shouldn't try to open a channel larger than what we can afford.
            throw new RuntimeException(e);
        }
    }
View Full Code Here

    }

    private void handleData(Payfile.Data data) throws IOException, ProtocolException {
        File file = handleToFile(data.getHandle());
        if (file == null)
            throw new ProtocolException("Unknown handle");
        if (data.getChunkId() == file.nextChunk - 1) {
            final byte[] bits = data.getData().toByteArray();
            file.bytesDownloaded += bits.length;
            file.downloadStream.write(bits);
            if ((data.getChunkId() + 1) * chunkSize >= file.getSize()) {
                // File is done.
                file.downloadStream.close();
                currentDownloads.remove(file);
                file.completionFuture.complete(null);
                currentFuture = null;
            } else {
                downloadNextChunk(file);
            }
        } else {
            throw new ProtocolException("Server sent wrong part of file");
        }
    }
View Full Code Here

        }
    }

    private void handleManifest(Payfile.Manifest manifest) throws ProtocolException {
        if (currentQuery == null)
            throw new ProtocolException("Got MANIFEST before QUERY_FILES");
        List<File> files = new ArrayList<>(manifest.getFilesCount());
        for (Payfile.File f : manifest.getFilesList()) {
            File file = new File(f.getFileName(), f.getDescription(), f.getHandle(), f.getSize(), f.getPricePerChunk());
            files.add(file);
        }
View Full Code Here

            try {
                running = true;
                while (true) {
                    int len = input.readInt();
                    if (len < 0 || len > 1024*1024)
                        throw new ProtocolException("Server sent message that's too large: " + len);
                    byte[] bits = new byte[len];
                    input.readFully(bits);
                    Payfile.PayFileMessage msg = Payfile.PayFileMessage.parseFrom(bits);
                    handle(msg);
                }
View Full Code Here

TOP

Related Classes of net.plan99.payfile.utils.Exceptions$CodeThatCanThrow

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.