PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/cc.creativecomputing/src/cc/creativecomputing/protocol/udp/CCUDPIn.java

http://creativecomputing.googlecode.com/
Java | 148 lines | 73 code | 19 blank | 56 comment | 5 complexity | 53b1bbe3d31a5ab02cc1954a96562e8c MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. package cc.creativecomputing.protocol.udp;
  2. import java.io.IOException;
  3. import java.net.DatagramPacket;
  4. import java.net.DatagramSocket;
  5. import java.net.InetAddress;
  6. import java.util.ArrayList;
  7. import java.util.Date;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. /**
  12. * <p>
  13. *
  14. * <pre>
  15. CCUDPIn receiver = new CCUDPIn(7000);
  16. CCUDPListener listener = new CCUDPListener() {
  17. public void onUDPInput(CCUDPByteInputStream theInput) {
  18. System.out.println("Message received!");
  19. }
  20. };
  21. receiver.addListener(listener);
  22. * </pre>
  23. * <p>
  24. */
  25. public class CCUDPIn implements Runnable {
  26. /**
  27. * Socket to listen to incoming UDP messages
  28. */
  29. private DatagramSocket _mySocket;
  30. /**
  31. * to this port the socket is listening
  32. */
  33. private final int _myPort;
  34. /**
  35. * true if UDPin is listening to incoming events
  36. */
  37. private boolean _myIsListening;
  38. private boolean _myClose = false;
  39. //Map of listeners reacting on all events
  40. private List<CCUDPListener> _myListeners = new ArrayList<CCUDPListener>();
  41. /**
  42. * Create an OSCPort that listens on the specified port.
  43. * @param thePort UDP port to listen on.
  44. * @see CCUDPOut
  45. */
  46. public CCUDPIn(final int thePort) {
  47. try {
  48. _myPort = thePort;
  49. _mySocket = new DatagramSocket(_myPort);
  50. } catch (Exception e) {
  51. throw new RuntimeException(e);
  52. }
  53. }
  54. public CCUDPIn(final DatagramSocket theSocket) {
  55. _myPort = theSocket.getLocalPort();
  56. _mySocket = theSocket;
  57. }
  58. /**
  59. * Close the socket if this hasn't already happened.
  60. * @see java.lang.Object#finalize()
  61. */
  62. protected void finalize() throws Throwable {
  63. super.finalize();
  64. }
  65. /**
  66. * Run the loop that listens for OSC on a socket until isListening becomes false.
  67. * @see java.lang.Runnable#run()
  68. */
  69. public void run() {
  70. byte[] buffer = new byte[1536];
  71. DatagramPacket packet = new DatagramPacket(buffer, 1536);
  72. while (_myIsListening && !_mySocket.isClosed()) {
  73. try {
  74. _mySocket.receive(packet);
  75. for(CCUDPListener myUDPListener:_myListeners) {
  76. myUDPListener.onUDPInput(new CCUDPByteInputStream(packet.getAddress(), buffer,packet.getLength()));
  77. }
  78. if(_myClose){
  79. _mySocket.close();
  80. }
  81. } catch (IOException e) {
  82. e.printStackTrace();
  83. }catch (ArrayIndexOutOfBoundsException e) {
  84. System.out.println("To many arguments");
  85. }
  86. }
  87. }
  88. /**
  89. * Start listening for incoming OSCPackets
  90. */
  91. public void startListening() {
  92. _myIsListening = true;
  93. Thread thread = new Thread(this);
  94. thread.start();
  95. }
  96. /**
  97. * Stop listening for incoming OSCPackets
  98. */
  99. public void stopListening() {
  100. _myIsListening = false;
  101. }
  102. /**
  103. * Am I listening for packets?
  104. */
  105. public boolean isListening() {
  106. return _myIsListening;
  107. }
  108. /**
  109. * Register the listener for incoming UDPPackets addressed to an address
  110. * @param theListener the object to invoke when a message comes in
  111. */
  112. public void addListener(final CCUDPListener theListener){
  113. _myListeners.add(theListener);
  114. }
  115. /**
  116. * Close the socket and free-up resources. It's recommended that clients call
  117. * this when they are done with the port.
  118. */
  119. public void close() {
  120. if(_myIsListening){
  121. _myClose = true;
  122. _myIsListening = false;
  123. }else{
  124. _mySocket.close();
  125. }
  126. }
  127. }