Build an array of interfaces with more than one server

Technical questions regarding the XTC tools and programming with XMOS.
themech
Member++
Posts: 17
Joined: Tue Sep 23, 2014 12:17 pm

Build an array of interfaces with more than one server

Post by themech »

Hi,

the documentation states that its possible to build an array of interfaces with one server and several clients. Is it also possible to have one client and serveral servers?

Furthermore is it possible to define an array of single interfaces with individual server and clients? For example like this:

Code: Select all

interface interface1{
    void var(int value);
};

void task1(server interface interface1 iface[]){
    select{
        case iface[int i].var(int x):
                printf("interface: %i, value: %i\n", i, x);
        break;
    }
}

void task2(client interface interface1 iface[]){
    iface[0].var(1);
}

int main(void){
    interface interface1 iface[6];
    par {
        task1(iface[0]);
        task2(iface[0]);
        task1(iface[1]);
        task2(iface[1]);
    }
    return 0;
}


richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

Yes, you can have one client connected to several servers using an interface array. I've amended your example code to demonstrate this:

Code: Select all

#include <stdio.h>

interface interface1{
    void var(int value);
};

void task1(server interface interface1 iface, int i) {
    select{
        case iface.var(int x):
                printf("interface: %i, value: %i\n", i, x);
        break;
    }
}

void task2(client interface interface1 iface[num], unsigned int num) {
    for (unsigned int i = 0; i < num; i++) {
        iface[i].var(i);
    }
}

int main(void){
    interface interface1 iface[2];
    par {
        task1(iface[0], 0);
        task1(iface[1], 1);
        task2(iface, 2);
    }
    return 0;
}
Here one client (task2) that connects to two servers (two instances of task1). If you run this example you'll get the following output:

Code: Select all

interface: 0, value: 0
interface: 1, value: 1
themech
Member++
Posts: 17
Joined: Tue Sep 23, 2014 12:17 pm

Post by themech »

Thanks that shortened my code a lot!