aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/cycling74/net/UdpSender.java
blob: 6ef7e346beefc89cdef33d7046f59fb4fb332d80 (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
package com.cycling74.net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

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

/** 
 * This portion of code is scheduled for pdj-0.8.5
 * IT IS NOT FUNCTIONAL
 */
public class UdpSender {
    InetAddress inetAddress;
	DatagramSocket sender;
	boolean init;
	
	String address = null;
	int port = -1;
	
	public UdpSender() {
	}
	
	public UdpSender(String address, int port) {
		this.address = address;
		this.port = port;
		initsocket();
	}
	
	public void send(Atom args[]) {
		send(Atom.toOneString(args).getBytes());
	}
	
	public void send(int i) {
		send(Integer.toString(i).getBytes());
	}
	
	public void send(float f) {
		send(Float.toString(f).getBytes());
	}

	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		this.address = address;
		initsocket();
	}
	
	public int getPort() {
		return port;
	}
	
	public void setPort(int port) {
		this.port = port;
		initsocket();
	}
	
	synchronized void initsocket() {
		try {
		    sender = null;
		    inetAddress = InetAddress.getByName(address);
			sender = new DatagramSocket();
		} catch (Exception e) {
			throw new MaxRuntimeException(e);
		}
	}

	void send(byte buff[]) {
	    if ( sender == null )
	        throw new MaxRuntimeException("UdpSender is not initialized");

	    try {
	        DatagramPacket packet = new DatagramPacket(buff, buff.length, inetAddress, port);
	        sender.send(packet);
	    } catch (IOException e) {
	        throw new MaxRuntimeException(e);
	    }

	}
}