more console commands
[amper.git] / amperrpc.d
blobde7204aafd275c4cbdd7b6df462ebcbd6ea89219
1 module amperrpc;
3 import core.time;
5 import std.concurrency;
7 import iv.cmdcon;
8 import iv.ncrpc;
9 import iv.vfs;
12 // ////////////////////////////////////////////////////////////////////////// //
13 __gshared void delegate (string path, bool append=false) amperScanDir;
14 __gshared void delegate () amperPlay;
15 __gshared void delegate () amperStop;
16 __gshared void delegate () amperTogglePause;
17 __gshared void delegate () amperClearPlaylist;
18 __gshared void delegate (string cmd) amperConsoleCommand;
21 // ////////////////////////////////////////////////////////////////////////// //
22 auto amperRPCall(alias func, A...) (A args) {
23 UDSocket sk;
24 sk.connect(AmperRPCSocket);
25 return sk.rpcall!func(args);
29 // ////////////////////////////////////////////////////////////////////////// //
30 enum AmperRPCSocket = "/k8/amper";
33 // ////////////////////////////////////////////////////////////////////////// //
34 void startRPCServer () {
35 srvsk.create(AmperRPCSocket);
37 servertid = spawn(&rpcServerThread, thisTid);
41 void stopRPCServer () {
42 UDSocket sk;
43 sk.connect(AmperRPCSocket);
44 sk.writeNum!ushort(RPCommand.Err);
48 // ////////////////////////////////////////////////////////////////////////// //
49 private:
50 __gshared Tid servertid;
51 __gshared UDSocket srvsk;
54 void rpcServerThread (Tid ownerTid) {
55 rpcRegisterEndpoint!amperScanDir(delegate (string path, bool append) { concmdf!"scan_dir \"%s\" %s"(path, append ? "tan" : "ona"); });
56 rpcRegisterEndpoint!amperPlay(delegate () { concmd("song_play"); });
57 rpcRegisterEndpoint!amperStop(delegate () { concmd("song_stop"); });
58 rpcRegisterEndpoint!amperTogglePause(delegate () { concmd("song_pause_toggle"); });
59 rpcRegisterEndpoint!amperClearPlaylist(delegate () { concmd("pl_clear"); });
60 rpcRegisterEndpoint!amperConsoleCommand(delegate (string cmd) { concmd(cmd); });
62 //conwriteln(rpcEndpointNames);
64 for (;;) {
65 receiveTimeout(Duration.min,
66 (Variant v) { conwriteln("FUUUUUU"); },
69 auto cl = srvsk.accept();
70 auto cmd = cl.readNum!ushort;
71 if (cmd != RPCommand.Call) {
72 if (cmd != RPCommand.Err) conwriteln("invalid command");
73 break;
75 //conwriteln("connection comes!");
76 cl.rpcProcessCall;