1 /* |
|
2 * Copyright (c) 2009 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: |
|
15 * This file contains headers of IPCommPlugin, |
|
16 * IPCommReaderThread, IPCommWriterThread and IPCommMonitorThread classes. |
|
17 */ |
|
18 |
|
19 #ifndef IPCOMMPLUGIN_H |
|
20 #define IPCOMMPLUGIN_H |
|
21 |
|
22 //#pragma warning ( disable : 4786 ) |
|
23 #include <iostream> |
|
24 #include <process.h> |
|
25 #include <string> |
|
26 #include <map> |
|
27 #include <comdef.h> |
|
28 |
|
29 #include "common.h" |
|
30 #include "plugin.h" |
|
31 #include "safequeue.h" |
|
32 #include "thread.h" |
|
33 #include "datagateway.h" // for g_DataGatewayDefaultTcpIpBufferSize |
|
34 |
|
35 // forward declarations |
|
36 class Socket; |
|
37 |
|
38 using namespace std; |
|
39 |
|
40 #define IP_INI_FILE_NAME "ipcommplugin.ini" |
|
41 |
|
42 #define IP_INI_LOCAL_PORT_PARAM "LOCAL_PORT" |
|
43 #define IP_INI_REMOTE_HOST_PARAM "REMOTE_HOST" |
|
44 #define IP_INI_REMOTE_PORT_PARAM "REMOTE_PORT" |
|
45 #define IP_INI_RECV_BUFFER_SIZE_PARAM "RECV_BUFFER_SIZE" |
|
46 |
|
47 #define PARAM_SWITCH_LOCAL_PORT_PARAM "-LOCAL_PORT" |
|
48 #define PARAM_SWITCH_REMOTE_HOST_PARAM "-REMOTE_HOST" |
|
49 #define PARAM_SWITCH_REMOTE_PORT_PARAM "-REMOTE_PORT" |
|
50 #define PARAM_SWITCH_RECV_BUFFER_SIZE_PARAM "-RECV_BUFFER_SIZE" |
|
51 |
|
52 const DWORD g_IPCommDefaultTimeout = 40000; //ms |
|
53 const DWORD g_IPPluginTimeout = 10000; //not used |
|
54 |
|
55 // Maximum time to wait |
|
56 extern long g_MaximumShutdownWaitTime; |
|
57 |
|
58 //********************************************************************************** |
|
59 // Class IPCommReaderThread |
|
60 // |
|
61 // This thread is used to read bytes from TCP/IP socket, encapsulate the bytes to Data objects |
|
62 // and push them to incoming queue |
|
63 //********************************************************************************** |
|
64 |
|
65 class IPCommReaderThread : public Thread<IPCommReaderThread> |
|
66 { |
|
67 public: |
|
68 IPCommReaderThread(SafeQueue<Data*>* q, long bufsize); |
|
69 /* |
|
70 * Main execution loop which reads bytes from socket, encapsulates the bytes to Data object and pushes them to incoming queue |
|
71 */ |
|
72 void Run(); |
|
73 void Stop(); |
|
74 bool IsRunning(); |
|
75 |
|
76 private: |
|
77 friend class IPCommMonitorThread; |
|
78 //incoming queue |
|
79 SafeQueue<Data*>* m_Queue; |
|
80 //Socket connected to mobile device |
|
81 Socket* m_Socket; |
|
82 bool m_Running; |
|
83 //size of buffer used when reading data from socket |
|
84 long m_TcpIpBufferSize; |
|
85 }; |
|
86 |
|
87 //********************************************************************************** |
|
88 // Class IPCommWriterThread |
|
89 // |
|
90 // This thread is used to write data from outgoing queue to TCP/IP port |
|
91 //********************************************************************************** |
|
92 |
|
93 class IPCommWriterThread : public Thread<IPCommWriterThread> |
|
94 { |
|
95 public: |
|
96 IPCommWriterThread(SafeQueue<Data*>* q); |
|
97 /* |
|
98 * Main execution loop which gets Data from outgoing queue and sends it to socket |
|
99 */ |
|
100 void Run(); |
|
101 void Stop(); |
|
102 bool IsRunning(); |
|
103 |
|
104 private: |
|
105 friend class IPCommMonitorThread; |
|
106 //outgoing queue |
|
107 SafeQueue<Data*>* m_Queue; |
|
108 //Socket connected to mobile device |
|
109 Socket* m_Socket; |
|
110 bool m_Running; |
|
111 }; |
|
112 |
|
113 //********************************************************************************** |
|
114 // Class IPCommMonitorThread |
|
115 // |
|
116 // This thread creates and starts reader and writer threads |
|
117 // The thread also monitors if reader and writer threads are running and restarts them in case either isn't running |
|
118 //********************************************************************************** |
|
119 |
|
120 class IPCommMonitorThread : public Thread<IPCommMonitorThread> |
|
121 { |
|
122 public: |
|
123 IPCommMonitorThread(SafeQueue<Data*>* TxQueue, |
|
124 SafeQueue<Data*>* RxQueue, |
|
125 int LocalPort, |
|
126 string& RemoteHost, |
|
127 int RemotePort, |
|
128 long RecvBufferSize); |
|
129 ~IPCommMonitorThread(); |
|
130 /* |
|
131 * Main execution loop of thread |
|
132 * -Creates reader and writer threads and starts them |
|
133 * -Monitors if either reader or writer thread aren't running and restarts them if not |
|
134 */ |
|
135 void Run(); |
|
136 void Stop(); |
|
137 bool IsRunning(); |
|
138 |
|
139 private: |
|
140 /* |
|
141 * This method has two functionalities |
|
142 * -It waits for incoming connections if local port is defined |
|
143 * -It tries to connect to remote host if local host is not defined |
|
144 */ |
|
145 void Connect(Socket*& s); |
|
146 |
|
147 private: |
|
148 bool m_Running; |
|
149 //outgoing queue |
|
150 SafeQueue<Data*>* m_TxQueue; |
|
151 //incoming queue |
|
152 SafeQueue<Data*>* m_RxQueue; |
|
153 //used to read data from TCP/IP port and push them to incoming queue |
|
154 IPCommReaderThread* m_ReaderThread; |
|
155 //used to write data from outgoing queue to TCP/IP port |
|
156 IPCommWriterThread* m_WriterThread; |
|
157 //port that thread should listen to |
|
158 int m_LocalPort; |
|
159 //hostname that thread should connect to |
|
160 string& m_RemoteHost; |
|
161 //Port that thread should connect to |
|
162 int m_RemotePort; |
|
163 long m_RecvBufferSize; |
|
164 }; |
|
165 |
|
166 //********************************************************************************** |
|
167 // Class IPCommPlugin |
|
168 // |
|
169 // This class implements a CommChannelPlugin used in TCP/IP communication with device |
|
170 //********************************************************************************** |
|
171 |
|
172 class IPCommPlugin : public CommChannelPlugin |
|
173 { |
|
174 public: |
|
175 /* |
|
176 * This method initializes IPCommPlugin and Starts IPCommMonitorThread |
|
177 */ |
|
178 DWORD Init(); |
|
179 /* |
|
180 * This method is used to push given data to outgoing queue and then |
|
181 * wait for data to become available and read all data into single Data object |
|
182 */ |
|
183 DWORD SendReceive( Data* data_in, Data** data_out, long timeout = g_IPPluginTimeout ); |
|
184 /* |
|
185 * This method pushes the given Data object(of type Data::EData) to outgoing queue |
|
186 */ |
|
187 DWORD Send( Data* data_in, long timeout = g_IPPluginTimeout ); |
|
188 /* |
|
189 * This method is used to read all data in incoming queue to single Data object and store the result |
|
190 * to the data object given parameter |
|
191 */ |
|
192 DWORD Receive( Data** data_out, long timeout = g_IPPluginTimeout ); |
|
193 /* |
|
194 * This method is used to wait for data to become available in incoming queue |
|
195 * and then read all data into single Data object which is given as parameter |
|
196 */ |
|
197 DWORD ReceiveWait( Data** data_out, long timeout = g_IPPluginTimeout ); |
|
198 /* |
|
199 * This method checks if data is available on incoming queue |
|
200 */ |
|
201 bool IsDataAvailable(); |
|
202 DWORD Open(); |
|
203 DWORD Close(); |
|
204 IPCommPlugin( const CommChannelPluginObserver* observer = NULL ); |
|
205 ~IPCommPlugin(); |
|
206 |
|
207 private: |
|
208 /* |
|
209 * This method initializes class member variables from values in map |
|
210 */ |
|
211 void CheckProperties( map<string, string>& props ); |
|
212 |
|
213 private: |
|
214 //outgoing queue |
|
215 SafeQueue<Data*> m_TxQueue; |
|
216 //incoming queue |
|
217 SafeQueue<Data*> m_RxQueue; |
|
218 //thread which starts and monitors reader and writer threads |
|
219 IPCommMonitorThread* m_MonitorThread; |
|
220 //port that IPCommPlugin should listen to |
|
221 int m_PropertyLocalPort; |
|
222 //hostname that IPCommPlugin should connect to |
|
223 string m_PropertyRemoteHost; |
|
224 //port that IPCommPlugin should connect to |
|
225 int m_PropertyRemotePort; |
|
226 long m_PropertyRecvBufferSize; |
|
227 }; |
|
228 |
|
229 #endif |
|
230 |
|
231 // End of the file |
|