summaryrefslogtreecommitdiff
path: root/src/server.cc
blob: 7d6127d42db3014d6deab5c0af22d6501f9062ae (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
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            server.cc
 *
 *  Mon Nov  8 11:35:01 CET 2004
 *  Copyright  2004 Bent Bisballe
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *    This file is part of MIaV.
 *
 *    MIaV is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    MIaV is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with MIaV; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */

/*
 * $Id$
 */

/*
 * $Log$
 * Revision 1.12  2005/05/07 10:56:18  deva
 * Changed print outs
 *
 * Revision 1.11  2005/05/03 08:31:59  deva
 * Removed the error object, and replaced it with a more generic info object.
 *
 * Revision 1.10  2005/05/02 19:47:43  deva
 * Fixed overlapping cpr numbers on the server (now it saves one cpr pr.
 * connection, and ignores any changes sent)
 *
 * Revision 1.9  2005/05/02 18:46:15  deva
 * Files are now saved in a custom folder (defined in miav.conf)
 *
 * Revision 1.8  2005/05/01 09:56:26  deva
 * Added Id and Log tags to all files
 */

#include "server.h"
#include "miav.h"

#include <stdio.h>
#include <stdlib.h>

// For mkdir
#include <sys/stat.h>
#include <sys/types.h>

// For unlink
#include <unistd.h>

#include "miav_config.h"

#include "mov_encoder.h"
#include "img_encoder.h"

#include "server_status.h"

#include "dv.h"

#include "info_console.h"

void saveFrameAsImage(char* cpr, Frame *f)
{
  ImgEncoder imgenc;
  struct tm *ltime;
  time_t t = time(NULL);
  FILE *fp;
  int cnt = 0;
  char fname[256];
  char birthmonth[3];
  char date[9];
  
  string *root = config->readString("server_root");

  // Test for server root writeability
  sprintf(fname, "%s/miavtemp.tmp%d", (char*)root->c_str(), rand());
  fp = fopen(fname, "w");
  if(!fp) {
    int r = rand();
    fprintf(stderr, "MIaV does not have write access to the server root [%s]\n", root->c_str());
    fprintf(stderr, "Redirecting output to [/tmp/miav-%d.mpg]\n", r);
    sprintf(fname, "/tmp/miav-%d.mpg", r);
    imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best.
    return;
  }
  fclose(fp);
  unlink(fname);

  // Check for cpr length correctness
  if(strlen(cpr) != 11) {
    int r = rand();
    fprintf(stderr, "Illigal CPR, it must have length 11, it had lentgh %d\n", strlen(cpr));
    fprintf(stderr, "Redirecting output to [/tmp/miav-%d.jpg]\n", r);
    sprintf(fname, "/tmp/miav-%d.mpg", r);
    imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best.
    return;
  }

  // Copy the bytes representing the birth month from the cpr
  // [dd][mm][yy]-[nn][nn]
  strncpy(birthmonth, &cpr[2], 2);
  birthmonth[2] = 0;

  // Create folder named birthmonth in server root
  sprintf(fname, "%s/%s", root->c_str(), birthmonth);
  if(!mkdir(fname, S_IRWXU) == -1 && errno != EEXIST) {
    int r = rand();
    fprintf(stderr, "Not possible to create subfolder %s\n", fname);
    fprintf(stderr, "Redirecting output to [/tmp/miav-%d.jpg]\n", r);
    sprintf(fname, "/tmp/miav-%d.mpg", r);
    imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best.
    return;
  }

  // Create folder named cpr in serverroot/birthmonth
  sprintf(fname, "%s/%s/%s", root->c_str(), birthmonth, cpr);
  if(!mkdir(fname, S_IRWXU) == -1 && errno != EEXIST) {
    int r = rand();
    fprintf(stderr, "Not possible to create subfolder %s\n", fname);
    fprintf(stderr, "Redirecting output to [/tmp/miav-%d.jpg]\n", r);
    sprintf(fname, "/tmp/miav-%d.mpg", r);
    imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best.
    return;
  }

  // Create date (today) in [yyyy][mm][dd]
  ltime = localtime(&t);
  sprintf(date, "%.4d%.2d%.2d", 
          ltime->tm_year + 1900, 
          ltime->tm_mon, 
          ltime->tm_mday);

  // Create filename: [serverroot]/[birthmonth]/[cpr]/[cpr]-[date]-[cnt].mpg
  sprintf(fname, "%s/%s/%s/%s-%s-%.3d.jpg", root->c_str(), birthmonth, cpr, cpr, date, cnt);

  // test filename-[cnt] for existamce cnt++ until not existing.
  fp = fopen(fname, "r");
  while(fp) {
    fclose(fp);
    cnt++;
    sprintf(fname, "%s/%s/%s/%s-%s-%.3d.jpg", root->c_str(), birthmonth, cpr, cpr, date, cnt);
    fp = fopen(fname, "r");
  }

  fprintf(stderr, "Success - using filename: [%s]\n", fname); fflush(stderr);
  imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best.
}
/*
struct tm
{
  int tm_sec;                   // Seconds.     [0-60] (1 leap second) 
  int tm_min;                   // Minutes.     [0-59] 
  int tm_hour;                  // Hours.       [0-23] 
  int tm_mday;                  // Day.         [1-31] 
  int tm_mon;                   // Month.       [0-11] 
  int tm_year;                  // Year - 1900.  
  int tm_wday;                  // Day of week. [0-6] 
  int tm_yday;                  // Days in year.[0-365] 
  int tm_isdst;                 // DST.         [-1/0/1]
};
*/

MovEncoder *newMovEncoder(char* cpr)
{
  MovEncoder *enc;
  struct tm *ltime;
  time_t t = time(NULL);
  FILE *fp;
  int cnt = 0;
  char fname[256];
  char birthmonth[3];
  char date[9];
  
  string *root = config->readString("server_root");

  // Test for server root writeability
  sprintf(fname, "%s/miavtemp.tmp%d", (char*)root->c_str(), rand());
  fp = fopen(fname, "w");
  if(!fp) {
    int r = rand();
    fprintf(stderr, "MIaV does not have write access to the server root [%s]\n", root->c_str());
    fprintf(stderr, "Redirecting output to [/tmp/miav-%d.mpg]\n", r);
    sprintf(fname, "/tmp/miav-%d.mpg", r);
    enc = new MovEncoder(fname);
    return enc;
  }
  fclose(fp);
  unlink(fname);

  // Check for cpr length correctness
  if(strlen(cpr) != 11) {
    int r = rand();
    fprintf(stderr, "Illigal CPR, it must have length 11, it had lentgh %d\n", strlen(cpr));
    fprintf(stderr, "Redirecting output to [/tmp/miav-%d.mpg]\n", r);
    sprintf(fname, "/tmp/miav-%d.mpg", r);
    enc = new MovEncoder(fname);
    return enc;
  }

  // Copy the bytes representing the birth month from the cpr
  // [dd][mm][yy]-[nn][nn]
  strncpy(birthmonth, &cpr[2], 2);
  birthmonth[2] = 0;

  // Create folder named birthmonth in server root
  sprintf(fname, "%s/%s", root->c_str(), birthmonth);
  if(!mkdir(fname, S_IRWXU) == -1 && errno != EEXIST) {
    int r = rand();
    fprintf(stderr, "Not possible to create subfolder %s\n", fname);
    fprintf(stderr, "Redirecting output to [/tmp/miav-%d.mpg]\n", r);
    sprintf(fname, "/tmp/miav-%d.mpg", r);
    enc = new MovEncoder(fname);
    return enc;
  }

  // Create folder named cpr in serverroot/birthmonth
  sprintf(fname, "%s/%s/%s", root->c_str(), birthmonth, cpr);
  if(!mkdir(fname, S_IRWXU) == -1 && errno != EEXIST) {
    int r = rand();
    fprintf(stderr, "Not possible to create subfolder %s\n", fname);
    fprintf(stderr, "Redirecting output to [/tmp/miav-%d.mpg]\n", r);
    sprintf(fname, "/tmp/miav-%d.mpg", r);
    enc = new MovEncoder(fname);
    return enc;
  }

  // Create date (today) in [yyyy][mm][dd]
  ltime = localtime(&t);
  sprintf(date, "%.4d%.2d%.2d", 
          ltime->tm_year + 1900, 
          ltime->tm_mon, 
          ltime->tm_mday);

  // Create filename: [serverroot]/[birthmonth]/[cpr]/[cpr]-[date]-[cnt].mpg
  sprintf(fname, "%s/%s/%s/%s-%s-%.3d.mpg", root->c_str(), birthmonth, cpr, cpr, date, cnt);

  // test filename-[cnt] for existamce cnt++ until not existing.
  fp = fopen(fname, "r");
  while(fp) {
    fclose(fp);
    cnt++;
    sprintf(fname, "%s/%s/%s/%s-%s-%.3d.mpg", root->c_str(), birthmonth, cpr, cpr, date, cnt);
    fp = fopen(fname, "r");
  }

  fprintf(stderr, "Success - using filename: [%s]\n", fname); fflush(stderr);
  enc = new MovEncoder(fname);
  return enc;
}

void newConnection(Socket *socket)
{
  char cpr[256];
  bool hasCpr = false;
  ServerStatus status;
  InfoConsole info;

  n_savestate savestate = LATER;
  n_header h;
  Frame *frame;
  Frame *freeze_frame = NULL;
  MovEncoder *enc = NULL;
  //  unsigned char dvbuf[DVPACKAGE_SIZE];

  frame = new Frame(NULL, DVPACKAGE_SIZE);

  printf("New connection[pid: %d]...\n", getpid());

  Network network = Network(socket, &info);
  while(int ret = network.recvPackage(&h, frame->data, frame->size)) {
    status.checkPoint();
    if(ret == -1) {
      fprintf(stderr, "An error occurred...!\n");
      break;
    }

    if(!hasCpr) {
      sprintf(cpr, h.header.h_data.cpr);
      hasCpr = true;
    }
    
    //    printf("Read: %d bytes ", ret);
    //    printf("typ: %d ", h.header_type);
    fprintf(stdout, "cpr: %s ", cpr);
    fprintf(stdout, "frz: %d ", h.header.h_data.freeze);
    fprintf(stdout, "sht: %d ", h.header.h_data.snapshot);
    fprintf(stdout, "save: %d ", h.header.h_data.savestate);
    fflush(stdout);
    
    if(h.header.h_data.snapshot) {
      if(freeze_frame) {
        saveFrameAsImage(cpr, freeze_frame);
        delete freeze_frame;
        freeze_frame = NULL;
      } else {
        saveFrameAsImage(cpr, frame);
      }
    }

    if(h.header.h_data.record) {
      if(!enc) enc = newMovEncoder(cpr);
      enc->encode(frame);
    }

    if(h.header.h_data.savestate) {
      savestate = h.header.h_data.savestate;
    }

    if(h.header.h_data.freeze) {
      if(freeze_frame) delete freeze_frame;
      freeze_frame = frame;
    } else {
      delete frame;
    }

    frame = new Frame(NULL, DVPACKAGE_SIZE);
  }

  // TODO: Use save state

  if(enc) delete enc;

  printf("Connection end[pid: %d]...\n", getpid());
  
}