代理服务器搭建雏形

ManagerSession.java

import java.net.ServerSocket;
import java.util.*;

/*
    管理Session的类
 */
public class ManagerSession implements CallBack{
    /*
        成员变量
     */
    TCPDeviceSession tcpDeviceSession;
    Map<String,String> MapDeviceAndSSH;//设备连接和SSH连接之间的映射
    TCPSSHSession SSHSession;//临时SSH连接
    Map<String, TCPSSHSession> MapTCPSSHSession;//SessionID与Session之间的映射

    /*
        成员函数
     */

    //构造函数
    public ManagerSession()
    {
        tcpDeviceSession = new TCPDeviceSession();
        MapDeviceAndSSH = new HashMap<>();
        //tcpDeviceSession.setNewSessionCallBack(this);
        //开启设备监听TCP
        tcpDeviceSession.run();
    }

    //设备上线回调函数
    public void DeviceSessionNewLink(String SessionID) {
        //设备上线建立新的SSH连接
        SSHSession = new TCPSSHSession();
        UUID uuid = new UUID(32,32);
        String UUIDString = String.valueOf(uuid.getLeastSignificantBits());
        MapTCPSSHSession.put(UUIDString,SSHSession);
        SSHSession.run();
        //建立映射关系
        MapDeviceAndSSH.put(SessionID,UUIDString);
    }

    //设备到SSH的映射查找
    void DeviceToSSHLink()
    {

    }
    //NewLink调用回调函数
    void NewLinkCallBack(CallBack callBack,String SessionID)
    {

    }

    public static void main(String []args)
    {
        ManagerSession managerSession = new ManagerSession();
    }


    @Override
    public void SessionNewLinkCallBack(String SessionID) {

    }
}

TCPDeviceSession.java

/*
    设备与代理服务器之间建立的Session
 */
public class TCPDeviceSession extends TCPSession{

    /*
            成员变量
         */
    private CallBack NewSessionCallBack = null;
    /*
        成员函数
     */
    public TCPDeviceSession()
    {
        super();
    }

    void Response()
    {
        //当有消息时回调给ManagerSession
        NewSessionCallBack.SessionNewLinkCallBack("test");
    }

    public CallBack getNewSessionCallBack() {
        return NewSessionCallBack;
    }

    public void setNewSessionCallBack(CallBack newSessionCallBack) {
        NewSessionCallBack = newSessionCallBack;
    }


}

TCPSession.java

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/*
    TCP连接类
 */
public class TCPSession extends Thread{
    /*
        类成员变量
     */
    private ServerSocket socket = null;
    private Map<String,Socket> socketMap = null;//建立的Session链接映射

    //创建一个线程池
    ExecutorService executorService =null;
    // 构造函数
    public TCPSession()
    {
        try
        {
            socket = new ServerSocket(8080);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        socketMap = new HashMap<>();//建立哈希Map
        executorService = Executors.newFixedThreadPool(100);
    }

    //开启监听
    void StartListen()
    {

        try
        {
            while(true)
            {
                Socket session = socket.accept();
                // 随机生成UUID
                UUID uuid =new UUID(32,32);
                String UUIDString = String.valueOf(uuid.getLeastSignificantBits());
                socketMap.put(UUIDString,session);
                //多线程建立每一个Session
                SessionThread sessionThread = new SessionThread(session);
                sessionThread.run();
                executorService.submit(sessionThread);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    class SessionThread implements Runnable
    {
        private Socket session;
        public SessionThread(Socket session)
        {
            this.session = session;
        }
        @Override
        public void run() {
            try {
                InputStream is = session.getInputStream();
                InputStreamReader isr=null;
                isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                char[] ReadByte = new char[1024];
                int length = br.read(ReadByte,0,1024);
                String Read = new String(ReadByte,0,length);
                System.out.println(Read);
                Send("200 OK /1.1 OK\r\nHello");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        //发送响应
        void Send(String send)
        {
            try
            {
                OutputStream outputStream = session.getOutputStream();
                outputStream.write(send.getBytes());
                outputStream.flush();
                outputStream.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }



    //开启多线程
    public void run()
    {
        StartListen();
    }

    //发送消息
    void Deal(String send)
    {

    }
    //销毁
    void DeleteSession(String SessionID)
    {
        try
        {
            socketMap.get(SessionID).close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    //更新
    void UpdateSession()
    {

    }
}

你可能感兴趣的:(JAVA,java,开发语言)