aboutsummaryrefslogtreecommitdiff
path: root/osp/go/cmd/osp.go
blob: ad8d7f0dcea00f020da4b78879178c44030e1bba (plain)
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package main

// TODO(pthatcher): Add response messages from receiver

//  Inject JS into viewURL to using .Eval and .Bind to send and receiver presentation connection messages

import (
	"context"

	"flag"
	"fmt"
	"log"

  "osp"

	"github.com/zserge/webview"
)

func runServer(ctx context.Context, mdnsInstanceName string, port int) {
	tlsCert, err := osp.GenerateTlsCert()
	if err != nil {
		log.Fatalln("Failed to generate TLS cert:", err.Error())
	}
	osp.RunPresentationReceiver(ctx, mdnsInstanceName, port, *tlsCert, viewUrl)
}

func browseMdns(ctx context.Context) {
	entries, err := osp.BrowseMdns(ctx)
	if (err != nil) {
		log.Fatalf("Failed to browse mDNS: %v\n", err)
	}
	for entry := range entries {
		log.Println(fmt.Sprintf("%s at %s(%s or %s):%d", entry.Instance, entry.HostName, entry.AddrIPv4, entry.AddrIPv6, entry.Port))
	}
}

func flingUrl(ctx context.Context, target string, url string) {
	log.Printf("Search for %s\n", target)
	entries, err := osp.BrowseMdns(ctx)
	if (err != nil) {
		log.Fatalf("Failed to browse mDNS: %v\n", err)
	}
	for entry := range entries {
		if entry.Instance == target {
			log.Printf("Fling %s to %s:%d\n", url, entry.HostName, entry.Port)
			err := osp.StartPresentation(ctx, entry.HostName, entry.Port, url);
			if err != nil {
				log.Fatalln("Failed to start presentation.");
			}
			break
		}
	}
}

func viewUrl(url string) {
	web := webview.New(webview.Settings{
		// Title: "Open Screen",
		URL:       url,
		Width:     800,
		Height:    600,
		Resizable: true,
	})
	web.Dispatch(func() {
		// web.SetFullscreen(true)
	})
	web.Dispatch(func() {
		// web.Eval();
		// web.Bind();
	})
	web.Run()
}

func main() {
	port := flag.Int("port", 10000, "port")
	flag.Parse()
	args := flag.Args()
	if len(args) < 1 {
		log.Fatalln("Usage: osp server name; osp browse; osp fling url; osp view url")
	}

	ctx := context.Background()

	cmd := args[0]
	switch cmd {
	case "server":
		if len(args) < 2 {
			log.Fatalln("Usage: osp server name")
		}
		mdnsInstanceName := args[1]
    runServer(ctx, mdnsInstanceName, *port)

	case "browse":
    browseMdns(ctx)

	case "fling":
		if len(args) < 3 {
			log.Fatalln("Usage: osp fling target url")
		}
		target := args[1]
		url := args[2]

    flingUrl(ctx, target, url)

	case "view":
		if len(args) < 2 {
			log.Fatalln("Usage: osp view url")
		}

		url := args[1]

		viewUrl(url)
	}
}