We now have a current svn snapshot of libqxt in our contrib dir, and
[quassel.git] / src / contrib / libqxt-2007-10-24 / tests / QxtFileLock / src / locktestclient.cpp
1 #include "locktestclient.h"
2 #include <QTimer>
3 #include <QTcpSocket>
4 #include <QDebug>
5 #include <QFile>
6 #include <QxtFileLock>
7
8 LockTestClient::LockTestClient(QObject *parent)
9  : QObject(parent)
10 {
11 }
12
13
14 LockTestClient::~LockTestClient()
15 {
16 }
17
18 void LockTestClient::startTests()
19 {
20     QTcpSocket socket;
21     socket.connectToHost ( "localhost", 55555);
22     char control;
23     
24     #define GetNextCommand()      if(socket.waitForReadyRead (-1) )\
25                                                         {\
26                                                             if(socket.bytesAvailable() > 1)\
27                                                                 qDebug()<<"Something is wrong here";\
28                                                             socket.getChar(&control);\
29                                                             if(control == 'a')\
30                                                             {\
31                                                                 socket.disconnectFromHost();\
32                                                                 return;\
33                                                             }\
34                                                             if(control != 'n')\
35                                                             { \
36                                                                  qDebug()<<"Wrong control command";\
37                                                             }\
38                                                         }
39     
40     if(socket.waitForConnected (-1))
41     {
42         QFile file("lock.file");
43         
44         if(!file.open(QIODevice::ReadWrite))
45         {
46             qDebug()<<"Could not open lockfile";
47             return;
48         }
49         
50         if(1)
51         {
52             GetNextCommand();
53             //Trying to readlock the same region
54             QxtFileLock lock(&file,0x10,20,QxtFileLock::ReadLock);
55             if(lock.lock())
56                 socket.putChar('s');    //s for success f for fail
57             else
58                 socket.putChar('f');
59             socket.waitForBytesWritten(-1);
60         }
61         
62         if(1)
63         {
64             GetNextCommand();
65              //Trying to lock the same region with different locks
66             QxtFileLock lock(&file,0x10,20,QxtFileLock::WriteLock); 
67             
68             if(!lock.lock())
69                 socket.putChar('s');    //s for success f for fail
70             else
71                 socket.putChar('f');
72             socket.waitForBytesWritten(-1);
73         }
74         
75         if(1)
76         {
77             GetNextCommand();
78              //Trying to writelock the same region
79             QxtFileLock lock(&file,0x10,20,QxtFileLock::WriteLock); 
80             
81             if(!lock.lock())
82                 socket.putChar('s');    //s for success f for fail
83             else
84                 socket.putChar('f');
85             socket.waitForBytesWritten(-1);
86         }
87         
88         if(1)
89         {
90             GetNextCommand();
91              //Trying to writelock different regions
92             QxtFileLock lock(&file,0x10+21,20,QxtFileLock::WriteLock); 
93             
94             if(lock.lock())
95                 socket.putChar('s');    //s for success f for fail
96             else
97                 socket.putChar('f');
98             socket.waitForBytesWritten(-1);
99         }
100         
101     }
102 }
103
104