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 netstd 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/netstd
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 namedpipe - namedpipe transport will be used (pipe address - "".test"")
47 http - http transport will be used (http address - ""localhost:9090"")
48 tcptls - tcp transport with tls will be used (host - ""localhost"", port - 9090)
51 none - (default) no transport factory will be used
52 buffered - buffered transport factory will be used
53 framed - framed transport factory will be used (this must match the client)
56 binary - (default) binary protocol will be used
57 compact - compact protocol will be used
58 json - json protocol will be used
66 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).
67 Password for certificate - "ThriftTest".
76 will diplay help information
78 Client.exe -tr:<transport> -pr:<protocol> -mc:<numClients>
79 will run client with specified arguments (tcp transport and binary protocol by default)
84 tcp - (default) tcp transport will be used (host - ""localhost"", port - 9090)
85 namedpipe - namedpipe transport will be used (pipe address - "".test"")
86 http - http transport will be used (address - ""http://localhost:9090"")
87 tcptls - tcp tls transport will be used (host - ""localhost"", port - 9090)
90 none - (default) no transport factory will be used
91 buffered - buffered transport factory will be used
92 framed - framed transport factory will be used (this must match the client)
95 binary - (default) binary protocol will be used
96 compact - compact protocol will be used
97 json - json protocol will be used
99 -mc (multiple clients):
100 <numClients> - number of multiple clients to connect to server (max 100, default 1)
104 Client.exe -tr:tcp -pr:binary -mc:10
108 For TcpTls mode certificate's file ThriftTest.pfx should be in directory
109 with binaries in case of command line usage (or at project level in case of debugging from IDE).
110 Password for certificate - "ThriftTest".
112 # How to test communication between NetCore and Python
114 * Generate code with the latest **thrift.exe** util
115 * Ensure that **thrift.exe** util generated folder **gen-py** with generated code for Python
116 * Create **client.py** and **server.py** from the code examples below and save them to the folder with previosly generated folder **gen-py**
117 * Run netstd samples (client and server) and python samples (client and server)
121 Samples of client and server code below use correct methods (operations)
122 and fields (properties) according to generated contracts from *.thrift files
124 At Windows 10 add record **127.0.0.1 testserver** to **C:\Windows\System32\drivers\etc\hosts** file
125 for correct work of python server
133 sys.path.append('gen-py')
135 from tutorial import Calculator
136 from tutorial.ttypes import InvalidOperation, Operation, Work
138 from thrift import Thrift
139 from thrift.transport import TSocket
140 from thrift.transport import TTransport
141 from thrift.protocol import TBinaryProtocol
146 transport = TSocket.TSocket('127.0.0.1', 9090)
148 # Buffering is critical. Raw sockets are very slow
149 transport = TTransport.TBufferedTransport(transport)
152 protocol = TBinaryProtocol.TBinaryProtocol(transport)
154 # Create a client to use the protocol encoder
155 client = Calculator.Client(protocol)
163 sum = client.Add(1, 1)
164 print(('1+1=%d' % (sum)))
168 work.Op = Operation.Divide
173 quotient = client.Calculate(1, work)
174 print('Whoa? You know how to divide by zero?')
175 print('FYI the answer is %d' % quotient)
176 except InvalidOperation as e:
177 print(('InvalidOperation: %r' % e))
179 work.Op = Operation.Substract
183 diff = client.Calculate(1, work)
184 print(('15-10=%d' % (diff)))
186 log = client.GetStruct(1)
187 print(('Check log: %s' % (log.Value)))
195 if __name__ == '__main__':
198 except Thrift.TException as tx:
199 print('%s' % tx.message)
209 sys.path.append('gen-py')
211 from tutorial import Calculator
212 from tutorial.ttypes import InvalidOperation, Operation
214 from shared.ttypes import SharedStruct
216 from thrift.transport import TSocket
217 from thrift.transport import TTransport
218 from thrift.protocol import TBinaryProtocol
219 from thrift.server import TServer
222 class CalculatorHandler:
229 def Add(self, n1, n2):
230 print('add(%d,%d)' % (n1, n2))
233 def Calculate(self, logid, work):
234 print('calculate(%d, %r)' % (logid, work))
236 if work.Op == Operation.Add:
237 val = work.Num1 + work.Num2
238 elif work.Op == Operation.Substract:
239 val = work.Num1 - work.Num2
240 elif work.Op == Operation.Multiply:
241 val = work.Num1 * work.Num2
242 elif work.Op == Operation.Divide:
244 x = InvalidOperation()
246 x.Why = 'Cannot divide by 0'
248 val = work.Num1 / work.Num2
250 x = InvalidOperation()
252 x.Why = 'Invalid operation'
257 log.Value = '%d' % (val)
258 self.log[logid] = log
262 def GetStruct(self, key):
263 print('getStruct(%d)' % (key))
269 if __name__ == '__main__':
270 handler = CalculatorHandler()
271 processor = Calculator.Processor(handler)
272 transport = TSocket.TServerSocket(host="testserver", port=9090)
273 tfactory = TTransport.TBufferedTransportFactory()
274 pfactory = TBinaryProtocol.TBinaryProtocolFactory()
276 server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
277 print('Starting the server...')
281 # You could do one of these for a multithreaded server
282 # server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
283 # server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)