⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 workspaceconnection.java

📁 基于jxta的局域网P2P文件共享,可以实现局域网中的文件p2p共享,实现文件快速传输及交流
💻 JAVA
字号:
package connex.core.net;

import java.net.URISyntaxException;
import java.io.IOException;
import net.jxta.id.ID;
import java.net.URI;
import net.jxta.document.AdvertisementFactory;
import net.jxta.id.IDFactory;
import net.jxta.pipe.InputPipe;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.pipe.OutputPipe;
import connex.core.WS.*;
import net.jxta.peergroup.*;
import net.jxta.pipe.PipeMsgListener;
import net.jxta.endpoint.Message;
import net.jxta.pipe.PipeMsgEvent;

import java.util.*;


/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author Hisham Khalil
 * @version 1.0
 */
public class WorkspaceConnection extends Connection implements PipeMsgListener {

    private InputPipe inputPipe = null;
    private OutputPipe outputPipe = null;
    private InputPipe privinputPipe = null;
    private OutputPipe privOutputPipe = null;

    /**
     * @directed
     * @link aggregationByValue 
     */
    private PipePair pipes = null;
    private Hashtable<String,
            OutputPipe> outPipeCash = new Hashtable<String, OutputPipe>();

    /**
     *
     * @param pg PeerGroup
     * @param listener ConnectionClient
     */
    public WorkspaceConnection(PeerGroup pg, ConnectionClient listener) {
        super(pg, listener);
    }

    /**
     *
     */
    public boolean connect() {

        pipes = PipeUtils.createWorkspacePipe(pg, listener.getClientName(), this);
        if (this.inputPipe == null) {
            this.inputPipe = pipes.getInputPipe();
        }
        if (this.outputPipe == null) {
            this.outputPipe = pipes.getOutputPipe();
        }

        return (inputPipe != null && outputPipe != null) ? true : false;
    }

    public boolean disConnect() {
        if (outputPipe != null) {
            outputPipe.close();
            outputPipe = null;
        }
        if (inputPipe != null) {
            inputPipe.close();
            inputPipe = null;
        }
        listener = null;
        Enumeration num = outPipeCash.elements();
        OutputPipe op;
        while (num.hasMoreElements()) {
            op = (OutputPipe) num.nextElement();

            op.close();
            outPipeCash.remove(op);

        }

        return true;
    }


    /**
     *
     * @param msg Message
     */
    public synchronized void send(Message msg) {
        try {
            outputPipe.send(msg);
            sent++;

        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    /**
     * Creates a private pipe additional to the WorkspacePipe and return the pipeID as String
     * @return String
     */
    public String getMyBackDoorID() {
        if (privinputPipe == null) {
            this.privinputPipe = PipeUtils.createInputPipe(this);
        }
        System.out.println(privinputPipe.getType() + " created for "+this.getOwner());
        return privinputPipe.getPipeID().toString();
    }

    public synchronized void sendToBackDoor(String backID, Message msg) {
        privOutputPipe = outPipeCash.get(backID);
        if (privOutputPipe == null) {
            privOutputPipe = PipeUtils.createOutputPipe(backID);
            if (privOutputPipe != null) {
                outPipeCash.put(backID, privOutputPipe);
            }
        }
        try {
            privOutputPipe.send(msg);
            sent++;
        } catch (Exception ex) {
        }

    }


    /**
     * Called for each pipe message event that occurs.
     *
     * @param  event  The event being received.
     */


    public synchronized void pipeMsgEvent(PipeMsgEvent event) {

        Message msg = event.getMessage();
        listener.reciveMessage(msg);
        recived++;

        return;
    }

    public ConnectionClient getOwner() {
        return listener;
    }

    public synchronized long getSentMessageCount() {
        return sent;
    }

    public synchronized long getRecivedMessageCount() {
        return recived;
    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -