📄 msslacceptor.java
字号:
package net.jumperz.app.MDragonfly;
import java.net.*;
import net.jumperz.security.MSecurityUtil;
import net.jumperz.util.*;
import java.io.*;
import javax.net.ssl.*;
import javax.net.*;
import java.security.*;
import java.security.cert.*;
import java.security.cert.Certificate;
import java.security.spec.*;
public final class MSslAcceptor
implements MCommand, MSubject1
{
private int port;
private SSLSocket socket;
private ServerSocket serverSocket;
private int state;
private String host;
private boolean terminated = false;
private KeyStore keyStore;
private MSubject1 subject = new MSubject1Impl();
public static final int ACCEPTED = 0;
public static final int ERROR = 1;
public static final int LISTENING = 2;
private static final int BACKLOG = 50;
//--------------------------------------------------------------------------------------
public MSslAcceptor( KeyStore in_keyStore, String in_Host, int in_port )
{
keyStore = in_keyStore;
host = in_Host;
port = in_port;
}
//--------------------------------------------------------------------------------------
public final void execute()
{
try
{
serverSocket = getNewSocket();
state = LISTENING;
}
catch( Exception e )
{
e.printStackTrace();
state = ERROR;
}
notify1();
while( !terminated )
{
try
{
while( true )
{
socket = ( SSLSocket )serverSocket.accept();
state = ACCEPTED;
notify1();
}
}
catch( IOException e )
{
e.printStackTrace();
if( e.getMessage().equals( "Connection reset by peer" ) )
{
// it's ok
}
else
{
state = ERROR;
notify1();
}
}
}
}
//--------------------------------------------------------------------------------------
public final void breakCommand()
{
terminated = true;
try
{
if( serverSocket != null )
{
serverSocket.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
}
//--------------------------------------------------------------------------------------
public final int getState()
{
return state;
}
//--------------------------------------------------------------------------------------
public final SSLSocket getSocket()
{
return socket;
}
//--------------------------------------------------------------------------------------
private ServerSocket getNewSocket()
throws Exception
{
ServerSocketFactory serverSocketFactory = MSecurityUtil.getServerSocketFactory( keyStore );
return serverSocketFactory.createServerSocket( port, BACKLOG, InetAddress.getByName( host ) );
}
//--------------------------------------------------------------------------------------
public void notify1()
{
subject.notify1();
}
//----------------------------------------------------------------
public void register1( MObserver1 observer )
{
subject.register1( observer );
}
//----------------------------------------------------------------
public void removeObservers1()
{
subject.removeObservers1();
}
//----------------------------------------------------------------
public void removeObserver1( MObserver1 observer )
{
subject.removeObserver1( observer );
}
//----------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -