changed folder name
This commit is contained in:
parent
4503531ec9
commit
80c6a52c69
73 changed files with 0 additions and 0 deletions
115
src/zutil/net/threaded/ThreadedTCPNetworkServer.java
Normal file
115
src/zutil/net/threaded/ThreadedTCPNetworkServer.java
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
package zutil.net.threaded;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.security.cert.CertificateException;
|
||||
|
||||
import javax.net.ssl.SSLServerSocketFactory;
|
||||
|
||||
import zutil.io.MultiPrintStream;
|
||||
|
||||
|
||||
/**
|
||||
* A simple web server that handles both cookies and
|
||||
* sessions for all the clients
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public abstract class ThreadedTCPNetworkServer extends Thread{
|
||||
public final int port;
|
||||
private File keyStore;
|
||||
private String keyStorePass;
|
||||
|
||||
/**
|
||||
* Creates a new instance of the sever
|
||||
*
|
||||
* @param port The port that the server should listen to
|
||||
*/
|
||||
public ThreadedTCPNetworkServer(int port){
|
||||
this(port, null, null);
|
||||
}
|
||||
/**
|
||||
* Creates a new instance of the sever
|
||||
*
|
||||
* @param port The port that the server should listen to
|
||||
* @param sslCert If this is not null then the server will use SSL connection with this keyStore file path
|
||||
* @param sslCert If this is not null then the server will use a SSL connection with the given certificate
|
||||
*/
|
||||
public ThreadedTCPNetworkServer(int port, File keyStore, String keyStorePass){
|
||||
this.port = port;
|
||||
this.keyStorePass = keyStorePass;
|
||||
this.keyStore = keyStore;
|
||||
}
|
||||
|
||||
|
||||
public void run(){
|
||||
ServerSocket ss = null;
|
||||
try{
|
||||
if(keyStorePass != null && keyStore != null){
|
||||
registerCertificate(keyStore, keyStorePass);
|
||||
ss = initSSL( port );
|
||||
}
|
||||
else{
|
||||
ss = new ServerSocket( port );
|
||||
}
|
||||
|
||||
while(true){
|
||||
Socket s = ss.accept();
|
||||
ThreadedTCPNetworkServerThread t = getThreadInstance( s );
|
||||
if( t!=null )
|
||||
new Thread( t ).start();
|
||||
else{
|
||||
MultiPrintStream.out.println("Unable to instantiate ThreadedTCPNetworkServerThread, closing connection!");
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace( MultiPrintStream.out );
|
||||
}
|
||||
|
||||
if( ss!=null ){
|
||||
try{
|
||||
ss.close();
|
||||
}catch(IOException e){ e.printStackTrace( MultiPrintStream.out ); }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns an new instance of the ThreadedTCPNetworkServerThread
|
||||
* that will handle the newly made connection, if an null value is returned
|
||||
* then the ThreadedTCPNetworkServer will close the new connection.
|
||||
*
|
||||
* @param s is an new connection to an host
|
||||
* @return a new instance of an thread or null
|
||||
*/
|
||||
protected abstract ThreadedTCPNetworkServerThread getThreadInstance( Socket s );
|
||||
|
||||
/**
|
||||
* Initiates a SSLServerSocket
|
||||
*
|
||||
* @param port The port to listen to
|
||||
* @return The SSLServerSocket
|
||||
* @throws IOException
|
||||
*/
|
||||
private ServerSocket initSSL(int port) throws IOException{
|
||||
SSLServerSocketFactory sslserversocketfactory =
|
||||
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
|
||||
return sslserversocketfactory.createServerSocket(port);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the given cert file to the KeyStore
|
||||
*
|
||||
* @param certFile The cert file
|
||||
*/
|
||||
protected void registerCertificate(File keyStore, String keyStorePass) throws CertificateException, IOException, KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException{
|
||||
System.setProperty("javax.net.ssl.keyStore", keyStore.getAbsolutePath());
|
||||
System.setProperty("javax.net.ssl.keyStorePassword", keyStorePass);
|
||||
}
|
||||
}
|
||||
12
src/zutil/net/threaded/ThreadedTCPNetworkServerThread.java
Normal file
12
src/zutil/net/threaded/ThreadedTCPNetworkServerThread.java
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
package zutil.net.threaded;
|
||||
|
||||
/**
|
||||
* The class that will handle the TCP connection will incclude
|
||||
* this interface
|
||||
*
|
||||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
public interface ThreadedTCPNetworkServerThread extends Runnable{
|
||||
|
||||
}
|
||||
116
src/zutil/net/threaded/ThreadedUDPNetwork.java
Normal file
116
src/zutil/net/threaded/ThreadedUDPNetwork.java
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
package zutil.net.threaded;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.MulticastSocket;
|
||||
import java.net.SocketException;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A simple web server that handles both cookies and
|
||||
* sessions for all the clients
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class ThreadedUDPNetwork extends Thread{
|
||||
public static final int BUFFER_SIZE = 512;
|
||||
|
||||
// Type of UDP socket
|
||||
enum UDPType{
|
||||
MULTICAST,
|
||||
UNICAST
|
||||
}
|
||||
protected final UDPType type;
|
||||
protected final int port;
|
||||
protected DatagramSocket socket;
|
||||
protected ThreadedUDPNetworkThread thread = null;
|
||||
|
||||
/**
|
||||
* Creates a new unicast Clien instance of the class
|
||||
*
|
||||
* @param thread is the class that will handle incoming packets
|
||||
* @throws SocketException
|
||||
*/
|
||||
public ThreadedUDPNetwork(ThreadedUDPNetworkThread thread) throws SocketException{
|
||||
this.type = UDPType.UNICAST;
|
||||
this.port = -1;
|
||||
setThread( thread );
|
||||
|
||||
socket = new DatagramSocket();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new unicast Server instance of the class
|
||||
*
|
||||
* @param thread is the class that will handle incoming packets
|
||||
* @param port is the port that the server should listen to
|
||||
* @throws SocketException
|
||||
*/
|
||||
public ThreadedUDPNetwork(ThreadedUDPNetworkThread thread, int port) throws SocketException{
|
||||
this.type = UDPType.UNICAST;
|
||||
this.port = port;
|
||||
setThread( thread );
|
||||
|
||||
socket = new DatagramSocket( port );
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new multicast Server instance of the class
|
||||
*
|
||||
* @param thread is the class that will handle incoming packets
|
||||
* @param port is the port that the server should listen to
|
||||
* @param multicast_addr is the multicast address that the server will listen on
|
||||
* @throws IOException
|
||||
*/
|
||||
public ThreadedUDPNetwork(ThreadedUDPNetworkThread thread, String multicast_addr, int port ) throws IOException{
|
||||
this.type = UDPType.MULTICAST;
|
||||
this.port = port;
|
||||
setThread( thread );
|
||||
|
||||
// init udp socket
|
||||
MulticastSocket msocket = new MulticastSocket( port );
|
||||
InetAddress group = InetAddress.getByName( multicast_addr );
|
||||
msocket.joinGroup( group );
|
||||
|
||||
socket = msocket;
|
||||
}
|
||||
|
||||
|
||||
public void run(){
|
||||
try{
|
||||
while(true){
|
||||
byte[] buf = new byte[BUFFER_SIZE];
|
||||
DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
||||
socket.receive( packet );
|
||||
if( thread!=null )
|
||||
thread.receivedPacket( packet, this );
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the given packet
|
||||
*
|
||||
* @param packet is the packet to send
|
||||
* @throws IOException
|
||||
*/
|
||||
public synchronized void send( DatagramPacket packet ) throws IOException{
|
||||
socket.send(packet);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the thread that will handle the incoming packets
|
||||
*
|
||||
* @param thread is the thread
|
||||
*/
|
||||
public void setThread(ThreadedUDPNetworkThread thread){
|
||||
this.thread = thread;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
21
src/zutil/net/threaded/ThreadedUDPNetworkThread.java
Normal file
21
src/zutil/net/threaded/ThreadedUDPNetworkThread.java
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package zutil.net.threaded;
|
||||
|
||||
import java.net.DatagramPacket;
|
||||
|
||||
/**
|
||||
* This interface is for processing received packets
|
||||
* from the TNetworkUDPServer
|
||||
*
|
||||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
public interface ThreadedUDPNetworkThread extends Runnable{
|
||||
|
||||
/**
|
||||
* Packet will be processed in this method
|
||||
*
|
||||
* @param packet is the received packet
|
||||
* @param network is the network class that received the packet
|
||||
*/
|
||||
public void receivedPacket(DatagramPacket packet, ThreadedUDPNetwork network);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue