forked from libswift/libswift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdgw.cpp
1208 lines (981 loc) · 38.6 KB
/
cmdgw.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* cmdgw.cpp
* command gateway for controlling swift engine via a TCP connection
*
* Created by Arno Bakker, Riccado Petrocco
* Copyright 2010-2012 TECHNISCHE UNIVERSITEIT DELFT. All rights reserved.
*
*/
#include "swift.h"
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/listener.h>
#include <event2/http.h>
#include <iostream>
#include <sstream>
using namespace swift;
// Send PLAY after receiving N bytes
#define CMDGW_MAX_PREBUF_BYTES (256*1024)
// Report swift download progress every 2^layer * chunksize bytes (so 0 = report every chunk)
#define CMDGW_FIRST_PROGRESS_BYTE_INTERVAL_AS_LAYER 0 // must be 0
#define CMDGW_PREBUF_PROGRESS_BYTE_INTERVAL_AS_LAYER 0
// Status of the swarm download
#define DLSTATUS_ALLOCATING_DISKSPACE 0
#define DLSTATUS_HASHCHECKING 2
#define DLSTATUS_DOWNLOADING 3
#define DLSTATUS_SEEDING 4
#define DLSTATUS_STOPPED_ON_ERROR 6
#define MAX_CMD_MESSAGE 1024
#define ERROR_NO_ERROR 0
#define ERROR_UNKNOWN_CMD -1
#define ERROR_MISS_ARG -2
#define ERROR_BAD_ARG -3
#define ERROR_BAD_SWARM -4
#define CMDGW_MAX_CLIENT 1024 // Arno: == maximum number of swarms per proc
struct cmd_gw_t {
int id;
evutil_socket_t cmdsock;
int td; // swift FD
bool moreinfo; // whether to report detailed stats (see SETMOREINFO cmd)
tint startt; // ARNOSMPTODO: debug speed measurements, remove
std::string mfspecname; // MULTIFILE
uint64_t startoff; // MULTIFILE: starting offset in content range of desired file
uint64_t endoff; // MULTIFILE: ending offset (careful, for an e.g. 100 byte interval this is 99)
bool playsent;
std::string xcontentdur;
std::string contentlenstr;
std::string mimetype;
} cmd_requests[CMDGW_MAX_CLIENT];
int cmd_gw_reqs_open = 0;
int cmd_gw_reqs_count = 0;
int cmd_gw_conns_open = 0;
struct evconnlistener *cmd_evlistener = NULL;
struct evbuffer *cmd_evbuffer = NULL; // Data received on cmd socket : WARNING: one for all cmd sockets
/*
* SOCKTUNNEL
* We added the ability for a process to tunnel data over swift's UDP socket.
* The process should send TUNNELSUSCRIBE (with a chosen prefix) and TUNNELSEND
* commands over the CMD * TCP socket. It will than receive TUNNELRECV commands
* from swift.
*/
typedef enum {
CMDGW_TUNNEL_SCAN4CRLF,
CMDGW_TUNNEL_READTUNNEL
} cmdgw_tunnel_t;
typedef enum {
SWIFT_STATUS_ERROR,
SWIFT_STATUS_OK,
SWIFT_STATUS_EXITING,
} swift_status_t;
std::vector< uint32_t > tunnel_channels_;
cmdgw_tunnel_t cmd_tunnel_state=CMDGW_TUNNEL_SCAN4CRLF;
uint32_t cmd_tunnel_expect=0;
Address cmd_tunnel_dest_addr;
uint32_t cmd_tunnel_dest_chanid;
evutil_socket_t cmd_tunnel_sock=INVALID_SOCKET;
// HTTP gateway address for PLAY cmd
Address cmd_gw_httpaddr;
uint64_t cmd_gw_livesource_disc_wnd=POPT_LIVE_DISC_WND_ALL;
popt_cont_int_prot_t cmd_gw_cipm=POPT_CONT_INT_PROT_MERKLE;
// Ric: directory containing the metadata
std::string cmd_gw_metadir;
#define cmd_gw_debug true
tint cmd_gw_last_open=0;
// Fwd defs
void CmdGwDataCameInCallback(struct bufferevent *bev, void *ctx);
int CmdGwReadLine(evutil_socket_t cmdsock);
void CmdGwNewRequestCallback(evutil_socket_t cmdsock, char *line);
void CmdGwProcessData(evutil_socket_t cmdsock);
void CmdGwFreeRequest(cmd_gw_t* req)
{
req->id = -1;
req->cmdsock = -1;
req->td = -1;
req->moreinfo = false;
req->startt = 0;
req->mfspecname = "";
req->startoff = -1;
req->endoff = -1;
req->playsent = false;
req->xcontentdur = "";
req->mimetype = "";
}
void CmdGwCloseConnection(evutil_socket_t sock)
{
// Close cmd connection and stop all associated downloads.
// Doesn't remove .mhash state or content
if (cmd_gw_debug)
fprintf(stderr,"CmdGwCloseConnection: ENTER %d\n", sock);
bool scanning = true;
while (scanning) {
scanning = false;
for (int i=0; i<cmd_gw_reqs_open; i++) {
cmd_gw_t* req = &cmd_requests[i];
if (req->cmdsock==sock) {
dprintf("%s @%i stopping-on-close transfer %i\n",tintstr(),req->id,req->td);
swift::Close(req->td);
// Remove from list and reiterate over it
CmdGwFreeRequest(req);
*req = cmd_requests[--cmd_gw_reqs_open];
scanning = true;
break;
}
}
}
if (cmd_evbuffer != NULL) {
evbuffer_free(cmd_evbuffer);
cmd_evbuffer = NULL;
}
// Arno, 2012-07-06: Close
swift::close_socket(sock);
cmd_gw_conns_open--;
// Arno, 2012-10-11: New policy Immediate shutdown on connection close,
// see CmdGwUpdateDLStatesCallback()
fprintf(stderr,"cmd: Shutting down on CMD connection close\n");
event_base_loopexit(Channel::evbase, NULL);
}
cmd_gw_t* CmdGwFindRequestByFD(int td)
{
for (int i=0; i<cmd_gw_reqs_open; i++)
if (cmd_requests[i].td==td)
return cmd_requests+i;
return NULL;
}
cmd_gw_t* CmdGwFindRequestBySwarmID(SwarmID &swarmid)
{
int td = swift::Find(swarmid);
if (td < 0)
return NULL;
else
return CmdGwFindRequestByFD(td);
}
void CmdGwGotCHECKPOINT(SwarmID &swarmid)
{
// Checkpoint the specified download
if (cmd_gw_debug)
fprintf(stderr,"cmd: GotCHECKPOINT: %s\n",swarmid.hex().c_str());
cmd_gw_t* req = CmdGwFindRequestBySwarmID(swarmid);
if (req == NULL)
return;
swift::Checkpoint(req->td);
}
void CmdGwGotREMOVE(SwarmID &swarmid, bool removestate, bool removecontent)
{
// Remove the specified download
if (cmd_gw_debug)
fprintf(stderr,"cmd: GotREMOVE: %s %d %d\n",swarmid.hex().c_str(),removestate,removecontent);
cmd_gw_t* req = CmdGwFindRequestBySwarmID(swarmid);
if (req == NULL) {
if (cmd_gw_debug)
fprintf(stderr,"cmd: GotREMOVE: %s not found, bad swarm?\n",swarmid.hex().c_str());
return;
}
dprintf("%s @%i remove transfer %i\n",tintstr(),req->id,req->td);
// Arno: schaap moved cleanup to SwarmManager
swift::Close(req->td, removestate, removecontent);
CmdGwFreeRequest(req);
*req = cmd_requests[--cmd_gw_reqs_open];
}
void CmdGwGotMAXSPEED(SwarmID &swarmid, data_direction_t ddir, double speed)
{
// Set maximum speed on the specified download
//fprintf(stderr,"cmd: GotMAXSPEED: %s %d %lf\n",swarmid.hex().c_str(),ddir,speed);
cmd_gw_t* req = CmdGwFindRequestBySwarmID(swarmid);
if (req == NULL)
return;
swift::SetMaxSpeed(req->td, ddir, speed);
}
void CmdGwGotSETMOREINFO(SwarmID &swarmid, bool enable)
{
cmd_gw_t* req = CmdGwFindRequestBySwarmID(swarmid);
if (req == NULL)
return;
req->moreinfo = enable;
}
void CmdGwGotPEERADDR(SwarmID &swarmid, Address &peer)
{
cmd_gw_t* req = CmdGwFindRequestBySwarmID(swarmid);
if (req == NULL)
return;
swift::AddPeer(peer, swarmid);
}
void CmdGwSendINFOHashChecking(evutil_socket_t cmdsock, SwarmID &swarmid)
{
// Send INFO DLSTATUS_HASHCHECKING message.
char cmd[MAX_CMD_MESSAGE];
sprintf(cmd,"INFO %s %d %" PRIi64 "/%" PRIi64 " %lf %lf %" PRIu32 " %" PRIu32 "\r\n",swarmid.hex().c_str(),
DLSTATUS_HASHCHECKING,(uint64_t)0,(uint64_t)0,0.0,0.0,0,0);
//fprintf(stderr,"cmd: SendINFO: %s", cmd);
send(cmdsock,cmd,strlen(cmd),0);
}
void CmdGwSendINFO(cmd_gw_t* req, int dlstatus)
{
// Send INFO message.
//if (cmd_gw_debug)
// fprintf(stderr,"cmd: SendINFO: F%d initdlstatus %d\n", req->td, dlstatus );
SwarmID swarmid = swift::GetSwarmID(req->td);
if (swarmid == SwarmID::NOSWARMID)
return; // Arno: swarm deleted, ignore
uint64_t size = swift::Size(req->td);
uint64_t complete = 0;
if (swift::ttype(req->td) == LIVE_TRANSFER)
complete = swift::SeqComplete(req->td,swift::GetHookinOffset(req->td));
else
complete = swift::Complete(req->td);
if (size > 0 && size == complete)
dlstatus = DLSTATUS_SEEDING;
if (!swift::IsOperational(req->td))
dlstatus = DLSTATUS_STOPPED_ON_ERROR;
// schaap FIXME: Are those active leechers and seeders, or potential
// leechers and seeders? In the latter case, get cached values when cached
// peers have been implemented.
uint32_t numleech = swift::GetNumLeechers(req->td);
uint32_t numseeds = swift::GetNumSeeders(req->td);
double dlspeed = swift::GetCurrentSpeed(req->td,DDIR_DOWNLOAD);
double ulspeed = swift::GetCurrentSpeed(req->td,DDIR_UPLOAD);
char cmd[MAX_CMD_MESSAGE];
sprintf(cmd,"INFO %s %d %" PRIu64 "/%" PRIu64 " %lf %lf %" PRIu32 " %" PRIu32 "\r\n",swarmid.hex().c_str(),dlstatus,
complete,size,dlspeed,ulspeed,numleech,numseeds);
send(req->cmdsock,cmd,strlen(cmd),0);
// MORESTATS
if (req->moreinfo) {
// Send detailed ul/dl stats in JSON format.
std::ostringstream oss;
oss.setf(std::ios::fixed,std::ios::floatfield);
oss.precision(5);
channels_t::iterator iter;
channels_t *peerchans = NULL;
ContentTransfer *ct = swift::GetActivatedTransfer(req->td);
if (ct)
peerchans = ct->GetChannels();
oss << "MOREINFO" << " " << swarmid.hex() << " ";
double tss = (double)Channel::Time() / 1000000.0L;
oss << "{\"timestamp\":\"" << tss << "\", ";
oss << "\"channels\":";
oss << "[";
if (peerchans != NULL) {
for (iter=peerchans->begin(); iter!=peerchans->end(); iter++) {
Channel *c = *iter;
if (c == NULL)
continue;
if (iter!=peerchans->begin())
oss << ", ";
oss << "{";
oss << "\"ip\": \"" << c->peer().ipstr() << "\", ";
oss << "\"port\": " << c->peer().port() << ", ";
oss << "\"raw_bytes_up\": " << c->raw_bytes_up() << ", ";
oss << "\"raw_bytes_down\": " << c->raw_bytes_down() << ", ";
oss << "\"bytes_up\": " << c->bytes_up() << ", ";
oss << "\"bytes_down\": " << c->bytes_down() << " ";
oss << "}";
}
}
oss << "], ";
oss << "\"raw_bytes_up\": " << Channel::global_raw_bytes_up << ", ";
oss << "\"raw_bytes_down\": " << Channel::global_raw_bytes_down << ", ";
oss << "\"bytes_up\": " << Channel::global_bytes_up << ", ";
oss << "\"bytes_down\": " << Channel::global_bytes_down << " ";
oss << "}";
oss << "\r\n";
std::stringbuf *pbuf=oss.rdbuf();
size_t slen = strlen(pbuf->str().c_str());
send(req->cmdsock,pbuf->str().c_str(),slen,0);
}
}
void CmdGwSendPLAY(cmd_gw_t *req)
{
// Send PLAY message to user
if (cmd_gw_debug)
fprintf(stderr,"cmd: SendPLAY: %d\n", req->td);
SwarmID swarmid = swift::GetSwarmID(req->td);
std::ostringstream oss;
oss << "PLAY ";
oss << swarmid.hex() << " ";
oss << "http://";
oss << cmd_gw_httpaddr.str();
oss << "/";
oss << swarmid.hex();
// Arno, 2013-09-25: Only append params that need to be echoed back
// by the HTTPGW, because the Transfer already exists.
bool added=false;
if (req->xcontentdur != "") {
if (!added)
oss << "?";
oss << "cd=" << req->xcontentdur;
added = true;
}
if (req->mimetype != "") {
if (!added)
oss << "?";
else
oss << "&";
oss << "mt=" << req->mimetype;
added = true;
}
// Arno FIXME: Should be replaced with swift::SetSize() method that stores value from URL.
if (req->contentlenstr != "") {
if (!added)
oss << "?";
else
oss << "&";
oss << "cl=" << req->contentlenstr;
added = true;
}
oss << "\r\n";
std::stringbuf *pbuf=oss.rdbuf();
if (cmd_gw_debug)
fprintf(stderr,"cmd: SendPlay: %s", pbuf->str().c_str());
size_t slen = strlen(pbuf->str().c_str());
send(req->cmdsock,pbuf->str().c_str(),slen,0);
}
void CmdGwSendERRORBySocket(evutil_socket_t cmdsock, std::string msg, const SwarmID& swarmid=SwarmID::NOSWARMID)
{
std::string cmd = "ERROR ";
cmd += swarmid.hex();
cmd += " ";
cmd += msg;
cmd += "\r\n";
if (cmd_gw_debug)
fprintf(stderr,"cmd: SendERROR: %s\n", cmd.c_str());
char *wire = strdup(cmd.c_str());
send(cmdsock,wire,strlen(wire),0);
free(wire);
}
/*
* For VOD and Live, wait until PREBUF_BYTES are in before sending PLAY.
*/
void CmdGwSwiftPrebufferProgressCallback(int td, bin_t bin)
{
//
// Subsequent bytes of content downloaded
//
//if (cmd_gw_debug)
// fprintf(stderr,"cmd: SwiftPrebuffProgress: %d\n", td );
cmd_gw_t* req = CmdGwFindRequestByFD(td);
if (req == NULL)
return;
uint64_t wantsize = std::min(req->endoff+1-req->startoff,(uint64_t)CMDGW_MAX_PREBUF_BYTES);
//if (cmd_gw_debug)
// fprintf(stderr,"cmd: SwiftPrebuffProgress: want %" PRIu64 " got %" PRIu64 "\n", swift::SeqComplete(req->td,req->startoff), wantsize );
if (swift::SeqComplete(req->td,req->startoff) >= wantsize) {
// First CMDGW_MAX_PREBUF_BYTES bytes received via swift,
// tell user to PLAY
// ARNOSMPTODO: bitrate-dependent prebuffering?
//if (cmd_gw_debug)
// fprintf(stderr,"cmd: SwiftPrebufferProgress: Prebuf done %d\n", td );
swift::RemoveProgressCallback(td,&CmdGwSwiftPrebufferProgressCallback);
CmdGwSendPLAY(req);
}
// wait for prebuffer
}
/*
* For single file static content, install a new callback that checks whether
* we have enough data prebuffered. For multifile, wait till the first chunks
* containing the multi-file spec have been loaded, then set download pointer
* to the desired file (via swift::Seek) and then wait till enough data is
* prebuffered (via CmdGwSwiftPrebufferingProcessCallback).
*/
void CmdGwSwiftVODFirstProgressCallback(int td, bin_t bin)
{
//
// First bytes of content downloaded (first in absolute sense)
//
if (cmd_gw_debug)
fprintf(stderr,"cmd: SwiftFirstProgress: %d\n", td);
cmd_gw_t* req = CmdGwFindRequestByFD(td);
if (req == NULL)
return;
if (swift::ttype(td) == LIVE_TRANSFER) {
// Shouldn't happen for LIVE
swift::RemoveProgressCallback(td,&CmdGwSwiftVODFirstProgressCallback);
return;
}
// VOD from here
Storage *storage = swift::GetStorage(td);
if (storage == NULL)
return;
if (!storage->IsReady()) {
// Wait until (multi-file) storage is ready
return;
}
swift::RemoveProgressCallback(td,&CmdGwSwiftVODFirstProgressCallback);
if (req->mfspecname == "") {
// Single file
req->startoff = 0;
req->endoff = swift::Size(req->td)-1;
CmdGwSwiftPrebufferProgressCallback(req->td,bin_t(0,0)); // in case file on disk
if (!req->playsent)
swift::AddProgressCallback(td,&CmdGwSwiftPrebufferProgressCallback,CMDGW_PREBUF_PROGRESS_BYTE_INTERVAL_AS_LAYER);
} else {
// MULTIFILE
// Have spec, seek to wanted file
storage_files_t sfs = storage->GetStorageFiles();
storage_files_t::iterator iter;
bool found = false;
for (iter = sfs.begin(); iter < sfs.end(); iter++) {
StorageFile *sf = *iter;
if (sf->GetSpecPathName() == req->mfspecname) {
if (cmd_gw_debug)
fprintf(stderr,"cmd: SwiftFirstProgress: Seeking to multifile %s for %d\n", req->mfspecname.c_str(), td);
int ret = swift::Seek(req->td,sf->GetStart(),SEEK_SET);
if (ret < 0) {
CmdGwSendERRORBySocket(req->cmdsock,"Error seeking to file in multi-file content.");
return;
}
found = true;
req->startoff = sf->GetStart();
req->endoff = sf->GetEnd();
CmdGwSwiftPrebufferProgressCallback(req->td,bin_t(0,0)); // in case file on disk
swift::AddProgressCallback(td,&CmdGwSwiftPrebufferProgressCallback,CMDGW_FIRST_PROGRESS_BYTE_INTERVAL_AS_LAYER);
break;
}
}
if (!found) {
if (cmd_gw_debug)
fprintf(stderr,"cmd: SwiftFirstProgress: Error file not found %d\n", td);
CmdGwSendERRORBySocket(req->cmdsock,"Individual file not found in multi-file content.",GetSwarmID(req->td));
return;
}
}
}
void CmdGwSwiftErrorCallback(evutil_socket_t cmdsock)
{
// Error on swift socket callback
const char *response = "ERROR Swift Engine Problem\r\n";
send(cmdsock,response,strlen(response),0);
//swift::close_socket(sock);
}
void CmdGwSwiftAllocatingDiskspaceCallback(int td, bin_t bin)
{
//if (cmd_gw_debug)
// fprintf(stderr,"cmd: CmdGwSwiftAllocatingDiskspaceCallback: ENTER %d\n", td );
// Called before swift starts reserving diskspace.
cmd_gw_t* req = CmdGwFindRequestByFD(td);
if (req == NULL)
return;
CmdGwSendINFO(req,DLSTATUS_ALLOCATING_DISKSPACE);
}
void CmdGwUpdateDLStateCallback(cmd_gw_t* req)
{
// Periodic callback, tell user INFO
CmdGwSendINFO(req,DLSTATUS_DOWNLOADING);
}
int icount=0;
void CmdGwUpdateDLStatesCallback()
{
// Called by swift main approximately every second
// Loop over all swarms
for (int i=0; i<cmd_gw_reqs_open; i++) {
cmd_gw_t* req = &cmd_requests[i];
CmdGwUpdateDLStateCallback(req);
}
}
void CmdGwDataCameInCallback(struct bufferevent *bev, void *ctx)
{
// do not process anything if the system has already shutdown
if (cmd_evbuffer == NULL) return;
// Turn TCP stream into lines deliniated by \r\n
evutil_socket_t cmdsock = bufferevent_getfd(bev);
//if (cmd_gw_debug)
// fprintf(stderr,"CmdGwDataCameIn: ENTER %d\n", cmdsock );
struct evbuffer *inputevbuf = bufferevent_get_input(bev);
int inlen = evbuffer_get_length(inputevbuf);
int ret = evbuffer_add_buffer(cmd_evbuffer,inputevbuf);
if (ret == -1) {
CmdGwCloseConnection(cmdsock);
return;
}
int totlen = evbuffer_get_length(cmd_evbuffer);
//if (cmd_gw_debug)
// fprintf(stderr,"cmdgw: TCPDataCameIn: State %d, got %d new bytes, have %d want %d\n", (int)cmd_tunnel_state, inlen, totlen, cmd_tunnel_expect );
CmdGwProcessData(cmdsock);
}
void CmdGwProcessData(evutil_socket_t cmdsock)
{
// Process CMD data in the cmd_evbuffer
if (cmd_tunnel_state == CMDGW_TUNNEL_SCAN4CRLF) {
int status=SWIFT_STATUS_ERROR;
do {
status = CmdGwReadLine(cmdsock);
if (status == SWIFT_STATUS_OK && cmd_tunnel_state == CMDGW_TUNNEL_READTUNNEL)
break;
if (status == SWIFT_STATUS_EXITING)
return;
} while (status == SWIFT_STATUS_OK);
}
// Not else!
if (cmd_tunnel_state == CMDGW_TUNNEL_READTUNNEL) {
// Got "TUNNELSEND addr size\r\n" command, now read
// size bytes, i.e., cmd_tunnel_expect bytes.
if (cmd_gw_debug)
fprintf(stderr,"cmdgw: procTCPdata: tunnel state, got " PRISIZET ", want %d\n", evbuffer_get_length(cmd_evbuffer),
cmd_tunnel_expect);
if (evbuffer_get_length(cmd_evbuffer) >= cmd_tunnel_expect) {
// We have all the tunneled data
CmdGwTunnelSendUDP(cmd_evbuffer);
// Process any remaining commands that came after the tunneled data
CmdGwProcessData(cmdsock);
}
}
}
int CmdGwReadLine(evutil_socket_t cmdsock)
{
// Parse cmd_evbuffer for lines, and call NewRequest when found
size_t rd=0;
char *cmd = evbuffer_readln(cmd_evbuffer,&rd, EVBUFFER_EOL_CRLF_STRICT);
if (cmd != NULL) {
CmdGwNewRequestCallback(cmdsock,cmd);
free(cmd);
if (cmd_evbuffer == NULL)
// Swift is shutting down
return SWIFT_STATUS_EXITING;
return SWIFT_STATUS_OK;
} else
return SWIFT_STATUS_ERROR;
}
int CmdGwHandleCommand(evutil_socket_t cmdsock, char *copyline);
void CmdGwNewRequestCallback(evutil_socket_t cmdsock, char *line)
{
// New command received from user
// CMD request line
char *copyline = new char[strlen(line)+1];
strcpy(copyline,line);
int ret = CmdGwHandleCommand(cmdsock,copyline);
if (ret < 0) {
dprintf("cmd: Error processing command %s\n", line);
std::string msg = "";
if (ret == ERROR_UNKNOWN_CMD)
msg = "unknown command";
else if (ret == ERROR_MISS_ARG)
msg = "missing parameter";
else if (ret == ERROR_BAD_ARG)
msg = "bad parameter";
// BAD_SWARM already sent, and not fatal
if (msg != "") {
CmdGwSendERRORBySocket(cmdsock,msg);
CmdGwCloseConnection(cmdsock);
}
}
delete copyline;
}
int CmdGwHandleCommand(evutil_socket_t cmdsock, char *copyline)
{
char *method=NULL,*paramstr = NULL;
char * token = strchr(copyline,' '); // split into CMD PARAM
if (token != NULL) {
*token = '\0';
paramstr = token+1;
} else
paramstr = (char*)"";
method = copyline;
if (cmd_gw_debug)
fprintf(stderr,"cmd: GOT %s %s\n", method, paramstr);
char *savetok = NULL;
if (!strcmp(method,"START")) {
// New START request
//fprintf(stderr,"cmd: START: new request %i\n",cmd_gw_reqs_count+1);
// Format: START url destdir [metadir]\r\n
std::string pstr = paramstr;
std::string url="",storagepath="", metadir="";
int sidx = pstr.find(" ");
if (sidx == std::string::npos) {
// No storage path or metadir
url = pstr;
storagepath = "";
} else {
url = pstr.substr(0,sidx);
std::string qstr = pstr.substr(sidx+1);
sidx = qstr.find(" ");
if (sidx == std::string::npos) {
// Only storage path
storagepath = qstr;
} else {
// Storage path and metadir
storagepath = qstr.substr(0,sidx);
metadir = qstr.substr(sidx+1);
}
}
// If no metadir in command, but one is set on swift command line use latter.
if (cmd_gw_metadir.compare("") && !metadir.compare(""))
metadir = cmd_gw_metadir;
if (metadir.length() > 0) {
if (metadir.substr(metadir.length()-std::string(FILE_SEP).length()).compare(FILE_SEP))
metadir += FILE_SEP;
}
// Parse URL
parseduri_t puri;
if (!swift::ParseURI(url,puri)) {
dprintf("cmd: START: cannot parse uri %s\n", url.c_str());
return ERROR_BAD_ARG;
}
SwarmMeta sm;
// Set configured defaults for CMDGW
sm.cont_int_prot_ = cmd_gw_cipm;
sm.live_disc_wnd_ = cmd_gw_livesource_disc_wnd;
// Convert parsed URI to config values
std::string errorstr = URIToSwarmMeta(puri,&sm);
if (errorstr != "") {
dprintf("cmd: START: Error parsing URI: %s\n", errorstr.c_str());
return ERROR_BAD_ARG;
}
std::string trackerstr = puri["server"];
std::string swarmidhexstr = puri["swarmidhex"];
std::string mfstr = puri["filename"];
std::string bttrackerurl = puri["et"];
std::string durstr = puri["cd"];
dprintf("cmd: START: %s with tracker %s chunksize %i duration %d storage %s metadir %s\n",swarmidhexstr.c_str(),
trackerstr.c_str(),sm.chunk_size_,sm.cont_dur_,storagepath.c_str(),metadir.c_str());
// Handle tracker
// External tracker via URL param
std::string trackerurl = "";
if (trackerstr == "" && bttrackerurl == "") {
trackerstr = Channel::trackerurl;
if (trackerstr == "") {
dprintf("cmd: START: tracker address must be URL server as hostname:port or ip:port, or set via ?bt=\n");
return ERROR_BAD_ARG;
}
}
// not else
if (bttrackerurl == "") {
trackerurl = SWIFT_URI_SCHEME;
trackerurl += "://";
trackerurl += trackerstr;
}
// initiate transmission
SwarmID swarmid(swarmidhexstr);
// Arno, 2012-06-12: Check for duplicate requests
cmd_gw_t* req = CmdGwFindRequestBySwarmID(swarmid);
if (req != NULL) {
dprintf("cmd: START: request for given root hash already exists\n");
return ERROR_BAD_ARG;
}
// Send INFO DLSTATUS_HASHCHECKING
CmdGwSendINFOHashChecking(cmdsock,swarmid);
// ARNOSMPTODO: disable/interleave hashchecking at startup
// ARNOTODO: Allow for deactivated swarms. Needs cheap tracker registration
bool activate=true;
int td = swift::Find(swarmid,activate);
if (td==-1) {
std::string filename;
if (storagepath != "")
filename = storagepath;
else
filename = swarmidhexstr;
if (durstr != "-1")
td = swift::Open(filename,swarmid,trackerurl,false,sm.cont_int_prot_,false,activate,sm.chunk_size_,metadir);
else
td = swift::LiveOpen(filename,swarmid,trackerurl,sm.injector_addr_,sm.cont_int_prot_,sm.live_disc_wnd_,sm.chunk_size_);
if (td == -1) {
CmdGwSendERRORBySocket(cmdsock,"bad swarm",swarmid);
return ERROR_BAD_SWARM;
}
}
// RATELIMIT
// swift::SetMaxSpeed(td,DDIR_DOWNLOAD,512*1024);
// All is well, register req
req = cmd_requests + cmd_gw_reqs_open++;
req->id = ++cmd_gw_reqs_count;
req->cmdsock = cmdsock;
req->td = td;
req->startt = usec_time();
req->mfspecname = mfstr;
req->playsent = false;
req->xcontentdur = puri["cd"];
req->contentlenstr = puri["cl"];
req->mimetype = puri["mt"];
dprintf("%s @%i start transfer %d\n",tintstr(),req->id,req->td);
//if (cmd_gw_debug)
// fprintf(stderr,"cmd: Already on disk is %" PRIu64 "/%" PRIu64 "\n", swift::Complete(td), swift::Size(td));
// Set progress callbacks
if (swift::ttype(req->td) == FILE_TRANSFER) {
// MULTIFILE
uint64_t minsize=CMDGW_MAX_PREBUF_BYTES;
Storage *storage = swift::GetStorage(req->td);
if (storage == NULL) {
dprintf("cmd: START: cannot get storage td %d\n", req->td);
CmdGwSendERRORBySocket(cmdsock,"bad swarm",swarmid);
return ERROR_BAD_SWARM;
}
storage_files_t sfs = storage->GetStorageFiles();
if (sfs.size() > 0)
minsize = sfs[0]->GetSize();
else if (swift::SeqComplete(td) > 0) // Arno, 2013-01-08: Support small files
minsize = std::min(swift::Size(td),minsize);
// Wait for first chunk, so we can handle MULTIFILE, then
// wait for prebuffering and then send PLAY to user.
// ARNOSMPTODO: OUTOFORDER: breaks with out-of-order download
if (swift::SeqComplete(td) >= minsize) {
CmdGwSwiftVODFirstProgressCallback(td,bin_t(0,0));
CmdGwSendINFO(req, DLSTATUS_DOWNLOADING);
} else {
swift::AddProgressCallback(td,&CmdGwSwiftVODFirstProgressCallback,CMDGW_FIRST_PROGRESS_BYTE_INTERVAL_AS_LAYER);
}
storage->AddOneTimeAllocationCallback(CmdGwSwiftAllocatingDiskspaceCallback);
} else {
// LIVE
// Wait for prebuffering and then send PLAY to user
swift::AddProgressCallback(td,&CmdGwSwiftPrebufferProgressCallback,CMDGW_PREBUF_PROGRESS_BYTE_INTERVAL_AS_LAYER);
}
} else if (!strcmp(method,"REMOVE")) {
// REMOVE roothash removestate removecontent\r\n
bool removestate = false, removecontent = false;
token = strtok_r(paramstr," ",&savetok); //
if (token == NULL)
return ERROR_MISS_ARG;
char *swarmidhexcstr = token;
token = strtok_r(NULL," ",&savetok); // removestate
if (token == NULL)
return ERROR_MISS_ARG;
removestate = !strcmp(token,"1");
token = strtok_r(NULL,"",&savetok); // removecontent
if (token == NULL)
return ERROR_MISS_ARG;
removecontent = !strcmp(token,"1");
std::string swarmidhexstr(swarmidhexcstr);
SwarmID swarmid(swarmidhexstr);
CmdGwGotREMOVE(swarmid,removestate,removecontent);
} else if (!strcmp(method,"MAXSPEED")) {
// MAXSPEED roothash direction speed-float-kb/s\r\n
data_direction_t ddir;
double speed;
token = strtok_r(paramstr," ",&savetok); //
if (token == NULL)
return ERROR_MISS_ARG;
char *swarmidhexcstr = token;
token = strtok_r(NULL," ",&savetok); // direction
if (token == NULL)
return ERROR_MISS_ARG;
ddir = !strcmp(token,"DOWNLOAD") ? DDIR_DOWNLOAD : DDIR_UPLOAD;
token = strtok_r(NULL,"",&savetok); // speed
if (token == NULL)
return ERROR_MISS_ARG;
int n = sscanf(token,"%lf",&speed);
if (n == 0) {
dprintf("cmd: MAXSPEED: speed is not a float\n");
return ERROR_MISS_ARG;
}
std::string swarmidhexstr(swarmidhexcstr);
SwarmID swarmid(swarmidhexstr);
CmdGwGotMAXSPEED(swarmid,ddir,speed*1024.0);
} else if (!strcmp(method,"CHECKPOINT")) {
// CHECKPOINT roothash\r\n
char *swarmidhexcstr = paramstr;
std::string swarmidhexstr(swarmidhexcstr);
SwarmID swarmid(swarmidhexstr);
CmdGwGotCHECKPOINT(swarmid);
} else if (!strcmp(method,"SETMOREINFO")) {
// GETMOREINFO roothash toggle\r\n
token = strtok_r(paramstr," ",&savetok); // hash
if (token == NULL)
return ERROR_MISS_ARG;
char *swarmidhexcstr = token;
token = strtok_r(NULL," ",&savetok); // bool
if (token == NULL)
return ERROR_MISS_ARG;
bool enable = (bool)!strcmp(token,"1");
std::string swarmidhexstr(swarmidhexcstr);
SwarmID swarmid(swarmidhexstr);
CmdGwGotSETMOREINFO(swarmid,enable);
} else if (!strcmp(method,"SHUTDOWN")) {
CmdGwCloseConnection(cmdsock);
// Tell libevent to stop processing events
event_base_loopexit(Channel::evbase, NULL);
} else if (!strcmp(method,"TUNNELSUBSCRIBE")) {
uint32_t channel;
int n = sscanf(paramstr,"%08x",&channel);
if (n != 1)
return ERROR_BAD_ARG;
if (!channel)
return ERROR_BAD_ARG;
// check if the channel is allowed (doesn't interfere with normal swift)
if (channel < SWIFT_MAX_INCOMING_CONNECTIONS + SWIFT_MAX_OUTGOING_CONNECTIONS)
return ERROR_BAD_ARG;
// check if the channel is already reserved
if (CmdGwTunnelCheckChannel(channel))
return ERROR_BAD_ARG;
tunnel_channels_.push_back(channel);
if (cmd_gw_debug)
fprintf(stderr,"cmdgw: Subscribed %" PRIu32 " as a new tunneling channel\n", channel);
} else if (!strcmp(method,"TUNNELUNSUBSCRIBE")) {
uint32_t channel;
int n = sscanf(paramstr,"%08x",&channel);
if (n != 1)
return ERROR_BAD_ARG;
if (!channel)
return ERROR_BAD_ARG;
// check if the channel actually exists
if (CmdGwTunnelCheckChannel(channel)) {
tunnel_channels_.erase(std::remove(tunnel_channels_.begin(), tunnel_channels_.end(), channel), tunnel_channels_.end());
if (cmd_gw_debug)
fprintf(stderr,"cmdgw: Unsubscribed channel %" PRIu32 "\n", channel);
} else if (cmd_gw_debug)
fprintf(stderr,"cmdgw: Unsubscribing channel %" PRIu32 " failed: No such channel subscribed\n", channel);
} else if (!strcmp(method,"TUNNELSEND")) {
token = strtok_r(paramstr,"/",&savetok); // dest addr
if (token == NULL)
return ERROR_MISS_ARG;
char *addrstr = token;
token = strtok_r(NULL," ",&savetok); // channel
if (token == NULL)