Exemple #1
0
        public static void PuppetPublishCallback(IAsyncResult ar)
        {
            PuppetPublishDelegate del = (PuppetPublishDelegate)((AsyncResult)ar).AsyncDelegate;

            del.EndInvoke(ar);
            return;
        }
Exemple #2
0
        static void ProcessCommand( string commandLine )
        {
            FileParsing.ScriptEventQueue commands = null;

            try {
                commands = FileParsing.PuppetScript.ReadCommand( commandLine );
            }
            catch ( Exception e ) {
                Console.WriteLine( "Error parsing comand" );
            }

            if ( commands != null ) {

                if ( commands.Count() == 0 ) {
                    Console.WriteLine( "Unknown command." );
                }
                while ( !commands.Empty() ) {
                    var command = commands.GetNextCommand();

                    if ( command.type == FileParsing.CommandType.Invalid ) {
                        continue;
                    }

                    LogMessage( command.fullInput );

                    // TODO: Implement all commands
                    if ( command.type == FileParsing.CommandType.Subscribe ) {
                        IPuppetSubscriber sub;
                        subscribers.TryGetValue( command.properties[ 0 ], out sub );
                        if ( sub != null ) {
                            //sub.ForceSubscribe( command.properties[ 1 ] );
                            PuppetSubscribeDelegate del = new PuppetSubscribeDelegate( sub.ForceSubscribe );
                            AsyncCallback remoteCallback = new AsyncCallback( PuppetSubscribeCallback );
                            IAsyncResult remAr = del.BeginInvoke( command.properties[ 1 ], remoteCallback, null );
                        }
                        else {
                            Console.WriteLine( "Invalid subscriber name: \"" + command.properties[ 0 ] + "\" Cannot process subscribe command." );
                        }
                    }
                    else if ( command.type == FileParsing.CommandType.Unsubscribe ) {
                        IPuppetSubscriber sub;
                        subscribers.TryGetValue( command.properties[ 0 ], out sub );
                        if ( sub != null ) {
                            //sub.ForceSubscribe( command.properties[ 1 ] );
                            PuppetSubscribeDelegate del = new PuppetSubscribeDelegate( sub.ForceUnsubscribe );
                            AsyncCallback remoteCallback = new AsyncCallback( PuppetSubscribeCallback );
                            IAsyncResult remAr = del.BeginInvoke( command.properties[ 1 ], remoteCallback, null );
                        }
                        else {
                            Console.WriteLine( "Invalid subscriber name: \"" + command.properties[ 0 ] + "\" Cannot process unsubscribe command." );
                        }
                    }
                    else if ( command.type == FileParsing.CommandType.Publish ) {
                        IPuppetPublisher pub;
                        publishers.TryGetValue(command.properties[0], out pub);

                        //pub.ForcePublish( Int32.Parse( command.properties[ 1 ] ), command.properties[ 2 ], Int32.Parse( command.properties[ 3 ] ) );
                        PuppetPublishDelegate del = new PuppetPublishDelegate( pub.ForcePublish );
                        AsyncCallback remoteCallback = new AsyncCallback( PuppetPublishCallback );
                        IAsyncResult remAr = del.BeginInvoke( Int32.Parse( command.properties[ 1 ] ), command.properties[ 2 ], Int32.Parse( command.properties[ 3 ] ), remoteCallback, null );
                    }
                    else if ( command.type == FileParsing.CommandType.Status ) {
                        foreach(var p in PuppetMaster.processes)
                        {
                            var obj = p.Value;
                            obj.Status();
                        }
                    }
                    else if ( command.type == FileParsing.CommandType.Crash ) {
                        IPuppetProcess proc;
                        processes.TryGetValue( command.properties[0], out proc );
                        if (proc != null) {
                            //proc.Crash();
                            PuppetCrashDelegate del = new PuppetCrashDelegate( proc.Crash );
                            AsyncCallback remoteCallback = new AsyncCallback( PuppetPublishCallback );
                            IAsyncResult remAr = del.BeginInvoke( remoteCallback, null );
                            //remove process after crashing it
                            processes.Remove(command.properties[0]);
                            subscribers.Remove(command.properties[0]);
                            publishers.Remove(command.properties[0]);
                            brokers.Remove(command.properties[0]);
                        }
                        else
                            Console.WriteLine("Invalid process name: \"" + command.properties[0] + "\" Cannot process crash command.");
                    }
                    else if ( command.type == FileParsing.CommandType.Freeze ) {
                        IPuppetProcess proc;
                        processes.TryGetValue(command.properties[0], out proc);
                        if (proc != null)
                        {
                            proc.Freeze();
                        }
                        else
                            Console.WriteLine("Invalid process name: \"" + command.properties[0] + "\" Cannot process freeze command.");

                    }
                    else if ( command.type == FileParsing.CommandType.Unfreeze ) {

                        IPuppetProcess proc;
                        processes.TryGetValue(command.properties[0], out proc);
                        if (proc != null)
                        {
                            proc.Unfreeze();
                        }
                        else
                            Console.WriteLine("Invalid process name: \"" + command.properties[0] + "\" Cannot process unfreeze command.");
                    }
                    else if ( command.type == FileParsing.CommandType.Wait ) {
                        int time = Int32.Parse( command.properties[ 0 ] );
                        Console.WriteLine( "Waiting: " + command.properties[ 0 ] + "ms" );
                        Thread.Sleep( time );
                        Console.WriteLine( "Waited." );
                    }

                    // Test prints TODO: Remove
                    /*Console.Write( "Command: " + command.type.ToString() );
                    Console.Write( " Properties: " );
                    if ( command.properties != null ) {
                        foreach ( string prop in command.properties ) {
                            Console.Write( prop + " " );
                        }
                    }
                    Console.WriteLine();*/
                } // End of while
            } // End of 'if null'
            else {
                Console.WriteLine( "Unrecognized command." );
            }
        }
Exemple #3
0
        static void ProcessCommand(string commandLine)
        {
            FileParsing.ScriptEventQueue commands = null;

            try {
                commands = FileParsing.PuppetScript.ReadCommand(commandLine);
            }
            catch (Exception e) {
                Console.WriteLine("Error parsing comand");
            }

            if (commands != null)
            {
                if (commands.Count() == 0)
                {
                    Console.WriteLine("Unknown command.");
                }
                while (!commands.Empty())
                {
                    var command = commands.GetNextCommand();

                    if (command.type == FileParsing.CommandType.Invalid)
                    {
                        continue;
                    }

                    LogMessage(command.fullInput);

                    // TODO: Implement all commands
                    if (command.type == FileParsing.CommandType.Subscribe)
                    {
                        IPuppetSubscriber sub;
                        subscribers.TryGetValue(command.properties[0], out sub);
                        if (sub != null)
                        {
                            //sub.ForceSubscribe( command.properties[ 1 ] );
                            PuppetSubscribeDelegate del            = new PuppetSubscribeDelegate(sub.ForceSubscribe);
                            AsyncCallback           remoteCallback = new AsyncCallback(PuppetSubscribeCallback);
                            IAsyncResult            remAr          = del.BeginInvoke(command.properties[1], remoteCallback, null);
                        }
                        else
                        {
                            Console.WriteLine("Invalid subscriber name: \"" + command.properties[0] + "\" Cannot process subscribe command.");
                        }
                    }
                    else if (command.type == FileParsing.CommandType.Unsubscribe)
                    {
                        IPuppetSubscriber sub;
                        subscribers.TryGetValue(command.properties[0], out sub);
                        if (sub != null)
                        {
                            //sub.ForceSubscribe( command.properties[ 1 ] );
                            PuppetSubscribeDelegate del            = new PuppetSubscribeDelegate(sub.ForceUnsubscribe);
                            AsyncCallback           remoteCallback = new AsyncCallback(PuppetSubscribeCallback);
                            IAsyncResult            remAr          = del.BeginInvoke(command.properties[1], remoteCallback, null);
                        }
                        else
                        {
                            Console.WriteLine("Invalid subscriber name: \"" + command.properties[0] + "\" Cannot process unsubscribe command.");
                        }
                    }
                    else if (command.type == FileParsing.CommandType.Publish)
                    {
                        IPuppetPublisher pub;
                        publishers.TryGetValue(command.properties[0], out pub);

                        //pub.ForcePublish( Int32.Parse( command.properties[ 1 ] ), command.properties[ 2 ], Int32.Parse( command.properties[ 3 ] ) );
                        PuppetPublishDelegate del            = new PuppetPublishDelegate(pub.ForcePublish);
                        AsyncCallback         remoteCallback = new AsyncCallback(PuppetPublishCallback);
                        IAsyncResult          remAr          = del.BeginInvoke(Int32.Parse(command.properties[1]), command.properties[2], Int32.Parse(command.properties[3]), remoteCallback, null);
                    }
                    else if (command.type == FileParsing.CommandType.Status)
                    {
                        foreach (var p in PuppetMaster.processes)
                        {
                            var obj = p.Value;
                            obj.Status();
                        }
                    }
                    else if (command.type == FileParsing.CommandType.Crash)
                    {
                        IPuppetProcess proc;
                        processes.TryGetValue(command.properties[0], out proc);
                        if (proc != null)
                        {
                            //proc.Crash();
                            PuppetCrashDelegate del            = new PuppetCrashDelegate(proc.Crash);
                            AsyncCallback       remoteCallback = new AsyncCallback(PuppetPublishCallback);
                            IAsyncResult        remAr          = del.BeginInvoke(remoteCallback, null);
                            //remove process after crashing it
                            processes.Remove(command.properties[0]);
                            subscribers.Remove(command.properties[0]);
                            publishers.Remove(command.properties[0]);
                            brokers.Remove(command.properties[0]);
                        }
                        else
                        {
                            Console.WriteLine("Invalid process name: \"" + command.properties[0] + "\" Cannot process crash command.");
                        }
                    }
                    else if (command.type == FileParsing.CommandType.Freeze)
                    {
                        IPuppetProcess proc;
                        processes.TryGetValue(command.properties[0], out proc);
                        if (proc != null)
                        {
                            proc.Freeze();
                        }
                        else
                        {
                            Console.WriteLine("Invalid process name: \"" + command.properties[0] + "\" Cannot process freeze command.");
                        }
                    }
                    else if (command.type == FileParsing.CommandType.Unfreeze)
                    {
                        IPuppetProcess proc;
                        processes.TryGetValue(command.properties[0], out proc);
                        if (proc != null)
                        {
                            proc.Unfreeze();
                        }
                        else
                        {
                            Console.WriteLine("Invalid process name: \"" + command.properties[0] + "\" Cannot process unfreeze command.");
                        }
                    }
                    else if (command.type == FileParsing.CommandType.Wait)
                    {
                        int time = Int32.Parse(command.properties[0]);
                        Console.WriteLine("Waiting: " + command.properties[0] + "ms");
                        Thread.Sleep(time);
                        Console.WriteLine("Waited.");
                    }

                    // Test prints TODO: Remove

                    /*Console.Write( "Command: " + command.type.ToString() );
                     * Console.Write( " Properties: " );
                     * if ( command.properties != null ) {
                     *  foreach ( string prop in command.properties ) {
                     *      Console.Write( prop + " " );
                     *  }
                     * }
                     * Console.WriteLine();*/
                } // End of while
            }     // End of 'if null'
            else
            {
                Console.WriteLine("Unrecognized command.");
            }
        } // End of function