annotate src/goodjava/webserver/ServerSentEvents.java @ 1741:623d9f62ab92

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 04 Nov 2022 10:03:12 -0600
parents c9c974817d0c
children d778f1f2598a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.webserver;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.Writer;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.OutputStreamWriter;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.io.BufferedWriter;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.io.IOException;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.net.Socket;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 import java.util.List;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 import java.util.ArrayList;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 import java.util.Map;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 import java.util.HashMap;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 import java.util.Collections;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 import java.util.Iterator;
1740
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
14 import java.util.Timer;
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
15 import java.util.TimerTask;
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
16 import goodjava.logging.Logger;
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
17 import goodjava.logging.LoggerFactory;
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 public class ServerSentEvents {
1740
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
21 private static final Logger logger = LoggerFactory.getLogger(ServerSentEvents.class);
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 private static class Con {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 final Socket socket;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 final Writer writer;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 Con(Socket socket) throws IOException {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 this.socket = socket;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 this.writer = new BufferedWriter( new OutputStreamWriter( socket.getOutputStream(), "UTF-8" ) );
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 private static class EventHandler {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 private final List<Con> cons = new ArrayList<Con>();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 synchronized void add(Con con) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 cons.add(con);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 synchronized void write( String url, String content ) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 Iterator<Con> iter = cons.iterator();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 while( iter.hasNext() ) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 Con con = iter.next();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 Writer writer = con.writer;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 try {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 writer.write(content);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 writer.write("\n\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 writer.flush();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 } catch(IOException e) {
1741
Franklin Schmidt <fschmidt@gmail.com>
parents: 1740
diff changeset
50 //logger.info("removing con from "+url);
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 iter.remove();
1741
Franklin Schmidt <fschmidt@gmail.com>
parents: 1740
diff changeset
52 try {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1740
diff changeset
53 con.socket.close();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1740
diff changeset
54 } catch(IOException e2) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1740
diff changeset
55 //logger.info("",e2);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1740
diff changeset
56 }
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 }
1740
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
59 if( cons.isEmpty() ) {
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 map.remove(url);
1740
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
61 //logger.info("removed "+url);
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
62 }
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 private static final Map<String,EventHandler> map
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 = Collections.synchronizedMap(new HashMap<String,EventHandler>());
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 static void add(Socket socket,Request request) throws IOException {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 Con con = new Con(socket);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 Writer writer = con.writer;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 writer.write("HTTP/1.1 200 OK\r\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 writer.write("Access-Control-Allow-Origin: *\r\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 writer.write("Cache-Control: no-cache\r\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 writer.write("Content-Type: text/event-stream\r\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 writer.write("\r\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 writer.flush();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 String url = request.url();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 EventHandler handler;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 synchronized(map) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 handler = map.get(url);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 if( handler==null ) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 handler = new EventHandler();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 map.put(url,handler);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 handler.add(con);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 public static void write( String url, String content ) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 EventHandler handler = map.get(url);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 if( handler != null )
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 handler.write(url,content);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 public static String toData(String message) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 if( message.endsWith("\n") )
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 message = message.substring( 0, message.length() - 1 );
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 return "data: " + message.replace( "\n", "\ndata: " ) + "\n";
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 public static void writeMessage( String url, String message ) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 write( url, toData(message) );
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107
1740
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
108 private static void sweep() {
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
109 List<String> urls;
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
110 synchronized(map) {
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
111 urls = new ArrayList<String>(map.keySet());
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
112 }
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
113 for( String url : urls ) {
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
114 write( url, "event: ping\n" );
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
115 }
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
116 }
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
117
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
118 private static final Timer sweeper = new Timer("ServerSentEvents",true);
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
119 static {
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
120 long period = 1000L*60*60; // hour
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
121 TimerTask task = new TimerTask() {
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
122 public void run() {
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
123 sweep();
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
124 }
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
125 };
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
126 sweeper.schedule(task,period,period);
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
127 }
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
128
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 private ServerSentEvents() {} // never
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 }