]> git.vomp.tv Git - vompclient.git/commitdiff
UDP server for inserting remote button presses
authorChris Tallon <chris@vomp.tv>
Tue, 18 Jul 2006 17:30:12 +0000 (17:30 +0000)
committerChris Tallon <chris@vomp.tv>
Tue, 18 Jul 2006 17:30:12 +0000 (17:30 +0000)
command.cc
command.h
dsock.cc
dsock.h
message.h
objects.mk
udp.cc [new file with mode: 0755]
udp.h [new file with mode: 0755]

index 540a6526187977d01a1ea0b7ca30f5bd462ffed7..08750ce659cc44f0961b95db16ceba7d8517a02e 100644 (file)
@@ -78,6 +78,7 @@ int Command::shutdown()
 void Command::stop()
 {
 //  VDR::getInstance()->cancelFindingServer();
+  udp.shutdown();
   irun = 0;
 }
 
@@ -135,6 +136,9 @@ void Command::run()
   viewman->add(vconnect);
   vconnect->run();
 
+  // Start method 2 of getting commands in...
+  udp.run(this);
+
   UCHAR button = 0;
   while(irun)
   {
@@ -281,6 +285,11 @@ void Command::processMessage(Message* m)
       doFromTheTop(true);
       break;
     }
+    case Message::UDP_BUTTON:
+    {
+      handleCommand(m->parameter);
+      break;
+    }
   }
 }
 
index 6b35c3b3e13b1b7d73dfb3038aae79e3f624330b..c38272c731d78f06aee6063c12e906eef6b9102f 100644 (file)
--- a/command.h
+++ b/command.h
@@ -58,6 +58,7 @@
 #include "i18n.h"
 #include "timerreceiver.h"
 #include "timers.h"
+#include "udp.h"
 
 class VConnect;
 
@@ -105,6 +106,8 @@ class Command : public MessageQueue
     VWallpaper* wallpaper;
     VInfo* connLost;
 
+    UDP udp;
+
     void processMessage(Message* m);
 };
 
index 36e6a8edf9eda566e41b95525c884f383b7c0ee1..af6cf1e88360c56cd5a9348aeed6b6f59fbb1993 100644 (file)
--- a/dsock.cc
+++ b/dsock.cc
 
 #include "dsock.h"
 
-ULONG DatagramSocket::iterate_ip = 0;
-
 DatagramSocket::DatagramSocket(short port)
 {
-  theInstance = this;
+  iterate_ip = 0;
   myPort = port;
   addrlen = sizeof(struct sockaddr);
   initted = false;
@@ -35,12 +33,6 @@ DatagramSocket::~DatagramSocket()
   if (initted) shutdown();
 }
 
-DatagramSocket* DatagramSocket::theInstance = 0;
-DatagramSocket* DatagramSocket::getInstance(void)
-{
-  return theInstance;
-}
-
 int DatagramSocket::init()
 {
   if (initted) return 0;
diff --git a/dsock.h b/dsock.h
index 25545e17b9bec147858cd0fccd87cc13fc1d4e39..aa8e488e7fefaa8631c84ff11210acf2a6465ea8 100644 (file)
--- a/dsock.h
+++ b/dsock.h
@@ -49,7 +49,6 @@ class DatagramSocket
   public:
     DatagramSocket(short);             // port
     ~DatagramSocket();
-    static DatagramSocket* getInstance(void);
     int init();
     void shutdown();
     unsigned char waitforMessage(unsigned char); // int =0-block =1-new wait =2-continue wait
@@ -62,9 +61,8 @@ class DatagramSocket
   private:
     bool initted;
     ULONG getIPNumber(ULONG num);
-    static ULONG iterate_ip;
+    ULONG iterate_ip;
     const static char DSOCKDEBUG = 0;
-    static DatagramSocket* theInstance;
     int socketnum;                  // Socket descriptor
     short myPort;                   // My port number
     struct sockaddr_in myAddr;      // My address
index c1a9f0e0f32e84132932860ec9ce024058a4b088..c0084ce7fcee8280559d10d2a3a9fb70b438b56c 100644 (file)
--- a/message.h
+++ b/message.h
@@ -63,6 +63,7 @@ class Message
     const static ULONG CHANGED_OPTIONS = 18;
     const static ULONG CONNECTION_LOST = 19;
     const static ULONG MOVE_RECORDING = 20;
+    const static ULONG UDP_BUTTON = 21;
 };
 
 #endif
index 1fd205192e81a3d92c1908790e0c59c816f2ca6b..aed0e6d4bef5e4e47e488e0d653fabbb1ca85df5 100644 (file)
@@ -1,5 +1,5 @@
 OBJECTS1 = command.o log.o tcp.o dsock.o thread.o timers.o i18n.o mutex.o     \\r
-           message.o messagequeue.o                                           \\r
+           message.o messagequeue.o udp.o                                     \\r
            vdr.o recman.o recording.o channel.o rectimer.o event.o directory.o\\r
            player.o vfeed.o afeed.o                                           \\r
            demuxer.o demuxervdr.o stream.o draintarget.o                      \\r
diff --git a/udp.cc b/udp.cc
new file mode 100755 (executable)
index 0000000..08a9d7b
--- /dev/null
+++ b/udp.cc
@@ -0,0 +1,256 @@
+/*
+    Copyright 2006 Chris Tallon
+
+    This file is part of VOMP.
+
+    VOMP 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.
+
+    VOMP 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 VOMP; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*/
+
+#include "udp.h"
+
+//void dump(unsigned char* data, USHORT size);
+//unsigned char dcc(UCHAR c);
+
+UDP::UDP()
+{
+  log = Log::getInstance();
+  initted = 0;
+}
+
+UDP::~UDP()
+{
+  shutdown();
+}
+
+int UDP::shutdown()
+{
+  if (!initted) return 1;
+  if (threadIsActive()) threadCancel();
+  ds->shutdown();
+
+  initted = 0;
+  return 1;
+}
+
+int UDP::run(MessageQueue* tcommandMessageQueue)
+{
+  if (threadIsActive()) return 1;
+  log->log("UDP", Log::DEBUG, "Starting UDP command server");
+
+  initted = 1;
+
+  commandMessageQueue = tcommandMessageQueue;
+  ds = new DatagramSocket(2000);
+
+  if (!ds->init())
+  {
+    log->log("UDP", Log::DEBUG, "DSock init error");
+    shutdown();
+    return 0;
+  }
+
+  if (!threadStart())
+  {
+    log->log("UDP", Log::DEBUG, "Thread start error");
+    shutdown();
+    return 0;
+  }
+
+  log->log("UDP", Log::DEBUG, "UDP command server started");
+  return 1;
+}
+
+void UDP::threadMethod()
+{
+  int retval;
+  while(1)
+  {
+    log->log("UDP", Log::DEBUG, "Starting wait");
+    retval = ds->waitforMessage(0);
+    log->log("UDP", Log::DEBUG, "Wait finished");
+
+    if (retval == 0)
+    {
+      log->log("UDP", Log::CRIT, "Wait for packet error");
+      return;
+    }
+    else if (retval == 1)
+    {
+      continue;
+    }
+    else
+    {
+      processRequest((UCHAR*)ds->getData(), ds->getDataLength());
+    }
+  }
+}
+
+void UDP::processRequest(UCHAR* data, int length)
+{
+  log->log("UDP", Log::DEBUG, "Got request");
+
+  char* temp = new char[length + 1];
+  memcpy(temp, data, length);
+  temp[length] = '\0';
+  int command = atoi(temp);
+  delete[] temp;
+
+  log->log("UDP", Log::DEBUG, "Command %i recieved", command);
+
+
+  Message *m = new Message();
+  m->to = commandMessageQueue;
+  m->message = Message::UDP_BUTTON;
+  m->parameter = command;
+  commandMessageQueue->postMessage(m);
+}
+
+/*
+void dump(unsigned char* data, USHORT size)
+{
+  printf("Size = %u\n", size);
+
+  USHORT c = 0;
+  while(c < size)
+  {
+    if ((size - c) > 15)
+    {
+      printf(" %02X %02X %02X %02X  %02X %02X %02X %02X  %02X %02X %02X %02X  %02X %02X %02X %02X  %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
+        data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
+        data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13], data[c+14], data[c+15],
+        dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
+        dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]), dcc(data[c+13]), dcc(data[c+14]), dcc(data[c+15]));
+      c += 16;
+    }
+    else
+    {
+      switch (size - c)
+      {
+        case 15:
+          printf(" %02X %02X %02X %02X  %02X %02X %02X %02X  %02X %02X %02X %02X  %02X %02X %02X     %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
+            data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
+            data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13], data[c+14],
+            dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
+            dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]), dcc(data[c+13]), dcc(data[c+14]));
+          c += 15;
+          break;
+        case 14:
+          printf(" %02X %02X %02X %02X  %02X %02X %02X %02X  %02X %02X %02X %02X  %02X %02X        %c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
+            data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
+            data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13],
+            dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
+            dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]), dcc(data[c+13]));
+          c += 14;
+          break;
+        case 13:
+          printf(" %02X %02X %02X %02X  %02X %02X %02X %02X  %02X %02X %02X %02X  %02X           %c%c%c%c%c%c%c%c%c%c%c%c%c\n",
+            data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
+            data[c+8], data[c+9], data[c+10], data[c+11], data[c+12],
+            dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
+            dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]));
+          c += 13;
+          break;
+        case 12:
+          printf(" %02X %02X %02X %02X  %02X %02X %02X %02X  %02X %02X %02X %02X               %c%c%c%c%c%c%c%c%c%c%c%c\n",
+            data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
+            data[c+8], data[c+9], data[c+10], data[c+11],
+            dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
+            dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]));
+          c += 12;
+          break;
+        case 11:
+          printf(" %02X %02X %02X %02X  %02X %02X %02X %02X  %02X %02X %02X                  %c%c%c%c%c%c%c%c%c%c%c\n",
+            data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
+            data[c+8], data[c+9], data[c+10],
+            dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
+            dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]));
+          c += 11;
+          break;
+        case 10:
+          printf(" %02X %02X %02X %02X  %02X %02X %02X %02X  %02X %02X                     %c%c%c%c%c%c%c%c%c%c\n",
+            data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
+            data[c+8], data[c+9],
+            dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
+            dcc(data[c+8]), dcc(data[c+9]));
+          c += 10;
+          break;
+        case 9:
+          printf(" %02X %02X %02X %02X  %02X %02X %02X %02X  %02X                        %c%c%c%c%c%c%c%c%c\n",
+            data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
+            data[c+8],
+            dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
+            dcc(data[c+8]));
+          c += 9;
+          break;
+        case 8:
+          printf(" %02X %02X %02X %02X  %02X %02X %02X %02X                            %c%c%c%c%c%c%c%c\n",
+            data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
+            dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]));
+          c += 8;
+          break;
+        case 7:
+          printf(" %02X %02X %02X %02X  %02X %02X %02X                               %c%c%c%c%c%c%c\n",
+            data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6],
+            dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]));
+          c += 7;
+          break;
+        case 6:
+          printf(" %02X %02X %02X %02X  %02X %02X                                  %c%c%c%c%c%c\n",
+            data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5],
+            dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]));
+          c += 6;
+          break;
+        case 5:
+          printf(" %02X %02X %02X %02X  %02X                                     %c%c%c%c%c\n",
+            data[c], data[c+1], data[c+2], data[c+3], data[c+4],
+            dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]));
+          c += 5;
+          break;
+        case 4:
+          printf(" %02X %02X %02X %02X                                         %c%c%c%c\n",
+            data[c], data[c+1], data[c+2], data[c+3],
+            dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]));
+          c += 4;
+          break;
+        case 3:
+          printf(" %02X %02X %02X                                            %c%c%c\n",
+            data[c], data[c+1], data[c+2],
+            dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]));
+          c += 3;
+          break;
+        case 2:
+          printf(" %02X %02X                                               %c%c\n",
+            data[c], data[c+1],
+            dcc(data[c]), dcc(data[c+1]));
+          c += 2;
+          break;
+        case 1:
+          printf(" %02X                                                  %c\n",
+            data[c],
+            dcc(data[c]));
+          c += 1;
+          break;
+      }
+    }
+  }
+}
+
+unsigned char dcc(UCHAR c)
+{
+  if (isspace(c)) return ' ';
+  if (isprint(c)) return c;
+  return '.';
+}
+*/
diff --git a/udp.h b/udp.h
new file mode 100755 (executable)
index 0000000..9133391
--- /dev/null
+++ b/udp.h
@@ -0,0 +1,60 @@
+/*
+    Copyright 2006 Chris Tallon
+
+    This file is part of VOMP.
+
+    VOMP 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.
+
+    VOMP 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 VOMP; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*/
+
+#ifndef UDP_H
+#define UDP_H
+
+#include <stdio.h>
+#include <signal.h>
+#include <ctype.h>
+
+#include "defines.h"
+#include "log.h"
+#include "dsock.h"
+#include "messagequeue.h"
+
+#ifdef WIN32
+#include "threadwin.h"
+#else
+#include "threadp.h"
+#endif
+
+class UDP : public Thread_TYPE
+{
+  public:
+    UDP();
+    virtual ~UDP();
+
+    int run(MessageQueue* commandMessageQueue);
+    int shutdown();
+
+  private:
+    void threadPostStopCleanup() {};
+
+    void threadMethod();
+    void processRequest(UCHAR* data, int length);
+
+    int initted;
+    DatagramSocket* ds;
+    MessageQueue* commandMessageQueue;
+    Log* log;
+};
+
+#endif