org.aotorrent.common.protocol.tracker.UDPAnnounceRequest.java Source code

Java tutorial

Introduction

Here is the source code for org.aotorrent.common.protocol.tracker.UDPAnnounceRequest.java

Source

/*
 * Copyright 2014 Napolov Dmitry
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.aotorrent.common.protocol.tracker;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * @author dmitry 17.08.14 16:12
 */
public class UDPAnnounceRequest implements UDPTrackerMessage {
    private static final int action = 1;

    private final long connectionId;
    private final int transactionId;
    private final byte[] infoHash;
    private final byte[] peerId;
    private final long downloaded;
    private final long left;
    private final long uploaded;
    private final int event;
    private final int port;

    public UDPAnnounceRequest(long connectionId, int transactionId, byte[] infoHash, byte[] peerId, long downloaded,
            long left, long uploaded, int event, int port) {
        this.connectionId = connectionId;
        this.transactionId = transactionId;
        this.infoHash = infoHash;
        this.peerId = peerId;
        this.downloaded = downloaded;
        this.left = left;
        this.uploaded = uploaded;
        this.event = event;
        this.port = port;
    }

    @Override
    public ByteBuf toTransmit() {
        final ByteBuf buf = Unpooled.buffer(98, 98);
        buf.writeLong(connectionId);
        buf.writeInt(action);
        buf.writeInt(transactionId);
        buf.writeBytes(infoHash);
        buf.writeBytes(peerId);
        buf.writeLong(downloaded);
        buf.writeLong(left);
        buf.writeLong(uploaded);
        buf.writeInt(event);
        buf.writeInt(0);
        buf.writeInt(0); // key
        buf.writeInt(-1);
        buf.writeShort(port);
        return buf;
    }
}