ManagerSession.java
import java.net.ServerSocket;
import java.util.*;
public class ManagerSession implements CallBack{
TCPDeviceSession tcpDeviceSession;
Map<String,String> MapDeviceAndSSH;
TCPSSHSession SSHSession;
Map<String, TCPSSHSession> MapTCPSSHSession;
public ManagerSession()
{
tcpDeviceSession = new TCPDeviceSession();
MapDeviceAndSSH = new HashMap<>();
tcpDeviceSession.run();
}
public void DeviceSessionNewLink(String SessionID) {
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);
}
void DeviceToSSHLink()
{
}
void NewLinkCallBack(CallBack callBack,String SessionID)
{
}
public static void main(String []args)
{
ManagerSession managerSession = new ManagerSession();
}
@Override
public void SessionNewLinkCallBack(String SessionID) {
}
}
TCPDeviceSession.java
public class TCPDeviceSession extends TCPSession{
private CallBack NewSessionCallBack = null;
public TCPDeviceSession()
{
super();
}
void Response()
{
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;
public class TCPSession extends Thread{
private ServerSocket socket = null;
private Map<String,Socket> socketMap = null;
ExecutorService executorService =null;
public TCPSession()
{
try
{
socket = new ServerSocket(8080);
}
catch (Exception e)
{
e.printStackTrace();
}
socketMap = new HashMap<>();
executorService = Executors.newFixedThreadPool(100);
}
void StartListen()
{
try
{
while(true)
{
Socket session = socket.accept();
UUID uuid =new UUID(32,32);
String UUIDString = String.valueOf(uuid.getLeastSignificantBits());
socketMap.put(UUIDString,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()
{
}
}