org.legacy.network.session.impl.OnDemandSession.java Source code

Java tutorial

Introduction

Here is the source code for org.legacy.network.session.impl.OnDemandSession.java

Source

/**
 * Copyright (c) 2014 Legacy 814
 *
 * 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 org.legacy.network.session.impl;

import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;

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

import org.legacy.Legacy;
import org.legacy.cache.Cache;
import org.legacy.network.protocol.messages.OnDemandEncryptionMessage;
import org.legacy.network.protocol.messages.OnDemandRequestMessage;
import org.legacy.network.protocol.messages.OnDemandResponseMessage;
import org.legacy.network.protocol.ondemand.XorEncoder;
import org.legacy.network.session.Session;
import org.legacy.network.session.filter.OnDemandFilter;
import org.legacy.utility.LegacyLogger;

/**
 * @author Im Frizzy <skype:kfriz1998>
 * @since Jul 14, 2014
 */
public class OnDemandSession extends Session {

    private final Deque<OnDemandRequestMessage> fileQueue = new ArrayDeque<OnDemandRequestMessage>();

    private boolean idle = true;

    private OnDemandFilter filter = new OnDemandFilter();

    /**
     * @param channel
     */
    public OnDemandSession(Channel channel) {
        super(channel);
    }

    /* (non-Javadoc)
     * @see org.legacy.network.session.Session#messageReceived(java.lang.Object)
     */
    @Override
    public void messageReceived(Object message) throws IOException {
        if (message instanceof OnDemandRequestMessage) {
            OnDemandRequestMessage request = (OnDemandRequestMessage) message;
            synchronized (fileQueue) {
                if (request.isPriority()) {
                    fileQueue.addFirst(request);
                } else {
                    fileQueue.addLast(request);
                }
                if (idle) {
                    filter.addPendingSession(this);
                    idle = false;
                }
            }
        } else if (message instanceof OnDemandEncryptionMessage) {
            OnDemandEncryptionMessage encryption = (OnDemandEncryptionMessage) message;
            XorEncoder encoder = (XorEncoder) channel.pipeline().get("OnDemandXorEncoder");
            encoder.setKey(encryption.getKey());
        }
    }

    /* (non-Javadoc)
     * @see org.legacy.network.session.Session#disconnect()
     */
    @Override
    public void disconnect() {
        fileQueue.clear();
    }

    public void processFileQueue() {
        OnDemandRequestMessage request;

        synchronized (fileQueue) {
            request = fileQueue.pop();
            if (fileQueue.isEmpty()) {
                idle = true;
            } else {
                filter.addPendingSession(this);
                idle = false;
            }
        }

        if (request != null) {
            int type = request.getType();
            int file = request.getFile();
            Cache cache = Legacy.getCache();
            ByteBuf buffer;
            try {
                if (type == 0xFF && file == 0xFF) {
                    buffer = Unpooled.wrappedBuffer(Legacy.getCache().getChecksumtable());
                } else {
                    buffer = Unpooled.wrappedBuffer(cache.getStore().read(type, file));
                    if (type != 255)
                        buffer = buffer.slice(0, buffer.readableBytes() - 2);
                }
                channel.write(new OnDemandResponseMessage(request.isPriority(), type, file, buffer));
            } catch (IOException ex) {
                LegacyLogger.fireErrorMessage(this, "Failed to service file request " + type + ", " + file + ".",
                        ex);
            }
        }
    }

}