//Joining the Republic


// The class Republic allows a very simple organisation of

// cooperation in changing groups over the local network.

// It automatically administers each member's address and server

// More details: see SimpleRepublic and Republic helpfiles


/////////////////////////////////////////////////////////////////

// Step 1: is it installed?

"Republic".include; // ... and recompile if needed.


/////////////////////////////////////////////////////////////////

// Step 2: one person creates a local network, all the others join it.



/////////////////////////////////////////////////////////////////

// Step 3: someone finds out the broadcast IP (it's the same for everyone)

// usually this works:

      unixCmd("ifconfig | grep broadcast | awk '{print $NF}'") 

// Or: "ifconfig" in Terminal and search the printout for "broadcast"




/////////////////////////////////////////////////////////////////

// Step 4: for everyone the same: make a republic with the broadcast IP.

r = Republic(NetAddr("169.254.255.255", 57120)).makeDefault;



/////////////////////////////////////////////////////////////////

// Step 5: let's give every person a unique nickname (symbol),

//         and an id a unique integer between (0..31).

// then join the Republic:

r.join(\yourName, 0); //<- your unique id here, and your name over there.


// you see the members appear on the post window.


r.addrs; // see whose addresses are collected already

r.servers; // see whose servers are collected already



/////////////////////////////////////////////////////////////////

// Step 6:  to share your code, evaluate the following:

// (use tab to chat from chat window)


(

g = OSCresponder(nil, '/hist', {|t,r,msg| 

History.enter(msg[2].asString, msg[1]) 

}).add; 

History.start;

History.makeWin;

History.forwardFunc = { |code|

r.send(\all, '/hist', r.nickname, code) 

};

History.localOff;

EZRepublicGui(republic: r);

);



/////////////////// Welcome ///////////////////////




// what to do now?


// e.g. share a SynthDef with all participants

(

SynthDef(\xxy, {|out, sustain = 1, freq = 440|

var env = Line.kr(0.1, 0, sustain, doneAction: 2);

var son = SinOsc.ar(freq * [1, 1.2, 1.5, 1.7]).sum;

Out.ar(out, son);

}).share;

)


// and send a synth to everyone:

(server: r.servers, instrument: \xxy, freq: exprand(300, 1000)).play;


// or to a random member:

(server: r.servers.choose, instrument: \xxy, freq: exprand(300, 1000)).play;


// For documentation of the session in the end:

History.document;



// For quitting republic:

(

History.stop;

g.remove;

r.leave;

);


// when you just end SC or recompile, you will leave automatically.