1 #ifndef _H_APPCLIENT
2 #define _H_APPCLIENT
3 
4 //
5 // The contents of this file are subject to the Mozilla Public
6 // License Version 1.1 (the "License"); you may not use this file
7 // except in compliance with the License. You may obtain a copy of
8 // the License at http://www.mozilla.org/MPL/
9 //
10 // Software distributed under the License is distributed on an "AS
11 // IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
12 // implied. See the License for the specific language governing
13 // rights and limitations under the License.
14 //
15 // The Original Code is State Machine Compiler (SMC).
16 //
17 // The Initial Developer of the Original Code is Charles W. Rapp.
18 // Portions created by Charles W. Rapp are
19 // Copyright (C) 2000 - 2003 Charles W. Rapp.
20 // All Rights Reserved.
21 //
22 // Contributor(s):
23 //
24 // Name
25 //  AppClient.h
26 //
27 // Description
28 //  Encapsulates a TcpClient object.
29 //
30 // RCS ID
31 // $Id: AppClient.h,v 1.4 2005/05/28 13:31:18 cwrapp Exp $
32 //
33 // CHANGE LOG
34 // $Log: AppClient.h,v $
35 // Revision 1.4  2005/05/28 13:31:18  cwrapp
36 // Updated C++ examples.
37 //
38 // Revision 1.0  2003/12/14 19:34:49  charlesr
39 // Initial revision
40 //
41 
42 #include "TcpClient.h"
43 #include "TcpServer.h"
44 #include "TcpConnectionListener.h"
45 
46 // Foward declarations.
47 class AppServer;
48 
49 class AppClient :
50     /* implements */ public TcpConnectionListener,
51     /* implements */ public TimerListener
52 {
53 // Member functions.
54 public:
55 
56     // Default constructor.
57     AppClient();
58 
59     // Create a client around an accepted connection.
60     AppClient(const char *host,
61               TcpClient& client,
62               AppServer& owner);
63 
64     ~AppClient();
65 
66     const char* getHost() const;
67 
68     // Create a TCP client object and open a connection to the
69     // specified service.
70     void open(const char *host,
71               const sockaddr_in& address);
72 
73     // Close the TCP service.
74     void close();
75 
76     // TCP connection listener callback methods.
77     void opened(TcpConnection& connection);
78     void openFailed(const char *reason,
79                     TcpConnection& connection);
80     void transmitted(TcpConnection& connection);
81     void transmitFailed(const char *reason,
82                         TcpConnection& connection);
83     void receive(const char *data,
84                  int size,
85                  TcpConnection& connection);
86     void halfClosed(TcpConnection& connection);
87     void closed(const char *reason,
88                 TcpConnection& connection);
89 
90     // Clients never receive this event.
accepted(TcpClient &,TcpServer &)91     void accepted(TcpClient&, TcpServer&) {};
92 
93     // Timer listener callback method.
94     void handleTimeout(const char *name);
95 
96 protected:
97 private:
98 
99     // Randomly set the transmit time.
100     void setTransmitTimer();
101 
102 // Member data.
103 public:
104 protected:
105 private:
106 
107     // The TCP service itself.
108     TcpClient *_client_socket;
109 
110     // If this was an accepted client connection, _owner points
111     // to the parent TCP service. This will be NULL otherwise.
112     AppServer *_owner;
113 
114     // The host to which we are connected.
115     char *_host;
116 
117     // Every time a message is sent, increment this counter.
118     int _messageCount;
119 
120     // Initialize the random number generator once per process.
121     static int _initFlag;
122 
123     // Class constants. Minimum and Maximum transmit timeout.
124     const static long MIN_SLEEP_TIME;
125     const static long MAX_SLEEP_TIME;
126 };
127 
128 #endif
129