summaryrefslogtreecommitdiff
path: root/cmpcj/src/de/schwarzrot/control/client/CMPClient.java
blob: 38c6ccb07639e96a7deab63700dc4950d1d55cad (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
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
/**
 * ======================== legal notice ======================
 * 
 * File:      CMPClient.java
 * Created:   13. June 2012, 04:57
 * Author:    <a href="mailto:geronimo013@gmx.de">Geronimo</a>
 * Project:   cmpc - a java frontend (client) part of compound media player
 *                   uses external players to play the media
 * 
 * CMP - compound media player
 * 
 * is a client/server mediaplayer intended to play any media from any workstation
 * without the need to export or mount shares. cmps is an easy to use backend
 * with a (ready to use) HTML-interface. Additionally the backend supports
 * authentication via HTTP-digest authorization.
 * cmpc is a client with vdr-like osd-menues.
 * 
 * Copyright (c) 2012 Reinhard Mantey, some rights reserved!
 * published under Creative Commons by-sa
 * For details see http://creativecommons.org/licenses/by-sa/3.0/
 * 
 * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp
 * 
 * --------------------------------------------------------------
 */
package de.schwarzrot.control.client;


import java.net.ConnectException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.Date;
import ca.odell.glazedlists.EventList;
import de.schwarzrot.base.util.ApplicationServiceProvider;
import de.schwarzrot.media.domain.AbstractMediaNode;
import de.schwarzrot.media.domain.Media;
import de.schwarzrot.media.domain.MediaServer;
import de.schwarzrot.media.domain.PlayList;
import de.schwarzrot.media.util.ElementConsumer;
import de.schwarzrot.media.util.ListLoader;
import de.schwarzrot.media.util.MedialistParser;


public class CMPClient implements ListLoader<Media> {
    public CMPClient(MediaServer serverDefinition) {
        server = serverDefinition;
    }


    public void execServerRescan() {
        try {
            URL request = new URL("http", server.getHostName(), server.getPort(), "/refresh");
            URLConnection conn = request.openConnection();

            conn.connect();
            conn.getContentLength();
        } catch (Throwable t) {
            throw new RuntimeException(t);
        }
    }


    public void execServerShutdown() {
        try {
            URL request = new URL("http", server.getHostName(), server.getPort(), "/stop");
            URLConnection conn = request.openConnection();

            conn.connect();
            conn.getContentLength();
        } catch (Throwable t) {
            throw new RuntimeException(t);
        }
    }


    public MediaServer getServerDefinition() {
        return server;
    }


    @Override
    public long[] loadFirst(ElementConsumer<Media> consumer) {
        MedialistParser jsonParser = ApplicationServiceProvider.getService(MedialistParser.class);
        URL request = null;
        Date start, end;
        long[] rv = new long[2];

        try {
            start = new Date();
            request = new URL("http", server.getHostName(), server.getPort(), "/?format=json");
            URLConnection conn = request.openConnection();
            final PlayList firstPlaylist = jsonParser.parseListChunk(conn.getInputStream());

            for (Media m : firstPlaylist.getResults())
                consumer.consumeElement(m);

            end = new Date();
            rv[0] = firstPlaylist.getResults().size();
            rv[1] = firstPlaylist.getTotal();
            System.out.println("processing of first playlist-part took " + (end.getTime() - start.getTime()) + " ms.");
        } catch (UnknownHostException uhe) {
            throw new RuntimeException("failed to connect to " + server.getHostName(), uhe);
        } catch (ConnectException ce) {
            throw new RuntimeException("connection failure with " + server.getHostName(), ce);
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return rv;
    }


    @Override
    public long loadRest(long[] listInfo, ElementConsumer<Media> consumer) {
        MedialistParser jsonParser = ApplicationServiceProvider.getService(MedialistParser.class);
        URL request = null;
        Date start, end;
        long rv = 0;

        try {
            PlayList next;
            URLConnection conn;
            long total = listInfo[1];
            long n = listInfo[0];
            long elementsRead = 0;
            String uri;

            start = new Date();
            try {
                while (n < total) {
                    uri = String.format("/?start=%d&limit=%d&format=json", n, 100);
                    request = new URL("http", server.getHostName(), server.getPort(), uri);
                    conn = request.openConnection();
                    next = jsonParser.parseListChunk(conn.getInputStream());
                    for (Media m : next.getResults())
                        consumer.consumeElement(m);
                    elementsRead = next.getResults().size();
                    n += elementsRead;
                    rv += elementsRead;
                    Thread.sleep(10);
                }
            } catch (UnknownHostException uhe) {
                throw new RuntimeException("failed to connect to " + server.getHostName(), uhe);
            } catch (ConnectException ce) {
                throw new RuntimeException("connection failure with " + server.getHostName(), ce);
            } catch (Throwable t) {
                System.err.println("Error on chunk #" + n);
                t.printStackTrace();
            }
            end = new Date();
            System.out.println("processing of rest of playlist took " + (end.getTime() - start.getTime()) + " ms.");
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return rv;
    }


    public void transmitChanges(EventList<AbstractMediaNode> changes) {
        for (AbstractMediaNode n : changes) {
            System.out.println("geänderter Eintrag: " + n.getClass().getSimpleName() + " - " + n.getName()); //$NON-NLS-1$ 
            System.out.println("\tverschieben von " + n.getOriginalPath().getAbsolutePath() + " nach "
                    + n.getRealPath().getAbsolutePath());

            //TODO: really create jobs for changes, so backend can participate on work

        }
    }

    private MediaServer server;
}