// Path: midi/src/main/java/com/disappointedpig/midi/internal_events/PacketEvent.java
// public class PacketEvent {
//     private InetAddress address;
//     private int port;
//     private byte[] data;
//     private int length;
//     public PacketEvent(final DatagramPacket packet) {
//         address = packet.getAddress();
//         port = packet.getPort();
//         data = packet.getData();
//         length = packet.getLength();
// //        Log.d("PacketEvent"," p:"+packet.getLength()+ " d:"+data.length);
//     }
// 
//     public Bundle getRInfo() {
//         Bundle rinfo = new Bundle();
//         rinfo.putString(com.disappointedpig.midi.MIDIConstants.RINFO_ADDR,address.getHostAddress());
//         rinfo.putInt(MIDIConstants.RINFO_PORT,port);
//         return rinfo;
//     }
// 
//     public InetAddress getAddress() {
//         return address;
//     }
// 
//     public int getPort() {
//         return port;
//     }
// 
//     public byte[] getData() {
//         return data;
//     }
// 
//     public int getLength() {
//         return data.length;
//     }
// }


import android.os.Bundle;
import android.util.Log;
import com.disappointedpig.midi.internal_events.PacketEvent;
import org.greenrobot.eventbus.EventBus;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
        return thread.getPriority();
    }

    void setThreadPriority(int priority) {
        thread.setPriority(priority);
    }

    void start() {
        isListening = true;

//        final Thread thread = new Thread(this);
//        // The JVM exits when the only threads running are all daemon threads.
        thread.setDaemon(true);
        thread.setPriority(Thread.NORM_PRIORITY);
        thread.start();
//        Log.d(TAG,"create thread : "+thread.getId());

    }

    void stop() {
        isListening = false;
    }

    private void handleRead(SelectionKey key) {
//        Log.d("MIDIPort2","handleRead");
        DatagramChannel c = (DatagramChannel) key.channel();
        UDPBuffer b = (UDPBuffer) key.attachment();
        try {
            b.buffer.clear();
            b.socketAddress = c.receive(b.buffer);
// Path: midi/src/main/java/com/disappointedpig/midi/internal_events/PacketEvent.java
// public class PacketEvent {
//     private InetAddress address;
//     private int port;
//     private byte[] data;
//     private int length;
//     public PacketEvent(final DatagramPacket packet) {
//         address = packet.getAddress();
//         port = packet.getPort();
//         data = packet.getData();
//         length = packet.getLength();
// //        Log.d("PacketEvent"," p:"+packet.getLength()+ " d:"+data.length);
//     }
// 
//     public Bundle getRInfo() {
//         Bundle rinfo = new Bundle();
//         rinfo.putString(com.disappointedpig.midi.MIDIConstants.RINFO_ADDR,address.getHostAddress());
//         rinfo.putInt(MIDIConstants.RINFO_PORT,port);
//         return rinfo;
//     }
// 
//     public InetAddress getAddress() {
//         return address;
//     }
// 
//     public int getPort() {
//         return port;
//     }
// 
//     public byte[] getData() {
//         return data;
//     }
// 
//     public int getLength() {
//         return data.length;
//     }
// }


// Path: midi/src/main/java/com/disappointedpig/midi/MIDIPort.java
import android.os.Bundle;
import android.util.Log;
import com.disappointedpig.midi.internal_events.PacketEvent;
import org.greenrobot.eventbus.EventBus;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;

        return thread.getPriority();
    }

    void setThreadPriority(int priority) {
        thread.setPriority(priority);
    }

    void start() {
        isListening = true;

//        final Thread thread = new Thread(this);
//        // The JVM exits when the only threads running are all daemon threads.
        thread.setDaemon(true);
        thread.setPriority(Thread.NORM_PRIORITY);
        thread.start();
//        Log.d(TAG,"create thread : "+thread.getId());

    }

    void stop() {
        isListening = false;
    }

    private void handleRead(SelectionKey key) {
//        Log.d("MIDIPort2","handleRead");
        DatagramChannel c = (DatagramChannel) key.channel();
        UDPBuffer b = (UDPBuffer) key.attachment();
        try {
            b.buffer.clear();
            b.socketAddress = c.receive(b.buffer);
            EventBus.getDefault().post(new PacketEvent(new DatagramPacket(b.buffer.array(),b.buffer.capacity(),b.socketAddress)));