summaryrefslogtreecommitdiff
path: root/automation/inc/automation/simplecm.hxx
blob: cc735a9de99c762f57e649450759e64c38230aa0 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org 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 Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
#ifndef _SIMPLECM_HXX
#define _SIMPLECM_HXX

#include "automation/simplecmdllapi.h"

#include <tools/link.hxx>
#include <tools/string.hxx>
#include <tools/stream.hxx>
#include <osl/socket.hxx>
#include <tools/debug.hxx>
#include <tools/datetime.hxx>

#include <automation/commdefines.hxx>

// CM stands for CommunicationManager
#define CM_UNLIMITED_CONNECTIONS    0xffff

typedef sal_uInt16 CM_NameType;
#define CM_DOTTED   ( (CM_NameType) 01 )
#define CM_FQDN     ( (CM_NameType) 02 )

typedef sal_uInt16 CM_InfoType;
// only one of these three defines may be used
#define CM_NO_TEXT      ( (CM_InfoType) 01 )
#define CM_SHORT_TEXT   ( (CM_InfoType) 02 )
#define CM_VERBOSE_TEXT ( (CM_InfoType) 03 )

#define CM_OPEN         ( (CM_InfoType) 0x0004 )
#define CM_CLOSE        ( (CM_InfoType) 0x0008 )
#define CM_RECEIVE      ( (CM_InfoType) 0x0010 )
#define CM_SEND         ( (CM_InfoType) 0x0020 )
#define CM_ERROR        ( (CM_InfoType) 0x0040 )
#define CM_MISC         ( (CM_InfoType) 0x0080 )

#define CM_USER_1       ( (CM_InfoType) 0x0100 )
#define CM_USER_2       ( (CM_InfoType) 0x0200 )
#define CM_USER_3       ( (CM_InfoType) 0x0400 )
#define CM_USER_4       ( (CM_InfoType) 0x0800 )

#define CM_ALL          ( CM_OPEN | CM_CLOSE | CM_RECEIVE | CM_SEND | CM_ERROR | CM_MISC )
#define CM_NONE         ( 0 )

#define CByteString( constAsciiStr ) ByteString( RTL_CONSTASCII_STRINGPARAM ( constAsciiStr ) )

#define INFO_MSG( Short, Long, Type, CLink ) \
{ \
    if ( (Type & GetInfoType()) > 0 ) \
    { \
        switch ( GetInfoType() & 03 ) \
        { \
            case CM_NO_TEXT: \
                { \
                       ByteString aByteString; \
                    CallInfoMsg( InfoString( aByteString, Type, CLink ) ); \
                } \
                break; \
            case CM_SHORT_TEXT: \
                { \
                       ByteString aByteString( Short ); \
                    CallInfoMsg( InfoString( aByteString, Type, CLink ) ); \
                } \
                break; \
            case CM_VERBOSE_TEXT: \
                { \
                       ByteString aByteString( Long ); \
                    CallInfoMsg( InfoString( aByteString, Type, CLink ) ); \
                } \
                break; \
            default: \
                break; \
        } \
    } \
}\

class CommunicationLink;

SV_DECL_REF( CommunicationLink )

class SIMPLECM_DLLPUBLIC InfoString : public ByteString
{
public:
    InfoString( ByteString &nMsg, CM_InfoType nIT, CommunicationLink *pCL = NULL ): ByteString( nMsg ), nInfoType( nIT ), pCommLink( pCL ) {;}
    CM_InfoType GetInfoType(){ return nInfoType; }
    CommunicationLinkRef GetCommunicationLink(){ return pCommLink; }
private:
    CM_InfoType nInfoType;
    CommunicationLinkRef pCommLink;
};

class PacketHandler;
class CommunicationManager;
class MultiCommunicationManager;
class CommunicationManagerServerAcceptThread;
class SIMPLECM_DLLPUBLIC CommunicationLink : public SvRefBase
{
protected:
    friend class CommunicationManager;
    friend class MultiCommunicationManager;
    friend class CommunicationManagerServerAcceptThread;
    // may not be stopped between the stream's reception and the callback's end

protected:  // so that it can only be deleted via Ref
    virtual ~CommunicationLink();
    void InvalidateManager() { pMyManager = NULL; }

    PacketHandler* pPacketHandler;

public:
    CommunicationLink( CommunicationManager *pMan );

    virtual sal_Bool StopCommunication()=0;
    virtual sal_Bool IsCommunicationError()=0;
    CommunicationManager* GetCommunicationManager(){ return pMyManager; }

    virtual ByteString GetCommunicationPartner( CM_NameType eType )=0;

    virtual ByteString GetMyName( CM_NameType eType )=0;

    virtual SvStream* GetBestCommunicationStream()=0;

    /** will call virtual function DoTransferDataStream to do actual work
        Purpos is to allow housekeeping
    **/
    sal_Bool TransferDataStream( SvStream *pDataStream, CMProtocol nProtocol = CM_PROTOCOL_OLDSTYLE );

    // delivers the ID that is named by the server
    // Due to that virtual communications over a physical link can be realised.
    // Because the communication must remain compatible to older versions the receiver must guess whether
    // the new or the old one is used because the order's head changes then
    sal_uInt16 GetProtocol(){ return nServiceProtocol; }

    // Commits the stream. The caller is responsible for the deletion.
    // The method must be called because there would be no further data received otherwise.
    SvStream* GetServiceData(){ SvStream *pTemp = pServiceData; pServiceData = NULL; return pTemp; }

    /// Allows for the release of the next callbacks. Is also called implicitly.
    void FinishCallback(){ bIsInsideCallback = sal_False; }

    /// Synchronous reception of the data. Only for command line - empty implementation for other uses.
    virtual sal_Bool ReceiveDataStream(){ return sal_False; }

    /// Statistics
    DateTime GetStart() { return aStart; }
    sal_uLong GetTotalBytes() { return nTotalBytes; }
    DateTime GetLastAccess() { return aLastAccess; }
    const ByteString& GetApplication() { return maApplication; }
    virtual void SetApplication( const ByteString& aApp );

protected:
    void CallInfoMsg( InfoString aMsg );
    CM_InfoType GetInfoType();
    CommunicationManager *pMyManager;
// These methods are called in the main context and are handed over to the manager.
    virtual DECL_LINK( ConnectionClosed, void* = NULL );
    virtual DECL_LINK( DataReceived, void* = NULL );

    virtual sal_Bool DoTransferDataStream( SvStream *pDataStream, CMProtocol nProtocol = CM_PROTOCOL_OLDSTYLE );

    SvStream *pServiceData;
    sal_uInt16 nServiceProtocol;
    sal_uInt16 nServiceHeaderType;

    /// Prevents the early release of the next callback.
    void StartCallback(){ bIsInsideCallback = sal_True; }
    sal_Bool bIsInsideCallback;

    virtual sal_Bool SendHandshake( HandshakeType aHandshakeType, SvStream* pData = NULL)=0;

    virtual sal_Bool ShutdownCommunication() = 0;   /// Really stop the Communication

    /// Statistics
    DateTime aStart;
    sal_uLong nTotalBytes;
    DateTime aLastAccess;

private:
    ByteString maApplication;

#if OSL_DEBUG_LEVEL > 1
public:
    // misc (debuging) purposes
    sal_Bool bFlag;
    sal_uLong nSomething;
#endif

};

SV_IMPL_REF( CommunicationLink );

class CommonSocketFunctions;
class SIMPLECM_DLLPUBLIC CommunicationManager
{
    friend class CommunicationLink;
    friend class CommonSocketFunctions;
public:
    CommunicationManager( sal_Bool bUseMultiChannel = sal_False );
    virtual ~CommunicationManager();

    virtual sal_Bool StartCommunication()=0;
    virtual sal_Bool StartCommunication( String aApp, String aParams );
    virtual sal_Bool StartCommunication( ByteString aHost, sal_uLong nPort );
    virtual sal_Bool StopCommunication()=0;
    virtual sal_Bool IsCommunicationRunning() { return bIsCommunicationRunning; }
//  virtual sal_Bool IsCommunicationError();


    virtual ByteString GetMyName( CM_NameType eType );

    virtual sal_Bool IsLinkValid( CommunicationLink* pCL )=0;   // necessary for call in destructor

    virtual sal_uInt16 GetCommunicationLinkCount()=0;
    virtual CommunicationLinkRef GetCommunicationLink( sal_uInt16 nNr )=0;

    CommunicationLinkRef GetLastNewLink() { return xLastNewLink; }

    void SetConnectionOpenedHdl( Link lConnectionOpened ){ mlConnectionOpened = lConnectionOpened; }
    void SetConnectionClosedHdl( Link lConnectionClosed ){ mlConnectionClosed = lConnectionClosed; }
    void SetDataReceivedHdl( Link lDataReceived ){ mlDataReceived = lDataReceived; }
    void SetInfoMsgHdl( Link lInfoMsg ){ mlInfoMsg = lInfoMsg; }

    void SetInfoType( CM_InfoType nIT ){ nInfoType = nIT; }
    CM_InfoType GetInfoType(){ return nInfoType; }

    sal_Bool IsMultiChannel(){ return bIsMultiChannel; }
    void SetApplication( const ByteString& aApp, sal_Bool bRunningLinks = sal_False );
    const ByteString& GetApplication() { return maApplication; }

protected:
    // These methods are called inside. They care for housekeeping if applicable
    // and call the respective method then.
    virtual void CallConnectionOpened( CommunicationLink* pCL );
    virtual void CallConnectionClosed( CommunicationLink* pCL );
    void CallDataReceived( CommunicationLink* pCL );
    void CallInfoMsg( InfoString aMsg );

    CM_InfoType nInfoType;

    //  These routines call the link or are overloaded.
    virtual void ConnectionOpened( CommunicationLink* pCL ){ mlConnectionOpened.Call( pCL ); }
    virtual void ConnectionClosed( CommunicationLink* pCL ){ mlConnectionClosed.Call( pCL ); }
    virtual void DataReceived( CommunicationLink* pCL ){ mlDataReceived.Call( pCL ); }
    virtual void InfoMsg( InfoString aMsg ){ mlInfoMsg.Call( &aMsg ); }

    sal_Bool bIsCommunicationRunning;

    virtual void DestroyingLink( CommunicationLink *pCL )=0;

private:
    ByteString maApplication;
    Link mlConnectionOpened;
    Link mlConnectionClosed;
    Link mlDataReceived;
    Link mlInfoMsg;
    CommunicationLinkRef xLastNewLink;

    sal_Bool bIsMultiChannel;
};

class SIMPLECM_DLLPUBLIC ICommunicationManagerClient
{
    friend class CommonSocketFunctions;
protected:
    virtual sal_Bool RetryConnect() { return sal_False; }   // might be able to start the application
};

class TCPIO;
class SIMPLECM_DLLPUBLIC SimpleCommunicationLinkViaSocket : public CommunicationLink
{
public:
    virtual sal_Bool IsCommunicationError();
    virtual sal_Bool StopCommunication();

    virtual ByteString GetCommunicationPartner( CM_NameType eType );
    virtual ByteString GetMyName( CM_NameType eType );
    virtual SvStream* GetBestCommunicationStream();
    virtual void SetApplication( const ByteString& aApp );

private:
    ByteString aCommunicationPartner;
    ByteString aMyName;

    TCPIO* pTCPIO;
    osl::StreamSocket* pStreamSocket;

protected:
    SimpleCommunicationLinkViaSocket( CommunicationManager *pMan, osl::StreamSocket* pSocket );
    virtual ~SimpleCommunicationLinkViaSocket();

    osl::StreamSocket* GetStreamSocket() { return pStreamSocket; }
    void SetStreamSocket( osl::StreamSocket* pSocket );

    SvStream *pReceiveStream;
    sal_Bool DoReceiveDataStream();             /// Recieve DataPacket from Socket
    virtual sal_Bool SendHandshake( HandshakeType aHandshakeType, SvStream* pData = NULL);
    sal_Bool bIsRequestShutdownPending;
    virtual void WaitForShutdown()=0;
    void SetNewPacketAsCurrent();
};

class SIMPLECM_DLLPUBLIC CommonSocketFunctions
{
public:
    sal_Bool DoStartCommunication( CommunicationManager *pCM, ICommunicationManagerClient *pCMC, ByteString aHost, sal_uLong nPort );
protected:
    virtual CommunicationLink *CreateCommunicationLink( CommunicationManager *pCM, osl::ConnectorSocket* pCS )=0;
};

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */