📄 server.java
字号:
//System.out.println( System.currentTimeMillis( ) + " Server.registerClient " + clientX.id + " " + customX ); synchronized ( clients ) { // searching for application instance for ( String id : customs.keySet( ) ) { // if got it, store client if ( customs.get( id ) == customX ) { ArrayList < Client > list = clients.get( id ); // if custom application is not unloaded if ( list != null ) list.add( clientX.client ); return; } } } } /** * Unpairs client with application * @param clientX ClientController instance * @param applicationIDX client's application **/ public static void unregisterClient ( IApplication customX , ClientController clientX ) { // System.out.println( System.currentTimeMillis( ) + " Server.unregisterClient " + clientX.id + " " + customX ); synchronized ( clients ) { // searching for application instance for ( String id : customs.keySet( ) ) { // if got it, delete it if ( customs.get( id ) == customX ) { ArrayList < Client > list = clients.get( id ); // if custom application is not unloaded if ( list != null ) list.remove( clientX.client ); return; } } } } /** * Registers a stream router * @param nameX stream name * @param routerX router instance **/ public static void registerRouter ( OStream routerX ) { // System.out.println( System.currentTimeMillis() + " Server.registerRouter " + routerX ); synchronized ( routers ) { routers.add( routerX ); } } /** * Unregisters a stream router * @param nameX stream name * @param routerX router instance **/ public static void unregisterRouter ( OStream routerX ) { // System.out.println( System.currentTimeMillis( ) + " Server.unregisterRouter " + nameX + " " + routerX ); synchronized ( routers ) { routers.remove( routerX ); } } /** * Registers a stream player * @param playerX player instance **/ public static void registerPlayer ( OStream playerX ) { // System.out.println( System.currentTimeMillis( ) + " Server.registerPlayer " + playerX ); synchronized ( players ) { players.add( playerX ); } } /** * Unregisters a stream player * @param playerX player istance **/ public static void unregisterPlayer ( OStream playerX ) { // System.out.println( System.currentTimeMillis() + " Server.unregisterPlayer " + playerX ); synchronized ( players ) { players.remove( playerX ); } } /** * Subscribes player to available router * @param playerX player instance * @param nameX stream name **/ public static void connectPlayer ( OStream playerX , String nameX ) { // System.out.println( System.currentTimeMillis() + " Server.connectPlayer " + playerX + " " + nameX ); synchronized ( routers ) { // search for router for ( OStream router : routers ) if ( router.getName( ).equals( nameX ) ) router.subscribe( playerX ); } } /** * UNSubscribes player from available router * @param playerX player instance * @param nameX stream name **/ public static void disconnectPlayer ( OStream playerX , String nameX ) { // System.out.println( System.currentTimeMillis() + " Server.connectPlayer " + playerX + " " + nameX ); synchronized ( routers ) { // search for router for ( OStream router : routers ) if ( router.getName( ).equals( nameX ) ) router.unsubscribe( playerX ); } } /** * Subscribe players to router * @param playerX player instance * @param nameX stream name */ public static void connectRouter ( OStream routerX , String nameX ) { // System.out.println( System.currentTimeMillis() + " Server.connectRouter " + routerX + " " + nameX ); synchronized ( players ) { // search for players for ( OStream player : players ) if ( player.getName( ).equals( nameX ) ) routerX.subscribe( player ); } } /** * Subscribe players to router * @param playerX player instance * @param nameX stream name */ public static void disconnectRouter ( OStream routerX , String nameX ) { // System.out.println( System.currentTimeMillis() + " Server.connectRouter " + routerX + " " + nameX ); synchronized ( players ) { // search for players for ( OStream player : players ) if ( player.getName( ).equals( nameX ) ) routerX.unsubscribe( player ); } } /** * Returns stream router names * @return copy of actual stream router names **/ public static ArrayList < String > getStreamNames ( ) { // System.out.println( System.currentTimeMillis() + " Server.getStreamRouters" ); ArrayList < String > names = new ArrayList < String > ( ); for ( OStream router : routers ) names.add( router.getName( ) ); return names; } /** * Creates a new Server instance * @param args command line arguments **/ public static void main ( String [ ] argumentsX ) { // print console greetings System.out.println( Library.SALUTE ); int index; List < String > arguments = Arrays.asList( argumentsX ); if ( argumentsX.length > 0 ) { index = arguments.indexOf( "port" ); if ( index != -1 ) Library.PORT = new Integer( arguments.get( index + 1 ) ); index = arguments.indexOf( "iostep" ); if ( index != -1 ) Library.STEPTIME = new Integer( arguments.get( index + 1 ) ); index = arguments.indexOf( "iobuffer" ); if ( index != -1 ) Library.IOBUFFER = new Integer( arguments.get( index + 1 ) ); index = arguments.indexOf( "iothreads" ); if ( index != -1 ) Library.IOTHREAD = new Integer( arguments.get( index + 1 ) ); index = arguments.indexOf( "streams" ); if ( index != -1 ) Library.STREAMDIR = new String( arguments.get( index + 1 ) ); index = arguments.indexOf( "applications" ); if ( index != -1 ) Library.CUSTOMDIR = new String( arguments.get( index + 1 ) ); if ( arguments.get( 0 ).equals( "start" ) ) new Server( ); else closeRequest( ); } else System.out.println( Library.PARAMS ); } /** * Shuts down server instance **/ public static void shutdown ( ) { // System.out.println( System.currentTimeMillis( ) + " Server.shutdown" ); // stop listening to new connections unregisterProcess( socketConnector , "sockets" ); // clone all applications container to avoid concurrencies ArrayList < String > ids = new ArrayList < String > ( customs.keySet( ) ); // close all applications for ( String id : ids ) unloadApplication( id ); // close process groups for ( ProcessGroup pool : pools.values( ) ) pool.close( ); } /** * Attempts to close a Milenia instance attached to a specific port * Creates a temporary file, then sends a zero byte to the selected port. The decoder * recognizes the attempt, since an rtmp handshake should start with 0x03 **/ public static void closeRequest ( ) { // System.out.println( System.currentTimeMillis( ) + " Server.closeRequest" ); try { // create temporary file and socket File trigger = new File( Library.CLOSEFILE ); Socket socket = new Socket( "localhost" , Library.PORT ); OutputStream stream = socket.getOutputStream( ); // trigger trigger.createNewFile( ); stream.write( 0 ); // cleanup stream.close( ); socket.close( ); } catch ( IOException exception ) { System.out.println( Library.NOPORT ); } } /** * Checks if shutdown request is valid by checking the existence of the temporary file **/ public static void shutdownCheck ( ) { // System.out.println( System.currentTimeMillis( ) + " Server.shutCheck" ); File trigger = new File( Library.CLOSEFILE ); if ( trigger.exists( ) ) { trigger.delete( ); shutdown( ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -