Stable multi-user buffer and re-enabled push support. Renamed DDV_->Mist. Closes #25

This commit is contained in:
Thulinma 2012-04-27 21:07:26 +02:00
parent f615427c95
commit 2701a0be46
5 changed files with 63 additions and 77 deletions

View file

@ -26,12 +26,7 @@ Util::Config::Config(){
listen_port = 4242;
daemon_mode = true;
interface = "0.0.0.0";
configfile = "/etc/ddvtech.conf";
username = "root";
ignore_daemon = false;
ignore_interface = false;
ignore_port = false;
ignore_user = false;
}
/// Parses commandline arguments.
@ -48,17 +43,15 @@ void Util::Config::parseArgs(int argc, char ** argv){
{"username",1,0,'u'},
{"no-daemon",0,0,'n'},
{"daemon",0,0,'d'},
{"configfile",1,0,'c'},
{"version",0,0,'v'}
};
while ((opt = getopt_long(argc, argv, optString, longOpts, 0)) != -1){
switch (opt){
case 'p': listen_port = atoi(optarg); ignore_port = true; break;
case 'i': interface = optarg; ignore_interface = true; break;
case 'n': daemon_mode = false; ignore_daemon = true; break;
case 'd': daemon_mode = true; ignore_daemon = true; break;
case 'c': configfile = optarg; break;
case 'u': username = optarg; ignore_user = true; break;
case 'p': listen_port = atoi(optarg); break;
case 'i': interface = optarg; break;
case 'n': daemon_mode = false; break;
case 'd': daemon_mode = true; break;
case 'u': username = optarg; break;
case 'v':
printf("%s\n", TOSTRING(VERSION));
exit(1);
@ -67,16 +60,9 @@ void Util::Config::parseArgs(int argc, char ** argv){
case '?':
std::string doingdaemon = "true";
if (!daemon_mode){doingdaemon = "false";}
if (confsection == ""){
printf("Options: -h[elp], -?, -v[ersion], -n[odaemon], -d[aemon], -p[ort] VAL, -i[nterface] VAL, -u[sername] VAL\n");
printf("Defaults:\n interface: %s\n port: %i\n daemon mode: %s\n username: %s\n", interface.c_str(), listen_port, doingdaemon.c_str(), username.c_str());
}else{
printf("Options: -h[elp], -?, -v[ersion], -n[odaemon], -d[aemon], -p[ort] VAL, -i[nterface] VAL, -c[onfigfile] VAL, -u[sername] VAL\n");
printf("Defaults:\n interface: %s\n port: %i\n daemon mode: %s\n configfile: %s\n username: %s\n", interface.c_str(), listen_port, doingdaemon.c_str(), configfile.c_str(), username.c_str());
printf("Username root means no change to UID, no matter what the UID is.\n");
printf("If the configfile exists, it is always loaded first. Commandline settings then overwrite the config file.\n");
printf("\nThis process takes it directives from the %s section of the configfile.\n", confsection.c_str());
}
printf("Options: -h[elp], -?, -v[ersion], -n[odaemon], -d[aemon], -p[ort] VAL, -i[nterface] VAL, -u[sername] VAL\n");
printf("Defaults:\n interface: %s\n port: %i\n daemon mode: %s\n username: %s\n", interface.c_str(), listen_port, doingdaemon.c_str(), username.c_str());
printf("Username root means no change to UID, no matter what the UID is.\n");
printf("This is %s version %s\n", argv[0], TOSTRING(VERSION));
exit(1);
break;
@ -84,37 +70,6 @@ void Util::Config::parseArgs(int argc, char ** argv){
}//commandline options parser
}
/// Parses the configuration file at configfile, if it exists.
/// Assumes confsection is set.
void Util::Config::parseFile(){
std::ifstream conf(configfile.c_str(), std::ifstream::in);
std::string tmpstr;
bool acc_comm = false;
size_t foundeq;
if (conf.fail()){
#if DEBUG >= 3
fprintf(stderr, "Configuration file %s not found - using build-in defaults...\n", configfile.c_str());
#endif
}else{
while (conf.good()){
getline(conf, tmpstr);
if (tmpstr[0] == '['){//new section? check if we care.
if (tmpstr == confsection){acc_comm = true;}else{acc_comm = false;}
}else{
if (!acc_comm){break;}//skip all lines in this section if we do not care about it
foundeq = tmpstr.find('=');
if (foundeq != std::string::npos){
if ((tmpstr.substr(0, foundeq) == "port") && !ignore_port){listen_port = atoi(tmpstr.substr(foundeq+1).c_str());}
if ((tmpstr.substr(0, foundeq) == "interface") && !ignore_interface){interface = tmpstr.substr(foundeq+1);}
if ((tmpstr.substr(0, foundeq) == "username") && !ignore_user){username = tmpstr.substr(foundeq+1);}
if ((tmpstr.substr(0, foundeq) == "daemon") && !ignore_daemon){daemon_mode = true;}
if ((tmpstr.substr(0, foundeq) == "nodaemon") && !ignore_daemon){daemon_mode = false;}
}//found equals sign
}//section contents
}//configfile line loop
}//configuration
}
/// Sets the current process' running user
void Util::setUser(std::string username){
if (username != "root"){

View file

@ -9,23 +9,15 @@
/// Contains utility code, not directly related to streaming media
namespace Util{
/// Deals with parsing configuration from files or commandline options.
/// Deals with parsing configuration from commandline options.
class Config{
private:
bool ignore_daemon;
bool ignore_interface;
bool ignore_port;
bool ignore_user;
public:
std::string confsection;
std::string configfile;
bool daemon_mode;
std::string interface;
int listen_port;
std::string username;
Config();
void parseArgs(int argc, char ** argv);
void parseFile();
public:
bool daemon_mode;
std::string interface;
int listen_port;
std::string username;
Config();
void parseArgs(int argc, char ** argv);
};
/// Will set the active user to the named username.

View file

@ -15,12 +15,6 @@
#endif
#ifndef CONFIGSECT
/// Configuration file section for this server.
#define CONFIGSECT None
#error "No configuration file section was set!"
#endif
#include "socket.h" //Socket library
#include "config.h" //utilities for config management
#include <signal.h>
@ -83,10 +77,8 @@ int main(int argc, char ** argv){
//set and parse configuration
Util::Config C;
C.confsection = TOSTRING(CONFIGSECT);
C.listen_port = DEFAULT_PORT;
C.parseArgs(argc, argv);
C.parseFile();
//setup a new server socket, for the correct interface and port
server_socket = Socket::Server(C.listen_port, C.interface);

View file

@ -3,6 +3,7 @@
/// Written by Jaron Vietor in 2010 for DDVTech
#include "socket.h"
#include <sys/stat.h>
#include <poll.h>
#include <netdb.h>
#include <sstream>
@ -647,3 +648,43 @@ bool Socket::Server::connected(){
/// Returns internal socket number.
int Socket::Server::getSocket(){return sock;}
/// Connect to a stream on the system.
/// Filters the streamname, removing invalid characters and
/// converting all letters to lowercase.
/// If a '?' character is found, everything following that character is deleted.
Socket::Connection Socket::getStream(std::string streamname){
//strip anything that isn't numbers, digits or underscores
for (std::string::iterator i=streamname.end()-1; i>=streamname.begin(); --i){
if (*i == '?'){streamname.erase(i, streamname.end()); break;}
if (!isalpha(*i) && !isdigit(*i) && *i != '_'){
streamname.erase(i);
}else{
*i=tolower(*i);
}
}
return Socket::Connection("/tmp/mist/stream_"+streamname);
}
/// Create a stream on the system.
/// Filters the streamname, removing invalid characters and
/// converting all letters to lowercase.
/// If a '?' character is found, everything following that character is deleted.
/// If the /tmp/ddvtech directory doesn't exist yet, this will create it.
Socket::Server Socket::makeStream(std::string streamname){
//strip anything that isn't numbers, digits or underscores
for (std::string::iterator i=streamname.end()-1; i>=streamname.begin(); --i){
if (*i == '?'){streamname.erase(i, streamname.end()); break;}
if (!isalpha(*i) && !isdigit(*i) && *i != '_'){
streamname.erase(i);
}else{
*i=tolower(*i);
}
}
std::string loc = "/tmp/mist/stream_"+streamname;
//attempt to create the /tmp/mist directory if it doesn't exist already.
//ignore errors - we catch all problems in the Socket::Server creation already
mkdir("/tmp/mist", S_IRWXU | S_IRWXG | S_IRWXO);
//create and return the Socket::Server
return Socket::Server(loc);
}

View file

@ -77,4 +77,10 @@ namespace Socket{
int getSocket(); ///< Returns internal socket number.
};
/// Connect to a stream on the system.
Connection getStream(std::string streamname);
/// Create a stream on the system.
Server makeStream(std::string streamname);
};