using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Net; using System.Net.Sockets; using System.IO; using System.Threading; namespace DCAAgent { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private NetworkStream output; private int usedPort = 0; private Thread readThread = null; private static bool connected = false; //private System.Windows.Forms.RichTextBox richTextBox1 = new RichTextBox( ); private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.MenuItem menuItem3; private System.Windows.Forms.StatusBar statusBar1; private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Panel panel2; private System.Windows.Forms.Panel panel4; private System.Windows.Forms.ListBox listBox1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); this.textBox1.KeyPress += new KeyPressEventHandler(this.textBox1_KeyPress); Connect( ); } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.menuItem2 = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System.Windows.Forms.MenuItem(); this.statusBar1 = new System.Windows.Forms.StatusBar(); this.panel1 = new System.Windows.Forms.Panel(); this.panel2 = new System.Windows.Forms.Panel(); this.panel4 = new System.Windows.Forms.Panel(); this.listBox1 = new System.Windows.Forms.ListBox(); this.panel2.SuspendLayout(); this.SuspendLayout(); // // textBox1 // this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.textBox1.Location = new System.Drawing.Point(16, 0); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(424, 22); this.textBox1.TabIndex = 0; this.textBox1.Text = ""; this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); // // button1 // this.button1.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right); this.button1.Location = new System.Drawing.Point(456, 0); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(64, 24); this.button1.TabIndex = 1; this.button1.Text = "assert"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right); this.button2.Location = new System.Drawing.Point(536, 0); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(64, 24); this.button2.TabIndex = 2; this.button2.Text = "retract"; this.button2.Click += new System.EventHandler(this.button2_Click); // // mainMenu1 // this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1}); // // menuItem1 // this.menuItem1.Index = 0; this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem2, this.menuItem3}); this.menuItem1.Text = "Connection"; // // menuItem2 // this.menuItem2.Index = 0; this.menuItem2.Shortcut = System.Windows.Forms.Shortcut.CtrlQ; this.menuItem2.Text = "Connect"; this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click); // // menuItem3 // this.menuItem3.Index = 1; this.menuItem3.Shortcut = System.Windows.Forms.Shortcut.CtrlS; this.menuItem3.Text = "Disconnect"; this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click); // // statusBar1 // this.statusBar1.Location = new System.Drawing.Point(0, 369); this.statusBar1.Name = "statusBar1"; this.statusBar1.Size = new System.Drawing.Size(616, 24); this.statusBar1.TabIndex = 6; this.statusBar1.Text = "Disconnected"; // // panel1 // this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom; this.panel1.Location = new System.Drawing.Point(0, 361); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(616, 8); this.panel1.TabIndex = 7; // // panel2 // this.panel2.Controls.AddRange(new System.Windows.Forms.Control[] { this.button2, this.button1, this.textBox1}); this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom; this.panel2.Location = new System.Drawing.Point(0, 337); this.panel2.Name = "panel2"; this.panel2.Size = new System.Drawing.Size(616, 24); this.panel2.TabIndex = 8; // // panel4 // this.panel4.Dock = System.Windows.Forms.DockStyle.Bottom; this.panel4.Location = new System.Drawing.Point(0, 329); this.panel4.Name = "panel4"; this.panel4.Size = new System.Drawing.Size(616, 8); this.panel4.TabIndex = 9; // // listBox1 // this.listBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.listBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.listBox1.ItemHeight = 16; this.listBox1.Location = new System.Drawing.Point(16, 8); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(584, 308); this.listBox1.TabIndex = 10; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(616, 393); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.listBox1, this.panel4, this.panel2, this.panel1, this.statusBar1}); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Menu = this.mainMenu1; this.Name = "Form1"; this.Text = "Agent"; this.Load += new System.EventHandler(this.Form1_Load); this.panel2.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Form1 form = new Form1(); Application.Run( form ); } protected void Client_Closing( object sender, CancelEventArgs e ) { System.Environment.Exit( System.Environment.ExitCode ); } internal void SendMsg( string msg ) { TcpClient client; try { client = new TcpClient( ); client.Connect( "localhost", 6000 ); output = client.GetStream( ); writer = new BinaryWriter( output ); writer.Write( msg ); writer.Close( ); output.Close( ); client.Close( ); } catch( Exception e ) { MessageBox.Show( "Exception sending " + msg + "\n" + e ); } } private void DumpFacts( BinaryWriter br, string facts ) { foreach( string s in listBox1.Items ) br.Write( s ); //int pos; //while( ( pos = facts.IndexOf( "\n" ) ) != -1 ) { // br.Write( facts.Substring( 0, pos ) ); // facts = facts.Substring( pos + 1 ); //} //if( facts.Length > 0 ) // br.Write( facts ); } static object connectionLock = new object( ); private string GetAllFacts( ListBox lv ) { string output = ""; //return lv.ToString( ); foreach( string s in lv.Items ) output += s; return output; } private void Connect( ) { /* Protocol: connect. . . . !claims. : conflicts. : . % first conflict : . : !conflicts. resolve( n ). % which one to use. : conflicts. : . % first conflict : . : !conflicts. resolve( n ). % which one to use. !ok */ lock( connectionLock ) { if( connected ) return; readThread = new Thread( new ThreadStart( TryRunAgent ) ); readThread.IsBackground = true; readThread.Start( ); TcpClient client = null; try { client = new TcpClient( ); client.Connect( "localhost", 6000 ); output = client.GetStream( ); backWriter = new BinaryWriter( output ); backReader = new BinaryReader( output ); backWriter.Write( "connect." ); DumpFacts( backWriter, GetAllFacts( this.listBox1 ) ); // this.richTextBox1.Text ); backWriter.Write( "!claims." ); while( backReader.ReadString( ).Equals( "conflicts." ) ) { ArrayList conflicts = new ArrayList( ); string next; while( !( next = backReader.ReadString( ) ).Equals( "!conflicts." ) ) conflicts.Add( next ); Form2 form2 = new Form2( this, conflicts ); form2.ShowDialog( ); int index = form2.GetSelectedIndex( ); backWriter.Write( index.ToString( ) ); } } catch( Exception e ) { readThread.Abort( ); MessageBox.Show( "Failed to talk to server." ); return; } finally { try { backWriter.Close( ); } catch { } try { backReader.Close( ); } catch { } try { output.Close( ); } catch { } try { client.Close( ); } catch { } } connected = true; this.statusBar1.Text = "Connected"; } } private void Disconnect( ) { lock( connectionLock ) { if( !connected ) return; if( readThread != null ) readThread.Abort( ); try { reader.Close( ); writer.Close( ); socketStream.Close( ); connection.Close( ); } catch { } connected = false; this.statusBar1.Text = "Disconnected"; readThread = null; Thread.Sleep( 1000 ); } } private void button1_Click(object sender, System.EventArgs e) { // assert lock( connectionLock ) { string toAssert = textBox1.Text; if( textBox1.Text.Equals( "" ) ) return; if( textBox1.Text.EndsWith( "." ) ) toAssert = textBox1.Text.Substring( 0, textBox1.Text.Length - 1 ); else toAssert = textBox1.Text; if( connected ) SendMsg( "assert( " + toAssert + " )." ); else this.listBox1.Items.Add( toAssert + "." ); textBox1.Text = ""; } } private string RemoveLine( string text, string line ) { int pos = text.IndexOf( line ); if( pos == -1 ) return null; return text.Substring( 0, pos ) + text.Substring( pos + line.Length + 1 ); } private void button2_Click(object sender, System.EventArgs e) { // retract lock( connectionLock ) { string toRetract = (string)( this.listBox1.SelectedItem ); if( toRetract == null ) return; toRetract = toRetract.Substring( 0, toRetract.Length - 1 ); //if( textBox1.Text.Equals( "" ) ) // return; //string toRetract = textBox1.Text; if( connected ) SendMsg( "retract( " + toRetract + " )." ); else { try { BroadcastRetract( textBox1.Text + "." ); } catch { } } textBox1.Text = ""; } } private void menuItem2_Click(object sender, System.EventArgs e) { // connect Connect( ); } private void menuItem3_Click(object sender, System.EventArgs e) { // disconnect Disconnect( ); } public void TryRunAgent( ) { if( !RunAgent( 6001 ) ) if( !RunAgent( 6002 ) ) if( !RunAgent( 6003 ) ) if( !RunAgent( 6004 ) ) MessageBox.Show( "Too many agents!" ); } Socket connection = null; NetworkStream socketStream = null; BinaryWriter backWriter = null; BinaryReader backReader = null; BinaryWriter writer = null; BinaryReader reader = null; public bool RunAgent( int port ) { TcpListener listener = null; while( true ) { try { listener = new TcpListener( port ); listener.Start( ); usedPort = port; connection = listener.AcceptSocket( ); socketStream = new NetworkStream( connection ); writer = new BinaryWriter( socketStream ); reader = new BinaryReader( socketStream ); } catch( Exception e ) { listener.Stop( ); return false; } try { string str = reader.ReadString( ); if( str.Equals( "broadcast_all." ) ) { string next; while( !( next = reader.ReadString( ) ).Equals( "end." ) ) BroadcastAssert( Strip( next ) ); } else if( str.StartsWith( "broadcast_assert" ) ) { BroadcastAssert( Strip( str ) ); } else if( str.StartsWith( "broadcast_retract" ) ) { BroadcastRetract( Strip( str ) ); } else if( str.Equals( "all_retract." ) ) BroadcastAllRetract( ); } catch( Exception e ) { MessageBox.Show( e.ToString( ) ); return true; } finally { try { reader.Close( ); } catch { } try { writer.Close( ); } catch { } try { socketStream.Close( ); } catch { } try { connection.Close( ); } catch { } try { listener.Stop( ); } catch { } } } } public void BroadcastAssert( string sin ) { this.listBox1.Items.Add( sin ); } public void BroadcastRetract( string sin ) { int i = this.listBox1.Items.IndexOf( sin ); this.listBox1.Items.RemoveAt( i ); //string text = richTextBox1.Text; //int i = text.IndexOf( sin ); //string newText = text.Substring( 0, i ) + // text.Substring( i + sin.Length + 1 ); //this.richTextBox1.Text = newText; } public void BroadcastAllRetract( ) { this.listBox1.Items.Clear( ); } public static string Strip( string sin ) { int i = sin.IndexOf( "(" ); int j = sin.LastIndexOf( ")" ); return sin.Substring( i + 2, j - i - 3 ); } private void Form1_Load(object sender, System.EventArgs e) { } private void textBox1_TextChanged(object sender, System.EventArgs e) { } private void textBox1_KeyPress( object sender, KeyPressEventArgs e ) { KeyPressEventArgs kpea = (KeyPressEventArgs)e; char c = kpea.KeyChar; if( c == (char)13 ) this.button1_Click( sender, e ); } private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { } private void panel3_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { } private void panel3_Paint_1(object sender, System.Windows.Forms.PaintEventArgs e) { } } }