skproto.h 3.42 KB
Newer Older
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
/*  ____  _          _ _          __  __
 * / ___|| | ___   _| (_)_   _____\ \/ /
 * \___ \| |/ / | | | | \ \ / / _ \\  / 
 *  ___) |   <| |_| | | |\ V /  __//  \ Remote Telescopes
 * |____/|_|\_\\__, |_|_| \_/ \___/_/\_\ For the masses
 *             |___/               
 *
 * Copyright (C) 2013 Franco (nextime) Lanza <nextime@nexlab.it>
 * Copyright (C) 2013 Ivan Bellia <skylive@skylive.it>
 *
 * All rights reserved.
 *
 * This file is part of SkyliveX.
 *
 * SkyliveX 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 3 of the License, or
 * (at your option) any later version.
 *
 * Foobar 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 *
 ********************************************************************
 *
 * File: 
 * 
 * Purpose:
 *
 */
35 36 37 38

#ifndef SKPROTO_H
#define SKPROTO_H

39 40
#include <QObject>
#include <QtPlugin>
41
#include <QHash>
42
#include <QTcpSocket>
43
//#include <QNetworkSession>
44
//#include <QByteArray>
45
#include <QQueue>
46
#include <QTimer>
47
#include "pluginsinterfaces.h"
48
#include "ipcmsg.h"
49

50 51
#define SENDER "skproto"

52 53 54
#define SERVERHOST "www.skylive.name"
#define SERVERPORT 8080

55 56
#define CLIENTVERSION "X_0.1.0"

57
#define MAX_PACKETREAD 2048
58 59 60

struct SKProtoMsg
{
61 62 63
  QString cmd;
  QString params;
  QString crc;
64
  int computed_crc;
65 66 67 68 69 70 71 72 73 74 75 76
};

enum _SM_TCPCLIENT
{
   HOME = 0,
   CONNECTED,
   COMMAND,
   PARAMS,
   CRC

}; //SM_TCPCLIENT = HOME;

77 78 79 80 81
#define PROTO_START     0x5b // [
#define PROTO_END       0x5d // ]
#define CMD_END         0x3e // >
#define PARAM_SEPARATOR 0x7c // |
#define PARAM_END       0x3a // :
82

83

84
class SkyliveProtocol;
root's avatar
root committed
85
typedef void (SkyliveProtocol::*SKHandlerFunction)(SKMessage);
86

87 88 89
class SkyliveProtocol : public QObject, SkylivexPluginInterface
{
   Q_OBJECT
Franco (nextime) Lanza's avatar
Franco (nextime) Lanza committed
90
   Q_PLUGIN_METADATA(IID "com.skylivex.SkylivexPlugin/1.0" FILE "skproto.json")
91 92
   Q_INTERFACES(SkylivexPluginInterface)

93
   private:
94
      QHash<QString, SKHandlerFunction> _handlers;
95
      QTcpSocket *tcpSocket;
96 97 98
      //QNetworkSession *networkSession;
      bool authenticated;
      _SM_TCPCLIENT SM_TCPCLIENT;
99 100
      SKProtoMsg protoMsg;
      QQueue<SKProtoMsg> protoQueue;
101
      QTimer* pktTimer;
102
      void clearPkt();
103
      QString cver;
104

105 106
   public:
      void startPlugin();
root's avatar
root committed
107
      void sendMessage(SKMessage msg);
108
      void registerHandler(QString type, SKHandlerFunction handler);
root's avatar
root committed
109 110
      void handle_connect(SKMessage msg);
      void handle_putlogin(SKMessage msg);
111
      void handle_publicsend(SKMessage msg);
112
      void handle_changetelescope(SKMessage msg);
113 114 115 116 117
      void sendPacket(const char* cmd, const char* params);
      void sendPacket(QString &cmd, QString &params);
      void sendPacket(SKProtoMsg &pkt);
      void sendPacket(QString &cmd, QList<QString> &paramlist);

118
   private slots:
119
      //void sessionOpened();
120
      void readFromNetwork();
121
      void clientConnected();
122
      void displayError(QAbstractSocket::SocketError);
123

124
   public slots:
125
      void pluginKicked();
root's avatar
root committed
126
      void receiveMessage(SKMessage msg);
127 128 129
   
   private slots:
     void processPackets();
130

131
   signals:
root's avatar
root committed
132
      void putMessage(SKMessage msg);
133
};
134
#endif