| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1 | # Building of samples for different platforms |
| 2 | |
| 3 | # Reused components |
| 4 | - NET Core Standard 2.0 |
| 5 | - NET Core App 2.0 |
| 6 | |
| 7 | # How to build |
| 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 |
| 14 | |
| 15 | # How to run |
| 16 | |
| 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 |
| 18 | |
| 19 | - build |
| 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**) |
| 22 | |
| 23 | #Notes |
| 24 | - Possible adding additional platforms after stabilization of .NET Core (runtimes, platforms (Red Hat Linux, OpenSuse, etc.) |
| 25 | |
| 26 | #Known issues |
| 27 | - In trace logging mode you can see some not important internal exceptions |
| 28 | |
| 29 | # Running of samples |
| 30 | Please install Thrift C# .NET Core library or copy sources and build them to correcly build and run samples |
| 31 | |
| 32 | # NetCore Server |
| 33 | |
| 34 | Usage: |
| 35 | |
| 36 | Server.exe -h |
| 37 | will diplay help information |
| 38 | |
| 39 | Server.exe -tr:<transport> -pr:<protocol> |
| 40 | will run server with specified arguments (tcp transport and binary protocol by default) |
| 41 | |
| 42 | Options: |
| 43 | |
| 44 | -tr (transport): |
| 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) |
| 51 | |
| 52 | -pr (protocol): |
| 53 | binary - (default) binary protocol will be used |
| 54 | compact - compact protocol will be used |
| 55 | json - json protocol will be used |
| 56 | |
| 57 | Sample: |
| 58 | |
| 59 | Server.exe -tr:tcp |
| 60 | |
| 61 | **Remarks**: |
| 62 | |
| 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". |
| 65 | |
| 66 | |
| 67 | |
| 68 | # NetCore Client |
| 69 | |
| 70 | Usage: |
| 71 | |
| 72 | Client.exe -h |
| 73 | will diplay help information |
| 74 | |
| 75 | Client.exe -tr:<transport> -pr:<protocol> -mc:<numClients> |
| 76 | will run client with specified arguments (tcp transport and binary protocol by default) |
| 77 | |
| 78 | Options: |
| 79 | |
| 80 | -tr (transport): |
| 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) |
| 87 | |
| 88 | -pr (protocol): |
| 89 | binary - (default) binary protocol will be used |
| 90 | compact - compact protocol will be used |
| 91 | json - json protocol will be used |
| 92 | |
| 93 | -mc (multiple clients): |
| 94 | <numClients> - number of multiple clients to connect to server (max 100, default 1) |
| 95 | |
| 96 | Sample: |
| 97 | |
| 98 | Client.exe -tr:tcp -pr:binary -mc:10 |
| 99 | |
| 100 | Remarks: |
| 101 | |
| 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". |
| 105 | |
| 106 | # How to test communication between NetCore and Python |
| 107 | |
| 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 netstd samples (client and server) and python samples (client and server) |
| 112 | |
| 113 | Remarks: |
| 114 | |
| 115 | Samples of client and server code below use correct methods (operations) |
| 116 | and fields (properties) according to generated contracts from *.thrift files |
| 117 | |
| 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 |
| 120 | |
| 121 | |
| 122 | **Python Client:** |
| 123 | |
| 124 | ```python |
| 125 | import sys |
| 126 | import glob |
| 127 | sys.path.append('gen-py') |
| 128 | |
| 129 | from tutorial import Calculator |
| 130 | from tutorial.ttypes import InvalidOperation, Operation, Work |
| 131 | |
| 132 | from thrift import Thrift |
| 133 | from thrift.transport import TSocket |
| 134 | from thrift.transport import TTransport |
| 135 | from thrift.protocol import TBinaryProtocol |
| 136 | |
| 137 | |
| 138 | def main(): |
| 139 | # Make socket |
| 140 | transport = TSocket.TSocket('127.0.0.1', 9090) |
| 141 | |
| 142 | # Buffering is critical. Raw sockets are very slow |
| 143 | transport = TTransport.TBufferedTransport(transport) |
| 144 | |
| 145 | # Wrap in a protocol |
| 146 | protocol = TBinaryProtocol.TBinaryProtocol(transport) |
| 147 | |
| 148 | # Create a client to use the protocol encoder |
| 149 | client = Calculator.Client(protocol) |
| 150 | |
| 151 | # Connect! |
| 152 | transport.open() |
| 153 | |
| 154 | client.Ping() |
| 155 | print('ping()') |
| 156 | |
| 157 | sum = client.Add(1, 1) |
| 158 | print(('1+1=%d' % (sum))) |
| 159 | |
| 160 | work = Work() |
| 161 | |
| 162 | work.Op = Operation.Divide |
| 163 | work.Num1 = 1 |
| 164 | work.Num2 = 0 |
| 165 | |
| 166 | try: |
| 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)) |
| 172 | |
| 173 | work.Op = Operation.Substract |
| 174 | work.Num1 = 15 |
| 175 | work.Num2 = 10 |
| 176 | |
| 177 | diff = client.Calculate(1, work) |
| 178 | print(('15-10=%d' % (diff))) |
| 179 | |
| 180 | log = client.GetStruct(1) |
| 181 | print(('Check log: %s' % (log.Value))) |
| 182 | |
| 183 | client.Zip() |
| 184 | print('zip()') |
| 185 | |
| 186 | # Close! |
| 187 | transport.close() |
| 188 | |
| 189 | if __name__ == '__main__': |
| 190 | try: |
| 191 | main() |
| 192 | except Thrift.TException as tx: |
| 193 | print('%s' % tx.message) |
| 194 | ``` |
| 195 | |
| 196 | |
| 197 | **Python Server:** |
| 198 | |
| 199 | |
| 200 | ```python |
| 201 | import glob |
| 202 | import sys |
| 203 | sys.path.append('gen-py') |
| 204 | |
| 205 | from tutorial import Calculator |
| 206 | from tutorial.ttypes import InvalidOperation, Operation |
| 207 | |
| 208 | from shared.ttypes import SharedStruct |
| 209 | |
| 210 | from thrift.transport import TSocket |
| 211 | from thrift.transport import TTransport |
| 212 | from thrift.protocol import TBinaryProtocol |
| 213 | from thrift.server import TServer |
| 214 | |
| 215 | |
| 216 | class CalculatorHandler: |
| 217 | def __init__(self): |
| 218 | self.log = {} |
| 219 | |
| 220 | def Ping(self): |
| 221 | print('ping()') |
| 222 | |
| 223 | def Add(self, n1, n2): |
| 224 | print('add(%d,%d)' % (n1, n2)) |
| 225 | return n1 + n2 |
| 226 | |
| 227 | def Calculate(self, logid, work): |
| 228 | print('calculate(%d, %r)' % (logid, work)) |
| 229 | |
| 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: |
| 237 | if work.Num2 == 0: |
| 238 | x = InvalidOperation() |
| 239 | x.WhatOp = work.Op |
| 240 | x.Why = 'Cannot divide by 0' |
| 241 | raise x |
| 242 | val = work.Num1 / work.Num2 |
| 243 | else: |
| 244 | x = InvalidOperation() |
| 245 | x.WhatOp = work.Op |
| 246 | x.Why = 'Invalid operation' |
| 247 | raise x |
| 248 | |
| 249 | log = SharedStruct() |
| 250 | log.Key = logid |
| 251 | log.Value = '%d' % (val) |
| 252 | self.log[logid] = log |
| 253 | |
| 254 | return val |
| 255 | |
| 256 | def GetStruct(self, key): |
| 257 | print('getStruct(%d)' % (key)) |
| 258 | return self.log[key] |
| 259 | |
| 260 | def Zip(self): |
| 261 | print('zip()') |
| 262 | |
| 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() |
| 269 | |
| 270 | server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) |
| 271 | print('Starting the server...') |
| 272 | server.serve() |
| 273 | print('done.') |
| 274 | |
| 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) |
| 278 | ``` |