Client.java revision 220:30369db7f5d2
1/*
2 * Copyright 1998-2007 Sun Microsystems, Inc.  All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package com.sun.hotspot.igv.connection;
26
27import com.sun.hotspot.igv.data.Group;
28import com.sun.hotspot.igv.data.services.GroupCallback;
29import com.sun.hotspot.igv.data.serialization.Parser;
30import com.sun.hotspot.igv.data.Properties.RegexpPropertyMatcher;
31import java.io.IOException;
32import java.io.InputStream;
33import java.net.Socket;
34import javax.swing.JTextField;
35import org.openide.util.Exceptions;
36import org.openide.xml.XMLUtil;
37import org.xml.sax.InputSource;
38import org.xml.sax.SAXException;
39import org.xml.sax.XMLReader;
40
41/**
42 *
43 * @author Thomas Wuerthinger
44 */
45public class Client implements Runnable, GroupCallback {
46
47    private Socket socket;
48    private JTextField networkTextField;
49    private GroupCallback callback;
50
51    public Client(Socket socket, JTextField networkTextField, GroupCallback callback) {
52        this.callback = callback;
53        this.socket = socket;
54        this.networkTextField = networkTextField;
55    }
56
57    public void run() {
58
59        try {
60            InputStream inputStream = socket.getInputStream();
61
62            if (networkTextField.isEnabled()) {
63
64                socket.getOutputStream().write('y');
65                InputSource is = new InputSource(inputStream);
66
67                try {
68                    XMLReader reader = XMLUtil.createXMLReader();
69                    Parser parser = new Parser(this);
70                    parser.parse(reader, is, null);
71                } catch (SAXException ex) {
72                    ex.printStackTrace();
73                }
74            } else {
75                socket.getOutputStream().write('n');
76            }
77
78            socket.close();
79        } catch (IOException ex) {
80            Exceptions.printStackTrace(ex);
81        }
82    }
83
84    public void started(final Group g) {
85        try {
86            RegexpPropertyMatcher matcher = new RegexpPropertyMatcher("name", ".*" + networkTextField.getText() + ".*");
87            if (g.getProperties().selectSingle(matcher) != null && networkTextField.isEnabled()) {
88                socket.getOutputStream().write('y');
89                callback.started(g);
90            } else {
91                socket.getOutputStream().write('n');
92            }
93        } catch (IOException e) {
94        }
95    }
96}
97