]> git.vomp.tv Git - vompclient.git/blob - vdr.h
Connection failed detection
[vompclient.git] / vdr.h
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #ifndef VDR_H
22 #define VDR_H
23
24 #include <stdio.h>
25 #include <time.h>
26 #include <pthread.h>
27 #include <vector>
28 #include <algorithm>
29
30 #include "defines.h"
31 #include "log.h"
32 #include "dsock.h"
33 #include "tcp.h"
34 #include "directory.h"
35 #include "recording.h"
36 #include "channel.h"
37 #include "event.h"
38 #include "rectimer.h"
39
40 using namespace std;
41
42 typedef vector<Event*> EventList;
43 typedef vector<Channel*> ChannelList;
44 typedef vector<RecTimer*> RecTimerList;
45
46 struct VDRServer
47 {
48   char* ip;
49   char* name;
50 };
51
52 struct RecTimerSorter     // : public binary_function<double, double, bool>
53 {
54   bool operator() (const RecTimer* a, const RecTimer* b)
55   {
56     return a->startTime < b->startTime;
57   }
58 };
59
60 struct ServerSorter
61 {
62   bool operator() (const VDRServer& a, const VDRServer& b)
63   {
64     if (strcmp(b.name, a.name) > 0) return true;
65     return false;
66   }
67 };
68
69 struct RecordingSorter
70 {
71   bool operator() (const Recording* a, const Recording* b)
72   {
73     int c = strcmp(b->getProgName(), a->getProgName());
74     if (c > 0) return true;
75     if (c < 0) return false;
76
77     return a->start < b->start;
78   }
79 };
80
81 struct DirectorySorter
82 {
83   bool operator() (const Directory* a, const Directory* b)
84   {
85     int c = strcmp(b->name, a->name);
86     if (c > 0) return true;
87     return false;
88   }
89 };
90
91 class VDR
92 {
93
94   public:
95     VDR();
96     ~VDR();
97     static VDR* getInstance();
98
99     int init(int port);
100     int shutdown();
101
102     void findServers(vector<VDRServer>& servers);
103     void cancelFindingServer();
104     void setServerIP(char*);
105     int connect();
106     void disconnect();
107     bool isConnected() { return connected; }
108     ULLONG getResumePoint(char* fileName);  // uses configLoad
109
110     void setReceiveWindow(size_t size);
111
112     // protocol functions
113     // for the following, if result == false then the connection has died
114     //  doLogin
115     //  getRecordingList
116     //  getChannelsList
117     //  getChannelSchedule
118     //  getRecTimersList
119     // isConnected can be called after the following to determine if still ok
120     //  getRecordingSummary
121     //  deleteRecording
122     //  streamRecording
123     //  rescanRecording
124     //  positionFromFrameNumber
125     //  streamChannel
126     //  getBlock
127     //  stopStreaming
128     //  configLoad
129     //  configSave
130     //  setEventTimer
131
132     int doLogin();
133
134     Directory* getRecordingsList();
135     char*      getRecordingSummary(char* fileName);
136     int        deleteRecording(char* fileName);
137     ULLONG     streamRecording(Recording* rec);
138     ULLONG     rescanRecording();
139     ULLONG     positionFromFrameNumber(ULONG frameNumber);
140
141     ChannelList* getChannelsList(ULONG type);
142     int          streamChannel(ULONG number);
143
144     UCHAR*     getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived);
145     int        stopStreaming();
146     EventList* getChannelSchedule(ULONG number);
147     EventList* getChannelSchedule(ULONG number, time_t start, ULONG duration);
148     int        configSave(char* section, char* key, const char* value);
149     char*      configLoad(char* section, char* key);
150     ULONG      setEventTimer(char* timerString);
151
152     RecTimerList* getRecTimersList();
153
154     // end
155
156     const static ULONG VIDEO = 1;
157     const static ULONG RADIO = 2;
158
159   private:
160     static VDR* instance;
161     Log* logger;
162     int initted;
163     int findingServer;
164     TCP* tcp;
165     int port;
166     char serverIP[16];
167     bool connected;
168     pthread_mutex_t mutex;
169
170     UCHAR* packet;
171     ULONG packetLength;
172     ULONG packetPos;
173
174     const static ULONG VDR_LOGIN               = 1;
175     const static ULONG VDR_GETRECORDINGLIST    = 2;
176     const static ULONG VDR_DELETERECORDING     = 3;
177     const static ULONG VDR_GETSUMMARY          = 4;
178     const static ULONG VDR_GETCHANNELLIST      = 5;
179     const static ULONG VDR_STREAMCHANNEL       = 6;
180     const static ULONG VDR_GETBLOCK            = 7;
181     const static ULONG VDR_STOPSTREAMING       = 8;
182     const static ULONG VDR_STREAMRECORDING     = 9;
183     const static ULONG VDR_GETCHANNELSCHEDULE  = 10;
184     const static ULONG VDR_CONFIGSAVE          = 11;
185     const static ULONG VDR_CONFIGLOAD          = 12;
186     const static ULONG VDR_RESCANRECORDING     = 13;
187     const static ULONG VDR_GETTIMERS           = 14;
188     const static ULONG VDR_SETTIMER            = 15;
189     const static ULONG VDR_POSFROMFRAME        = 16;
190
191     int  getPacket();
192     void freePacket();
193     int  serverError();
194     char*  extractString();
195     ULONG  extractULONG();
196     ULLONG extractULLONG();
197     long   extractLONG();
198 };
199
200 #endif