front_processing/extern/TUIO_JAVA/src/com/illposed/osc/OSCPortOut.java
changeset 0 6fefd4afe506
child 9 0f44b7360c8d
equal deleted inserted replaced
-1:000000000000 0:6fefd4afe506
       
     1 /**
       
     2  * @author cramakrishnan
       
     3  *
       
     4  * Copyright (C) 2004, C. Ramakrishnan / Illposed Software
       
     5  * All rights reserved.
       
     6  * 
       
     7  * See license.txt (or license.rtf) for license information.
       
     8  * 
       
     9  * 
       
    10  * OSCPortOut is the class that sends OSC messages.
       
    11  *
       
    12  * To send OSC, in your code you should instantiate and hold onto an OSCPort
       
    13  * pointing at the address and port number for the receiver.
       
    14  *
       
    15  * When you want to send an OSC message, call send().
       
    16  *
       
    17  * An example based on com.illposed.osc.test.OSCPortTest::testMessageWithArgs() :
       
    18 		OSCPort sender = new OSCPort();
       
    19 		Object args[] = new Object[2];
       
    20 		args[0] = new Integer(3);
       
    21 		args[1] = "hello";
       
    22 		OSCMessage msg = new OSCMessage("/sayhello", args);
       
    23 		 try {
       
    24 			sender.send(msg);
       
    25 		 } catch (Exception e) {
       
    26 			 showError("Couldn't send");
       
    27 		 }
       
    28  */
       
    29 
       
    30 package com.illposed.osc;
       
    31 
       
    32 import java.net.*;
       
    33 import java.io.IOException;
       
    34 import com.illposed.osc.utility.OSCByteArrayToJavaConverter;
       
    35 
       
    36 public class OSCPortOut extends OSCPort {
       
    37 
       
    38 	protected InetAddress address;
       
    39 
       
    40 	/**
       
    41 	 * Create an OSCPort that sends to newAddress, newPort
       
    42 	 * @param newAddress InetAddress
       
    43 	 * @param newPort int
       
    44 	 */
       
    45 	public OSCPortOut(InetAddress newAddress, int newPort) throws SocketException {
       
    46 		socket = new DatagramSocket();
       
    47 		address = newAddress;
       
    48 		port = newPort;
       
    49 	}
       
    50 
       
    51 	/**
       
    52 	 * Create an OSCPort that sends to newAddress, on the standard SuperCollider port
       
    53 	 * @param newAddress InetAddress
       
    54 	 *
       
    55 	 * Default the port to the standard one for SuperCollider
       
    56 	 */
       
    57 	public OSCPortOut(InetAddress newAddress) throws SocketException {
       
    58 		this(newAddress, defaultSCOSCPort);
       
    59 	}
       
    60 
       
    61 	/**
       
    62 	 * Create an OSCPort that sends to localhost, on the standard SuperCollider port
       
    63 	 * Default the address to localhost
       
    64 	 * Default the port to the standard one for SuperCollider
       
    65 	 */
       
    66 	public OSCPortOut() throws UnknownHostException, SocketException {
       
    67 		this(InetAddress.getLocalHost(), defaultSCOSCPort);
       
    68 	}
       
    69 	
       
    70 	/**
       
    71 	 * @param aPacket OSCPacket
       
    72 	 */
       
    73 	public void send(OSCPacket aPacket) throws IOException {
       
    74 		byte[] byteArray = aPacket.getByteArray();
       
    75 		DatagramPacket packet = 
       
    76 			new DatagramPacket(byteArray, byteArray.length,address,port);
       
    77 		socket.send(packet);
       
    78 	}
       
    79 }