All Downloads are FREE. Search and download functionalities are using the official Maven repository.

chat.dim.stargate.StarShip Maven / Gradle / Ivy

/* license: https://mit-license.org
 *
 *  Star Gate: Interfaces for network connection
 *
 *                                Written in 2020 by Moky 
 *
 * ==============================================================================
 * The MIT License (MIT)
 *
 * Copyright (c) 2020 Albert Moky
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * ==============================================================================
 */
package chat.dim.stargate;

import java.lang.ref.WeakReference;
import java.util.Date;

import chat.dim.mtp.protocol.DataType;
import chat.dim.mtp.protocol.Package;
import chat.dim.mtp.protocol.TransactionID;
import chat.dim.gate.Ship;
import chat.dim.tlv.Data;

public class StarShip implements Ship {

    // retry
    public static int EXPIRES = 120 * 1000;  // 2 minutes
    public static int RETRIES = 2;

    // priorities
    public static final int URGENT = -1;
    public static final int NORMAL = 0;
    public static final int SLOWER = 1;

    final int priority;

    // for retry
    long timestamp = 0;  // last time
    int retries = -1;

    private final Package pack;
    private final WeakReference delegateRef;

    public StarShip(int priority, Package pack, StarGate.Delegate delegate) {
        super();
        this.priority = priority;
        this.pack = pack;
        delegateRef = new WeakReference<>(delegate);
    }

    public StarShip(int priority, byte[] payload, StarGate.Delegate delegate) {
        this(priority, Package.create(DataType.Message, payload.length, new Data(payload)), delegate);
    }

    TransactionID getTransactionID() {
        return pack.head.sn;
    }

    StarShip update() {
        timestamp = (new Date()).getTime();
        retries += 1;
        return this;
    }

    //
    //  Ship
    //

    @Override
    public Package getPackage() {
        return pack;
    }

    @Override
    public StarGate.Delegate getDelegate() {
        return delegateRef.get();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy