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