aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/cycling74/net/TcpReceiver.java
blob: 3de2343d1753ded159e3d20b6a07a4a30705067a (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.cycling74.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import com.cycling74.max.Atom;
import com.cycling74.max.Callback;
import com.cycling74.max.MaxRuntimeException;
import com.cycling74.max.MaxSystem;

/**
 * Class wrapper to receive atoms via TCP/IP using the class
 * TcpSender.
 *
 * This class is a work in progress and have been lightly tested.
 */
public class TcpReceiver implements Runnable {
    ServerSocket receiver;
    
	Callback callback = null;
	
	String debugString = null;
	int port = -1;
	boolean runnable = true;
	
	public TcpReceiver() {
	    
	}

    public TcpReceiver(int port) {
        this.port = port;
    }

    public TcpReceiver(int port, Object caller, String method) {
        this.port = port;
        this.callback = new Callback(caller, method, new Object[] { new Atom[0] });
    }

	
	public void close() {
		if ( receiver == null ) 
			return;
		runnable = false;
		try {
            receiver.close();
        } catch (IOException e) {
            e.printStackTrace();
        }		
	}
	
	public int getPort() {
		return port;
	}
	
	public void setActive(boolean active) {
	    if ( port == -1 )
            throw new MaxRuntimeException("No TCP port specified");
	    
		if ( active == true ) {
			try {
                receiver = new ServerSocket(port);
            } catch (IOException e) {
                throw new MaxRuntimeException(e);
            }
            runnable = true;
			new Thread(this, "TcpSender[" + port + "]").start();
		} else {
			close();
		}
		
	}
	
	public void setCallback(Object caller, String methodName) {
		try {
		    this.callback = new Callback(caller, methodName, new Object[] { new Atom[0] });
		} catch (Exception e) {
			throw new MaxRuntimeException(e);
		}
	}
	
	public void setPort(int port) {
		setActive(false);
		this.port = port;
	}
	
	public void setDebugString(String debugString) {
		this.debugString = debugString;
	}
	
	private void parseMessage(BufferedReader reader) throws IOException {
	    while(runnable) { 
	        String msg = reader.readLine();
            if ( debugString != null )
                MaxSystem.post(debugString + " " + msg);
        
            if ( callback != null ) {
                callback.setArgs(Atom.parse(msg));
                try {  
                    callback.execute();
                } catch( Exception e ) {
                    e.printStackTrace();
                }
            }
	    }
	}
	
	public void run() {
		try {
			while(runnable) {
	            Socket socket = receiver.accept();
	            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
	            parseMessage(reader);
			}
		} catch (Exception e) {
			if ( runnable != false) {
				runnable = false;
				throw new MaxRuntimeException(e);
			}
		}
	}
}