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