1 # Building of samples for different platforms
4 - NET Core Standard 2.0
8 - Download and install the latest .NET Core SDK for your platform https://www.microsoft.com/net/core#windowsvs2015 (archive for SDK 1.0.0-preview2-003121 located by: https://github.com/dotnet/core/blob/master/release-notes/download-archive.md)
9 - Ensure that you have thrift.exe which supports netcore lib and it added to PATH
10 - Go to current folder
11 - Run **build.sh** or **build.cmd** from the root of cloned repository
12 - Check tests in **src/Tests** folder
13 - Continue with /tutorials/netcore
17 Notes: dotnet run supports passing arguments to app after -- symbols (https://docs.microsoft.com/en-us/dotnet/articles/core/tools/dotnet-run) - example: **dotnet run -- -h** will show help for app
20 - go to folder (Client/Server)
21 - run with specifying of correct parameters **dotnet run -tr:tcp -pr:multiplexed**, **dotnet run -help** (later, after migration to csproj and latest SDK will be possibility to use more usable form **dotnet run -- arguments**)
24 - Possible adding additional platforms after stabilization of .NET Core (runtimes, platforms (Red Hat Linux, OpenSuse, etc.)
27 - In trace logging mode you can see some not important internal exceptions
30 Please install Thrift C# .NET Core library or copy sources and build them to correcly build and run samples
37 will diplay help information
39 Server.exe -tr:<transport> -pr:<protocol>
40 will run server with specified arguments (tcp transport and binary protocol by default)
45 tcp - (default) tcp transport will be used (host - ""localhost"", port - 9090)
46 tcpbuffered - tcp buffered transport will be used (host - ""localhost"", port - 9090)
47 namedpipe - namedpipe transport will be used (pipe address - "".test"")
48 http - http transport will be used (http address - ""localhost:9090"")
49 tcptls - tcp transport with tls will be used (host - ""localhost"", port - 9090)
50 framed - tcp framed transport will be used (host - ""localhost"", port - 9090)
53 binary - (default) binary protocol will be used
54 compact - compact protocol will be used
55 json - json protocol will be used
63 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).
64 Password for certificate - "ThriftTest".
73 will diplay help information
75 Client.exe -tr:<transport> -pr:<protocol> -mc:<numClients>
76 will run client with specified arguments (tcp transport and binary protocol by default)
81 tcp - (default) tcp transport will be used (host - ""localhost"", port - 9090)
82 tcpbuffered - buffered transport over tcp will be used (host - ""localhost"", port - 9090)
83 namedpipe - namedpipe transport will be used (pipe address - "".test"")
84 http - http transport will be used (address - ""http://localhost:9090"")
85 tcptls - tcp tls transport will be used (host - ""localhost"", port - 9090)
86 framed - tcp framed transport will be used (host - ""localhost"", port - 9090)
89 binary - (default) binary protocol will be used
90 compact - compact protocol will be used
91 json - json protocol will be used
93 -mc (multiple clients):
94 <numClients> - number of multiple clients to connect to server (max 100, default 1)
98 Client.exe -tr:tcp -pr:binary -mc:10
102 For TcpTls mode certificate's file ThriftTest.pfx should be in directory
103 with binaries in case of command line usage (or at project level in case of debugging from IDE).
104 Password for certificate - "ThriftTest".
106 # How to test communication between NetCore and Python
108 * Generate code with the latest **thrift.exe** util
109 * Ensure that **thrift.exe** util generated folder **gen-py** with generated code for Python
110 * Create **client.py** and **server.py** from the code examples below and save them to the folder with previosly generated folder **gen-py**
111 * Run netcore samples (client and server) and python samples (client and server)
115 Samples of client and server code below use correct methods (operations)
116 and fields (properties) according to generated contracts from *.thrift files
118 At Windows 10 add record **127.0.0.1 testserver** to **C:\Windows\System32\drivers\etc\hosts** file
119 for correct work of python server
127 sys.path.append('gen-py')
129 from tutorial import Calculator
130 from tutorial.ttypes import InvalidOperation, Operation, Work
132 from thrift import Thrift
133 from thrift.transport import TSocket
134 from thrift.transport import TTransport
135 from thrift.protocol import TBinaryProtocol
140 transport = TSocket.TSocket('127.0.0.1', 9090)
142 # Buffering is critical. Raw sockets are very slow
143 transport = TTransport.TBufferedTransport(transport)
146 protocol = TBinaryProtocol.TBinaryProtocol(transport)
148 # Create a client to use the protocol encoder
149 client = Calculator.Client(protocol)
157 sum = client.Add(1, 1)
158 print(('1+1=%d' % (sum)))
162 work.Op = Operation.Divide
167 quotient = client.Calculate(1, work)
168 print('Whoa? You know how to divide by zero?')
169 print('FYI the answer is %d' % quotient)
170 except InvalidOperation as e:
171 print(('InvalidOperation: %r' % e))
173 work.Op = Operation.Substract
177 diff = client.Calculate(1, work)
178 print(('15-10=%d' % (diff)))
180 log = client.GetStruct(1)
181 print(('Check log: %s' % (log.Value)))
189 if __name__ == '__main__':
192 except Thrift.TException as tx:
193 print('%s' % tx.message)
203 sys.path.append('gen-py')
205 from tutorial import Calculator
206 from tutorial.ttypes import InvalidOperation, Operation
208 from shared.ttypes import SharedStruct
210 from thrift.transport import TSocket
211 from thrift.transport import TTransport
212 from thrift.protocol import TBinaryProtocol
213 from thrift.server import TServer
216 class CalculatorHandler:
223 def Add(self, n1, n2):
224 print('add(%d,%d)' % (n1, n2))
227 def Calculate(self, logid, work):
228 print('calculate(%d, %r)' % (logid, work))
230 if work.Op == Operation.Add:
231 val = work.Num1 + work.Num2
232 elif work.Op == Operation.Substract:
233 val = work.Num1 - work.Num2
234 elif work.Op == Operation.Multiply:
235 val = work.Num1 * work.Num2
236 elif work.Op == Operation.Divide:
238 x = InvalidOperation()
240 x.Why = 'Cannot divide by 0'
242 val = work.Num1 / work.Num2
244 x = InvalidOperation()
246 x.Why = 'Invalid operation'
251 log.Value = '%d' % (val)
252 self.log[logid] = log
256 def GetStruct(self, key):
257 print('getStruct(%d)' % (key))
263 if __name__ == '__main__':
264 handler = CalculatorHandler()
265 processor = Calculator.Processor(handler)
266 transport = TSocket.TServerSocket(host="testserver", port=9090)
267 tfactory = TTransport.TBufferedTransportFactory()
268 pfactory = TBinaryProtocol.TBinaryProtocolFactory()
270 server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
271 print('Starting the server...')
275 # You could do one of these for a multithreaded server
276 # server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
277 # server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)