-
Notifications
You must be signed in to change notification settings - Fork 0
/
gostream.go
85 lines (73 loc) · 1.62 KB
/
gostream.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
// Author: Marcin Matlaszek https://github.com/emate
package main
import (
"flag"
"fmt"
"net"
"os"
"time"
"github.com/ActiveState/tail"
)
func ReadFile(filename string, stream chan<- string) {
seekinfo := tail.SeekInfo{Whence: os.SEEK_END}
t, _ := tail.TailFile(filename, tail.Config{Follow: true, Location: &seekinfo})
for line := range t.Lines {
stream <- line.Text + "\n"
}
}
func client(conn net.Conn, c <-chan string) {
for msg := range c {
conn.Write([]byte(msg))
}
}
func server(channels map[chan string]bool, input <-chan string) {
for {
fmt.Println("Servering!")
for msg := range input {
for k, _ := range channels {
fmt.Println("Sending")
k <- msg
}
time.Sleep(1 * time.Second)
}
}
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage %s <FILENAME>", os.Args[0])
flag.PrintDefaults()
os.Exit(1)
}
func main() {
flag.Usage = usage
address := flag.String("l", "localhost:8080", "Listen address")
flag.Parse()
args := flag.Args()
if len(args) < 1 {
fmt.Println("Filepath argument missing")
usage()
}
var filename string = args[0]
if _, err := os.Stat(filename); os.IsNotExist(err) {
fmt.Printf("no such file : %s", filename)
usage()
}
connections := make(map[net.Conn]bool)
channels := make(map[chan string]bool)
filestream := make(chan string)
fmt.Println(*address)
l, err := net.Listen("tcp", *address)
if err != nil {
panic(err)
}
defer l.Close()
go ReadFile(filename, filestream)
go server(channels, filestream)
for {
c, _ := l.Accept()
defer c.Close()
channel := make(chan string)
channels[channel] = true
connections[c] = true
go client(c, channel)
}
}