OpenCores
URL https://opencores.org/ocsvn/adv_debug_sys/adv_debug_sys/trunk

Subversion Repositories adv_debug_sys

[/] [adv_debug_sys/] [trunk/] [Software/] [AdvancedWatchpointControl/] [src/] [advancedWatchpointControl/] [networkSystem.java] - Blame information for rev 51

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 51 nyawn
// The reference to mainControl is only used for log messages and
2
// network status.  It could easily be replace with a dedicated
3
// log-handling object.
4
 
5
package advancedWatchpointControl;
6
 
7
import java.io.BufferedReader;
8
import java.io.DataOutputStream;
9
import java.io.IOException;
10
import java.io.InputStreamReader;
11
import java.net.Socket;
12
import java.net.UnknownHostException;
13
import java.nio.charset.Charset;
14
 
15
 
16
public class networkSystem /*implements Runnable*/ {
17
 
18
        private Socket sockfd = null;
19
        private DataOutputStream ostream = null;
20
        private BufferedReader istream = null;
21
        private boolean isValid = false;
22
        // We use mainControl for logging / displaying network state
23
        private mainControl mCtrl = null;
24
 
25
 
26
        public networkSystem(mainControl mc) {
27
                mCtrl = mc;
28
        }
29
 
30
        protected void finalize() throws Throwable {
31
                disconnect(true);
32
        }
33
 
34
        public void connect(String hostname, int portNumber) {
35
                disconnect(true);
36
                System.out.println("Network connecting");
37
                try {
38
                        sockfd = new Socket(hostname, portNumber);
39
                        sockfd.setSoLinger(false, 1);
40
                    istream = new BufferedReader(new InputStreamReader(sockfd.getInputStream(), Charset.forName("US-ASCII")));
41
                    ostream = new DataOutputStream(sockfd.getOutputStream());
42
            }
43
                catch (UnknownHostException e) {
44
                        disconnect(false);
45
                mCtrl.setNetworkStatus(mainControl.connectionStatus.CONNECT_ERROR);
46
            mCtrl.setLogMessage("Unknown host: " + e);
47
            return;
48
        }
49
            catch (IOException e) {
50
                disconnect(false);
51
                mCtrl.setNetworkStatus(mainControl.connectionStatus.CONNECT_ERROR);
52
                mCtrl.setLogMessage("Connect error: " + e.getMessage());
53
                return;
54
            }
55
 
56
            isValid = true;
57
        mCtrl.setNetworkStatus(mainControl.connectionStatus.CONNECTED);
58
        String msg = "Connected to server \"" + hostname + "\", port " + portNumber;
59
        mCtrl.setLogMessage(msg);
60
        System.out.println("Network connected");
61
        mCtrl.doReadAllRegisters();  // Gets regs from target, sets GUI
62
        }
63
 
64
        public void disconnect(boolean waitForReaderExit) {
65
                System.out.println("Network disconnecting");
66
                isValid = false;
67
 
68
                try {
69
                        if(sockfd != null)
70
                                sockfd.shutdownInput();
71
                        if(sockfd != null)
72
                                sockfd.close();
73
                }
74
                catch (IOException e) {
75
                        // Ignore errors when closing socket
76
                }
77
 
78
        sockfd = null;
79
        istream = null;
80
        ostream = null;
81
        }
82
 
83
        public boolean isConnected() {
84
                return isValid;
85
        }
86
 
87
        public boolean sendData(String outdata) {
88
                if(isValid) {
89
                        try {
90
                                ostream.writeBytes(outdata);
91
                        }
92
                        catch (IOException e) {
93
                        mCtrl.setNetworkStatus(mainControl.connectionStatus.CONNECT_ERROR);
94
                        mCtrl.setLogMessage(e.getMessage());
95
                        disconnect(true);
96
                        return false;
97
                        }
98
                }
99
                else {
100
                        mCtrl.setLogMessage("Not connected: connect to server first.");
101
                        return false;
102
                }
103
                return true;
104
        }
105
 
106
        public int getChar() throws IOException {
107
 
108
                if(istream != null)
109
                        return istream.read();
110
 
111
                throw(new IOException("Network reader is null!"));
112
        }
113
 
114
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.