| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1 | # Building of samples for different platforms |
| 2 | |
| Jens Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame^] | 3 | # Requirements |
| 4 | - NET Core Standard 3.1 (LTS) runtime or SDK (see below for further info) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 5 | |
| 6 | # How to build |
| Jens Geyer | ec43954 | 2019-11-01 19:19:44 +0100 | [diff] [blame] | 7 | - Download and install the latest .NET Core SDK for your platform https://dotnet.microsoft.com/download/dotnet-core |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 8 | - 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 Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame^] | 16 | Depending 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 Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 17 | |
| 18 | - build |
| 19 | - go to folder (Client/Server) |
| Jens Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame^] | 20 | - run the generated executables: server first, then client from a second console |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 21 | |
| 22 | #Known issues |
| 23 | - In trace logging mode you can see some not important internal exceptions |
| 24 | |
| 25 | # Running of samples |
| Jens Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame^] | 26 | On 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 Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 27 | |
| 28 | # NetCore Server |
| 29 | |
| 30 | Usage: |
| 31 | |
| Jens Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame^] | 32 | Server -h |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 33 | will diplay help information |
| 34 | |
| Jens Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame^] | 35 | Server -tr:<transport> -pr:<protocol> |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 36 | will run server with specified arguments (tcp transport and binary protocol by default) |
| 37 | |
| 38 | Options: |
| 39 | |
| 40 | -tr (transport): |
| 41 | tcp - (default) tcp transport will be used (host - ""localhost"", port - 9090) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 42 | 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 Smith | 0bc4712 | 2019-03-27 11:41:34 -0400 | [diff] [blame] | 45 | |
| 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 Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 50 | |
| 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 | |
| 56 | Sample: |
| 57 | |
| Jens Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame^] | 58 | Server -tr:tcp |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 59 | |
| 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 | |
| 69 | Usage: |
| 70 | |
| Jens Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame^] | 71 | Client -h |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 72 | will diplay help information |
| 73 | |
| Jens Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame^] | 74 | Client -tr:<transport> -pr:<protocol> -mc:<numClients> |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 75 | will run client with specified arguments (tcp transport and binary protocol by default) |
| 76 | |
| 77 | Options: |
| 78 | |
| 79 | -tr (transport): |
| 80 | tcp - (default) tcp transport will be used (host - ""localhost"", port - 9090) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 81 | 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 Smith | 0bc4712 | 2019-03-27 11:41:34 -0400 | [diff] [blame] | 84 | |
| 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 Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 89 | |
| 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 | |
| 98 | Sample: |
| 99 | |
| Jens Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame^] | 100 | Client -tr:tcp -pr:binary -mc:10 |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 101 | |
| 102 | Remarks: |
| 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 Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame^] | 110 | * Generate code with the latest **thrift** utility |
| 111 | * Ensure that **thrift** generated folder **gen-py** with generated code for Python exists |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 112 | * 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 | |
| 115 | Remarks: |
| 116 | |
| 117 | Samples of client and server code below use correct methods (operations) |
| 118 | and fields (properties) according to generated contracts from *.thrift files |
| 119 | |
| 120 | At Windows 10 add record **127.0.0.1 testserver** to **C:\Windows\System32\drivers\etc\hosts** file |
| 121 | for correct work of python server |
| 122 | |
| 123 | |
| 124 | **Python Client:** |
| 125 | |
| 126 | ```python |
| 127 | import sys |
| 128 | import glob |
| 129 | sys.path.append('gen-py') |
| 130 | |
| 131 | from tutorial import Calculator |
| 132 | from tutorial.ttypes import InvalidOperation, Operation, Work |
| 133 | |
| 134 | from thrift import Thrift |
| 135 | from thrift.transport import TSocket |
| 136 | from thrift.transport import TTransport |
| 137 | from thrift.protocol import TBinaryProtocol |
| 138 | |
| 139 | |
| 140 | def 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 | |
| 191 | if __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 |
| 203 | import glob |
| 204 | import sys |
| 205 | sys.path.append('gen-py') |
| 206 | |
| 207 | from tutorial import Calculator |
| 208 | from tutorial.ttypes import InvalidOperation, Operation |
| 209 | |
| 210 | from shared.ttypes import SharedStruct |
| 211 | |
| 212 | from thrift.transport import TSocket |
| 213 | from thrift.transport import TTransport |
| 214 | from thrift.protocol import TBinaryProtocol |
| 215 | from thrift.server import TServer |
| 216 | |
| 217 | |
| 218 | class 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 | |
| 265 | if __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 | ``` |