001/* 002 * $Revision: 1353 $ 003 * $Author: tgutwin $ 004 * $Date: 2020-06-08 20:11:56 -0700 (Mon, 08 Jun 2020) $ 005 * $URL: svn://fred.webarts.bc.ca/open/trunk/projects/WebARTS/ca/bc/webarts/tools/sockets/JOrbisSocketServer.java $ 006 */ 007/* 008 * 009 * Written by Tom Gutwin - WebARTS Design. 010 * Copyright (C) 2014 WebARTS Design, North Vancouver Canada 011 * http://www.webarts.bc.ca 012 * 013 * This program is free software; you can redistribute it and/or modify 014 * it under the terms of the GNU General Public License as published by 015 * the Free Software Foundation; either version 2 of the License, or 016 * (at your option) any later version. 017 * 018 * This program is distributed in the hope that it will be useful, 019 * but WITHOUT ANY WARRANTY; without_ even the implied warranty of 020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 021 * GNU General Public License for more details. 022 * 023 * You should have received a copy of the GNU General Public License 024 * along with this program; if not, write to the Free Software 025 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 026 */ 027package ca.bc.webarts.tools.sockets; 028 029import java.io.DataInputStream; 030import java.io.PrintStream; 031import java.io.BufferedReader; 032import java.io.InputStreamReader; 033import java.io.IOException; 034import java.net.Socket; 035import java.net.UnknownHostException; 036 037import com.jcraft.jogg.*; 038 039import com.jcraft.jorbis.*; 040 041import ca.bc.webarts.JOggPlayerListener; 042 043 044/** 045 * A Multi-Threaded TCP Socket server that listens for requests and then performs Ogg Vorbis playing functions 046 * (wrapped in the {@link PirateTunesClientThread PirateTunesClientThread}) on the server. 047 * It acts as a Network based relay of requests. 048 * It works well with its companion class {@link ca.bc.webarts.tools.sockets.TCPSocketClient TCPSocketClient}. <br><br> 049 * 050 * This class has a main method to execute directly as an app listening on its {@link #DEFAULT_PORT DEFAULT_port} {@link #DEFAULT_PORT 44444}.<pre> 051 * java ca.bc.webarts.tools.sockets.TCPSocketServer [portNumber] 052 * - portNumber is optional</pre> 053 * Its default function is to take whatever message 054 * is sent and <b>execute it directly on the commanline</b>. You should change this to whatever you want. <br><br> 055 * It can also be wrapped in another java class<br>Example usage :<br> 056 * <pre> TCPSocketServer tcpServer = new TCPSocketServer(portToUse); 057 058 int i = 0; 059 boolean posted = false; 060 while (tcpServer.getSocket()!=null) 061 { 062 try 063 { 064 // Listening for another client connection 065 if (debug_>1) System.out.println("Accepting new connections "); 066 posted = tcpServer.postConnection(tcpServer.getSocket().accept()); 067 068 //if (debug_>1) System.out.println("connectionPosted> "+posted); 069 if (!posted) 070 { 071 if (debug_>0) System.out.println(" NOT Posted: ClientThreads MAXXED: "); 072 } 073 } 074 catch (IOException e) 075 { 076 System.out.println(e); 077 } 078 } 079 </pre> 080 **/public class JOrbisSocketServer extends ca.bc.webarts.tools.sockets.TCPSocketServer 081{ 082 protected static String clientThreadClassname_ = "ca.bc.webarts.tools.sockets.PirateTunesClientThread"; 083 protected static JOggPlayerListener player_ = JOggPlayerListener.getInstance(); 084 085 /** Default constructor that uses default server and port. **/ 086 public JOrbisSocketServer() 087 { 088 super(); 089 setClientThreadClassname(); 090 } 091 092 093 /** Constructor that uses sets a specified server and port. **/ 094 public JOrbisSocketServer( int portNum) 095 { 096 super(portNum); 097 setClientThreadClassname(); 098 } 099 100 101 /** Gets an instantiated ClientThread connected tothe assigned socket. **/ 102 @Override 103 public ClientThread getClientThreadInstance() 104 { 105 setClientThreadClassname(); 106 System.out.println(" JOrbisSocketServer: Getting new instance of PirateTunesClientThread"); 107 PirateTunesClientThread pt = new PirateTunesClientThread(clientSocket_); 108 //pt.setPlayer(player_); 109 return pt; 110 } 111 112 113 public static void setClientThreadClassname(){setClientThreadClassname(getClientThreadClassname());} 114 115 116 public static void main(String[] args) 117 { 118 int portToUse= TCPSocketServer.DEFAULT_PORT; 119 if (args.length < 1) 120 { 121 System.out.println("Usage: java ca.bc.webarts.tools.sockets.JOrbisSocketServer <portNumber>\n" + 122 " Using DEFAULT port number=" + portToUse); 123 } 124 else 125 { 126 System.out.println("JOrbisSocketServer Starting." ); 127 if (args.length >0) 128 try { portToUse= Integer.valueOf(args[0]).intValue();System.out.println(" Using port number=" + portToUse);} 129 catch (Exception ex) 130 { 131 System.out.println(" ERROR: ya gotta gimme a number to use for the listeningPORT\n Please try again if you want a non-Defaultport" ); 132 } 133 else 134 { 135 // use defaults for port 136 System.out.println("Usage: java ca.bc.webarts.tools.sockets.JOrbisSocketServer <portNumber>\n" + 137 " Using DEFAULT port number=" + portToUse); 138 } 139 } 140 JOrbisSocketServer jorbisServer = new JOrbisSocketServer(portToUse); 141 142 int i = 0; 143 boolean posted = false; 144 while (jorbisServer.getSocket()!=null) 145 { 146 try 147 { 148 // Listening for another client connection 149 if (debug_>1) System.out.println("\n*\n*\n*\n JOrbisSocketServer: Accepting new connections !\n*\n*\n*"); 150 posted = jorbisServer.postConnection(jorbisServer.getSocket().accept()); // accept blocks until a connection comes in 151 152 //if (debug_>1) System.out.println("connectionPosted> "+posted); 153 if (!posted) 154 { 155 if (debug_>0) System.out.println(" NOT Posted: ClientThreads MAXXED: "); 156 } 157 } 158 catch (IOException e) 159 { 160 System.out.println(e); 161 } 162 i++; 163 } 164 if (debug_>0) System.out.println("Done, Exiting"); 165 } 166}