Add a test client and server for C#.
authorDavid Reiss <dreiss@apache.org>
Wed, 2 Apr 2008 22:10:17 +0000 (22:10 +0000)
committerDavid Reiss <dreiss@apache.org>
Wed, 2 Apr 2008 22:10:17 +0000 (22:10 +0000)
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665633 13f79535-47bb-0310-9956-ffa450edef68

test/csharp/CSharpClient.cs [new file with mode: 0644]
test/csharp/CSharpServer.cs [new file with mode: 0644]

diff --git a/test/csharp/CSharpClient.cs b/test/csharp/CSharpClient.cs
new file mode 100644 (file)
index 0000000..2cb6a83
--- /dev/null
@@ -0,0 +1,68 @@
+using System;\r
+using Thrift;\r
+using Thrift.Protocol;\r
+using Thrift.Server;\r
+using Thrift.Transport;\r
+\r
+\r
+namespace CSharpTutorial\r
+{\r
+    public class CSharpClient\r
+    {\r
+        public static void Main()\r
+        {\r
+            try\r
+            {\r
+                TTransport transport = new TSocket("localhost", 9090);\r
+                TProtocol protocol = new TBinaryProtocol(transport);\r
+                Calculator.Client client = new Calculator.Client(protocol);\r
+\r
+                transport.Open();\r
+\r
+                client.ping();\r
+                Console.WriteLine("ping()");\r
+\r
+                int sum = client.add(1, 1);\r
+                Console.WriteLine("1+1={0}", sum);\r
+\r
+                Work work = new Work();\r
+\r
+                work.op = Operation.DIVIDE;\r
+                work.num1 = 1;\r
+                work.num2 = 0;\r
+                try\r
+                {\r
+                    int quotient = client.calculate(1, work);\r
+                    Console.WriteLine("Whoa we can divide by 0");\r
+                }\r
+                catch (InvalidOperation io)\r
+                {\r
+                    Console.WriteLine("Invalid operation: " + io.why);\r
+                }\r
+\r
+                work.op = Operation.SUBTRACT;\r
+                work.num1 = 15;\r
+                work.num2 = 10;\r
+                try\r
+                {\r
+                    int diff = client.calculate(1, work);\r
+                    Console.WriteLine("15-10={0}", diff);\r
+                }\r
+                catch (InvalidOperation io)\r
+                {\r
+                    Console.WriteLine("Invalid operation: " + io.why);\r
+                }\r
+\r
+                SharedStruct log = client.getStruct(1);\r
+                Console.WriteLine("Check log: {0}", log.value);\r
+\r
+                transport.Close();\r
+            }\r
+            catch (TApplicationException x)\r
+            {\r
+                Console.WriteLine(x.StackTrace);\r
+            }\r
+\r
+        }\r
+    }\r
+}
\ No newline at end of file
diff --git a/test/csharp/CSharpServer.cs b/test/csharp/CSharpServer.cs
new file mode 100644 (file)
index 0000000..32cc703
--- /dev/null
@@ -0,0 +1,112 @@
+\feffusing System;\r
+using System.Collections.Generic;\r
+using System.Linq;\r
+using System.Text;\r
+using Thrift.Server;\r
+using Thrift.Transport;\r
+\r
+namespace CSharpTutorial\r
+{\r
+    public class CalculatorHandler : Calculator.Iface\r
+    {\r
+        Dictionary<int, SharedStruct> log;\r
+\r
+        public CalculatorHandler()\r
+        {\r
+            log = new Dictionary<int, SharedStruct>();\r
+        }\r
+\r
+        public void ping()\r
+        {\r
+            Console.WriteLine("ping()");\r
+        }\r
+\r
+        public int add(int n1, int n2)\r
+        {\r
+            Console.WriteLine("add({0},{1})", n1, n2);\r
+            return n1 + n2;\r
+        }\r
+\r
+        public int calculate(int logid, Work work)\r
+        {\r
+            Console.WriteLine("calculate({0}, [{1},{2},{3}])", logid, work.op, work.num1, work.num2);\r
+            int val = 0;\r
+            switch (work.op)\r
+            {\r
+                case Operation.ADD:\r
+                    val = work.num1 + work.num2;\r
+                    break;\r
+\r
+                case Operation.SUBTRACT:\r
+                    val = work.num1 - work.num2;\r
+                    break;\r
+\r
+                case Operation.MULTIPLY:\r
+                    val = work.num1 * work.num2;\r
+                    break;\r
+\r
+                case Operation.DIVIDE:\r
+                    if (work.num2 == 0)\r
+                    {\r
+                        InvalidOperation io = new InvalidOperation();\r
+                        io.what = (int)work.op;\r
+                        io.why = "Cannot divide by 0";\r
+                        throw io;\r
+                    }\r
+                    val = work.num1 / work.num2;\r
+                    break;\r
+\r
+                default:\r
+                    {\r
+                        InvalidOperation io = new InvalidOperation();\r
+                        io.what = (int)work.op;\r
+                        io.why = "Unknown operation";\r
+                        throw io;\r
+                    }\r
+            }\r
+\r
+            SharedStruct entry = new SharedStruct();\r
+            entry.key = logid;\r
+            entry.value = val.ToString();\r
+            log[logid] = entry;\r
+\r
+            return val;\r
+        }\r
+\r
+        public SharedStruct getStruct(int key)\r
+        {\r
+            Console.WriteLine("getStruct({0})", key);\r
+            return log[key];\r
+        }\r
+\r
+        public void zip()\r
+        {\r
+            Console.WriteLine("zip()");\r
+        }\r
+    }\r
+\r
+    public class CSharpServer\r
+    {\r
+        public static void Main()\r
+        {\r
+            try\r
+            {\r
+                CalculatorHandler handler = new CalculatorHandler();\r
+                Calculator.Processor processor = new Calculator.Processor(handler);\r
+                TServerTransport serverTransport = new TServerSocket(9090);\r
+                TServer server = new TSimpleServer(processor, serverTransport);\r
+\r
+                // Use this for a multithreaded server\r
+                // server = new TThreadPoolServer(processor, serverTransport);\r
+\r
+                Console.WriteLine("Starting the server...");\r
+                server.Serve();\r
+            }\r
+            catch (Exception x)\r
+            {\r
+                Console.WriteLine(x.StackTrace);\r
+            }\r
+            Console.WriteLine("done.");\r
+        }\r
+    }\r
+}\r