summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client/decoder.cc30
-rw-r--r--client/decoder.h9
-rw-r--r--client/networksender.cc76
-rw-r--r--client/networksender.h25
-rw-r--r--client/status.h4
-rw-r--r--client/statusbar.cc50
-rw-r--r--client/statusbar.h7
-rw-r--r--lib/Makefile.am4
-rw-r--r--lib/liblua_wrapper.cc24
-rw-r--r--lib/liblua_wrapper.h4
-rw-r--r--lib/network.cc39
-rw-r--r--lib/network.h5
-rw-r--r--lib/socket.cc142
-rw-r--r--lib/socket.h50
-rw-r--r--lib/status.h17
-rw-r--r--lib/tcp_socket.cc108
-rw-r--r--lib/tcp_socket.h39
-rw-r--r--lib/udp_socket.cc108
-rw-r--r--lib/udp_socket.h48
-rw-r--r--server/miav_server.cc65
-rw-r--r--server/server.cc2
-rw-r--r--server/server_status.cc59
-rw-r--r--server/server_status.h25
23 files changed, 712 insertions, 228 deletions
diff --git a/client/decoder.cc b/client/decoder.cc
index afc09c9..a86dbd6 100644
--- a/client/decoder.cc
+++ b/client/decoder.cc
@@ -30,7 +30,7 @@
#include "transcoder.h"
-#define READ_DV_FROM_FILE
+//#define READ_DV_FROM_FILE
#include "dv.h"
#ifdef READ_DV_FROM_FILE
@@ -191,32 +191,24 @@ bool Decoder::eventFilter(QObject *o, QEvent *e)
return false;
}
-Status Decoder::status()
+std::vector<thread_status_t> Decoder::status()
{
- Status s;
-
- s.server_diskspace = 0xffffffff;
- s.server_diskspace_max = 0xffffffff;
- s.server_load = 0xffffffff;
- s.server_load_max = 0xffffffff;
- s.server_ping_ms = 0xffffffff;
- s.server_fps = -1.0;
+ std::vector<thread_status_t> v;
sendersmutex.lock();
- // Get the server disk status etc.
- if(senders.isEmpty() == false) senders.back()->getServerStatus(&s);
-
- // fprintf(stderr, "Load: %d of %d - ", s.server_load, s.server_load_max);
- // fprintf(stderr, "Space: %d of %d\n", s.server_diskspace, s.server_diskspace_max);
-
- // Read out the queue sizes
QLinkedList<NetworkSender*>::iterator i;
for(i = senders.begin(); i != senders.end(); i++) {
NetworkSender *ns = *i;
- s.queue_sizes.push_back(ns->queueSize());
+
+ // TODO: Cleanup (get both in one call)
+ thread_status_t s;
+ s.queuelen = ns->queueSize();
+ s.fps = ns->getfps();
+
+ v.push_back(s);
}
sendersmutex.unlock();
- return s;
+ return v;
}
diff --git a/client/decoder.h b/client/decoder.h
index 99cdd58..50c0307 100644
--- a/client/decoder.h
+++ b/client/decoder.h
@@ -36,7 +36,12 @@
#include "dv.h"
#include "networksender.h"
-#include "status.h"
+#include <vector>
+
+typedef struct {
+ double fps;
+ int queuelen;
+} thread_status_t;
class Decoder : public QThread
{
@@ -55,7 +60,7 @@ public:
void pframeAcquire();
void pframeRelease();
- Status status();
+ std::vector<thread_status_t> status();
protected:
bool eventFilter(QObject *o, QEvent *e);
diff --git a/client/networksender.cc b/client/networksender.cc
index e51fbe3..0aa1036 100644
--- a/client/networksender.cc
+++ b/client/networksender.cc
@@ -25,35 +25,41 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "networksender.h"
-#include "status.h"
#include "configuration.h"
#include "info.h"
+#include <sys/time.h>
+
#include <util.h>
NetworkSender::NetworkSender(QString cpr)
{
if(MIaV::config->get("server_addr", &ip))
- MIaV::info->error("Could not read the symbol [server_addr] from the donf file!");
+ MIaV::info->error("Could not read the symbol [server_addr] from the conf file!");
if(MIaV::config->get("server_port", &port))
- MIaV::info->error("Could not read the symbol [server_port] from the donf file!");
+ MIaV::info->error("Could not read the symbol [server_port] from the conf file!");
// Connect
- socket = new Socket(port);
- socket->sconnect((char*)ip.c_str());
+ socket = new TCPSocket(port, ip);
+ if(socket->connect()) {
+ MIaV::info->error("Could not connect to %s on port %d because: %s",
+ ip.c_str(), port, socket->error().c_str());
+ }
network = new Network(socket);
- server_diskspace = 0xffffffff;
- server_diskspace_max = 0xffffffff;
- server_load = 0xffffffff;
- server_load_max = 0xffffffff;
- server_ping_ms = 0xffffffff;
- server_fps = -1.0;
+ fps = 0.0;
running = true;
+ gettimeofday(&oldtime, NULL);
+ for(int cnt = 0; cnt < BUFFERSIZE; cnt++) {
+ frametime[cnt] = 41660;
+ }
+ gettimeofday(&time, NULL);
+ interval = 0;
+
fprintf(stderr, "Connect [%p]\n", this);
}
@@ -115,20 +121,9 @@ void NetworkSender::run()
if(frame->aframe) delete frame->aframe;
delete frame;
- // Status status;
- // network->recvStatus(&status);
-
// Set status
statusmutex.lock();
- server_diskspace_max = 0;//status.diskspace_max;
- server_diskspace = 0;//status.server_diskspace;
- server_load = 0;//status.load;
- server_load_max = 0;//status.load_max;
-
- // TODO: Calculate these values.
- server_ping_ms = 0;
- server_fps = 0;
-
+ checkPoint();
statusmutex.unlock();
}
}
@@ -144,14 +139,35 @@ void NetworkSender::stop()
terminatesemaphore.acquire(); // Wait for the thread to stop
}
-void NetworkSender::getServerStatus(Status *status)
+double NetworkSender::getfps()
{
+ double f;
+
statusmutex.lock();
- status->server_diskspace = server_diskspace;
- status->server_diskspace_max = server_diskspace_max;
- status->server_load = server_load;
- status->server_load_max = server_load_max;
- status->server_ping_ms = server_ping_ms;
- status->server_fps = server_fps;
+ f = fps;
statusmutex.unlock();
+
+ return f;
}
+
+void NetworkSender::checkPoint()
+{
+ for(int cnt = BUFFERSIZE - 1; cnt > 0; cnt--) {
+ frametime[cnt] = frametime[cnt-1];
+ }
+
+ frametime[0] = (1000000 * time.tv_sec + time.tv_usec) - (1000000 * oldtime.tv_sec + oldtime.tv_usec);
+
+ oldtime.tv_sec = time.tv_sec;
+ oldtime.tv_usec = time.tv_usec;
+
+ gettimeofday(&time, NULL);
+
+ double total = 0.0;
+ for(int cnt = 0; cnt < BUFFERSIZE; cnt++) {
+ total += (double)frametime[cnt];
+ }
+ fps = 1000000.0 / (total / (double)BUFFERSIZE);
+
+}
+
diff --git a/client/networksender.h b/client/networksender.h
index 589004b..e752a18 100644
--- a/client/networksender.h
+++ b/client/networksender.h
@@ -35,13 +35,14 @@
#include <frame.h>
-#include "status.h"
-
-#include "socket.h"
+#include "tcp_socket.h"
#include "network.h"
#include <string>
+// How many steps to do avarage calculation over.
+#define BUFFERSIZE 100
+
class NetworkSender : public QThread
{
public:
@@ -54,10 +55,10 @@ public:
void run();
void stop();
- void getServerStatus(Status *status);
+ double getfps();
private:
- Socket *socket;
+ TCPSocket *socket;
Network *network;
std::string ip;
@@ -72,12 +73,14 @@ private:
// Server stats
QMutex statusmutex;
- unsigned int server_diskspace;
- unsigned int server_diskspace_max;
- unsigned int server_load;
- unsigned int server_load_max;
- unsigned int server_ping_ms;
- double server_fps;
+ double fps;
+
+ // Timer stuff (to calc the fps)
+ long long interval;
+ unsigned int frametime[BUFFERSIZE];
+ struct timeval time;
+ struct timeval oldtime;
+ void checkPoint();
};
#endif/*__MIAV_NETWORKSENDER_H__*/
diff --git a/client/status.h b/client/status.h
index a3ccad7..b91d673 100644
--- a/client/status.h
+++ b/client/status.h
@@ -26,7 +26,7 @@
*/
#ifndef __MIAV_STATUS_H__
#define __MIAV_STATUS_H__
-
+/*
#define UNKNOWN 0xffffffff
class Status {
@@ -40,5 +40,5 @@ public:
unsigned int server_ping_ms;
double server_fps;
};
-
+*/
#endif/*__MIAV_STATUS_H__*/
diff --git a/client/statusbar.cc b/client/statusbar.cc
index 1e25ebe..e49d45a 100644
--- a/client/statusbar.cc
+++ b/client/statusbar.cc
@@ -26,8 +26,21 @@
*/
#include "statusbar.h"
+#include "configuration.h"
+
+#include "info.h"
+
StatusBar::StatusBar(QWidget *parent, Decoder *decoder): QStatusBar(parent)
{
+ if(MIaV::config->get("server_addr", &ip))
+ MIaV::info->error("Could not read the symbol [server_addr] from the conf file!");
+
+ if(MIaV::config->get("server_port", &port))
+ MIaV::info->error("Could not read the symbol [server_port] from the conf file!");
+
+ // socket.setPort(port);
+ // socket.setAddress(ip);
+
this->decoder = decoder;
diskspace = new QProgressBar(this);
@@ -59,15 +72,23 @@ StatusBar::StatusBar(QWidget *parent, Decoder *decoder): QStatusBar(parent)
startTimer(100);
}
+#define UNKNOWN 0
void StatusBar::timerEvent(QTimerEvent *event)
{
- Status s = decoder->status();
+ UDPSocket socket(port, ip);
+
+ status_request_t request;
+ status_t status;
+ if(socket.write(&request, sizeof(request))==-1) fprintf(stderr, "ERROR! WRITE %s\n", socket.error().c_str());
+ if(socket.read(&status, sizeof(status))==-1) fprintf(stderr, "ERROR! READ %s\n", socket.error().c_str());
+
+ std::vector<thread_status_t> s = decoder->status();
QString statusmsg;
- if(s.queue_sizes.size() > 0) {
- for(int cnt = 0; cnt < s.queue_sizes.size(); cnt++) {
+ if(s.size() > 0) {
+ for(int cnt = 0; cnt < s.size(); cnt++) {
QString next;
- next.sprintf("[%d]", s.queue_sizes[cnt]);
+ next.sprintf("[%d]", s[cnt].queuelen);
statusmsg.prepend(next);
}
statusmsg.prepend("Queue: ");
@@ -77,24 +98,25 @@ void StatusBar::timerEvent(QTimerEvent *event)
}
QString dummy;
- if(s.server_ping_ms != UNKNOWN) ping->setText(dummy.sprintf("ping %d ms", s.server_ping_ms));
- else ping->setText("ping N/A");
-
- if(s.server_fps != -1) fps->setText(dummy.sprintf("fps %.2f", s.server_fps));
+ if(s.size() && s[0].fps != -1) fps->setText(dummy.sprintf("fps %.2f", s[0].fps));
else fps->setText("fps N/A");
- if(s.server_diskspace_max != UNKNOWN) {
- diskspace->setRange(0, s.server_diskspace_max);
- diskspace->setValue(s.server_diskspace);
+ /*
+ if(s.server_ping_ms != UNKNOWN) ping->setText(dummy.sprintf("ping %d ms", s.server_ping_ms));
+ else ping->setText("ping N/A");
+ */
+ if(status.diskspace_max != UNKNOWN) {
+ diskspace->setRange(0, status.diskspace_max);
+ diskspace->setValue(status.diskspace);
diskspace->setEnabled(true);
} else {
diskspace->setRange(0, 0);
diskspace->setEnabled(false);
}
- if(s.server_load_max != UNKNOWN) {
- load->setRange(0, s.server_load_max);
- load->setValue(s.server_load);
+ if(status.load_max != UNKNOWN) {
+ load->setRange(0, status.load_max);
+ load->setValue(status.load);
load->setEnabled(true);
} else {
load->setRange(0, 0);
diff --git a/client/statusbar.h b/client/statusbar.h
index 1b0a2c5..991483c 100644
--- a/client/statusbar.h
+++ b/client/statusbar.h
@@ -34,6 +34,9 @@
#include <QLabel>
#include <QProgressBar>
+#include <string>
+#include "udp_socket.h"
+
class StatusBar : public QStatusBar
{
public:
@@ -47,6 +50,10 @@ protected:
private:
Decoder *decoder;
+ std::string ip;
+ int port;
+ // UDPSocket socket;
+
// Content
QLabel *messagefield;
QProgressBar *diskspace;
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 6ee8bf4..4e2b923 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -16,6 +16,8 @@ libmiav_la_SOURCES = \
network.cc \
semaphore.cc \
socket.cc \
+ udp_socket.cc \
+ tcp_socket.cc \
status.cc \
thread.cc \
threadsafe_queue.cc \
@@ -42,6 +44,8 @@ EXTRA_DIST = \
queue.h \
semaphore.h \
socket.h \
+ udp_socket.h \
+ tcp_socket.h \
status.h \
thread.h \
threadsafe_queue.h \
diff --git a/lib/liblua_wrapper.cc b/lib/liblua_wrapper.cc
index 6535f4e..9e06be9 100644
--- a/lib/liblua_wrapper.cc
+++ b/lib/liblua_wrapper.cc
@@ -51,10 +51,10 @@ int LibLUAWrapper::loadFile(char *fname)
case LUA_ERRSYNTAX: //syntax error during pre-compilation;
case LUA_ERRMEM: //memory allocation error.
case LUA_ERRFILE: //cannot open/read the file.
- error = std::string(lua_tostring(L, lua_gettop(L)));
+ strerr = std::string(lua_tostring(L, lua_gettop(L)));
return 1;
default:
- error = std::string("Unknown return value of luaL_loadfile.");
+ strerr = std::string("Unknown return value of luaL_loadfile.");
return 1;
}
@@ -66,10 +66,10 @@ int LibLUAWrapper::loadFile(char *fname)
case LUA_ERRRUN:// a runtime error.
case LUA_ERRMEM:// memory allocation error. For such errors, Lua does not call the error handler function.
case LUA_ERRERR:// error while running the error handler function.
- error = std::string(lua_tostring(L, lua_gettop(L)));
+ strerr = std::string(lua_tostring(L, lua_gettop(L)));
return 1;
default:
- error = std::string("Unknown return value of lua_pcall.");
+ strerr = std::string("Unknown return value of lua_pcall.");
return 1;
}
@@ -87,10 +87,10 @@ int LibLUAWrapper::loadBuffer(char *buffer)
break;
case LUA_ERRSYNTAX: //syntax error during pre-compilation;
case LUA_ERRMEM: //memory allocation error.
- error = std::string(lua_tostring(L, lua_gettop(L)));
+ strerr = std::string(lua_tostring(L, lua_gettop(L)));
return 1;
default:
- error = std::string("Unknown return value of luaL_loadstring.");
+ strerr = std::string("Unknown return value of luaL_loadstring.");
return 1;
}
@@ -102,10 +102,10 @@ int LibLUAWrapper::loadBuffer(char *buffer)
case LUA_ERRRUN:// a runtime error.
case LUA_ERRMEM:// memory allocation error. For such errors, Lua does not call the error handler function.
case LUA_ERRERR:// error while running the error handler function.
- error = std::string(lua_tostring(L, lua_gettop(L)));
+ strerr = std::string(lua_tostring(L, lua_gettop(L)));
return 1;
default:
- error = std::string("Unknown return value of lua_pcall.");
+ strerr = std::string("Unknown return value of lua_pcall.");
return 1;
}
@@ -164,9 +164,9 @@ std::string LibLUAWrapper::getString(char *name)
return val;
}
-std::string LibLUAWrapper::getError()
+std::string LibLUAWrapper::error()
{
- return error;
+ return strerr;
}
#ifdef LUA_TEST
@@ -184,12 +184,12 @@ int main()
LibLUAWrapper lua;
if(lua.loadBuffer((char*)preload)) {
- fprintf(stderr, "LUA buffer error: %s\n", lua.getError().c_str());
+ fprintf(stderr, "LUA buffer error: %s\n", lua.error().c_str());
return 1;
}
if(lua.loadFile("test.lua")) {
- fprintf(stderr, "LUA load error: %s\n", lua.getError().c_str());
+ fprintf(stderr, "LUA load error: %s\n", lua.error().c_str());
return 1;
}
diff --git a/lib/liblua_wrapper.h b/lib/liblua_wrapper.h
index 70eb8a8..30371fe 100644
--- a/lib/liblua_wrapper.h
+++ b/lib/liblua_wrapper.h
@@ -54,11 +54,11 @@ public:
bool getBoolean(char *name);
std::string getString(char *name);
- std::string getError();
+ std::string error();
private:
lua_State *L;
- std::string error;
+ std::string strerr;
};
#endif/*__MIAV_LIBLUA_WRAPPER_H__*/
diff --git a/lib/network.cc b/lib/network.cc
index cb3f94e..4957bb6 100644
--- a/lib/network.cc
+++ b/lib/network.cc
@@ -50,11 +50,11 @@ Network::~Network()
int Network::write(void *buf, int size)
{
- if(!s->isConnected()) {
+ if(!s->connected) {
// MIaV::info->error("Write attempted to a socket not connected!");
return -1;
}
- int n = send(s->ssocket, buf, size, MSG_WAITALL);
+ int n = send(s->sock, buf, size, MSG_WAITALL);
if(n == -1) {
MIaV::info->error("An error occurred!");
@@ -65,11 +65,11 @@ int Network::write(void *buf, int size)
int Network::read(void *buf, int size)
{
- if(!s->isConnected()) {
+ if(!s->connected) {
// MIaV::info->error("Read attempted from a socket not connected!");
return -1;
}
- int n = recv(s->ssocket, buf, size, MSG_WAITALL);
+ int n = recv(s->sock, buf, size, MSG_WAITALL);
if(n == -1) {
MIaV::info->error("An error occurred!");
@@ -95,7 +95,7 @@ int Network::sendPackage(n_header *h, void* buf, int bufsz)
struct msghdr msg;
struct iovec iovecs[2];
- if(!s->isConnected()) {
+ if(!s->connected) {
// MIaV::info->error("Write attempted to a socket not connected!");
return -1;
}
@@ -111,7 +111,7 @@ int Network::sendPackage(n_header *h, void* buf, int bufsz)
msg.msg_iov[1].iov_base = buf;
msg.msg_iov[1].iov_len = bufsz;
- int n = sendmsg(s->ssocket, &msg, 0);
+ int n = sendmsg(s->sock, &msg, 0);
if(n < 0) {
MIaV::info->error("A network error ocurred during sendPackage!");
return -1;
@@ -125,7 +125,7 @@ int Network::recvPackage(n_header *h, void* buf, int bufsz)
struct msghdr msg;
struct iovec iovecs[2];
- if(!s->isConnected()) {
+ if(!s->connected) {
// MIaV::info->error("Read attempted to a socket not connected!");
return -1;
}
@@ -141,7 +141,7 @@ int Network::recvPackage(n_header *h, void* buf, int bufsz)
msg.msg_iov = iovecs;
msg.msg_iovlen = 2;
- int n = recvmsg(s->ssocket, &msg, MSG_WAITALL);
+ int n = recvmsg(s->sock, &msg, MSG_WAITALL);
if(n < 0) {
MIaV::info->error("A network error ocurred during recvPackage!");
@@ -161,7 +161,7 @@ int Network::sendFrame(Frame *frame)
struct msghdr msg;
struct iovec iovecs[2];
- if(!s->isConnected()) {
+ if(!s->connected) {
// MIaV::info->error("Write attempted to a socket not connected!");
return -1;
}
@@ -196,7 +196,7 @@ int Network::sendFrame(Frame *frame)
msg.msg_iov[1].iov_base = aframe;
msg.msg_iov[1].iov_len = aframesize;
- int n = sendmsg(s->ssocket, &msg, 0);
+ int n = sendmsg(s->sock, &msg, 0);
if(n < 0) {
MIaV::info->error("A network error ocurred during sendPackage!");
@@ -215,7 +215,7 @@ Frame *Network::recvFrame()
struct msghdr msg;
struct iovec iovecs[2];
- if(!s->isConnected()) {
+ if(!s->connected) {
// MIaV::info->error("Read attempted to a socket not connected!");
return NULL;
}
@@ -239,7 +239,7 @@ Frame *Network::recvFrame()
msg.msg_iov = iovecs;
msg.msg_iovlen = 2;
- int n = recvmsg(s->ssocket, &msg, MSG_WAITALL);
+ int n = recvmsg(s->sock, &msg, MSG_WAITALL);
if(n < 0) {
MIaV::info->error("A network error ocurred during recvPackage!");
@@ -269,18 +269,3 @@ Frame *Network::recvFrame()
return frame;
}
-int Network::sendStatus(Status *status)
-{
- return 0;
-}
-
-int Network::recvStatus(Status *status)
-{
- /*
- status.diskspace = 10;
- status.diskspace_max = 100;
- status.load = 80;
- status.load_max = 100;
- */
- return 0;
-}
diff --git a/lib/network.h b/lib/network.h
index dc7d614..eb545d4 100644
--- a/lib/network.h
+++ b/lib/network.h
@@ -29,6 +29,7 @@
#define __MIAVLIB_NETWORK_H__
#include "socket.h"
+
#include "package.h"
#include "frame.h"
#include "status.h"
@@ -55,10 +56,6 @@ public:
int sendFrame(Frame *frame);
Frame *recvFrame();
- // Status message communication
- int sendStatus(Status *status);
- int recvStatus(Status *status);
-
private:
Socket *s;
diff --git a/lib/socket.cc b/lib/socket.cc
index 6189d23..37e985a 100644
--- a/lib/socket.cc
+++ b/lib/socket.cc
@@ -24,13 +24,147 @@
* along with MIaV; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
-#include <config.h>
-
#include "socket.h"
+
+#include <errno.h>
+
+// for gethostbyname
+#include <netdb.h>
+
+// for inet_addr
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+Socket::Socket(int port, std::string addr)
+{
+ this->prt = port;
+ this->addr = addr;
+ connected = false;
+}
+
+Socket::~Socket()
+{
+ disconnect();
+}
+
+int Socket::setPort(int port)
+{
+ if(connected) {
+ strerr = "Cannot change port, while socket is connected.";
+ return 1;
+ }
+
+ this->prt = port;
+ return 0;
+}
+
+int Socket::port()
+{
+ return prt;
+}
+
+int Socket::setAddress(std::string addr)
+{
+ if(connected) {
+ strerr = "Cannot change address, while socket is connected.";
+ return 1;
+ }
+
+ this->addr = addr;
+ return 0;
+}
+
+std::string Socket::address()
+{
+ return addr;
+}
+
+static int _connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen)
+{return connect(sockfd, serv_addr, addrlen);}
+int Socket::connect()
+{
+ if(connected) {
+ strerr = "Could not connect, socket is already connected.";
+ return 1;
+ }
+
+ // Do DNS lookup
+ char *ip;
+ struct in_addr **addr_list;
+ struct hostent *he;
+ he = gethostbyname(addr.c_str());
+ if(!he || !he->h_length) {
+ strerr = hstrerror(h_errno);
+ return 1;
+ }
+
+ addr_list = (struct in_addr **)he->h_addr_list;
+ // for(int i = 0; addr_list[i] != NULL; i++) {
+ ip = inet_ntoa(*addr_list[0]);
+ // }
+
+ struct sockaddr_in socketaddr;
+ memset((char *) &socketaddr, sizeof(socketaddr), 0);
+ socketaddr.sin_family = AF_INET;
+ socketaddr.sin_port = htons(prt);
+ socketaddr.sin_addr.s_addr = inet_addr(ip);
+
+ if(_connect(sock, (struct sockaddr*)&socketaddr, sizeof(socketaddr))) {
+ strerr = strerror(errno);
+ return 1;
+ }
+
+ connected = true;
+
+ return 0;
+}
+
+int Socket::disconnect()
+{
+ if(!connected) {
+ strerr = "Could not disconnect, socket is already disconnected.";
+ return 1;
+ }
+
+ close(sock);
+ connected = false;
+
+ return 0;
+}
+
+std::string Socket::error()
+{
+ return strerr;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+/*
+
#include "info.h"
#include <errno.h>
+
Socket::Socket()
{
connected = false;
@@ -73,7 +207,7 @@ Socket Socket::slisten()
Socket s;
if(err) {
- //MIaV::info->error("Socket: No socket present!");
+ // MIaV::info->error("Socket: No socket present!");
return s;
}
if(!connected) {
@@ -147,3 +281,5 @@ bool Socket::hasError()
{
return err != 0;
}
+
+*/
diff --git a/lib/socket.h b/lib/socket.h
index dde4729..f82cd82 100644
--- a/lib/socket.h
+++ b/lib/socket.h
@@ -3,7 +3,7 @@
* socket.h
*
* Mon Nov 8 10:49:33 CET 2004
- * Copyright 2004 Bent Bisballe
+ * Copyright 2004 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
@@ -24,10 +24,49 @@
* along with MIaV; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
-#include "config.h"
-#ifndef __MIAVLIB_SOCKET_H__
-#define __MIAVLIB_SOCKET_H__
+#ifndef __MIAV_SOCKET_H__
+#define __MIAV_SOCKET_H__
+#include <string>
+
+#define PORT_UNDEFINED -1
+
+class Network;
+
+class Socket {
+ friend class Network;
+ // friend class Socket;
+public:
+ Socket(int port, std::string addr);
+ ~Socket();
+
+ int setPort(int port);
+ int port();
+
+ int setAddress(std::string addr);
+ std::string address();
+
+ int connect();
+ int disconnect();
+
+ std::string error();
+
+protected:
+ bool connected;
+
+ // Error string
+ std::string strerr;
+
+ // c socket
+ int sock;
+
+ // host info
+ int prt;
+ std::string addr;
+};
+
+
+/* // Old interface
#include <stdio.h>
#include <string.h>
@@ -42,6 +81,7 @@ public:
Socket();
Socket(u_short port);
~Socket();
+
Socket slisten();
int sconnect(char *ip);
bool isConnected();
@@ -54,5 +94,7 @@ public:
private:
int err;
};
+*/
+
#endif/*__SOCKET_H__*/
diff --git a/lib/status.h b/lib/status.h
index d947123..3988aad 100644
--- a/lib/status.h
+++ b/lib/status.h
@@ -30,14 +30,15 @@
/**
* Internal data class for the server status info.
*/
-class Status {
-public:
- unsigned long long int diskspace;
- unsigned long long int diskspace_max;
- unsigned short load;
- unsigned short load_max;
-};
-
+typedef struct {
+ unsigned char diskspace;
+ unsigned char diskspace_max;
+ unsigned char load;
+ unsigned char load_max;
+} status_t;
+typedef struct {
+ char magic[4];
+} status_request_t;
#endif/*__MIAV_STATUS_H__*/
diff --git a/lib/tcp_socket.cc b/lib/tcp_socket.cc
new file mode 100644
index 0000000..8468df9
--- /dev/null
+++ b/lib/tcp_socket.cc
@@ -0,0 +1,108 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * tcp_socket.cc
+ *
+ * Fri Aug 18 00:12:34 CEST 2006
+ * Copyright 2006 Bent Bisballe Nyeng
+ * 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.
+ */
+#include "tcp_socket.h"
+
+#include <errno.h>
+
+// for connect, listen, bind and accept
+#include <sys/types.h>
+#include <sys/socket.h>
+
+// For socket
+#include <sys/types.h>
+#include <sys/socket.h>
+
+// For TCP
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+
+// For inet_ntoa
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+TCPSocket::TCPSocket(int port, std::string addr) : Socket(port, addr)
+{
+ sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+}
+
+static int _listen(int sockfd, int backlog){return listen(sockfd, backlog);}
+int TCPSocket::listen(TCPSocket *s)
+{
+ if(!s) {
+ strerr = "TCPSocket is a NULL pointer.";
+ return 1;
+ }
+
+ if(s->connected) {
+ strerr = "TCPSocket already connected.";
+ return 1;
+ }
+
+ if(!connected) {
+ struct sockaddr_in socketaddr;
+ memset((char *) &socketaddr, sizeof(socketaddr), 0);
+ socketaddr.sin_family = AF_INET;
+ socketaddr.sin_port = htons(prt);
+ socketaddr.sin_addr.s_addr = htonl(INADDR_ANY);
+
+ if(bind(sock, (struct sockaddr*)&socketaddr, sizeof(socketaddr)) == -1) {
+ strerr = "Socket: bind() failed! ";
+ strerr.append(strerror(errno));
+ return 1;
+ }
+
+ if(_listen(sock, 5) == -1) {
+ strerr = "Socket: listen() failed! ";
+ strerr.append(strerror(errno));
+ return 1;
+ }
+
+ connected = true;
+ }
+
+ // accept new connection and get its connection descriptor
+ struct sockaddr_in ssocketaddr;
+ int csalen = sizeof(ssocketaddr);
+
+ s->sock = accept(sock, (struct sockaddr*)&ssocketaddr, (socklen_t*)&csalen);
+
+ if (s->sock < 0) {
+ s->connected = false;
+ strerr = "Socket: accept() failed! ";
+ strerr.append(strerror(errno));
+ return 1;
+ }
+
+ // Fill in the host data.
+ s->setAddress(inet_ntoa(ssocketaddr.sin_addr));
+ s->setPort(ssocketaddr.sin_port);
+
+ s->connected = true;
+ return 0;
+}
diff --git a/lib/tcp_socket.h b/lib/tcp_socket.h
new file mode 100644
index 0000000..5815de3
--- /dev/null
+++ b/lib/tcp_socket.h
@@ -0,0 +1,39 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * tcp_socket.h
+ *
+ * Fri Aug 18 00:12:33 CEST 2006
+ * Copyright 2006 Bent Bisballe Nyeng
+ * 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.
+ */
+#ifndef __MIAV_TCP_SOCKET_H__
+#define __MIAV_TCP_SOCKET_H__
+
+#include "socket.h"
+
+class TCPSocket : public Socket {
+public:
+ TCPSocket(int port = PORT_UNDEFINED, std::string addr = "");
+
+ int listen(TCPSocket *socket);
+};
+
+#endif/*__MIAV_TCP_SOCKET_H__*/
diff --git a/lib/udp_socket.cc b/lib/udp_socket.cc
new file mode 100644
index 0000000..9668cc6
--- /dev/null
+++ b/lib/udp_socket.cc
@@ -0,0 +1,108 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * udp_socket.cc
+ *
+ * Fri Aug 18 00:12:25 CEST 2006
+ * Copyright 2006 Bent Bisballe Nyeng
+ * 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.
+ */
+#include "udp_socket.h"
+
+#include <errno.h>
+
+// For socket
+#include <sys/types.h>
+#include <sys/socket.h>
+
+// For UDP
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+// For inet_ntoa
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "info.h"
+
+UDPSocket::UDPSocket(int port, std::string addr) : Socket(port, addr)
+{
+ sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+ if(sock == -1) MIaV::info->error("Could not create UDP socket: %s", strerror(errno));
+}
+
+int UDPSocket::write(void *buf, int size)
+{
+ int n;
+
+ if(!connected) {
+ memset((char *) &sa, sizeof(sa), 0);
+ sa.sin_family = AF_INET;
+ sa.sin_port = htons(prt);
+ if (inet_aton(addr.c_str(), &sa.sin_addr)==0) {
+ MIaV::info->error("An error occurred in inet_aton! %s", strerror(errno));
+ }
+ connected = true;
+ }
+
+ n = sendto(sock, buf, size, 0, (const sockaddr*)&sa, (socklen_t)sizeof(sa));
+
+ if(n==-1) {
+ MIaV::info->error("An error occurred! %s", strerror(errno));
+ }
+
+ return n;
+}
+
+int UDPSocket::read(void *buf, int size)
+{
+ int n;
+
+ if(!connected) {
+ struct sockaddr_in si_me;
+ si_me.sin_family = AF_INET;
+ si_me.sin_port = htons(prt);
+ si_me.sin_addr.s_addr = htonl(INADDR_ANY);
+
+ if (bind(sock, (const sockaddr*)&si_me, (socklen_t)sizeof(si_me))==-1) {
+ strerr = "UDPSocet failed on bind. ";
+ strerr.append(strerror(errno));
+ return -1;
+ }
+
+ connected = true;
+ }
+
+ int slen = sizeof(sa);
+ n = recvfrom(sock, buf, size, 0, (sockaddr*)&sa, (socklen_t*)&slen);
+
+ if(n == -1) {
+ strerr = "UDPSocet failed on read. ";
+ strerr.append(strerror(errno));
+ return -1;
+ }
+
+ // Fill in the host data.
+ setAddress(inet_ntoa(sa.sin_addr));
+ setPort(sa.sin_port);
+
+ return n;
+}
diff --git a/lib/udp_socket.h b/lib/udp_socket.h
new file mode 100644
index 0000000..d89fcb1
--- /dev/null
+++ b/lib/udp_socket.h
@@ -0,0 +1,48 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * udp_socket.h
+ *
+ * Fri Aug 18 00:12:24 CEST 2006
+ * Copyright 2006 Bent Bisballe Nyeng
+ * 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.
+ */
+#ifndef __MIAV_UDP_SOCKET_H__
+#define __MIAV_UDP_SOCKET_H__
+
+#include "socket.h"
+
+// for sockaddr_in
+#include <netinet/in.h>
+
+class UDPSocket : public Socket {
+public:
+ UDPSocket(int port = PORT_UNDEFINED, std::string addr = "");
+
+ // Raw communication
+ int write(void *buf, int size);
+ int read(void *buf, int size);
+
+private:
+ struct sockaddr_in sa;
+
+};
+
+#endif/*__MIAV_UDP_SOCKET_H__*/
diff --git a/server/miav_server.cc b/server/miav_server.cc
index 8cd7a42..1682dad 100644
--- a/server/miav_server.cc
+++ b/server/miav_server.cc
@@ -31,14 +31,15 @@
#include "info_console.h"
+#include "server_status.h"
+
#include <stdio.h>
#include <string.h>
#include <string>
#include "server.h"
-#include "socket.h"
-#include "network.h"
+#include "tcp_socket.h"
#include <signal.h>
#include <errno.h>
@@ -177,59 +178,71 @@ int main(int argc, char *argv[])
if(config.get("server_port", &port))
info.error("Could not read symbol [server_port] from the conf file!");
- pid_t childpid; // variable to store the child's pid
-
signal(SIGCLD, SIG_IGN); // Ved SIGCHILD til IGNORE maa wait/waitpid ikke kaldes
// (ellers kommer der kernel-brok)
+ pid_t childpid;
+ childpid = fork();
+ switch(childpid) {
+ case -1:
+ MIaV::info->log("Fork error: %s", strerror(errno));
+ return 1;
+
+ case 0: // fork() returns 0 to the child process
+ if(server_status(port)) exit(1);
+ return 0;
+
+ default: // fork() returns new pid to the parent process
+ break;
+ }
+
MIaV::info->info("Starting MIaV server v. %s", VERSION);
MIaV::info->info("Listening on port %d", port);
- Socket *socket = new Socket(port);
-
- if(socket->hasError()) {
+ TCPSocket socket(port);
+ /*
+ if(socket.hasError()) {
MIaV::info->error("Listening socket has errors, quitting.");
- delete socket;
return 1;
}
-
+ */
while(1) {
- Socket *csocket = new Socket(socket->slisten());
+ TCPSocket csocket;
+ if(socket.listen(&csocket)) {
+ MIaV::info->error("Server socket has errors, quitting. %s", socket.error().c_str());
+ }
- if(socket->hasError()) {
+ /*
+ if(socket.hasError()) {
MIaV::info->error("Server socket has errors, quitting.");
- delete csocket;
break;
}
-
- if(csocket->hasError()) {
+ */
+ /*
+ if(csocket.hasError()) {
MIaV::info->error("Child socket has errors, quitting.");
- delete csocket;
break;
}
-
- if(!csocket->isConnected()) {
+ */
+ /*
+ if(!csocket.isConnected()) {
MIaV::info->error("Child socket is not connected, quitting.");
- delete csocket;
break;
}
-
+ */
childpid = fork();
-
switch(childpid) {
case -1: // fork() returns -1 on failure
MIaV::info->log("Fork error: %s", strerror(errno));
- exit(1);
+ return 1;
+
case 0: // fork() returns 0 to the child process
- delete socket; // Close listen socket.
- newConnection(csocket);
- delete csocket; // Close communication socket.
- exit(0);
+ newConnection(&csocket);
+ return 0;
default: // fork() returns new pid to the parent process
break;
}
}
- delete socket;
return 0;
}
diff --git a/server/server.cc b/server/server.cc
index 5088fa5..dfe024a 100644
--- a/server/server.cc
+++ b/server/server.cc
@@ -65,7 +65,7 @@ void newConnection(Socket *socket)
Network network(socket);
MIaV::info->info("CONNECTION OPENED");
- MIaV::info->info("New connection (%s)", inet_ntoa(socket->socketaddr.sin_addr));
+ MIaV::info->info("New connection (%s)", socket->address().c_str());
Frame *frame;
while((frame = network.recvFrame())) {
diff --git a/server/server_status.cc b/server/server_status.cc
index 88bd35c..b1837b8 100644
--- a/server/server_status.cc
+++ b/server/server_status.cc
@@ -3,7 +3,7 @@
* server_status.cc
*
* Fri Apr 29 13:58:26 CEST 2005
- * Copyright 2005 Bent Bisballe
+ * Copyright 2005 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
@@ -24,52 +24,31 @@
* along with MIaV; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
-#include <config.h>
#include "server_status.h"
-#include "info.h"
-#include <stdio.h>
-ServerStatus::ServerStatus()
-{
- gettimeofday(&oldtime, NULL);
-
- for(int cnt = 0; cnt < BUFFERSIZE; cnt++) {
- frametime[cnt] = 41660;
- }
-
- gettimeofday(&time, NULL);
+#include "udp_socket.h"
+#include "status.h"
- interval = 0;
-}
-
-ServerStatus::~ServerStatus()
+int server_status(int port)
{
-}
+ status_t s;
+ status_request_t r;
-void ServerStatus::checkPoint()
-{
- for(int cnt = BUFFERSIZE - 1; cnt > 0; cnt--) {
- frametime[cnt] = frametime[cnt-1];
- }
- frametime[0] = (1000000 * time.tv_sec + time.tv_usec) - (1000000 * oldtime.tv_sec + oldtime.tv_usec);
+ s.diskspace_max = 0xff;
+ s.diskspace = 0;
+
+ s.load_max = 0xff;
+ s.load = 0;
- oldtime.tv_sec = time.tv_sec;
- oldtime.tv_usec = time.tv_usec;
+ while(1) {
+ UDPSocket socket(port);
- gettimeofday(&time, NULL);
+ if(socket.read(&r, sizeof(r)) == -1) fprintf(stderr, "ERROR! READ %s\n", socket.error().c_str());
+ if(socket.write(&s, sizeof(s)) == -1) fprintf(stderr, "ERROR! WRITE %s\n", socket.error().c_str());
- interval += frametime[0];
- if(interval > UPD) {
- interval = 0;
- double total = 0.0;
- for(int cnt = 0; cnt < BUFFERSIZE; cnt++) {
- total += (double)frametime[cnt];
- }
- MIaV::info->info("Status - fps: %f", 1000000.0 / (total / (double)BUFFERSIZE));
+ s.diskspace++;
+ s.load++;
}
-
-}
-/*
-date(1), gettimeofday(2), ctime(3), ftime(3)
-*/
+ return 0;
+}
diff --git a/server/server_status.h b/server/server_status.h
index 82ec8ec..25346ec 100644
--- a/server/server_status.h
+++ b/server/server_status.h
@@ -3,7 +3,7 @@
* server_status.h
*
* Fri Apr 29 13:58:26 CEST 2005
- * Copyright 2005 Bent Bisballe
+ * Copyright 2005 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
@@ -24,30 +24,9 @@
* along with MIaV; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
-#include "config.h"
#ifndef __MIAV_SERVER_STATUS_H__
#define __MIAV_SERVER_STATUS_H__
-#include <sys/time.h>
-
-// How many steps to do avarage calculation over.
-#define BUFFERSIZE 100
-
-// Interval in us (microseconds)
-#define UPD 60 * 1000 * 1000 // 1 minute
-
-class ServerStatus {
-public:
- ServerStatus();
- ~ServerStatus();
-
- void checkPoint();
-
-private:
- long long interval;
- unsigned int frametime[BUFFERSIZE];
- struct timeval time;
- struct timeval oldtime;
-};
+int server_status(int port);
#endif/*__MIAV_SERVER_STATUS_H__*/