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 54

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 54 nyawn
////////////////////////////////////////////////////////////////
2
//
3
// networkSystem.java
4
//
5
// Copyright (C) 2010 Nathan Yawn 
6
//                    (nyawn@opencores.org)
7
//
8
// This class performs the low-level network I/O.
9
//
10
////////////////////////////////////////////////////////////////
11
//
12
// This source file may be used and distributed without
13
// restriction provided that this copyright statement is not
14
// removed from the file and that any derivative work contains
15
// the original copyright notice and the associated disclaimer.
16
// 
17
// This source file is free software; you can redistribute it
18
// and/or modify it under the terms of the GNU General
19
// Public License as published by the Free Software Foundation;
20
// either version 3.0 of the License, or (at your option) any
21
// later version.
22
//
23
// This source is distributed in the hope that it will be
24
// useful, but WITHOUT ANY WARRANTY; without even the implied 
25
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
26
// PURPOSE.  See the GNU Lesser General Public License for more
27
// details.
28
// 
29
// You should have received a copy of the GNU General
30
// Public License along with this source; if not, download it 
31
// from http://www.gnu.org/licenses/gpl.html
32
//
33
////////////////////////////////////////////////////////////////
34 51 nyawn
 
35
package advancedWatchpointControl;
36
 
37
import java.io.BufferedReader;
38
import java.io.DataOutputStream;
39
import java.io.IOException;
40
import java.io.InputStreamReader;
41
import java.net.Socket;
42
import java.net.UnknownHostException;
43
import java.nio.charset.Charset;
44
 
45
 
46 54 nyawn
public class networkSystem {
47 51 nyawn
 
48
        private Socket sockfd = null;
49
        private DataOutputStream ostream = null;
50
        private BufferedReader istream = null;
51
        private boolean isValid = false;
52
        // We use mainControl for logging / displaying network state
53
        private mainControl mCtrl = null;
54
 
55
 
56
        public networkSystem(mainControl mc) {
57
                mCtrl = mc;
58
        }
59
 
60
        protected void finalize() throws Throwable {
61
                disconnect(true);
62
        }
63
 
64
        public void connect(String hostname, int portNumber) {
65
                disconnect(true);
66
                System.out.println("Network connecting");
67
                try {
68
                        sockfd = new Socket(hostname, portNumber);
69
                        sockfd.setSoLinger(false, 1);
70
                    istream = new BufferedReader(new InputStreamReader(sockfd.getInputStream(), Charset.forName("US-ASCII")));
71
                    ostream = new DataOutputStream(sockfd.getOutputStream());
72
            }
73
                catch (UnknownHostException e) {
74
                        disconnect(false);
75
                mCtrl.setNetworkStatus(mainControl.connectionStatus.CONNECT_ERROR);
76
            mCtrl.setLogMessage("Unknown host: " + e);
77
            return;
78
        }
79
            catch (IOException e) {
80
                disconnect(false);
81
                mCtrl.setNetworkStatus(mainControl.connectionStatus.CONNECT_ERROR);
82
                mCtrl.setLogMessage("Connect error: " + e.getMessage());
83
                return;
84
            }
85
 
86
            isValid = true;
87
        mCtrl.setNetworkStatus(mainControl.connectionStatus.CONNECTED);
88
        String msg = "Connected to server \"" + hostname + "\", port " + portNumber;
89
        mCtrl.setLogMessage(msg);
90
        System.out.println("Network connected");
91
        mCtrl.doReadAllRegisters();  // Gets regs from target, sets GUI
92
        }
93
 
94
        public void disconnect(boolean waitForReaderExit) {
95
                System.out.println("Network disconnecting");
96
                isValid = false;
97
 
98
                try {
99
                        if(sockfd != null)
100
                                sockfd.shutdownInput();
101
                        if(sockfd != null)
102
                                sockfd.close();
103
                }
104
                catch (IOException e) {
105
                        // Ignore errors when closing socket
106
                }
107
 
108
        sockfd = null;
109
        istream = null;
110
        ostream = null;
111
        }
112
 
113
        public boolean isConnected() {
114
                return isValid;
115
        }
116
 
117
        public boolean sendData(String outdata) {
118
                if(isValid) {
119
                        try {
120
                                ostream.writeBytes(outdata);
121
                        }
122
                        catch (IOException e) {
123
                        mCtrl.setNetworkStatus(mainControl.connectionStatus.CONNECT_ERROR);
124
                        mCtrl.setLogMessage(e.getMessage());
125
                        disconnect(true);
126
                        return false;
127
                        }
128
                }
129
                else {
130
                        mCtrl.setLogMessage("Not connected: connect to server first.");
131
                        return false;
132
                }
133
                return true;
134
        }
135
 
136
        public int getChar() throws IOException {
137
 
138
                if(istream != null)
139
                        return istream.read();
140
 
141
                throw(new IOException("Network reader is null!"));
142
        }
143
 
144
}

powered by: WebSVN 2.1.0

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