Mercurial > hg > Database > Christie-sharp
changeset 31:6399d784c6d1
add FizzBuzz Test
author | riono <e165729@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 13 Apr 2021 18:25:15 +0900 |
parents | 96fc5e71274e |
children | 3a7a71ee8738 |
files | Christie_net.csproj Test/Example/FizzBuzz/Counter.cs Test/Example/FizzBuzz/FizzBuzz.cs Test/Example/FizzBuzz/StartFizzBuzz.cs Test/Example/HelloWorld/FinishHelloWorld.cs Test/Example/HelloWorld/HelloWorldCodeGear.cs Test/RewritingTest/AsynchronousSocketListener.cs Test/RewritingTest/SocketListenerTask.cs daemon/AcceptThread.cs daemon/ChristieDaemon.cs |
diffstat | 10 files changed, 294 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/Christie_net.csproj Tue Apr 06 22:02:23 2021 +0900 +++ b/Christie_net.csproj Tue Apr 13 18:25:15 2021 +0900 @@ -3,11 +3,10 @@ <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp3.1</TargetFramework> - <StartupObject>Christie_net.Test.Example.HelloWorld.StartHelloWorld</StartupObject> + <StartupObject>Christie_net.Test.Example.FizzBuzz.StartFizzBuzz</StartupObject> </PropertyGroup> <ItemGroup> <PackageReference Include="MessagePack" Version="2.1.143" /> </ItemGroup> - </Project>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test/Example/FizzBuzz/Counter.cs Tue Apr 13 18:25:15 2021 +0900 @@ -0,0 +1,19 @@ +using Christie_net.annotation; +using Christie_net.codegear; + +namespace Christie_net.Test.Example.FizzBuzz { +public class Counter: CodeGear { + [Take] private int num; + + public override void Run(CodeGearManager cgm) { + if (num <= 100) { + GetDgm("FizzBuzz").Put("num", num); + cgm.GetLocalDGM().Put("num", num+1); + cgm.Setup(new Counter()); + } else { + GetDgm("FizzBuzz").Put("num", -1); + cgm.GetLocalDGM().Finish(); + } + } +} +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test/Example/FizzBuzz/FizzBuzz.cs Tue Apr 13 18:25:15 2021 +0900 @@ -0,0 +1,22 @@ +using System; +using Christie_net.annotation; +using Christie_net.codegear; + +namespace Christie_net.Test.Example.FizzBuzz { +public class FizzBuzz : CodeGear { + [Take] private int num; + + public override void Run(CodeGearManager cgm) { + if (num % 3 == 0 && num % 5 == 0) { + Console.WriteLine(num + ":FizzBuzz"); + }else if (num % 3 == 0) { + Console.WriteLine(num + ":Fizz"); + }else if (num % 5 == 0) { + Console.WriteLine(num + ":Buzz"); + } else { + Console.WriteLine(num); + } + cgm.Setup(new FizzBuzz()); + } +} +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test/Example/FizzBuzz/StartFizzBuzz.cs Tue Apr 13 18:25:15 2021 +0900 @@ -0,0 +1,19 @@ +using Christie_net.annotation; +using Christie_net.codegear; + +namespace Christie_net.Test.Example.FizzBuzz { +public class StartFizzBuzz : StartCodeGear { + [Take] private int i = 1; + + public StartFizzBuzz(CodeGearManager cgm) : base(cgm) { } + + public static void Main(string[] args) { + CodeGearManager counter = CreateCgm(10001); + CodeGearManager fizzbuzz = CreateCgm(10002); + counter.Setup(new Counter()); + fizzbuzz.Setup(new FizzBuzz()); + counter.CreateRemoteDGM("FizzBuzz", "localhost", 10002); + counter.GetLocalDGM().Put("num", 1); + } +} +} \ No newline at end of file
--- a/Test/Example/HelloWorld/FinishHelloWorld.cs Tue Apr 06 22:02:23 2021 +0900 +++ b/Test/Example/HelloWorld/FinishHelloWorld.cs Tue Apr 13 18:25:15 2021 +0900 @@ -4,8 +4,8 @@ namespace Christie_net.Test.Example.HelloWorld { public class FinishHelloWorld : CodeGear { - [Take] public string hello; - [Take] public string world; + [Take] private string hello; + [Take] private string world; public override void Run(CodeGearManager cgm) { //Console.WriteLine("fin:" + hello + " " + world);
--- a/Test/Example/HelloWorld/HelloWorldCodeGear.cs Tue Apr 06 22:02:23 2021 +0900 +++ b/Test/Example/HelloWorld/HelloWorldCodeGear.cs Tue Apr 13 18:25:15 2021 +0900 @@ -4,7 +4,7 @@ namespace Christie_net.Test.Example.HelloWorld { public class HelloWorldCodeGear : CodeGear { - [Take] public string helloWorld; + [Take] string helloWorld; public override void Run(CodeGearManager cgm) { Console.Write(helloWorld + " ");
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test/RewritingTest/AsynchronousSocketListener.cs Tue Apr 13 18:25:15 2021 +0900 @@ -0,0 +1,160 @@ +using System; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading; + +// State object for reading client data asynchronously +public class StateObject +{ + // Size of receive buffer. + public const int BufferSize = 1024; + + // Receive buffer. + public byte[] buffer = new byte[BufferSize]; + + // Received data string. + public StringBuilder sb = new StringBuilder(); + + // Client socket. + public Socket workSocket = null; +} + +public class AsynchronousSocketListener +{ + // Thread signal. + public static ManualResetEvent allDone = new ManualResetEvent(false); + + public AsynchronousSocketListener() + { + } + + public static void StartListening() + { + // Establish the local endpoint for the socket. + // The DNS name of the computer + // running the listener is "host.contoso.com". + IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); + IPAddress ipAddress = ipHostInfo.AddressList[0]; + IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); + + // Create a TCP/IP socket. + Socket listener = new Socket(ipAddress.AddressFamily, + SocketType.Stream, ProtocolType.Tcp ); + + // Bind the socket to the local endpoint and listen for incoming connections. + try { + listener.Bind(localEndPoint); + listener.Listen(100); + + while (true) { + // Set the event to nonsignaled state. + allDone.Reset(); + + // Start an asynchronous socket to listen for connections. + Console.WriteLine("Waiting for a connection..."); + listener.BeginAccept( + new AsyncCallback(AcceptCallback), + listener ); + + // Wait until a connection is made before continuing. + allDone.WaitOne(); + } + + } catch (Exception e) { + Console.WriteLine(e.ToString()); + } + + Console.WriteLine("\nPress ENTER to continue..."); + Console.Read(); + + } + + public static void AcceptCallback(IAsyncResult ar) + { + // Signal the main thread to continue. + allDone.Set(); + + // Get the socket that handles the client request. + Socket listener = (Socket) ar.AsyncState; + Socket handler = listener.EndAccept(ar); + + // Create the state object. + StateObject state = new StateObject(); + state.workSocket = handler; + handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, + new AsyncCallback(ReadCallback), state); + } + + public static void ReadCallback(IAsyncResult ar) + { + String content = String.Empty; + + // Retrieve the state object and the handler socket + // from the asynchronous state object. + StateObject state = (StateObject) ar.AsyncState; + Socket handler = state.workSocket; + + // Read data from the client socket. + int bytesRead = handler.EndReceive(ar); + + if (bytesRead > 0) { + // There might be more data, so store the data received so far. + state.sb.Append(Encoding.ASCII.GetString( + state.buffer, 0, bytesRead)); + + // Check for end-of-file tag. If it is not there, read + // more data. + content = state.sb.ToString(); + if (content.IndexOf("<EOF>") > -1) { + // All the data has been read from the + // client. Display it on the console. + Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", + content.Length, content ); + // Echo the data back to the client. + Send(handler, content); + } else { + // Not all data received. Get more. + handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, + new AsyncCallback(ReadCallback), state); + } + } + } + + private static void Send(Socket handler, String data) + { + // Convert the string data to byte data using ASCII encoding. + byte[] byteData = Encoding.ASCII.GetBytes(data); + + // Begin sending the data to the remote device. + handler.BeginSend(byteData, 0, byteData.Length, 0, + new AsyncCallback(SendCallback), handler); + } + + private static void SendCallback(IAsyncResult ar) + { + try + { + // Retrieve the socket from the state object. + Socket handler = (Socket) ar.AsyncState; + + // Complete sending the data to the remote device. + int bytesSent = handler.EndSend(ar); + Console.WriteLine("Sent {0} bytes to client.", bytesSent); + + handler.Shutdown(SocketShutdown.Both); + handler.Close(); + + } + catch (Exception e) + { + Console.WriteLine(e.ToString()); + } + } + + public static int Main(String[] args) + { + StartListening(); + return 0; + } +} \ No newline at end of file
--- a/Test/RewritingTest/SocketListenerTask.cs Tue Apr 06 22:02:23 2021 +0900 +++ b/Test/RewritingTest/SocketListenerTask.cs Tue Apr 13 18:25:15 2021 +0900 @@ -1,6 +1,7 @@ using System; using System.Net; using System.Net.Sockets; +using System.Text; using System.Threading; using System.Threading.Tasks; @@ -15,15 +16,53 @@ public void Run () { // Thread thread = new Thread (new ThreadStart (MethodThread)); // thread.Start(); - Task task = Task.Run(() => MethodThread()); + string data = null; + byte[] bytes = null; + Task task = Task.Factory.StartNew(() => { + Socket handler = socket.Accept (); + Console.WriteLine ("Accept:" + handler.LocalEndPoint); + + string data = null; + byte[] bytes = null; + while (true) { + bytes = new byte[1024]; + int bytesRec = handler.Receive(bytes); + data = Encoding.ASCII.GetString(bytes, 0, bytesRec); + if (data.IndexOf("<EOF>") > -1) { + break; + } + + Console.WriteLine("Text received : {0}", data); + byte[] msg = Encoding.ASCII.GetBytes(data + ":sv"); + handler.Send(msg); + } + handler.Shutdown(SocketShutdown.Both); + handler.Close(); + }, + TaskCreationOptions.LongRunning); } private void MethodThread() { - Socket listener = socket.Accept (); + Socket handler = socket.Accept (); + Console.WriteLine ("Accept:" + handler.LocalEndPoint); + + string data = null; + byte[] bytes = null; while (true) { - Console.WriteLine ("Accept:" + listener.LocalEndPoint); - Thread.Sleep(1000); + bytes = new byte[1024]; + int bytesRec = handler.Receive(bytes); + data = Encoding.ASCII.GetString(bytes, 0, bytesRec); + if (data.IndexOf("<EOF>") > -1) { + break; + } + + Console.WriteLine("Text received : {0}", data); + byte[] msg = Encoding.ASCII.GetBytes(data + ":sv"); + handler.Send(msg); } + + handler.Shutdown(SocketShutdown.Both); + handler.Close(); } public static void Main () { @@ -31,21 +70,35 @@ IPAddress ipAddress = host.AddressList[0]; IPEndPoint localEndPoint = new IPEndPoint (ipAddress, 11000); + string data = null; + byte[] bytes = null; + try { Socket ss = new Socket (ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); ss.Bind(localEndPoint); ss.Listen(10); + // Socket handler = ss.Accept(); + // while (true) { - // Console.WriteLine("Accept:" + listener.LocalEndPoint); + // bytes = new byte[1024]; + // int bytesRec = handler.Receive(bytes); + // data = Encoding.ASCII.GetString(bytes, 0, bytesRec); + // if (data.IndexOf("<EOF>") > -1) { + // break; + // } + // + // Console.WriteLine("Text received : {0}", data); + // byte[] msg = Encoding.ASCII.GetBytes(data + ":sv"); + // handler.Send(msg); + // // } - + SocketListenerTask newThread = new SocketListenerTask(ss); newThread.Run (); - // Console.WriteLine("fin"); - // listener.Shutdown(SocketShutdown.Both); - // listener.Close(); + Console.WriteLine("fin"); + } catch (Exception e) { Console.WriteLine (e.ToString ()); }
--- a/daemon/AcceptThread.cs Tue Apr 06 22:02:23 2021 +0900 +++ b/daemon/AcceptThread.cs Tue Apr 13 18:25:15 2021 +0900 @@ -22,7 +22,6 @@ socket = soc.Accept(); socket.NoDelay = true; Console.WriteLine("Accept " + socket.LocalEndPoint + ":" + ((IPEndPoint)socket.LocalEndPoint).Port); - Connection connection = new Connection(socket, cgm); string key = "accept" + counter; @@ -37,6 +36,8 @@ () => outbound.Run(), TaskCreationOptions.LongRunning); counter++; + + Console.WriteLine("fin"); } catch (Exception e) { Console.WriteLine(e.StackTrace); }
--- a/daemon/ChristieDaemon.cs Tue Apr 06 22:02:23 2021 +0900 +++ b/daemon/ChristieDaemon.cs Tue Apr 13 18:25:15 2021 +0900 @@ -28,6 +28,12 @@ socket.Bind(localEndPoint); socket.Listen((int)SocketOptionName.MaxConnections); + // Socket ss = null; + // ss = socket.Accept(); + // ss.NoDelay = true; + // Console.WriteLine("Accept " + ss.LocalEndPoint + ":" + ((IPEndPoint)ss.LocalEndPoint).Port); + + acceptThread = new AcceptThread(socket, cgm); Task.Factory.StartNew( () => acceptThread.Run(),