blob: 297f4ee326dbec66c556bb411c9b2adbec43f42d [file] [log] [blame] [view]
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001# Building of samples for different platforms
2
Jens Geyerffb97e12019-12-06 23:43:08 +01003# Requirements
4- NET Core Standard 3.1 (LTS) runtime or SDK (see below for further info)
Jens Geyeraa0c8b32019-01-28 23:27:45 +01005
6# How to build
Jens Geyerec439542019-11-01 19:19:44 +01007- Download and install the latest .NET Core SDK for your platform https://dotnet.microsoft.com/download/dotnet-core
Jens Geyeraa0c8b32019-01-28 23:27:45 +01008- Ensure that you have thrift.exe which supports netstd lib and it added to PATH
9- Go to current folder
10- Run **build.sh** or **build.cmd** from the root of cloned repository
11- Check tests in **src/Tests** folder
12- Continue with /tutorials/netstd
13
14# How to run
15
Jens Geyerffb97e12019-12-06 23:43:08 +010016Depending on the platform, the name of the generated executables will vary. On Linux, it is just "client" or "server", on Windows it is "Client.exe" and "Server.exe". In the following, we use the abbreviated form "Client" and "Server".
Jens Geyeraa0c8b32019-01-28 23:27:45 +010017
18- build
19- go to folder (Client/Server)
Jens Geyerffb97e12019-12-06 23:43:08 +010020- run the generated executables: server first, then client from a second console
Jens Geyeraa0c8b32019-01-28 23:27:45 +010021
22#Known issues
23- In trace logging mode you can see some not important internal exceptions
24
25# Running of samples
Jens Geyerffb97e12019-12-06 23:43:08 +010026On machines that do not have the SDK installed, you need to install the NET Core runtime first. The SDK is only needed to build programs, otherwise the runtime is sufficient.
Jens Geyeraa0c8b32019-01-28 23:27:45 +010027
28# NetCore Server
29
30Usage:
31
Jens Geyerffb97e12019-12-06 23:43:08 +010032 Server -h
Jens Geyeraa0c8b32019-01-28 23:27:45 +010033 will diplay help information
34
Jens Geyerffb97e12019-12-06 23:43:08 +010035 Server -tr:<transport> -pr:<protocol>
Jens Geyeraa0c8b32019-01-28 23:27:45 +010036 will run server with specified arguments (tcp transport and binary protocol by default)
37
38Options:
39
40 -tr (transport):
41 tcp - (default) tcp transport will be used (host - ""localhost"", port - 9090)
Jens Geyeraa0c8b32019-01-28 23:27:45 +010042 namedpipe - namedpipe transport will be used (pipe address - "".test"")
43 http - http transport will be used (http address - ""localhost:9090"")
44 tcptls - tcp transport with tls will be used (host - ""localhost"", port - 9090)
Kyle Smith0bc47122019-03-27 11:41:34 -040045
46 -bf (buffering):
47 none - (default) no transport factory will be used
48 buffered - buffered transport factory will be used
49 framed - framed transport factory will be used (this must match the client)
Jens Geyeraa0c8b32019-01-28 23:27:45 +010050
51 -pr (protocol):
52 binary - (default) binary protocol will be used
53 compact - compact protocol will be used
54 json - json protocol will be used
55
56Sample:
57
Jens Geyerffb97e12019-12-06 23:43:08 +010058 Server -tr:tcp
Jens Geyeraa0c8b32019-01-28 23:27:45 +010059
60**Remarks**:
61
62 For TcpTls mode certificate's file ThriftTest.pfx should be in directory with binaries in case of command line usage (or at project level in case of debugging from IDE).
63 Password for certificate - "ThriftTest".
64
65
66
67# NetCore Client
68
69Usage:
70
Jens Geyerffb97e12019-12-06 23:43:08 +010071 Client -h
Jens Geyeraa0c8b32019-01-28 23:27:45 +010072 will diplay help information
73
Jens Geyerffb97e12019-12-06 23:43:08 +010074 Client -tr:<transport> -pr:<protocol> -mc:<numClients>
Jens Geyeraa0c8b32019-01-28 23:27:45 +010075 will run client with specified arguments (tcp transport and binary protocol by default)
76
77Options:
78
79 -tr (transport):
80 tcp - (default) tcp transport will be used (host - ""localhost"", port - 9090)
Jens Geyeraa0c8b32019-01-28 23:27:45 +010081 namedpipe - namedpipe transport will be used (pipe address - "".test"")
82 http - http transport will be used (address - ""http://localhost:9090"")
83 tcptls - tcp tls transport will be used (host - ""localhost"", port - 9090)
Kyle Smith0bc47122019-03-27 11:41:34 -040084
85 -bf (buffering):
86 none - (default) no transport factory will be used
87 buffered - buffered transport factory will be used
88 framed - framed transport factory will be used (this must match the client)
Jens Geyeraa0c8b32019-01-28 23:27:45 +010089
90 -pr (protocol):
91 binary - (default) binary protocol will be used
92 compact - compact protocol will be used
93 json - json protocol will be used
94
95 -mc (multiple clients):
96 <numClients> - number of multiple clients to connect to server (max 100, default 1)
97
98Sample:
99
Jens Geyerffb97e12019-12-06 23:43:08 +0100100 Client -tr:tcp -pr:binary -mc:10
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100101
102Remarks:
103
104 For TcpTls mode certificate's file ThriftTest.pfx should be in directory
105 with binaries in case of command line usage (or at project level in case of debugging from IDE).
106 Password for certificate - "ThriftTest".
107
108# How to test communication between NetCore and Python
109
Jens Geyerffb97e12019-12-06 23:43:08 +0100110* Generate code with the latest **thrift** utility
111* Ensure that **thrift** generated folder **gen-py** with generated code for Python exists
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100112* Create **client.py** and **server.py** from the code examples below and save them to the folder with previosly generated folder **gen-py**
113* Run netstd samples (client and server) and python samples (client and server)
114
115Remarks:
116
117Samples of client and server code below use correct methods (operations)
118and fields (properties) according to generated contracts from *.thrift files
119
120At Windows 10 add record **127.0.0.1 testserver** to **C:\Windows\System32\drivers\etc\hosts** file
121for correct work of python server
122
123
124**Python Client:**
125
126```python
127import sys
128import glob
129sys.path.append('gen-py')
130
131from tutorial import Calculator
132from tutorial.ttypes import InvalidOperation, Operation, Work
133
134from thrift import Thrift
135from thrift.transport import TSocket
136from thrift.transport import TTransport
137from thrift.protocol import TBinaryProtocol
138
139
140def main():
141 # Make socket
142 transport = TSocket.TSocket('127.0.0.1', 9090)
143
144 # Buffering is critical. Raw sockets are very slow
145 transport = TTransport.TBufferedTransport(transport)
146
147 # Wrap in a protocol
148 protocol = TBinaryProtocol.TBinaryProtocol(transport)
149
150 # Create a client to use the protocol encoder
151 client = Calculator.Client(protocol)
152
153 # Connect!
154 transport.open()
155
156 client.Ping()
157 print('ping()')
158
159 sum = client.Add(1, 1)
160 print(('1+1=%d' % (sum)))
161
162 work = Work()
163
164 work.Op = Operation.Divide
165 work.Num1 = 1
166 work.Num2 = 0
167
168 try:
169 quotient = client.Calculate(1, work)
170 print('Whoa? You know how to divide by zero?')
171 print('FYI the answer is %d' % quotient)
172 except InvalidOperation as e:
173 print(('InvalidOperation: %r' % e))
174
175 work.Op = Operation.Substract
176 work.Num1 = 15
177 work.Num2 = 10
178
179 diff = client.Calculate(1, work)
180 print(('15-10=%d' % (diff)))
181
182 log = client.GetStruct(1)
183 print(('Check log: %s' % (log.Value)))
184
185 client.Zip()
186 print('zip()')
187
188 # Close!
189 transport.close()
190
191if __name__ == '__main__':
192 try:
193 main()
194 except Thrift.TException as tx:
195 print('%s' % tx.message)
196```
197
198
199**Python Server:**
200
201
202```python
203import glob
204import sys
205sys.path.append('gen-py')
206
207from tutorial import Calculator
208from tutorial.ttypes import InvalidOperation, Operation
209
210from shared.ttypes import SharedStruct
211
212from thrift.transport import TSocket
213from thrift.transport import TTransport
214from thrift.protocol import TBinaryProtocol
215from thrift.server import TServer
216
217
218class CalculatorHandler:
219 def __init__(self):
220 self.log = {}
221
222 def Ping(self):
223 print('ping()')
224
225 def Add(self, n1, n2):
226 print('add(%d,%d)' % (n1, n2))
227 return n1 + n2
228
229 def Calculate(self, logid, work):
230 print('calculate(%d, %r)' % (logid, work))
231
232 if work.Op == Operation.Add:
233 val = work.Num1 + work.Num2
234 elif work.Op == Operation.Substract:
235 val = work.Num1 - work.Num2
236 elif work.Op == Operation.Multiply:
237 val = work.Num1 * work.Num2
238 elif work.Op == Operation.Divide:
239 if work.Num2 == 0:
240 x = InvalidOperation()
241 x.WhatOp = work.Op
242 x.Why = 'Cannot divide by 0'
243 raise x
244 val = work.Num1 / work.Num2
245 else:
246 x = InvalidOperation()
247 x.WhatOp = work.Op
248 x.Why = 'Invalid operation'
249 raise x
250
251 log = SharedStruct()
252 log.Key = logid
253 log.Value = '%d' % (val)
254 self.log[logid] = log
255
256 return val
257
258 def GetStruct(self, key):
259 print('getStruct(%d)' % (key))
260 return self.log[key]
261
262 def Zip(self):
263 print('zip()')
264
265if __name__ == '__main__':
266 handler = CalculatorHandler()
267 processor = Calculator.Processor(handler)
268 transport = TSocket.TServerSocket(host="testserver", port=9090)
269 tfactory = TTransport.TBufferedTransportFactory()
270 pfactory = TBinaryProtocol.TBinaryProtocolFactory()
271
272 server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
273 print('Starting the server...')
274 server.serve()
275 print('done.')
276
277 # You could do one of these for a multithreaded server
278 # server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
279 # server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)
280```