File: JavaCast.java

package info (click to toggle)
icecast 1.0.0-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 392 kB
  • ctags: 243
  • sloc: ansic: 2,408; sh: 194; makefile: 139; perl: 137; java: 91; sql: 17
file content (118 lines) | stat: -rw-r--r-- 2,387 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import java.net.*;
import java.lang.*;
import java.io.*;

public class JavaCast extends Thread {

	JavaCast() {
		start();
	}

	public void run() {
		try {
			System.out.println("Starting up the server...");

			ThreadGroup threadGroup = new ThreadGroup("clients");
			
			ServerSocket ssEncoder, ssClient;
			
			ssEncoder = new ServerSocket(8001);
			ssClient = new ServerSocket(8000);

			EncoderThread et = new EncoderThread(ssEncoder.accept(), threadGroup);
			System.out.println("Encoder connected...");
			while (true) {
				Socket client = ssClient.accept();

				System.out.println("Client connected...");

				ClientThread ct = new ClientThread(client, threadGroup);
			}	
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}

	public static void main(String argv[]) {
		new JavaCast();
	}

}

class EncoderThread extends Thread {

	Socket sock;
	ThreadGroup threadGroup;
        Thread[] threadList = new Thread[25];
	byte buffer[] = new byte[1024];

	EncoderThread(Socket s, ThreadGroup ts) {
		sock = s;
		threadGroup = ts;
		start();
	}

	public void run() {
		try {
			DataInputStream in = new DataInputStream(sock.getInputStream());
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));

			while (in.readUnsignedByte() != 10) ;
			out.write("OK\n");
			out.flush();
			
			while (true) {
				in.readFully(buffer);
					
				threadGroup.enumerate(threadList);

				for (int i = 0; i < threadList.length; i++) {
 					if (threadList[i] != null && threadList[i].isAlive()) {
						((ClientThread)(threadList[i])).sendData(new String(buffer));
					}
				}
			}
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}
}

class ClientThread extends Thread {
	Socket sock;
	boolean dataAvail;
	String buffer;

	ClientThread(Socket s, ThreadGroup tg) {
		super(tg, "client");

		sock = s;
		start();
	}

	public void run() {
		serveClient();	
	}

	public synchronized void serveClient() {
		try {
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));

			out.write("HTTP/1.0 200 OK\n");
			out.write("Content-type: audio/x-mpeg\n");
			out.write("\n");

			while (true) {
				wait();
				out.write(buffer);
			}
	        } catch (Exception e) {
 			System.out.println(e.toString());
		}	
	}

	public synchronized void sendData(String s) {
		buffer = s;
		notify();
	}
}