Server.java revision 1472:c18cbe5936b8
11849Swollman/*
2108176Skan * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
3108176Skan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4108176Skan *
5108176Skan * This code is free software; you can redistribute it and/or modify it
6108176Skan * under the terms of the GNU General Public License version 2 only, as
7108176Skan * published by the Free Software Foundation.  Oracle designates this
8108176Skan * particular file as subject to the "Classpath" exception as provided
9108176Skan * by Oracle in the LICENSE file that accompanied this code.
10108176Skan *
11108176Skan * This code is distributed in the hope that it will be useful, but WITHOUT
12108176Skan * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13108176Skan * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14108176Skan * version 2 for more details (a copy is included in the LICENSE file that
15108176Skan * accompanied this code).
16108176Skan *
17108176Skan * You should have received a copy of the GNU General Public License version
18108176Skan * 2 along with this work; if not, write to the Free Software Foundation,
19108176Skan * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20108176Skan *
21108176Skan * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22108176Skan * or visit www.oracle.com if you need additional information or have any
23108176Skan * questions.
24108176Skan */
25108176Skanpackage com.sun.hotspot.igv.connection;
26108176Skan
27108176Skanimport com.sun.hotspot.igv.data.Group;
28108176Skanimport com.sun.hotspot.igv.data.services.GroupCallback;
291849Swollmanimport com.sun.hotspot.igv.data.services.GroupReceiver;
301849Swollmanimport com.sun.hotspot.igv.settings.Settings;
3185437Speterimport java.awt.Component;
3293000Sobrienimport java.io.IOException;
331849Swollmanimport java.net.ServerSocket;
341849Swollmanimport java.net.Socket;
35108176Skanimport java.util.prefs.PreferenceChangeEvent;
36108176Skanimport java.util.prefs.PreferenceChangeListener;
37108176Skanimport javax.swing.SwingUtilities;
38108176Skanimport org.openide.DialogDisplayer;
39108176Skanimport org.openide.NotifyDescriptor;
40184548Speterimport org.openide.util.RequestProcessor;
41217106Skib
42217106Skib/**
43 *
44 * @author Thomas Wuerthinger
45 */
46public class Server implements GroupCallback, GroupReceiver, PreferenceChangeListener {
47
48    private javax.swing.JPanel jPanel1;
49    private javax.swing.JCheckBox networkCheckBox;
50    private javax.swing.JTextField networkTextField;
51    private ServerSocket serverSocket;
52    private GroupCallback callback;
53    private int port;
54    private Runnable serverRunnable;
55
56    public Component init(GroupCallback callback) {
57
58        this.callback = callback;
59
60        jPanel1 = new javax.swing.JPanel();
61        networkTextField = new javax.swing.JTextField();
62        networkCheckBox = new javax.swing.JCheckBox();
63
64
65        jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
66        jPanel1.setLayout(new java.awt.BorderLayout(10, 10));
67        jPanel1.add(networkTextField, java.awt.BorderLayout.CENTER);
68
69        networkCheckBox.setSelected(true);
70        org.openide.awt.Mnemonics.setLocalizedText(networkCheckBox, "Receive when name contains");
71        networkCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
72        networkCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
73        networkCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
74
75            public void stateChanged(javax.swing.event.ChangeEvent evt) {
76                networkCheckBoxChanged(evt);
77            }
78        });
79        jPanel1.add(networkCheckBox, java.awt.BorderLayout.WEST);
80        networkCheckBox.getAccessibleContext().setAccessibleName("Read from network when name contains");
81
82        initializeNetwork();
83        Settings.get().addPreferenceChangeListener(this);
84        return jPanel1;
85    }
86
87    private void networkCheckBoxChanged(javax.swing.event.ChangeEvent evt) {
88        networkTextField.setEnabled(networkCheckBox.isSelected());
89    }
90
91    public void preferenceChange(PreferenceChangeEvent e) {
92
93        int curPort = Integer.parseInt(Settings.get().get(Settings.PORT, Settings.PORT_DEFAULT));
94        if (curPort != port) {
95            initializeNetwork();
96        }
97    }
98
99    private void initializeNetwork() {
100
101        int curPort = Integer.parseInt(Settings.get().get(Settings.PORT, Settings.PORT_DEFAULT));
102        this.port = curPort;
103        try {
104            serverSocket = new java.net.ServerSocket(curPort);
105        } catch (IOException ex) {
106            NotifyDescriptor message = new NotifyDescriptor.Message("Could not create server. Listening for incoming data is disabled.", NotifyDescriptor.ERROR_MESSAGE);
107            DialogDisplayer.getDefault().notifyLater(message);
108            return;
109        }
110
111        Runnable runnable = new Runnable() {
112
113            public void run() {
114                while (true) {
115                    try {
116                        Socket clientSocket = serverSocket.accept();
117                        if (serverRunnable != this) {
118                            clientSocket.close();
119                            return;
120                        }
121                        RequestProcessor.getDefault().post(new Client(clientSocket, networkTextField, Server.this), 0, Thread.MAX_PRIORITY);
122                    } catch (IOException ex) {
123                        serverSocket = null;
124                        NotifyDescriptor message = new NotifyDescriptor.Message("Error during listening for incoming connections. Listening for incoming data is disabled.", NotifyDescriptor.ERROR_MESSAGE);
125                        DialogDisplayer.getDefault().notifyLater(message);
126                        return;
127                    }
128                }
129            }
130        };
131
132        serverRunnable = runnable;
133
134        RequestProcessor.getDefault().post(runnable, 0, Thread.MAX_PRIORITY);
135    }
136
137    public void started(final Group g) {
138        SwingUtilities.invokeLater(new Runnable() {
139
140            public void run() {
141                callback.started(g);
142            }
143        });
144    }
145}
146