From: <br...@us...> - 2008-08-14 23:18:12
|
Revision: 322 http://acmcontester.svn.sourceforge.net/acmcontester/?rev=322&view=rev Author: brus07 Date: 2008-08-14 23:18:22 +0000 (Thu, 14 Aug 2008) Log Message: ----------- Move all functionality from Form to special class. Simple refactoring in FormClass. Modified Paths: -------------- ACMServer/trunk/ACMServer/Tester/Form1.Designer.cs ACMServer/trunk/ACMServer/Tester/Form1.cs ACMServer/trunk/ACMServer/Tester/Tester.csproj Added Paths: ----------- ACMServer/trunk/ACMServer/Tester/Library/ ACMServer/trunk/ACMServer/Tester/Library/SocketClientGate.cs Modified: ACMServer/trunk/ACMServer/Tester/Form1.Designer.cs =================================================================== --- ACMServer/trunk/ACMServer/Tester/Form1.Designer.cs 2008-08-12 23:06:49 UTC (rev 321) +++ ACMServer/trunk/ACMServer/Tester/Form1.Designer.cs 2008-08-14 23:18:22 UTC (rev 322) @@ -32,8 +32,6 @@ this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); - this.textBox2 = new System.Windows.Forms.TextBox(); - this.button3 = new System.Windows.Forms.Button(); this.textBox3 = new System.Windows.Forms.TextBox(); this.statusStrip1 = new System.Windows.Forms.StatusStrip(); this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel(); @@ -46,13 +44,12 @@ this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - this.textBox1.Location = new System.Drawing.Point(12, 90); + this.textBox1.Location = new System.Drawing.Point(12, 70); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both; - this.textBox1.Size = new System.Drawing.Size(268, 158); + this.textBox1.Size = new System.Drawing.Size(268, 178); this.textBox1.TabIndex = 0; - this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); // // button1 // @@ -74,25 +71,6 @@ this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // - // textBox2 - // - this.textBox2.Enabled = false; - this.textBox2.Location = new System.Drawing.Point(149, 35); - this.textBox2.Name = "textBox2"; - this.textBox2.Size = new System.Drawing.Size(100, 20); - this.textBox2.TabIndex = 3; - // - // button3 - // - this.button3.Enabled = false; - this.button3.Location = new System.Drawing.Point(174, 61); - this.button3.Name = "button3"; - this.button3.Size = new System.Drawing.Size(75, 23); - this.button3.TabIndex = 4; - this.button3.Text = "Send"; - this.button3.UseVisualStyleBackColor = true; - this.button3.Click += new System.EventHandler(this.button3_Click); - // // textBox3 // this.textBox3.Location = new System.Drawing.Point(129, 9); @@ -129,8 +107,6 @@ this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.statusStrip1); this.Controls.Add(this.textBox3); - this.Controls.Add(this.button3); - this.Controls.Add(this.textBox2); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); @@ -150,8 +126,6 @@ private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; - private System.Windows.Forms.TextBox textBox2; - private System.Windows.Forms.Button button3; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.StatusStrip statusStrip1; private System.Windows.Forms.Timer timer1; Modified: ACMServer/trunk/ACMServer/Tester/Form1.cs =================================================================== --- ACMServer/trunk/ACMServer/Tester/Form1.cs 2008-08-12 23:06:49 UTC (rev 321) +++ ACMServer/trunk/ACMServer/Tester/Form1.cs 2008-08-14 23:18:22 UTC (rev 322) @@ -5,16 +5,16 @@ using System.Drawing; using System.Text; using System.Windows.Forms; -using AcmContester.Library.Connector; using System.IO; using AcmContester.Library.LibraryExtention; using System.Threading; +using AcmContester.Tester.Library; namespace Tester { public partial class Form1 : Form { - static SocketClient socket; + static SocketClientGate socket; public Form1() { @@ -28,8 +28,9 @@ { if (socket == null) { - socket = new SocketClient(textBox3.Text); - socket.onDataArrived += DataArrived; + socket = new SocketClientGate(textBox3.Text); + socket.onAddLogText += AddText; + //socket.onDataArrived += DataArrived; } socket.Connect(); } @@ -48,137 +49,36 @@ socket = null; } } - - private void button3_Click(object sender, EventArgs e) + + delegate void AddTextCallback(string text); + public void AddText(string text) { - if (socket.IsConnected() == false) - MessageBox.Show("Client: Bida"); - socket.Send(textBox2.Text); - } - private void DataArrived(string message) - { - UpdateTextLog("\r\n" + message); - if (SystemMessage(message) == true) - return; - AddWork(message); - } - - bool SystemMessage(string message) - { - char[] chars = new char[1]; - chars[0] = ' '; - string[] mes = message.Split(chars); - if (mes[0] == "test") + if (this.textBox1.InvokeRequired) { - string result = ""; - if (IsBusy() == true) - { - result = "busy"; - } - else - { - result = "free"; - } - string addStr = ""; - for (int i = 1; i < mes.Length; i++) - { - result += " " + mes[i]; - } - socket.Send(result); - return true; + AddTextCallback d = new AddTextCallback(AddText); + this.Invoke(d, new object[] { text }); } - return false; - } - - static string source; - static object working = 0; - Thread thread = new Thread(new ThreadStart(Go)); - bool IsBusy() - { - lock (working) + else { - if ((int)working == 1 || (thread.ThreadState == ThreadState.Running)) + string timeStr = DateTime.Now.ToLongTimeString() + "." + DateTime.Now.Millisecond.ToString(); + textBox1.Text += timeStr + " " + text + Environment.NewLine; + //if (autoScrollCheckBox.Checked) { - return true; ; + textBox1.Select(textBox1.Text.Length, 0); + textBox1.ScrollToCaret(); } } - return false; } - void AddWork(string message) - { - if (IsBusy() == true) - { - UpdateTextLog("\r\n--------- busy ---------"); - return; - } - source = message; - onDataArrived += UpdateTextLog; - thread = new Thread(new ThreadStart(Go)); - thread.Start(); - } - static void Go() - { - lock (working) - { - working = 1; - } - Testing(source); - lock (working) - { - working = 0; - } - } - static void Testing(string message) - { - string result; - try - { - result = Checker.Checker.GetResult(message); - } - catch (Exception ex) - { - Log log = Log.GetLog(); - log.Loging("Form1::DataArrived: Error: " + ex.Message, Log.Priority.ERR); - log.Loging("Form1::DataArrived: Error: " + ex.StackTrace, Log.Priority.ERR); - return; - } - - if (onDataArrived != null) - { - onDataArrived("\r\n" + result); - onDataArrived("\r\n--------- free ---------"); - } - //UpdateTextLog("\r\n" + result); - lock(socket) - socket.Send(result); - } - - - public delegate void DataArrivedDelegate(string s); - public static event DataArrivedDelegate onDataArrived; - delegate void UpdateTextLogCallback(string message); private void UpdateTextLog(string message) { - if (this.textBox1.InvokeRequired) - { - UpdateTextLogCallback d = new UpdateTextLogCallback(UpdateTextLog); - this.Invoke(d, new object[] { message }); - } - else - { - textBox1.Text += message; - textBox1.Select(textBox1.Text.Length, 0); - textBox1.ScrollToCaret(); - } - + AddText(message); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { - thread.Abort(); Disconnect(); } @@ -207,12 +107,5 @@ UpdateTextLog("\r\n\tLogFile can't open. Working without logging to LogFile."); } } - - private void textBox1_TextChanged(object sender, EventArgs e) - { - textBox1.Select(textBox1.Text.Length, 0); - textBox1.ScrollToCaret(); - } - } } \ No newline at end of file Property changes on: ACMServer/trunk/ACMServer/Tester/Library ___________________________________________________________________ Added: tsvn:logminsize + 5 Added: ACMServer/trunk/ACMServer/Tester/Library/SocketClientGate.cs =================================================================== --- ACMServer/trunk/ACMServer/Tester/Library/SocketClientGate.cs (rev 0) +++ ACMServer/trunk/ACMServer/Tester/Library/SocketClientGate.cs 2008-08-14 23:18:22 UTC (rev 322) @@ -0,0 +1,163 @@ +using System; +using AcmContester.Library.Connector; +using System.Threading; +using AcmContester.Library.LibraryExtention; +using AcmContester.Library.LibraryExtention.Data; + +namespace AcmContester.Tester.Library +{ + class SocketClientGate: SocketClient + { + public delegate void DataArrivedDelegate(string s); + public event DataArrivedDelegate onDataArrived; + + public SocketClientGate(string IP) + : base(IP) + { + base.onDataArrived +=new SocketClient.DataArrivedDelegate(SocketClientGate_onDataArrived); + } + + void SocketClientGate_onDataArrived(string message) + { + if (SystemMessage(message) == true) + return; + OnAddLogText("Receive: "+message); + //OnDataArrived(message); + AddWork(message); + } + + public new void Disconnect() + { + if (thread != null) + thread.Abort(); + base.Disconnect(); + } + + + private void OnDataArrived(string message) + { + if (onDataArrived != null) + { + onDataArrived(message); + } + } + + + private bool SystemMessage(string message) + { + char[] chars = new char[1]; + chars[0] = ' '; + string[] mes = message.Split(chars); + if (mes[0] == "test") + { + OnAddLogText("_sys_Receive: " + message); + string result = ""; + if (IsBusy() == true) + { + result = "busy"; + } + else + { + result = "free"; + } + for (int i = 1; i < mes.Length; i++) + { + result += " " + mes[i]; + } + OnAddLogText("\t_sys_Send: " + result); + base.Send(result); + return true; + } + return false; + } + + string source = ""; + object working = 0; + Thread thread = new Thread(new ParameterizedThreadStart(Go)); + private bool IsBusy() + { + lock (working) + { + if ((int)working == 1 || (thread.ThreadState == ThreadState.Running)) + { + return true; + } + } + return false; + } + + private void AddWork(string message) + { + if (IsBusy() == true) + { + OnAddLogText("--------- busy ---------"); + return; + } + + //TODO: tut potribno stavyty lock + // a znimaty vze u samomu Thread'i + // (dyv. na inshe TODO:) + lock(source) + source = message; + + //TODO: potribno jakos obnulyty status thread, + // tak shob ne potribno bulo stvorjuvaty novyj + thread = new Thread(new ParameterizedThreadStart(Go)); + thread.Start(this); + } + + static void Go(object data) + { + if (data is SocketClient == false) + throw new ArgumentException(); + + SocketClientGate gate = (SocketClientGate)data; + + lock (gate.working) + gate.working = 1; + + string copySource; + //TODO: tut potribno znimaty postavlenyj lock + // toj lock postavlenyj u potoci sho vyklykav cej + lock (gate.source) + copySource = gate.source; + gate.Run(copySource); + + lock (gate.working) + gate.working = 0; + } + + void Run(string message) + { + string result; + try + { + result = Checker.Checker.GetResult(message); + } + catch (Exception ex) + { + Log log = Log.GetLog(); + log.Loging("Form1::DataArrived: Error: " + ex.Message, Log.Priority.ERR); + log.Loging("Form1::DataArrived: Error: " + ex.StackTrace, Log.Priority.ERR); + return; + } + + //TODO: ne znaju chy tut potriben lcok + lock (this) + { + Result res = Result.CreateFromXml(result); + OnAddLogText("\tSend: " + res.Submit.id + " result - " + res.res); + this.Send(result); + } + } + + + public delegate void AddLogTextDelegate(string s); + public event AddLogTextDelegate onAddLogText; + private void OnAddLogText(string message) + { + if (onAddLogText != null) + onAddLogText(message); + } + } +} Modified: ACMServer/trunk/ACMServer/Tester/Tester.csproj =================================================================== --- ACMServer/trunk/ACMServer/Tester/Tester.csproj 2008-08-12 23:06:49 UTC (rev 321) +++ ACMServer/trunk/ACMServer/Tester/Tester.csproj 2008-08-14 23:18:22 UTC (rev 322) @@ -66,6 +66,7 @@ <DependentUpon>Settings.settings</DependentUpon> <DesignTimeSharedInput>True</DesignTimeSharedInput> </Compile> + <Compile Include="Library\SocketClientGate.cs" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\Checker\Checker.csproj"> @@ -76,6 +77,10 @@ <Project>{211DD6A5-2D73-439E-8722-ED2C89ED1DDB}</Project> <Name>Connector</Name> </ProjectReference> + <ProjectReference Include="..\Library\Data\Data.csproj"> + <Project>{30C0EFA3-36A8-4C6F-8FEC-28F771D4933F}</Project> + <Name>Data</Name> + </ProjectReference> <ProjectReference Include="..\Library\LibraryExtention\LibraryExtention.csproj"> <Project>{A8135069-F8BA-4E5D-835F-3FF3F350AA5D}</Project> <Name>LibraryExtention</Name> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |