source: pacpussensors/trunk/Alasca/AlascaSocket.cpp@ 97

Last change on this file since 97 was 16, checked in by DHERBOMEZ Gérald, 11 years ago

add standards replay components for some sensors that has to be used with DBITEPlayer application: StdDbtPlayerComponents

File size: 3.4 KB
Line 
1/*********************************************************************
2// created: 2007/11/13 - 10:47
3// filename: AlascaSocket.cpp
4//
5// author: Gerald Dherbomez
6// Copyright Heudiasyc UMR UTC/CNRS 6599
7//
8// version: $Id: $
9//
10// purpose: Management of the socket connection with the Alasca
11*********************************************************************/
12
13#include "AlascaSocket.h"
14
15#include <QCoreApplication>
16#include <QDebug>
17
18#include "AlascaComponent.h"
19#include "Pacpus/kernel/Log.h"
20
21namespace pacpus {
22
23DECLARE_STATIC_LOGGER("pacpus.base.AlascaSocket");
24
25//////////////////////////////////////////////////////////////////////////
26/// Constructor
27AlascaSocket::AlascaSocket(QObject * parent)
28{
29 socket = new QTcpSocket(this);
30 connect( socket, SIGNAL(connected()), this,SLOT(socketConnected()) );
31 connect( socket, SIGNAL(disconnected()),this, SLOT(socketConnectionClosed()) );
32 connect( socket, SIGNAL(readyRead()),this, SLOT(socketReadyRead()) );
33 connect( socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)) );
34 myParent = (AlascaComponent *) parent;
35}
36
37//////////////////////////////////////////////////////////////////////////
38/// Destructor
39AlascaSocket::~AlascaSocket()
40{
41 delete socket;
42}
43
44//////////////////////////////////////////////////////////////////////////
45/// public slot
46/// connection to server
47void AlascaSocket::connectToServer(QString host, int port)
48{
49 qDebug("trying to connect to server");
50 socket->connectToHost(host,port);
51}
52
53//////////////////////////////////////////////////////////////////////////
54/// protected slot
55int AlascaSocket::socketConnected()
56{
57 qDebug() << "we are connected to the server " << socket->peerName().toLatin1()
58 << " at the port " << socket->peerPort() << " via the socket " << socket->socketDescriptor();
59 emit configuration();
60
61 return 1;
62}
63
64//////////////////////////////////////////////////////////////////////////
65/// protected slot
66void AlascaSocket::socketConnectionClosed()
67{
68 qDebug("the connection was closed");
69}
70
71//////////////////////////////////////////////////////////////////////////
72/// protected slot
73/// new incoming data to read
74void AlascaSocket::socketReadyRead()
75{
76 AlascaFrame * frame = new AlascaFrame();
77 frame->time = road_time();
78 frame->size = socket->bytesAvailable();
79 frame->msg = new char[frame->size];
80
81 if (!frame->msg) {
82 LOG_FATAL("cannot allocate memory of size " << frame->size
83 << " - file: " << __FILE__
84 << " line: " << __LINE__
85 );
86 }
87
88 frame->size = socket->read( frame->msg, frame->size);
89
90 AlascaFrameEvent *e = new AlascaFrameEvent;
91 e->frame = frame;
92 QCoreApplication::postEvent(myParent, e);
93}
94
95//////////////////////////////////////////////////////////////////////////
96/// protected slot
97void AlascaSocket::socketError(QAbstractSocket::SocketError e)
98{
99 qWarning("socket error number %d",e);
100}
101
102//////////////////////////////////////////////////////////////////////////
103/// Send data to server via a text stream
104void AlascaSocket::sendToServer(QString data)//a adapter aux données binaires
105{
106 mutex.lock();
107 QTextStream os(socket);
108 os << data << endl;
109 mutex.unlock();
110 qDebug() << "data sent to server: " << data.toLatin1();//a adapter aussi
111}
112
113} // namespace pacpus
Note: See TracBrowser for help on using the repository browser.