-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedihttp.go
1675 lines (1417 loc) · 62.1 KB
/
edihttp.go
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
// edihttp
package main
import (
"crypto/rand"
"edisplitter"
"encoding/base64"
"encoding/binary"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"sync"
"strconv"
"strings"
"time"
)
type EdiFrame struct {
SubchannelId uint8
EdiData []byte
}
var ipAddresses []net.IP
var hostAddress []string
//SBT_SUBCHANS
var mSbtConfig = make(map[uint8]int)
type toggleSlideMapIdx struct {
toggleStartTime int64
toggleBuffPos int64
}
//map from SubchanId to map from BuffTogglePoint to DLabel
var (
mTogglesMap map[uint8]map[int64]edisplitter.DynamicLabel = make(map[uint8]map[int64]edisplitter.DynamicLabel)
//map subchanId to toggle start time
mTogglesSlideMap = make(map[uint8][]toggleSlideMapIdx)
mToggleMapMutex = sync.RWMutex{}
)
func cmdFlagProvided(cmdName string) (provided bool) {
flag.Visit(func(f *flag.Flag) {
if f.Name == cmdName {
provided = true
}
})
return
}
var mListOnlyCmd = false
//Configuration file
type Configuration struct {
AfVersion int `json:"afVersion"`
ConfigVersion float32 `json:"configVersion"`
Hosts string `json:"hosts"`
HttpPort int `json:"httpPort"`
MaxGlobalSbt int `json:"maxGlobalSbt"`
ProxyPort int `json:"proxyPort"`
SbtChans string `json:"sbtChans"`
UdpPort int `json:"udpPort"`
Verbose bool `json:"verbose"`
}
var mConfiguration *Configuration = nil
func loadConfiguration(config string) {
mConfiguration = new(Configuration)
configFile, err := os.Open(config)
defer configFile.Close()
if err == nil {
confParser := json.NewDecoder(configFile)
err = confParser.Decode(mConfiguration)
if err != nil {
fmt.Printf("%s : Error decoding config file: %s - %s\n", time.Now().Format(time.UnixDate), config, err.Error())
}
}
}
const EDIHTTP_CONF_FILENAME string = "edihttp.conf"
//Docker environment variables
const ENV_EDIHTTP_CONFIGPATH string = "EDIHTTP_CONFIGPATH"
const ENV_EDIHTTP_MAX_GLOB_SBT string = "EDIHTTP_MAX_TIMESHIFT_GLOBAL"
const ENV_EDIHTTP_SBT_SUBCHANS string = "EDIHTTP_SBT_SUBCHANS"
const ENV_EDIHTTP_VERBOSE string = "EDIHTTP_VERBOSE"
const ENV_EDIHTTP_UDP_PORT string = "EDIHTTP_UDP_PORT"
const ENV_EDIHTTP_HTTP_PORT string = "EDIHTTP_HTTP_PORT"
const ENV_EDIHTTP_PROXY_PORT string = "EDIHTTP_PROXY_PORT"
const ENV_EDIHTTP_HOSTS string = "EDIHTTP_HOSTS"
//Command line parameters
const CMD_EDIHTTP_CONFIGPATH string = "configPath"
const CMD_EDIHTTP_MAXSBT string = "maxsbt"
const CMD_EDIHTTP_SBTCHANS string = "sbtchans"
const CMD_EDIHTTP_AFVERSION string = "afversion"
const CMD_EDIHTTP_HOSTS string = "hosts"
const CMD_EDIHTTP_UDPPORT string = "udpport"
const CMD_EDIHTTP_HTTPPORT string = "httpport"
const CMD_EDIHTTP_PROXYPORT string = "proxyport"
const CMD_EDIHTTP_LISTSUBCHANS string = "listsubchans"
const CMD_EDIHTTP_VERBOSE string = "verbose"
func main() {
var err error
var cmdConfigPath string
var sbtchans string
var outHttpPort int
var verbose = false
var overrideProxyPort int
var inUdpPort int
var configPath string
cmdMaxSbt := flag.Int(CMD_EDIHTTP_MAXSBT, 60, "the available global sbt buffer for all timeshiftable subchannels in minutes, the minimum is 5.")
flag.StringVar(&sbtchans, CMD_EDIHTTP_SBTCHANS, "", "comma separated list of subchannel-ids that are available for timeshifting.\n\te.g. --sbtchans=2,12,16 or --sbtchans=0x02,0x0C,0x10.\n" +
"Use --sbtchans=all to enable timeshifting for all subchannels.\n" +
"To set the maximum sbt buffer for a specific subchannel use\n\t--sbtchans=2:10,12:60,16:120 to have a buffer for subchannel 2 of 10 minutes, for subchannel 12 of 60 minutes...\n" +
"This overrides the 'maxsbt' parameter for the specific subchannel")
afVersion := flag.Int(CMD_EDIHTTP_AFVERSION, 1, "set the AF version to use. Experimental feature to reduce EDI overhead.")
ipOverrideString := flag.String(CMD_EDIHTTP_HOSTS, "", "comma separated list of IP addresses to display on web overview page")
flag.IntVar(&inUdpPort, CMD_EDIHTTP_UDPPORT, 50000, "Set the incoming EDI UDP port")
flag.IntVar(&outHttpPort, CMD_EDIHTTP_HTTPPORT, 8187, "Set the port of the HTTP streams and overview page")
flag.IntVar(&overrideProxyPort, CMD_EDIHTTP_PROXYPORT, 0, "Set the port where to receive streams on. Only useful if a (reverse) proxy is used.")
flag.BoolVar(&mListOnlyCmd, CMD_EDIHTTP_LISTSUBCHANS, false, "Lists all available subchannels and exits")
flag.BoolVar(&verbose, CMD_EDIHTTP_VERBOSE, false, "Enables debug output on stdout")
flag.StringVar(&cmdConfigPath, CMD_EDIHTTP_CONFIGPATH,"./", "Set the path to 'edihttp.conf' file, e.g. /etc/ediconf/")
flag.Parse()
if envConfigPath := os.Getenv(ENV_EDIHTTP_CONFIGPATH); len(envConfigPath) > 0 {
fmt.Printf("%s : Config ENV configPath: %s\n", time.Now().String(), envConfigPath)
configPath = envConfigPath
}
if cmdFlagProvided(CMD_EDIHTTP_CONFIGPATH) || len(configPath) == 0{
fmt.Printf("%s : Config CMD configPath: %s\n", time.Now().Format(time.UnixDate), cmdConfigPath)
configPath = cmdConfigPath
}
if !strings.HasSuffix(configPath, "/") {
configPath = configPath + "/"
fmt.Printf("%s : Loading config from appended: %s\n", time.Now().Format(time.UnixDate), configPath)
}
configPath += EDIHTTP_CONF_FILENAME
fmt.Printf("%s : Loading config from: %s\n", time.Now().Format(time.UnixDate), configPath)
loadConfiguration(configPath)
//Gloabel SBT buffer config
if envMaxGlobalTimeshift, err := strconv.Atoi(os.Getenv(ENV_EDIHTTP_MAX_GLOB_SBT)); err == nil {
mConfiguration.MaxGlobalSbt = envMaxGlobalTimeshift
}
if cmdFlagProvided(CMD_EDIHTTP_MAXSBT ) {
mConfiguration.MaxGlobalSbt = *cmdMaxSbt
}
if mConfiguration.MaxGlobalSbt == 0 {
mConfiguration.MaxGlobalSbt = *cmdMaxSbt
}
//Subchans config
if envSbtchans := os.Getenv(ENV_EDIHTTP_SBT_SUBCHANS); len(envSbtchans) > 0 {
mConfiguration.SbtChans = envSbtchans
fmt.Printf("%s : Config ENV sbtchans: %s\n", time.Now().Format(time.UnixDate), envSbtchans)
}
if cmdFlagProvided(CMD_EDIHTTP_SBTCHANS) || len(mConfiguration.SbtChans) == 0 {
fmt.Printf("%s : Config CMD sbtchans: %s\n", time.Now().Format(time.UnixDate), sbtchans)
mConfiguration.SbtChans = sbtchans
}
//Verbosity config
if envVerboseStr := os.Getenv(ENV_EDIHTTP_VERBOSE); len(envVerboseStr) > 0 {
envVerbose, err := strconv.ParseBool(envVerboseStr)
if err == nil {
mConfiguration.Verbose = envVerbose
}
}
if cmdFlagProvided(CMD_EDIHTTP_VERBOSE) {
mConfiguration.Verbose = verbose
}
edisplitter.SetVerbosity(mConfiguration.Verbose)
edisplitter.SetAfVersion(*afVersion)
if envUdpPort, err := strconv.Atoi(os.Getenv(ENV_EDIHTTP_UDP_PORT)); err == nil {
mConfiguration.UdpPort = envUdpPort
}
if cmdFlagProvided(CMD_EDIHTTP_UDPPORT) {
mConfiguration.UdpPort = inUdpPort
}
if mConfiguration.UdpPort == 0 {
mConfiguration.UdpPort = inUdpPort
}
if envHttpPort, err := strconv.Atoi(os.Getenv(ENV_EDIHTTP_HTTP_PORT)); err == nil {
mConfiguration.HttpPort = envHttpPort
}
if cmdFlagProvided(CMD_EDIHTTP_HTTPPORT) {
mConfiguration.HttpPort = outHttpPort
}
if mConfiguration.HttpPort == 0 {
mConfiguration.HttpPort = outHttpPort
}
if envProxyPort, err := strconv.Atoi(os.Getenv(ENV_EDIHTTP_PROXY_PORT)); err == nil {
fmt.Printf("%s : Config ENV proxyport: %d\n", time.Now().Format(time.UnixDate), envProxyPort)
mConfiguration.ProxyPort = envProxyPort
}
if cmdFlagProvided(CMD_EDIHTTP_PROXYPORT) {
fmt.Printf("%s : Config CMD proxyport: %d\n", time.Now().Format(time.UnixDate), overrideProxyPort)
mConfiguration.ProxyPort = overrideProxyPort
}
if mConfiguration.ProxyPort == 0 {
fmt.Printf("%s : Config proxyport not given, setting it to HTTPport\n", time.Now().Format(time.UnixDate))
mConfiguration.ProxyPort = mConfiguration.HttpPort
}
if envSbtChans := os.Getenv(ENV_EDIHTTP_SBT_SUBCHANS); len(envSbtChans) > 0 {
mConfiguration.SbtChans = envSbtChans
}
if cmdFlagProvided(CMD_EDIHTTP_SBTCHANS) || len(mConfiguration.SbtChans) == 0 {
mConfiguration.SbtChans = sbtchans
}
if envHosts := os.Getenv(ENV_EDIHTTP_HOSTS); len(envHosts) > 0 {
mConfiguration.Hosts = envHosts
fmt.Printf("%s : Config ENV hosts: %s\n", time.Now().Format(time.UnixDate), envHosts)
}
if cmdFlagProvided(CMD_EDIHTTP_HOSTS) || len(mConfiguration.Hosts) == 0 {
mConfiguration.Hosts = *ipOverrideString
fmt.Printf("%s : Config CMD hosts: %s\n", time.Now().Format(time.UnixDate), *ipOverrideString)
}
hostAddress = strings.Split(mConfiguration.Hosts, ",")
if len(mConfiguration.SbtChans) > 0 {
if mConfiguration.SbtChans == "all" {
if mConfiguration.Verbose { fmt.Printf("%s : SBT adding all possible Subchannels enabled\n", time.Now().Format(time.UnixDate)) }
for subId := uint8(0); subId <= 0x3F; subId++ {
mSbtConfig[subId] = mConfiguration.MaxGlobalSbt
}
} else {
sbtChansSplit := strings.Split(mConfiguration.SbtChans, ",")
for _, sbtChan := range sbtChansSplit {
sbtSubMax := strings.Split(sbtChan, ":")
if len(sbtSubMax) > 0 {
var subchanId uint64
var parseErr error
var subMaxSbt = mConfiguration.MaxGlobalSbt
if strings.HasPrefix(sbtSubMax[0], "0x") {
subchanId, parseErr = strconv.ParseUint(strings.TrimPrefix(sbtSubMax[0], "0x"), 16, 8)
} else {
subchanId, parseErr = strconv.ParseUint(sbtSubMax[0], 10, 8)
}
if mConfiguration.Verbose { fmt.Printf("%s : SBT Chan: 0x%02X\n", time.Now().Format(time.UnixDate), subchanId) }
if parseErr != nil {
if mConfiguration.Verbose { fmt.Printf("%s : SBT Error handling sbtchans cmd parameter: %s\n", time.Now().Format(time.UnixDate), sbtSubMax[0]) }
os.Exit(1)
}
if len(sbtSubMax) == 2 {
subMaxSbt, parseErr = strconv.Atoi(sbtSubMax[1])
if parseErr != nil {
if mConfiguration.Verbose { fmt.Printf("%s : SBT Error handling sbtchans cmd parameter: %s\n", time.Now().Format(time.UnixDate), sbtSubMax[0]) }
os.Exit(1)
}
if mConfiguration.Verbose { fmt.Printf("%s : SBT MaxSbt: %d\n", time.Now().Format(time.UnixDate), subMaxSbt) }
}
if mConfiguration.Verbose { fmt.Printf("%s : SBT: Subchannel 0x%02X mAxSbt: %d\n", time.Now().Format(time.UnixDate), subchanId, subMaxSbt) }
mSbtConfig[uint8(subchanId)] = subMaxSbt
}
}
}
}
if mConfiguration.Verbose { fmt.Printf("%s : USE_HTTPS: %s\n", time.Now().Format(time.UnixDate), os.Getenv("USE_HTTPS")) }
useSslInt, err := strconv.Atoi(os.Getenv("USE_HTTPS"))
if err != nil {
useSslInt = 0
}
if mConfiguration.Verbose { fmt.Printf("%s : OVERRIDE_PROXY_PORT: %d - %s\n", time.Now().Format(time.UnixDate), mConfiguration.ProxyPort, os.Getenv("OVERRIDE_PROXY_PORT")) }
ifaces, err := net.Interfaces()
if err == nil {
for _, i := range ifaces {
addrs, err := i.Addrs()
if mConfiguration.Verbose { fmt.Printf("%s : IPIfaceName: %s\n", time.Now().Format(time.UnixDate), i.Name) }
if err == nil {
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
if ip.To4() != nil {
if mConfiguration.Verbose { fmt.Printf("%s : IPNetAddress: %s\n", time.Now().Format(time.UnixDate), ip) }
ipAddresses = append(ipAddresses, ip)
}
case *net.IPAddr:
ip = v.IP
if mConfiguration.Verbose { fmt.Printf("%s : IPAddress: %s\n", time.Now().Format(time.UnixDate), ip) }
}
// process IP address
}
}
}
} else {
if mConfiguration.Verbose { fmt.Println("ErrorOccured getting interfaces") }
}
useSsl := false
if useSslInt == 1 {
useSsl = true
}
outHttpPortS := ":" + strconv.Itoa(mConfiguration.HttpPort)
if mConfiguration.Verbose { fmt.Printf("%s : ENV: %d - %s\n", time.Now().Format(time.UnixDate), mConfiguration.UdpPort, outHttpPortS) }
go listenServer(mConfiguration.UdpPort, outHttpPortS, useSsl)
select {}
}
var mDabServiceNew []*edisplitter.DabSrv = nil
func listenServer(udpInPort int, outHttpPort string, usessl bool) {
if mConfiguration.Verbose { fmt.Printf("%s : Starting to listen\n", time.Now().Format(time.UnixDate)) }
addr := net.UDPAddr{
Port: udpInPort,
IP: net.ParseIP("0.0.0.0"),
}
con, err := net.ListenUDP("udp", &addr)
if err != nil {
if mConfiguration.Verbose { fmt.Printf("%s : Error listening on UDP socket %v\n", time.Now().Format(time.UnixDate), err) }
return
}
ediSplitterChan := make(chan []byte, 10000)
go edisplitter.ParseEdiData(ediSplitterChan)
var mDoneCbNew func (dabServices []*edisplitter.DabSrv)
mDoneCbNew = func (dabServices []*edisplitter.DabSrv) {
if mConfiguration.Verbose { fmt.Printf("%s : DONECBNew: %d\n", time.Now().Format(time.UnixDate), len(dabServices)) }
edisplitter.UnregisterServicesReadyCallbackNew(mDoneCbNew)
mDabServiceNew = dabServices
//For debugging purpose FullEdi
fullSrv := new(edisplitter.DabSrv)
fullSrv.ServiceId = 0xFFFF
fullSrv.ServiceLabel = "FullEDI"
fullSrv.ServiceShortLabel = "FEDI"
fullSrv.NumSrvComponents = 1
fullSrv.CAId = 0
fullSrv.IsProgramme = true
fullSrv.AfFrameOutput = make(chan []byte, 10)
fullComp := new(edisplitter.DabSrvComponent)
fullComp.IsPrimary = true
fullComp.SCIDs = 0xFF
fullComp.ASCTy = 63
fullComp.ServiceId = 0xFFFF
fullComp.SubChannelId = 0xFF
fullSubchan := new(edisplitter.DabSrvSubchannel)
fullComp.Subchannel = fullSubchan
fullSrv.DabServiceComponents = append(fullSrv.DabServiceComponents, fullComp)
mDabServiceNew = append(mDabServiceNew, fullSrv)
//FullEdi
if mListOnlyCmd {
fmt.Printf("------------------------------\n")
fmt.Printf("-- %d DabServices available --\n", len(dabServices))
fmt.Printf("------------------------------\n")
for _, dabSrv := range dabServices {
fmt.Printf("-- Label: %16s --\n-- isProgramme: %5t --\n", dabSrv.ServiceLabel, dabSrv.IsProgramme)
for _, srvComp := range dabSrv.DabServiceComponents {
if srvComp.IsPrimary {
fmt.Printf("-- SubchannelId: 0x%02X - %2d --\n", srvComp.Subchannel.SubchannelId, srvComp.Subchannel.SubchannelId)
break
}
}
fmt.Printf("------------------------------\n")
}
os.Exit(0)
}
if !usessl {
if mConfiguration.Verbose { fmt.Printf("%s : Starting unsecure WebserverNew: %s\n", time.Now().Format(time.UnixDate), outHttpPort) }
go startWebserverNew(mDabServiceNew, outHttpPort)
} else {
if mConfiguration.Verbose { fmt.Printf("%s : Starting secure WebserverNew: %s\n", time.Now().Format(time.UnixDate), outHttpPort) }
go startSecureWebserverNew(mDabServiceNew, outHttpPort)
}
}
edisplitter.RegisterServicesReadyCallbackNew(mDoneCbNew)
mToggleCallback := func(dLabel edisplitter.DynamicLabel) {
mToggleMapMutex.Lock()
if _, exists := mTogglesMap[dLabel.SubchanId]; !exists {
if mConfiguration.Verbose { fmt.Printf("%s : ToggleC adding map for SubchanId: 0x%02X\n", time.Now().Format(time.UnixDate), dLabel.SubchanId) }
mTogglesMap[dLabel.SubchanId] = make(map[int64]edisplitter.DynamicLabel)
}
mToggleMapMutex.Unlock()
//map AF-Sequencenumber to Bufferposition
if sbtBuffer, exists := sbtBuffers[dLabel.SubchanId]; exists {
if sbtBuffer.isTimeshiftable {
var toggleBufPos int64 = (int64(dLabel.AfSeqNum) + (int64(dLabel.AfMulti) * 65535)) - int64(sbtBuffer.zeroAfNum)
if toggleBufPos >= 0 {
dLabel.ToggleId = toggleBufPos
if mConfiguration.Verbose {
fmt.Printf("%s : ToggleC_0x%02X BufferAfZero: %d, ToggleAf: %d, AfMultiVal: %d, ToggleBuf: %d, %s\n", time.Now().Format(time.UnixDate), dLabel.SubchanId, int64(sbtBuffer.zeroAfNum), int64(dLabel.AfSeqNum), uint64(dLabel.AfMulti)*65535, toggleBufPos, dLabel.FullLabel)
}
edisplitter.MPendingToggleMutex.Lock()
edisplitter.MPendingToggle[dLabel.SubchanId] = &dLabel
edisplitter.MPendingToggleMutex.Unlock()
mToggleMapMutex.Lock()
mTogglesMap[dLabel.SubchanId][toggleBufPos] = dLabel
mTogglesSlideMap[dLabel.SubchanId] = append(mTogglesSlideMap[dLabel.SubchanId], toggleSlideMapIdx{
toggleStartTime: dLabel.ReceiveTime,
toggleBuffPos: toggleBufPos,
})
mToggleMapMutex.Unlock()
} else {
if mConfiguration.Verbose { fmt.Printf("%s : Invalid ToggleBuffPos 0x%02X BufferAfZero: %d, ToggleAf: %d, AfMultiVal: %d, ToggleBuf: %d\n", time.Now().Format(time.UnixDate), dLabel.SubchanId, int64(sbtBuffer.zeroAfNum), int64(dLabel.AfSeqNum), uint64(dLabel.AfMulti)*65535, toggleBufPos) }
}
}
}
}
edisplitter.RegisterToggleCallback(mToggleCallback)
mSlideshowCallback := func(slide edisplitter.MotSlideshow) {
toggleSlidePath := fmt.Sprintf("toggleslides/%d/", slide.SubchannelId)
if _, err := os.Stat(toggleSlidePath); os.IsNotExist(err) {
if mConfiguration.Verbose { fmt.Printf("%s : SLSCB for SubchanId: 0x%02X, creating dir at %s\n", time.Now().Format(time.UnixDate), slide.SubchannelId, toggleSlidePath) }
_ = os.MkdirAll(toggleSlidePath, os.ModePerm)
}
mToggleMapMutex.Lock()
if _, exists := mTogglesSlideMap[slide.SubchannelId]; exists {
lenList := len(mTogglesSlideMap[slide.SubchannelId])
toggleDiff := int64(-31000)
var toggle edisplitter.DynamicLabel
toggleBuffPos := int64(0)
if lenList > 0 {
lastToggleTime := mTogglesSlideMap[slide.SubchannelId][lenList-1].toggleStartTime
toggleDiff = lastToggleTime - slide.ReceiveTime.UnixNano()/1000000
toggleBuffPos = mTogglesSlideMap[slide.SubchannelId][lenList-1].toggleBuffPos
toggle = mTogglesMap[slide.SubchannelId][toggleBuffPos]
}
if toggleDiff >= -30000 {
if mConfiguration.Verbose { fmt.Printf("%s : SLSCB_0x%02X saving toggle: %s : %d TimeDiff: %d : %s - %d\n", time.Now().Format(time.UnixDate), slide.SubchannelId, toggle.FullLabel, toggle.ReceiveTime, toggleDiff, slide.ContentName, mTogglesSlideMap[slide.SubchannelId][lenList-1].toggleBuffPos) }
writeErr := ioutil.WriteFile(fmt.Sprintf("%s%d", toggleSlidePath, mTogglesSlideMap[slide.SubchannelId][lenList-1].toggleBuffPos), slide.ImageData, 0644)
if writeErr != nil {
if mConfiguration.Verbose { fmt.Printf("%s : SLSCB write failed: %s\n", time.Now().Format(time.UnixDate), writeErr) }
}
togCopy := toggle
togCopy.SlideMime = slide.ContentMime
togCopy.SlidePath = "/" + toggleSlidePath + strconv.Itoa(int(toggleBuffPos))
mTogglesMap[slide.SubchannelId][toggleBuffPos] = togCopy
if mConfiguration.Verbose { fmt.Printf("%s : Adding LiveToggle again for SLS update for Subchan: 0x%02X - %d - %s\n", time.Now().Format(time.UnixDate), slide.SubchannelId, togCopy.ToggleId, time.Now())}
edisplitter.MPendingToggleMutex.Lock()
edisplitter.MPendingToggle[slide.SubchannelId] = &togCopy
edisplitter.MPendingToggleMutex.Unlock()
if mConfiguration.Verbose { fmt.Printf("%s : SLSCB_0x%02X Saved Toggle: %s - %s\n", time.Now().Format(time.UnixDate), slide.SubchannelId, mTogglesMap[slide.SubchannelId][toggleBuffPos].SlideMime, mTogglesMap[slide.SubchannelId][toggleBuffPos].SlidePath) }
}
}
mToggleMapMutex.Unlock()
}
edisplitter.RegisterSlideshowCallback(mSlideshowCallback)
udpBuff := make([]byte, 3900)
for {
read, _, err := con.ReadFromUDP(udpBuff)
if err != nil {
fmt.Println("Error reading from UDP socket")
return
}
ediData := make([]byte, read)
copy(ediData, udpBuff[:read])
//For debugging purpose FullEdi
if mDabServiceNew != nil {
for _, srv := range mDabServiceNew {
if srv.ServiceId == 0xFFFF {
//fmt.Printf("FullEdi sending data\n")
srv.AfFrameOutput <- ediData
break
}
}
}
//FullEdi
ediSplitterChan <- ediData
}
}
type SbtClient struct {
clientToken string
readAfs uint64
ediDataChan []chan []byte
bufferBurst bool
burstChan *chan []byte
paused bool
pauseHb int64
}
type StreamingServer struct {
HttpServer http.Server
// EdiFrames are pushed to this channel by the main dabservice routine
EdiData chan *EdiFrame
// New client connections
newClients chan func() (uint8, string, uint64, chan []byte)
// Closed client connections
closingClients chan func() (uint8, chan []byte)
// Client connections registry
clients map[uint8][]chan []byte
sbtClients map[uint8][]*SbtClient
}
func startWebserverNew(srvs []*edisplitter.DabSrv, httpPort string) {
fmt.Printf("%s : Starting server\n", time.Now().Format(time.UnixDate))
streamingServer := NewServer()
for _, dabSrv := range srvs {
if mConfiguration.Verbose { fmt.Printf("%s : Starting HandleStreamGo for: 0x%08X\n", time.Now().Format(time.UnixDate), dabSrv.ServiceId) }
go handleStreamGoNew(streamingServer, dabSrv)
}
fmt.Printf("%s : Starting to listen on HTTP port %s\n", time.Now().Format(time.UnixDate), httpPort)
err := http.ListenAndServe(httpPort, streamingServer)
if err != nil {
fmt.Printf("%s : Error occured at starting Webserver: %s\n", time.Now().Format(time.UnixDate), err)
}
}
func startSecureWebserverNew(srvs []*edisplitter.DabSrv, httpPort string) {
fmt.Println("Starting Secure Webserver")
streamingServer := NewServer()
for _, dabSrv := range srvs {
if mConfiguration.Verbose { fmt.Printf("Starting HandleStreamGo for: 0x%08X\n", dabSrv.ServiceId) }
go handleStreamGoNew(streamingServer, dabSrv)
}
fmt.Println("Starting to listen on https")
tlsErr := http.ListenAndServeTLS(":8188", "/etc/letsencrypt/live/edistream.irt.de/fullchain.pem", "/etc/letsencrypt/live/edistream.irt.de/privkey.pem", streamingServer)
if tlsErr != nil {
if mConfiguration.Verbose { fmt.Printf("Error listening for https requests: %s\n", tlsErr) }
}
}
func NewServer() (server *StreamingServer) {
// Instantiate
server = &StreamingServer{
HttpServer: http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 2 * time.Second,
},
newClients: make(chan func() (uint8, string, uint64, chan []byte)),
closingClients: make(chan func() (uint8, chan []byte)),
EdiData: make(chan *EdiFrame, 1000), //addedBuffer for client-load testing
sbtClients: make(map[uint8][]*SbtClient),
}
// Set it running - listening and broadcasting events
go server.listen()
return
}
// Listen on different channels and act accordingly
func (server *StreamingServer) listen() {
for {
select {
case s := <-server.closingClients:
sub, dataChan := s()
for _, delClient := range server.sbtClients[sub] {
for chanIdx, delChan := range delClient.ediDataChan {
if delChan == dataChan {
delClient.ediDataChan = append(delClient.ediDataChan[:chanIdx], delClient.ediDataChan[chanIdx+1:]...)
fmt.Printf("%s : Found channel to delete with TimeshiftToken: %s, %d clients remaining\n", time.Now().Format(time.UnixDate), delClient.clientToken, len(delClient.ediDataChan))
}
}
}
case s := <-server.newClients:
sub, clientToken, readAfs, subChan := s()
newSbtClient := new(SbtClient)
newSbtClient.ediDataChan = append(newSbtClient.ediDataChan, subChan)
newSbtClient.clientToken = clientToken
newSbtClient.readAfs = readAfs
/*newSbtClient.bufferBurst = true
newSbtClient.burstChan = &subChan
newSbtClient.paused = false
server.sbtClients[sub] = append(server.sbtClients[sub], newSbtClient)
*/
if mConfiguration.Verbose { fmt.Printf("%s : SBT Added Client %d for Subchan: %d with CLientToken: %s\n", time.Now().Format(time.UnixDate), len(server.clients[sub]), sub, clientToken) }
FindDabSrv:
for _, dabSrv := range mDabServiceNew {
for _, srvComp := range dabSrv.DabServiceComponents {
if srvComp.SubChannelId == sub {
if mConfiguration.Verbose { fmt.Printf("%s : SBT Added Client sending DETIAF\n", time.Now().Format(time.UnixDate)) }
//TODO FullEdi
if srvComp.SubChannelId != 0xFF {
for detiCnt := 0; detiCnt < 4; detiCnt++ {
subChan <- edisplitter.CreateDetiAF(dabSrv)
}
}
//TODO FullEdi
break FindDabSrv
}
}
}
newSbtClient.bufferBurst = true
newSbtClient.burstChan = &subChan
newSbtClient.paused = false
server.sbtClients[sub] = append(server.sbtClients[sub], newSbtClient)
}
}
}
//POST request json structure for controlling timeshiftclient. Mapping from timeshiftToken (lowercase) to public TimeshiftToken
type SbtPostRequest struct {
TimeshiftToken string `json:"timeshiftToken"`
Action string `json:"action"`
WantedPos int64 `json:"wantedPos"`
WantedUts int64 `json:"wantedUts"`
ToggleId int64 `json:"toggleId"`
}
func (sbtPost *SbtPostRequest) UnmarshalJSON(b []byte) error {
type xSbtPostRequest SbtPostRequest
xSbtPost := &xSbtPostRequest{WantedUts:-1, ToggleId:-1}
if err := json.Unmarshal(b, xSbtPost); err != nil {
return err
}
*sbtPost = SbtPostRequest(*xSbtPost)
return nil
}
func (server *StreamingServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
//TODO burst buffer request with seconds of buffer
if req.Method == http.MethodOptions {
if mConfiguration.Verbose { fmt.Printf("%s : HTTP_OPTIONS %s\n", time.Now().Format(time.UnixDate), req) }
subchanIdString := strings.TrimPrefix(req.URL.Path, "/services/")
if mConfiguration.Verbose { fmt.Printf("%s : SBT ClientToken string: %s\n", time.Now().Format(time.UnixDate), subchanIdString) }
subchanId, err := strconv.Atoi(subchanIdString)
sbtBuffersMutex.RLock()
if err != nil && sbtBuffers[uint8(subchanId)].isTimeshiftable {
rw.Header().Set("Access-Control-Allow-Methods", "POST")
}
sbtBuffersMutex.RUnlock()
rw.Header().Set("Access-Control-Allow-Origin", "*")
rw.Header().Set("Access-Control-Allow-Headers", "content-type")
written, err := rw.Write(nil)
if err != nil {
if mConfiguration.Verbose { fmt.Printf("%s : Error Writing OPTIONS: %d\n", time.Now().Format(time.UnixDate), written) }
http.Error(rw, "", http.StatusBadRequest)
}
}
//POST control method incoming
if req.Method == http.MethodPost {
if mConfiguration.Verbose { fmt.Printf("%s : HTTP_POST method: %s \n %s\n", time.Now().Format(time.UnixDate), req.URL, req.Body) }
var postReqJson SbtPostRequest
err := json.NewDecoder(req.Body).Decode(&postReqJson)
if err != nil {
if mConfiguration.Verbose { fmt.Printf("%s : JsonDecoded failed: %s\n", time.Now().Format(time.UnixDate), err.Error()) }
http.Error(rw, "", http.StatusBadRequest)
return
}
if mConfiguration.Verbose { fmt.Printf("%s : JsonDecoded: Token: %s, WantedPos: %d\n", time.Now().Format(time.UnixDate), postReqJson.TimeshiftToken, postReqJson.WantedPos) }
reqClientToken := postReqJson.TimeshiftToken
wantedPosMs := postReqJson.WantedPos
wantedUts := postReqJson.WantedUts
toggleId := postReqJson.ToggleId
fmt.Printf("%s : SeekPTS: WantedUTS: %d, ToggleId: %d\n", time.Now().Format(time.UnixDate),wantedUts, toggleId)
action := postReqJson.Action
if mConfiguration.Verbose { fmt.Printf("%s : SBT HTTP POST action: %s clientToken: %s, WantedPos: %d\n", time.Now().Format(time.UnixDate), action, reqClientToken, wantedPosMs) }
id := strings.TrimPrefix(req.URL.Path, "/services/")
if mConfiguration.Verbose { fmt.Printf("%s : SBT ClientToken string: %s\n", time.Now().Format(time.UnixDate), id) }
subchanIdInt, err := strconv.Atoi(id)
subchanId := uint8(subchanIdInt)
if err == nil {
if action == "items" {
if mConfiguration.Verbose { fmt.Printf("%s : ToggleC for 0x%02X: %d\n", time.Now().Format(time.UnixDate), subchanId, len(mTogglesMap[subchanId])) }
var togglesJson = []byte("[")
mToggleMapMutex.Lock()
for _, toggleItem := range mTogglesMap[subchanId] {
if mConfiguration.Verbose { fmt.Printf("%s : ToggleC Marshaling: %s\n", time.Now().Format(time.UnixDate), toggleItem.FullLabel) }
togJson, marshErr := json.Marshal(toggleItem)
if marshErr == nil {
togglesJson = append(togglesJson, togJson...)
togglesJson = append(togglesJson, []byte(",")...)
}
}
mToggleMapMutex.Unlock()
if len(togglesJson) > 2 {
togglesJson = togglesJson[:len(togglesJson)-1]
}
togglesJson = append(togglesJson, []byte("]")...)
if mConfiguration.Verbose { fmt.Printf("%s : ToggleCMarshal: %s\n", time.Now().Format(time.UnixDate), togglesJson) }
rw.Header().Set("Cache-Control", "no-cache")
rw.Header().Set("Connection", "close")
rw.Header().Set("Access-Control-Allow-Origin", "*")
_, _ = rw.Write(togglesJson)
return
}
//TODO live item while client is paused
//TODO there must be some bug with connecting tokens that exists but with the wrong service
for _, sbtCli := range server.sbtClients[(subchanId)] {
if mConfiguration.Verbose { fmt.Printf("%s : SBT searching client: %s : %s\n", time.Now().Format(time.UnixDate), sbtCli.clientToken, reqClientToken) }
if sbtCli.clientToken == reqClientToken {
if mConfiguration.Verbose { fmt.Printf("%s : SBT found client at AFPos: %d\n", time.Now().Format(time.UnixDate), sbtCli.readAfs) }
if action == "toggle" {
if mConfiguration.Verbose { fmt.Printf("%s : ToggleCSeek to %d\n", time.Now().Format(time.UnixDate), wantedPosMs) }
if wantedPosMs >= 0 && wantedPosMs <= sbtBuffers[subchanId].maxAfs {
sbtCli.readAfs = uint64(wantedPosMs)
rw.Header().Set("Cache-Control", "no-cache")
rw.Header().Set("Connection", "close")
rw.Header().Set("Access-Control-Allow-Origin", "*")
_, _ = rw.Write(nil)
} else {
http.Error(rw, "Invalid toggle", http.StatusBadRequest)
}
break
}
if action == "seek" {
sbtCli.bufferBurst = true
//Read max timeshift time | maxAfs * 24 ms/AF
maxTimeshiftTime := sbtBuffers[subchanId].maxTsMs
if wantedUts >= 0 {
if wantedUts >= 9999999999999 {
if mConfiguration.Verbose {
fmt.Printf("%s : Maximum timeshift length of %d exceeded: %d\n", time.Now().Format(time.UnixDate), maxTimeshiftTime, wantedPosMs)
}
http.Error(rw, "only seconds and microseconds wantedUts timestamps supported", http.StatusBadRequest)
break
}
var wantedPosix time.Time
if wantedUts > 9999999999 {
wantedPosix = time.Unix(0, wantedUts*int64(time.Millisecond))
fmt.Printf("%s : SeekPTS SBT millis: %s\n", time.Now().Format(time.UnixDate), wantedPosix.String())
} else {
wantedPosix = time.Unix(wantedUts, 0)
fmt.Printf("%s : SeekPTS SBT second: %s\n", time.Now().Format(time.UnixDate), wantedPosix.String())
}
firstPosTime := sbtBuffers[subchanId].lastPosTime.Add(time.Millisecond * time.Duration(-sbtBuffers[subchanId].maxTsMs))
if mConfiguration.Verbose {
fmt.Printf("%s : SeekPTS SBT Fir: %s\n", time.Now().Format(time.UnixDate), firstPosTime.String())
}
if mConfiguration.Verbose {
fmt.Printf("%s : SeekPTS SBT Max: %s\n", time.Now().Format(time.UnixDate), sbtBuffers[subchanId].lastPosTime.String())
}
var seekBuffPos uint64 = 0
if wantedPosix.Before(firstPosTime) {
//TODO the mod here may be wrong
seekBuffPos = uint64((sbtBuffers[subchanId].buffPos + 1) % sbtBuffers[subchanId].maxAfs)
if mConfiguration.Verbose {
fmt.Printf("%s : SeekPTS SBT Zero CurBuffPos: %d, MaxAfs: %d, SeekPos: %d\n", time.Now().Format(time.UnixDate), sbtBuffers[subchanId].buffPos, sbtBuffers[subchanId].maxAfs, seekBuffPos)
}
sbtCli.readAfs = seekBuffPos
rw.Header().Set("Cache-Control", "no-cache")
rw.Header().Set("Connection", "close")
rw.Header().Set("Access-Control-Allow-Origin", "*")
_, _ = rw.Write(nil)
break
} else if wantedPosix.After(sbtBuffers[subchanId].lastPosTime) {
if mConfiguration.Verbose {
fmt.Printf("%s : SeekPTS SBT in the future seeking to realtime\n", time.Now().Format(time.UnixDate))
}
sbtCli.readAfs = uint64(sbtBuffers[subchanId].buffPos)
rw.Header().Set("Cache-Control", "no-cache")
rw.Header().Set("Connection", "close")
rw.Header().Set("Access-Control-Allow-Origin", "*")
_, _ = rw.Write(nil)
break
} else if (wantedPosix.Equal(firstPosTime) || wantedPosix.After(firstPosTime)) && (wantedPosix.Equal(sbtBuffers[subchanId].lastPosTime) || wantedPosix.Before(sbtBuffers[subchanId].lastPosTime)) {
if mConfiguration.Verbose {
fmt.Printf("%s : SeekPTS SBT is in range: %s - %d\n", time.Now().Format(time.UnixDate), sbtBuffers[subchanId].lastPosTime.Sub(wantedPosix).String(), sbtBuffers[subchanId].lastPosTime.Sub(wantedPosix).Milliseconds())
}
numAfsFromEnd := sbtBuffers[subchanId].lastPosTime.Sub(wantedPosix).Milliseconds() / 24
if mConfiguration.Verbose { fmt.Printf("%s : SeekPTS SBT NumAfs from end: %d\n", time.Now().Format(time.UnixDate), numAfsFromEnd) }
curBufPos := sbtBuffers[subchanId].buffPos
if curBufPos-numAfsFromEnd < 0 {
if mConfiguration.Verbose { fmt.Printf("%s : SeekPTS SBT seekBuffPos negative: %d\n", time.Now().Format(time.UnixDate), curBufPos-numAfsFromEnd) }
seekBuffPos = uint64(sbtBuffers[subchanId].maxAfs + (curBufPos - numAfsFromEnd))
} else {
if mConfiguration.Verbose { fmt.Printf("%s : SeekPTS SBT seekBuffPos positive: %d\n", time.Now().Format(time.UnixDate), curBufPos-numAfsFromEnd) }
seekBuffPos = uint64(curBufPos - numAfsFromEnd)
}
sbtCli.readAfs = seekBuffPos
rw.Header().Set("Cache-Control", "no-cache")
rw.Header().Set("Connection", "close")
rw.Header().Set("Access-Control-Allow-Origin", "*")
_, _ = rw.Write(nil)
break
}
}
if mConfiguration.Verbose { fmt.Printf("%s : MaxTimeshift Time: %d\n", time.Now().Format(time.UnixDate), maxTimeshiftTime) }
//Convert wantedPos to AFs
wantedPosAfs := wantedPosMs / 24
if mConfiguration.Verbose { fmt.Printf("%s : WantedPos AFs: %d\n", time.Now().Format(time.UnixDate), wantedPosAfs) }
// time from buffPos zero
if wantedPosMs >= 0 {
if wantedPosMs <= maxTimeshiftTime {
//Realtime
if wantedPosMs == 0 {
sbtCli.readAfs = uint64(sbtBuffers[subchanId].buffPos)
} else {
curBufPos := sbtBuffers[subchanId].buffPos
var wantedBuffPos uint64 = 0
if curBufPos-wantedPosAfs < 0 {
if mConfiguration.Verbose {
fmt.Printf("%s : Buffer unwrap for CurPos: %d - WantedAfs: %d \n", time.Now().Format(time.UnixDate), curBufPos, wantedPosAfs)
}
wantedBuffPos = uint64(sbtBuffers[subchanId].maxAfs + (curBufPos - wantedPosAfs))
if mConfiguration.Verbose {
fmt.Printf("%s : Buffer unwrap to WantedBuffPos: %d - CurPos: %d\n", time.Now().Format(time.UnixDate), wantedBuffPos, curBufPos)
}
} else {
wantedBuffPos = uint64(curBufPos - wantedPosAfs)
if mConfiguration.Verbose {
fmt.Printf("%s : Buffer to WantedBuffPos: %d - CurPos: %d\n", time.Now().Format(time.UnixDate), wantedBuffPos, curBufPos)
}
}
sbtCli.readAfs = wantedBuffPos
}
rw.Header().Set("Cache-Control", "no-cache")
rw.Header().Set("Connection", "close")
rw.Header().Set("Access-Control-Allow-Origin", "*")
_, _ = rw.Write(nil)
} else {
//write error
if mConfiguration.Verbose { fmt.Printf("%s : Maximum timeshift length of %d exceeded: %d\n", time.Now().Format(time.UnixDate), maxTimeshiftTime, wantedPosMs) }
http.Error(rw, "WantedPos exceeds maximum timeshift", http.StatusBadRequest)
}
} else {
if mConfiguration.Verbose { fmt.Printf("%s : Negative wantedPos %d\n", time.Now().Format(time.UnixDate), wantedPosMs) }
http.Error(rw, "Negative wantedPos", http.StatusBadRequest)
}
break
}
if action == "pause" {
sbtCli.paused = true
sbtCli.pauseHb = time.Now().Unix()
break
}
if action == "play" {
sbtCli.paused = false
sbtCli.pauseHb = 0
break
}
}
}
} else {
http.Error(rw, "", http.StatusBadRequest)
}
}
if req.Method == http.MethodGet {
if mConfiguration.Verbose { fmt.Printf("%s : HTTP_GET method: %s %s\n", time.Now().Format(time.UnixDate), req.URL, req.Body) }
flusher, ok := rw.(http.Flusher)
if !ok {
http.Error(rw, "Streaming unsupported!", http.StatusInternalServerError)
return
}
id := strings.TrimPrefix(req.URL.Path, "/services/")
if strings.Contains(id, "toggleslides") {
if mConfiguration.Verbose { fmt.Printf("%s : GETREQ contains toggleslides\n", time.Now().Format(time.UnixDate)) }
toggleReqSplit := strings.Split(id, "/")
if len(toggleReqSplit) == 4 {
toggleSlidePath := toggleReqSplit[1] + "/" + toggleReqSplit[2] + "/" + toggleReqSplit[3]
if mConfiguration.Verbose { fmt.Printf("%s : GETREQ ToggleSlide reconstructed path: %s\n", time.Now().Format(time.UnixDate), toggleSlidePath) }
rw.Header().Set("Cache-Control", "no-cache")
rw.Header().Set("Connection", "close")
rw.Header().Set("Access-Control-Allow-Origin", "*")
if _, err := os.Stat(toggleSlidePath); !os.IsNotExist(err) {
if mConfiguration.Verbose { fmt.Printf("%s : GETREQ file exists\n", time.Now().Format(time.UnixDate)) }
imgData, err := ioutil.ReadFile(toggleSlidePath)
if err != nil {
if mConfiguration.Verbose { fmt.Printf("%s : GETREQ file read error: %s\n", time.Now().Format(time.UnixDate), err) }
}
_, _ = rw.Write(imgData)
return
} else {
if mConfiguration.Verbose { fmt.Printf("%s : GETREQ file doesn't exists\n", time.Now().Format(time.UnixDate)) }
http.Error(rw, "", http.StatusNotFound)
//TODO how to kill connection
//_, _ = rw.Write(nil)
return
}
}
}