/* -*- 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.22 2005/06/14 12:29:40 deva * Incorporated the use of the Info object everywhere... also using the log functionality. * * Revision 1.21 2005/05/26 21:32:39 deva * *** empty log message *** * * Revision 1.20 2005/05/26 12:48:36 deva * *** empty log message *** * * Revision 1.19 2005/05/22 16:34:44 deva * * Fix: Connection is now taken down when taking a sanpshot without recording. * * Revision 1.18 2005/05/22 15:49:22 deva * Added multithreaded encoding support. * * Revision 1.17 2005/05/17 19:16:26 deva * * Made new mpeg writer work, with proper file permissions. * * Revision 1.16 2005/05/17 15:12:51 deva * Fixed file rights (All read on files and directories, and all execute on directories). * * Revision 1.15 2005/05/17 14:30:56 deva * Added code, preparing threaded encoding. * * Revision 1.14 2005/05/16 16:00:57 deva * * Lots of stuff! * * Revision 1.13 2005/05/09 16:40:20 deva * * Added optimize yuv conversion code * * 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 #include // For mkdir #include #include // For unlink #include // For errno #include #include "miav_config.h" #include "mov_encoder_thread.h" #include "img_encoder.h" #include "server_status.h" #include "dv.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.jpg]\n", r); sprintf(fname, "/tmp/miav-%d", 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 length %d\n", strlen(cpr)); fprintf(stderr, "Redirecting output to [/tmp/miav-%d.jpg]\n", r); sprintf(fname, "/tmp/miav-%d", 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 | S_IRGRP | S_IXGRP | S_IXOTH | S_IROTH) == -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", 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 | S_IRGRP | S_IXGRP | S_IXOTH | S_IROTH) == -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", 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"); } sprintf(fname, "%s/%s/%s/%s-%s-%.3d", root->c_str(), birthmonth, cpr, cpr, date, cnt); fprintf(stderr, "Success - using filename: [%s.jpg]\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] }; */ MovEncoderThread *newMovEncoder(char* cpr) { /* MovEncoderThread *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", r); enc = new MovEncoderThread(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", r); enc = new MovEncoderThread(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 | S_IRGRP | S_IXGRP | S_IXOTH | S_IROTH) == -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", r); enc = new MovEncoderThread(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 | S_IRGRP | S_IXGRP | S_IXOTH | S_IROTH) == -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", r); enc = new MovEncoderThread(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"); } sprintf(fname, "%s/%s/%s/%s-%s-%.3d", root->c_str(), birthmonth, cpr, cpr, date, cnt); fprintf(stderr, "Success - using filename: [%s.mpg]\n", fname); fflush(stderr); enc = new MovEncoderThread(fname); return enc; */ return NULL; } void newConnection(Socket *socket, Info *info) { char cpr[256]; bool hasCpr = false; ServerStatus status; n_savestate savestate = LATER; n_header h; Frame *frame; Frame *freeze_frame = NULL; MovEncoderThread *enc = NULL; // unsigned char dvbuf[DVPACKAGE_SIZE]; frame = new Frame(NULL, DVPACKAGE_SIZE); info->log("New connection"); Network network = Network(socket, info); while(int ret = network.recvPackage(&h, frame->data, frame->size)) { status.checkPoint(); if(ret == -1) { info->error("A network error ocurred, terminating session"); 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.savestate) { savestate = h.header.h_data.savestate; } if(h.header.h_data.freeze) { if(freeze_frame) delete freeze_frame; // copy the frame into another temporary one. freeze_frame = new Frame(frame->data, frame->size); } // This one must be last! if(h.header.h_data.record) { // if(!enc) enc = newMovEncoder(cpr); if(!enc) enc = new MovEncoderThread(cpr, info); enc->encode(frame); } frame = new Frame(NULL, DVPACKAGE_SIZE); } // TODO: Use save state info->log("Closing connection..."); if(enc) delete enc; info->log("Connection closed"); }