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/JOggPlayerSocketServer.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 ca.bc.webarts.tools.sockets.PirateTunesClientThread PirateTunesClientThread}) on the server. 047 * It acts as a Network based relay of vorbis ogg requests. 048 * It works well with its companion class {@link ca.bc.webarts.tools.sockets.TCPSocketClient TCPSocketClient}. <br><br> 049 * For the Vorbis Ogg Playing, {@link ca.bc.webarts.tools.sockets.PirateTunesClientThread PirateTunesClientThread} 050 * uses the {@link ca.bc.webarts.JOggPlayerListener JOggPlayerListener} singleton JOggPlayer class. <br><br> 051 * 052 * This jOggPlayerServer class has a main method to execute directly as an app listening on its {@link #DEFAULT_PORT DEFAULT_port} {@link #DEFAULT_PORT 44444}.<pre> 053 * java ca.bc.webarts.tools.sockets.JOggPlayerSocketServer [portNumber] 054 * - portNumber is optional</pre> 055 * Its default function is to take whatever message 056 * is sent and <b>execute it directly on the commanline</b>. You should change this to whatever you want. <br><br> 057 * It can also be wrapped in another java class<br>Example usage :<br> 058 * <pre> JOggPlayerSocketServer jOggPlayerServer = new JOggPlayerSocketServer(portToUse); 059 060 int i = 0; 061 boolean posted = false; 062 while (jOggPlayerServer.getSocket()!=null) 063 { 064 try 065 { 066 // Listening for another client connection 067 if (debug_>1) System.out.println("Accepting new connections "); 068 posted = jOggPlayerServer.postConnection(jOggPlayerServer.getSocket().accept()); 069 070 //if (debug_>1) System.out.println("connectionPosted> "+posted); 071 if (!posted) 072 { 073 if (debug_>0) System.out.println(" NOT Posted: ClientThreads MAXXED: "); 074 } 075 } 076 catch (IOException e) 077 { 078 System.out.println(e); 079 } 080 } 081 </pre> 082 **/public class JOggPlayerSocketServer extends ca.bc.webarts.tools.sockets.TCPSocketServer 083{ 084 /** Name of the forked thread that will run the clients request **/ 085 protected static String clientThreadClassname_ = "ca.bc.webarts.tools.sockets.PirateTunesClientThread"; 086 protected static JOggPlayerListener player_ = JOggPlayerListener.getInstance(); 087 088 /** Default constructor that uses default server and port. **/ 089 public JOggPlayerSocketServer() 090 { 091 super(); 092 setClientThreadClassname(); 093 } 094 095 096 /** Constructor that uses sets a specified server and port. **/ 097 public JOggPlayerSocketServer( int portNum) 098 { 099 super(portNum); 100 setClientThreadClassname(); 101 } 102 103 104 /** Gets an instantiated ClientThread connected tothe assigned socket. **/ 105 @Override 106 public ClientThread getClientThreadInstance() 107 { 108 setClientThreadClassname(); 109 if (debug_>1) System.out.println(" JOggPlayerSocketServer: Getting new instance of PirateTunesClientThread"); 110 PirateTunesClientThread pt = new PirateTunesClientThread(clientSocket_); 111 //pt.setPlayer(player_); 112 return pt; 113 } 114 115 116 public static void setClientThreadClassname(){setClientThreadClassname(getClientThreadClassname());} 117 118 119 public static void main(String[] args) 120 { 121 int portToUse= TCPSocketServer.DEFAULT_PORT; 122 if (args.length < 1) 123 { 124 System.out.println("Usage: java ca.bc.webarts.tools.sockets.JOggPlayerSocketServer <portNumber>\n" + 125 " Using DEFAULT port number=" + portToUse); 126 } 127 else 128 { 129 System.out.println("JOggPlayerSocketServer Starting." ); 130 if (args.length >0) 131 try { portToUse= Integer.valueOf(args[0]).intValue();System.out.println(" Using port number=" + portToUse);} 132 catch (Exception ex) 133 { 134 System.out.println(" ERROR: ya gotta gimme a number to use for the listening PORT\n Please try again if you want a non-Defaultport" ); 135 } 136 else 137 { 138 // use defaults for port 139 System.out.println("Usage: java ca.bc.webarts.tools.sockets.JOggPlayerSocketServer <portNumber>\n" + 140 " Using DEFAULT port number=" + portToUse); 141 } 142 } 143 if (debug_>=0) System.out.println("Starting listening on port="+portToUse); 144 JOggPlayerSocketServer jorbisServer = new JOggPlayerSocketServer(portToUse); 145 146 int i = 0; 147 boolean posted = false; 148 while (jorbisServer.getSocket()!=null) 149 { 150 try 151 { 152 // Listening for another client connection 153 if (debug_>1) System.out.println("\n*\n*\n*\n JOggPlayerSocketServer: Accepting new connections !\n*"); 154 posted = jorbisServer.postConnection(jorbisServer.getSocket().accept()); // accept blocks until a connection comes in 155 156 //if (debug_>1) System.out.println("connectionPosted> "+posted); 157 if (!posted) 158 { 159 if (debug_>0) System.out.println(" NOT Posted: ClientThreads MAXXED: "); 160 } 161 } 162 catch (IOException e) 163 { 164 System.out.println(e); 165 } 166 i++; 167 } 168 if (debug_>0) System.out.println("Done, Exiting"); 169 } 170}