• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.chromium;
2 
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.io.OutputStreamWriter;
7 import java.net.Socket;
8 import java.net.UnknownHostException;
9 import java.util.Set;
10 import java.util.concurrent.CopyOnWriteArraySet;
11 
12 import javax.net.SocketFactory;
13 import javax.servlet.ServletException;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 
17 import org.eclipse.jetty.websocket.WebSocket;
18 import org.eclipse.jetty.websocket.WebSocketServlet;
19 
20 public class IRCProxyWebSocket extends WebSocketServlet {
21 
22   private static final long serialVersionUID = 1L;
23 
24   private final Set<ChatWebSocket> members_ =
25       new CopyOnWriteArraySet<ChatWebSocket>();
26 
doGet(HttpServletRequest request, HttpServletResponse response)27   protected void doGet(HttpServletRequest request, HttpServletResponse response)
28       throws ServletException ,IOException {
29     getServletContext().getNamedDispatcher("default").forward(request,response);
30   }
31 
doWebSocketConnect(HttpServletRequest request, String protocol)32   protected WebSocket doWebSocketConnect(HttpServletRequest request,
33                                          String protocol) {
34     return new ChatWebSocket();
35   }
36 
37   class ChatWebSocket implements WebSocket, Runnable {
38     Outbound outbound_;
39     Socket socket_ = null;
40     OutputStreamWriter out_;
41     BufferedReader in_;
42     Thread thread_;
43     byte frame_ = 0;
44 
onConnect(Outbound outbound)45     public void onConnect(Outbound outbound) {
46       outbound_= outbound;
47     }
48 
onMessage(byte frame, byte[] data,int offset, int length)49     public void onMessage(byte frame, byte[] data,int offset, int length) {}
50 
onMessage(byte frame, String data)51     public void onMessage(byte frame, String data) {
52       try {
53         if (socket_ == null) {
54           try {
55             // We assume the client is going to connect and initiate a
56             // connection with the message "server:port".
57             String tokens[] = data.split(":");
58             socket_ = SocketFactory.getDefault().createSocket(tokens[0],
59                 Integer.parseInt(tokens[1]));
60             out_ = new OutputStreamWriter(socket_.getOutputStream());
61             InputStreamReader in = new InputStreamReader(
62                 socket_.getInputStream());
63             in_ = new BufferedReader(in);
64 
65             members_.add(this);
66             thread_ = new Thread(this);
67             thread_.start();
68 
69           } catch (UnknownHostException e) {
70             // TODO Auto-generated catch block
71             e.printStackTrace();
72           } catch (IOException e) {
73             // TODO Auto-generated catch block
74             e.printStackTrace();
75           }
76         } else {
77           System.out.print(">> " + data);
78           out_.write(data);
79           out_.flush();
80         }
81       } catch (IOException e) {
82         // TODO Auto-generated catch block
83         e.printStackTrace();
84       }
85     }
86 
onDisconnect()87     public void onDisconnect() {
88       try {
89         socket_.close();
90         thread_.stop();
91       } catch (IOException e) {
92         // TODO Auto-generated catch block
93         e.printStackTrace();
94       }
95       members_.remove(this);
96     }
97 
98     @Override
run()99     public void run() {
100       while(true) {
101         try {
102            if (in_.ready()) {
103             String line = in_.readLine();
104             System.out.println("<< " + line);
105             outbound_.sendMessage(frame_, line + "\r\n");
106 
107           } else {
108             Thread.sleep(100);
109           }
110         } catch (IOException e) {
111         } catch (InterruptedException e) {
112         }
113       }
114     }
115   }
116 }
117