javacommons/gcfprotocols/socket/serverconnection/inc/socketserverconnection.h
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2007-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  socketserverconnection.h
       
    15 *
       
    16 */
       
    17 
       
    18 #ifndef SOCKETSERVERCONNECTION_H
       
    19 #define SOCKETSERVERCONNECTION_H
       
    20 #include <string>
       
    21 #include <sys/socket.h>
       
    22 #include <sys/select.h>
       
    23 #include <unistd.h>
       
    24 #include <errno.h>
       
    25 #include <arpa/inet.h>
       
    26 #include <netinet/in.h>
       
    27 #include <fcntl.h>
       
    28 #include "serverconnection.h"
       
    29 #include "javaosheaders.h"
       
    30 #include "logger.h"
       
    31 #include "connectionlistener.h"
       
    32 #include "javacommonutils.h"
       
    33 #include "monitor.h"
       
    34 
       
    35 
       
    36 using namespace java::push;
       
    37 
       
    38 /**
       
    39 * This is the base class for Native Peer of server socket.
       
    40 * NativeSocketServerConnection will make calls to this class to create a carry out the all server socket operations.
       
    41 */
       
    42 
       
    43 class SocketServerConnection : public ServerConnection
       
    44 {
       
    45 
       
    46 public:
       
    47     SocketServerConnection();
       
    48     OS_IMPORT SocketServerConnection(const std::wstring& aUri,const std::wstring& aFilter);
       
    49     OS_IMPORT virtual ~SocketServerConnection();
       
    50 
       
    51     virtual void open(ConnectionListener* aListener);
       
    52     virtual void close();
       
    53     virtual std::wstring getUri() const;
       
    54     virtual std::wstring getFilter() const;
       
    55     OS_IMPORT virtual void setFilter(const std::wstring& aFilter);
       
    56 
       
    57     OS_IMPORT int open();
       
    58     OS_IMPORT int accept();
       
    59     OS_IMPORT void setNormalServerConnection();
       
    60     OS_IMPORT bool isNormalServerConnection();
       
    61 
       
    62     /**
       
    63     * close , to close the server connection
       
    64     * @param[in]  fd: server socket descriptor.
       
    65     * @return 0 on successful completion.. If error, then the
       
    66     *         value of errno should be negated and returned. (return -errno)
       
    67     */
       
    68 
       
    69     OS_IMPORT int close(int fd);
       
    70 
       
    71 private:
       
    72 
       
    73     /**
       
    74     * open is called by listening thread to create a new server socket to the specified port.
       
    75     * @param[in]  aPort: port on which server listens.
       
    76     * @return 0 on successful completion.. If error, then the
       
    77     *         value of errno should be negated and returned. (return -errno)
       
    78     */
       
    79 
       
    80     int open(int aPort);
       
    81 
       
    82     /**
       
    83     * accept is called by listening thread to accept an incoming connection. This will frist use the select() api to check for any
       
    84     * incoming connection. Then it calls the accept() system call to accept the connection
       
    85     * @param[in]  fd: server socket descriptor.
       
    86     * @return 0 on successful completion.. If error, then the
       
    87     *         value of errno should be negated and returned. (return -errno)
       
    88     */
       
    89 
       
    90     int accept(int fd);
       
    91 
       
    92 
       
    93     /**
       
    94     * setNonBlocking , to make the server socket as non-blocking. fcntl() system call of OpenC is used to set the socket option O_NONBLOCK.
       
    95     * @param[in]  fd: server socket descriptor.
       
    96     * @return 0 on successful completion.. If error, then the
       
    97     *         value of errno should be negated and returned. (return -errno)
       
    98     */
       
    99 
       
   100     void setNonBlocking(int fd);
       
   101 
       
   102     static void* listenThread(void* params);
       
   103 
       
   104     /**
       
   105      * SetActivityFlag method  updates status whether
       
   106      * ServerConnection object has pending message(= message arrived but not yet read).
       
   107      * @param aFlag Updating logic of pending message flag:
       
   108      *                    - Set it 'true' when message arrives to the connection.
       
   109      *                    - Set it 'false' when ServerConnection::open() operation has been called.
       
   110      */
       
   111     void setActivityFlag(bool aFlag);
       
   112 
       
   113 private:
       
   114     ConnectionListener* mListener;
       
   115 
       
   116     pthread_t mThreadId;
       
   117     bool mKeepRunning;
       
   118 
       
   119     int mListenSocket;
       
   120     int mAcceptSocket;
       
   121     std::wstring mUri;
       
   122     std::wstring mFilter;
       
   123     int mPort;
       
   124     bool mIsAppRunning;
       
   125     bool mIsNormalServerConnection;
       
   126     java::util::Monitor* mOpenMonitor;
       
   127     int mError;
       
   128 };
       
   129 
       
   130 #endif // SOCKETSERVERCONNECTION_H