summaryrefslogtreecommitdiff
path: root/tutorials/java/src/phpdbg/ui/DebugSocket.java
blob: 0dfa10763d623c2220d7859b1649663b90d74d70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package phpdbg.ui;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import phpdbg.ui.JConsole.MessageType;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Manage input and output data
 * @author krakjoe
 */
public class DebugSocket extends Socket implements Runnable {
    private final Boolean reader;
    private final JConsole main;
    private Boolean quit;

    public DebugSocket(final String host, final Integer port, final JConsole main, Boolean reader) throws IOException {
        super(host, port);
        
        this.main = main;
        this.reader = reader;
        this.quit = false;
        
        synchronized(main) {
            if (!main.isConnected()) {
                main.setConnected(true);
            }
        }
    }
    
    public void quit() {
        synchronized(this) {
            quit = true;
            this.notifyAll(); 
        }
    }
    
    @Override public void run() {
        try {
            synchronized(this) {
                do {
                    if (reader) {
                        String command;
                        OutputStream output = getOutputStream();
                        
                        this.wait();
                        
                        command = main.getInputField().getText();
                        /* send command to stdin socket */
                        if (command != null) {    
                            output.write(
                               command.getBytes());
                            output.write("\n".getBytes());
                            output.flush();
                        }
                        main.getInputField().setText(null);
                    } else {
                        InputStream input = getInputStream();
                        /* get data from stdout socket */
                        byte[] bytes = new byte[1];
                        do {
                            /* this is some of the laziest programming I ever done */
                            if (input.available() == 0) {
                                this.wait(400);
                                continue;
                            }
                            
                            if (input.read(bytes, 0, 1) > -1) {
                                main.getOutputField()
                                        .appendANSI(new String(bytes));
                            }
                        } while (!quit);
                    }
                } while(!quit);
            }
        } catch (IOException | InterruptedException ex) {
            if (!quit) {
                main.messageBox(ex.getMessage(), MessageType.ERROR);
            }
        } finally {
            try {
                close();
            } catch (IOException ex) { /* naughty me */ } finally {
               synchronized(main) {
                   if (main.isConnected()) {
                       main.setConnected(false);
                   }
               } 
            }
        }
    }
}