ArtNet port change?

Hi, i have a dude using ArtNet library…

How i change the UDP port???

Thanks in advance.

1 Like

Not sure if the owner of the library hangs out in this forum. You should ask this question in his repo as well.

If I were you, I would dig into the source and figure out if there is a handle to change the port. If you provide a sample code then it will help narrow down the search in the source code.

Kf

1 Like

Thanks kfrajer for answer, i try to modify the source for this feature and works fine :slight_smile:

I do not know how to do compile this source in jar… when i compile it i share the new updated library .

I didn’t suggest to compile it. My suggestion was more about finding is there is a way to change the port by checking if there was an available setter for this. This is the approach I use when there is not good docs available and you need the feature like “now”. You can try adding the feature if you need to. Good luck building it and I hope it works!

Kf

1 Like

Hi, I have not found the implementation in the library to make use of custom ports, I have to add this functionality, I have it added, now I need to compile the library again and ready, although for now I use it with an .pde file where I have the class that encapsulates the customized client / server.

@cansik updated ArtNet quite recently, adding socket reuse last year:

and bumping the repo just 16 days ago:

…so I would suggest forking that repo, making the patch that you are using there to add a custom port, and then submitting your change as a pull request to the original repo. Before you do that, though, you might want to open an issue on the original repo and ask if there is an easier way of doing what you are trying to do:

1 Like

Thanks for an idea, when i finish my actual project i send a feature request to author :wink:

At this moment the project is in very advanced stat and i not change anything of libraries, the project are comercial.

When i finish the proyect i publish my ‘netMessage’ API to connect any number of sketches, the API is the simplest concept of send and receive data same as oscP5, but working in android has a server “oscP5” not works actually in android as a server…

a litle example are:

netMessage m = new netMessage();
m.setDestination("192.168.1.128");
m.add("thing_1");
m.add("bla bla bla");
m.send();

this send an array ob byte´s automatically to destination, and on destination when paquet arrives..

onNetEvent( StringList msg ){

switch(msg.get(1)){

case "thing_1":
exit();
break;

}

when packet arrives to destination… destination make exit()…

it´s simple and effective hehe :wink:

As @jeremydouglass already mentioned, I try to be active on updating the library. Port selection was something I did not implemented to keep the API a bit simpler, but I guess it is something that makes sense. So I implemented it in the newest version 0.5.1 and released the library some minutes ago. But it will take some time to update the contribution manager.

// inport / output port
ArtNetClient artnet = new ArtNetClient(new ArtNetBuffer(), 8000, 8000);
3 Likes

Really ArtNet it´s a good library for multi platform projects, works perfectly in windows, mac and Android.
For me actually it´s the best network library for processing.

This is my code for transform ArtNet library as more similar to oscP5 :slight_smile:

/* 
    
    netLibZero 0.1 by Luis lopez martinez..
    15/02/2019.
    
*/
import netP5.*;
import ch.bildspur.artnet.*;
netClient artnet;
final byte NET_NEW_DATA = 0x01;    // new incomming messages transport first byte..
final byte NET_OLD_DATA = 0x00;    // when i read this message y put this first byte to mark it..
final byte NET_TOK_DATA = 0x07;    // delimitier for datagram tokens..
String LOCAL_IP_ADDRESS = "";
final int  NET_BUFFER_SIZE = 1024;
//------------------------------------------------------------
void netStart(int portRX, int portTX){
    // create artnet client without buffer (no receving needed)
    artnet = new netClient(portRX, portTX);
    artnet.start();
    LOCAL_IP_ADDRESS = NetInfo.lan();
    thread("netRcvController");
}
//------------------------------------------------------------
void netRcvController(){
    while(true){
        onArtNetEvent();
    }
}
//------------------------------------------------------------
/* CLASE MENSAJE DE RED.. */
class netMessage{
    byte delimitier = NET_TOK_DATA;
    String destination = "";
    int bufferSize = NET_BUFFER_SIZE;
    byte[] buffer = new byte[bufferSize];
    byte[] dmxData;
    int it = 1;
    //-------------CONSTRUCTOR MSG----------------------------
    netMessage(){
        buffer[0] = NET_NEW_DATA;
        for(int i=1; i<bufferSize; i++){
            buffer[i] = 0x00;
        }
        this.add(LOCAL_IP_ADDRESS);
    }
    //-------------SIMPLE ADD DATA TO MSG---------------------
    void add(String data){
        int len = data.length();                        // obtain lenght of data..
        if(it+len > bufferSize){
            println("ERROR: netMessage data stack overflow. "+it+len+" stack!", " when max buffer size are: " + bufferSize );
            return;
        }
        int offset = it;                                // set offset to insert data..
        for(int i=0; i<len; i++){
            buffer[offset+i] = (byte)data.charAt(i);    // insert data..
            it++;
        }
        buffer[it] = delimitier;                        // insert token delimitier..
        it++;
    }
    //-------------SET DESTINATION NET USER-------------------
    void setDestination(String ip){
        this.destination = ip;
    }
    //-------------SIMPLE SEND MSG TO NETWORK-----------------
    void send(){
        int length = 1;                    // min 1 byte of tx..
        for(int i=0; i<bufferSize; i++){   // read buffer..
            if(buffer[i] == 0x00){         // if found null byte..
                length = i;                // this is the last byte to tx..
                i = bufferSize;            // break loop..
            }
        }
        
        dmxData = new byte[length];        // create final datagram in byte[]..
        
        for(int i=0; i<length; i++){
            dmxData[i] = buffer[i];
        }
        
        artnet.unicastDmx(destination, 0, 0, dmxData);
    }
}
//------------------------------------------------------------
/* RECEPTOR DE MENSAJE DE RED */
boolean onArtNetEvent(){
    byte[] data = artnet.readDmxData(0, 0);
    
    try{
        if(data[0] == NET_NEW_DATA){
            data[0] = NET_OLD_DATA;
            StringList datum = new StringList();
            String buffer = "";
            for(int i=1; i<data.length; i++){
                switch(data[i]){
                    case NET_TOK_DATA:
                        datum.append(buffer);
                        buffer = "";
                        break;
                    default:
                        buffer += char(data[i]);
                        break;
                }
            }
            onNetEvent(datum);
            return true;
        }
    }catch(Exception e){
        
    }
    return false;
}
//------------------------------------------------------------
String getMyIp(){
    String ip = NetInfo.lan();
    return ip;
}
//------------------------------------------------------------
//------------------------------------------------------------
//------------------------------------------------------------
//------------------------------------------------------------
//------------------------------------------------------------
//------------------------------------------------------------
//------------------------------------------------------------
import ch.bildspur.artnet.events.ArtNetServerEventAdapter;
import ch.bildspur.artnet.packets.ArtDmxPacket;
import ch.bildspur.artnet.packets.ArtNetPacket;

import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import static ch.bildspur.artnet.packets.PacketType.ART_OUTPUT;

public class netClient {
    private ArtNetServer server;
    
    private int sequenceId = 0;
    private boolean isRunning = false;

    private ArtNetBuffer inputBuffer;
    
    // ADD port select by Luis lopez martinez..
    private int portRX = 6454;
    private int portTX = 6454;
    
    
    /**
     * Creates a new ArtNetClient.
     */
    public netClient(int portRX, int portTX) {
        this(new ArtNetBuffer(), portRX, portTX);
    }

    /**
     * Creates a new ArtNetClient.
     * @param inputBuffer Input buffer implementation. If null, no data is received.
     */
    public netClient(ArtNetBuffer inputBuffer, int portRX, int portTX) {
        // init input buffer
        this.inputBuffer = inputBuffer;
        server = new ArtNetServer(portRX, portTX);
    }

    /**
     * Start client with default arguments (listen on broadcast).
     */
    public void start() {
        // use default network interface
        this.start((InetAddress)null);
    }

    /**
     * Start client with specific network interface address.
     * @param networkInterfaceAddress Network interface address to listen to.
     */
    public void start(String networkInterfaceAddress)
    {
        try {
            this.start(InetAddress.getByName(networkInterfaceAddress));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    /**
     * Start client with specific network interface address.
     * @param networkInterfaceAddress Network interface address to listen to.
     */
    public void start(InetAddress networkInterfaceAddress) {
        if (isRunning)
            return;

        // reset buffer if present
        if (inputBuffer != null)
            inputBuffer.clear();

        try {
            server.addListener(
                    new ArtNetServerEventAdapter() {
                        @Override
                        public void artNetPacketReceived(ArtNetPacket packet) {
                            onPacketReceived(packet);
                        }
                    });

            server.start(networkInterfaceAddress);

            isRunning = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Stops the client and udp server.
     */
    public void stop() {
        if (!isRunning)
            return;

        server.stop();

        isRunning = false;
    }

    /**
     * Send a dmx package as broadcast package.
     * @param subnet Receiving subnet.
     * @param universe Receiving universe.
     * @param dmxData Dmx data to send.
     */
    public void broadcastDmx(int subnet, int universe, byte[] dmxData) {
        server.broadcastPacket(createDmxPacket(subnet, universe, dmxData));
    }

    /**
     * Send a dmx package to a specific unicast address.
     * @param address Receiver address.
     * @param subnet Receiving subnet.
     * @param universe Receiving universe.
     * @param dmxData Dmx data to send.
     */
    public void unicastDmx(String address, int subnet, int universe, byte[] dmxData) {
        try {
            this.unicastDmx(InetAddress.getByName(address), subnet, universe, dmxData);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    /**
     * Send a dmx package to a specific unicast address.
     * @param node Receiver node.
     * @param subnet Receiving subnet.
     * @param universe Receiving universe.
     * @param dmxData Dmx data to send.
     */
    public void unicastDmx(ArtNetNode node, int subnet, int universe, byte[] dmxData) {
        server.unicastPacket(createDmxPacket(subnet, universe, dmxData), node.getIPAddress());
    }

    /**
     * Send a dmx package to a specific unicast address.
     * @param address Receiver address.
     * @param subnet Receiving subnet.
     * @param universe Receiving universe.
     * @param dmxData Dmx data to send.
     */
    public void unicastDmx(InetAddress address, int subnet, int universe, byte[] dmxData) {
        server.unicastPacket(createDmxPacket(subnet, universe, dmxData), address);
    }

    private ArtDmxPacket createDmxPacket(int subnet, int universe, byte[] dmxData) {
        ArtDmxPacket dmx = new ArtDmxPacket();

        dmx.setUniverse(subnet, universe);
        dmx.setSequenceID(++sequenceId);
        dmx.setDMX(dmxData, dmxData.length);

        sequenceId %= 256;

        return dmx;
    }

    private void onPacketReceived(final ArtNetPacket packet) {
        // only store input data if buffer is created
        if (inputBuffer == null)
            return;

        if (packet.getType() != ART_OUTPUT)
            return;

        ArtDmxPacket dmxPacket = (ArtDmxPacket) packet;
        int subnet = dmxPacket.getSubnetID();
        int universe = dmxPacket.getUniverseID();

        inputBuffer.setDmxData((short) subnet, (short) universe, dmxPacket.getDmxData());
    }

    /**
     * Read dmx data from the buffer implementation.
     * @param subnet Subnet to read from.
     * @param universe Universe to read from.
     * @return Dmx data array.
     */
    public byte[] readDmxData(int subnet, int universe) {
        return readDmxData((short) subnet, (short) universe);
    }

    /**
     * Read dmx data from the buffer implementation.
     * @param subnet Subnet to read from.
     * @param universe Universe to read from.
     * @return Dmx data array.
     */
    public byte[] readDmxData(short subnet, short universe) {
        return inputBuffer.getDmxData(subnet, universe);
    }

    public ArtNetServer getArtNetServer() {
        return server;
    }

    public boolean isRunning() {
        return isRunning;
    }

    public ArtNetBuffer getInputBuffer() {
        return inputBuffer;
    }

    public void setInputBuffer(ArtNetBuffer inputBuffer) {
        this.inputBuffer = inputBuffer;
    }
}
//------------------------------------------------------------