Exemplo n.º 1
0
Arquivo: Program.cs Projeto: qcjwq/MEF
        //http://blog.csdn.net/yiruoyun/article/details/6580238
        static void Main(string[] args)
        {
            //1、指定装配
            //var container = new CompositionContainer();
            //var batch = new CompositionBatch();
            //var client = new Client();
            //batch.AddPart(new StringProvider1());
            //batch.AddPart(client);
            //container.Compose(batch);

            //2、通过程序集
            //var container = new CompositionContainer(new AssemblyCatalog(typeof(Client).Assembly));
            //var client = new Client();
            //container.SatisfyImportsOnce(client);

            //3、DirectoryCatalog,该目标路径下的所有程序集都会被遍历查询
            //var container = new CompositionContainer(new DirectoryCatalog(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "*.exe"));
            //var client = new Client();
            //container.SatisfyImportsOnce(client);

            //4、程序集和DirectoryCatalog相结合
            IClient client = new Client();
            var catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new AssemblyCatalog(typeof(IClient).Assembly));
            var container = new CompositionContainer(catalog);
            container.ComposeParts(client);

            client.Print();

            Console.ReadKey();
        }
Exemplo n.º 2
0
        /// <summary>
        /// The object creates all the hashtables that will hold the information about the player
        /// and the connection. Also who is in what lobby.
        /// We then start listening for players and it will not go past AcceptTcpClient()
        /// unless someone is connecting. When someone connects we create a client object.
        /// </summary>
        public Server()
        {
            players = new Hashtable(50);
            playersByConnect = new Hashtable(50);
            lobbies = new Hashtable(50);
            playerInLobby = new Hashtable(50);

            listener = new TcpListener(IPAddress.Any, 7707);

            Console.WriteLine("Listening for players..");
            while(true)
            {
                listener.Start();

                if (listener.Pending())
                {
                    TcpClient connection = listener.AcceptTcpClient();
                    Console.WriteLine("Someone connected");

                    Client client = new Client(connection);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Ajouter/Modifier un client
        /// </summary>
        /// <param name="s"></param>
        private static char enterClient(Client c=null)
        {
            char choix = '\u0000'; //unicode de null
            string _nom=null;
            string _prenom=null;
            string _tel=null;
            string _mail=null;
            char _reponse;
            //pour un abonné
            string _anneeA=null;
            string _solde=null;
            //pour un non abonné
            string _numCB=null;
            string _dateValide=null;
            string _cvc=null;

            bool ajout = false;
            bool abonne = false;

            do
            {
                Console.Clear();

                if (c == null)
                    ajout = true;

                if (ajout)

                {
                    //ici, on crée un nouveau client
                    Console.WriteLine(Environment.NewLine);
                    Console.WriteLine("--- Nouveau client ---");
                    Console.WriteLine(Environment.NewLine);
                    /*
                     * saisir les informations
                     */
                    Console.Write("Nom : ");
                    _nom = Console.ReadLine();
                    Console.Write("Prenom : ");
                    _prenom = Console.ReadLine();
                    Console.Write("Telephone : ");
                    _tel = Console.ReadLine();
                    Console.Write("Mail : ");
                    _mail = Console.ReadLine();
                    do
                    {
                        Console.WriteLine(Environment.NewLine);
                        Console.Write("Abonné (O/N) : ");
                        _reponse = Console.ReadKey().KeyChar;
                    } while (_reponse != 'o' && _reponse != 'O' && _reponse != 'n' && _reponse != 'N');
                    switch (_reponse)
                    {
                        case 'o':
                        case 'O':
                            abonne = true;
                            Console.Write("Année abonnement : ");
                            _anneeA = Console.ReadLine();
                            Console.Write("Solde : ");
                            _solde = Console.ReadLine();
                            break;
                        case 'n':
                        case 'N' :
                            abonne = false;
                            Console.Write("N° CB : ");
                            _numCB = Console.ReadLine();
                            Console.Write("Expire fin (JJ/MM/YYYY) : ");
                            _dateValide = Console.ReadLine();
                            Console.Write("Cryptogramme (3 chiffres) : ");
                            _cvc = Console.ReadLine();
                            break;
                    }
                    Console.WriteLine(Environment.NewLine);
                }
                else
                {
                    //ici, on modifie un client existant
                    //ici, on crée un nouveau client
                    Console.WriteLine(Environment.NewLine);
                    Console.WriteLine("--- Modification client ---");
                    Console.WriteLine(Environment.NewLine);
                    /*
                     * saisir les informations
                     */
                    Console.Write("Nom ({0}) : ", c.nom);
                    _nom = Console.ReadLine();
                    Console.Write("Prenom ({0}) : ", c.prenom);
                    _prenom = Console.ReadLine();
                    Console.Write("Telephone ({0}) : ", c.tel);
                    _tel = Console.ReadLine();
                    Console.Write("Mail ({0}) : ", c.mail);
                    _mail = Console.ReadLine();
                    if (c is ClientAbonne)
                    {
                        abonne = true;
                        Console.Write("Année abonnement ({0}) : ",((ClientAbonne)c).anneeAbonnement);
                        _anneeA = Console.ReadLine();
                        Console.Write("Solde ({0}): ",((ClientAbonne)c).solde);
                        _solde = Console.ReadLine();
                    }
                    else
                    {
                        abonne = false;
                        Console.Write("N° CB ({0}) : ", ((ClientNonAbonne)c).numCB);
                        _numCB = Console.ReadLine();
                        Console.Write("Expire fin (JJ/MM/YYYY)({0}) : ", ((ClientNonAbonne)c).dateValide);
                        _dateValide = Console.ReadLine();
                        Console.Write("Cryptogramme (3 chiffres)  : ", ((ClientNonAbonne)c).cvc);
                        _cvc = Console.ReadLine();
                    }
                    Console.WriteLine(Environment.NewLine);
                }

                Console.WriteLine("V. valider - P. Retour menu principal");
                choix = Console.ReadKey().KeyChar;
                switch (choix)
                {
                    case 'v':
                    case 'V':
                        try
                        {
                            if (ajout)
                            {
                                if (abonne)
                                {
                                    //ici, on crée un nouvel abonne
                                    ClientAbonne ca = new ClientAbonne();
                                    ca.nom = _nom;
                                    ca.prenom = _prenom;
                                    ca.tel = _tel;
                                    ca.mail = _mail;
                                    ca.anneeAbonnement = int.Parse(_anneeA);
                                    ca.solde = double.Parse(_solde);

                                    //ajout de l'abonné à la liste des clients
                                    fc.clients.Add(ca);
                                }
                                else
                                {
                                    //ici, on crée un nouvel non abonné
                                    ClientNonAbonne cna = new ClientNonAbonne();
                                    cna.nom = _nom;
                                    cna.prenom = _prenom;
                                    cna.tel = _tel;
                                    cna.mail = _mail;
                                    cna.numCB = _numCB;
                                    cna.dateValide = DateTime.Parse(_dateValide);
                                    cna.cvc = int.Parse(_cvc);

                                    //ajout du non abonné à la liste des clients
                                    fc.clients.Add(cna);
                                }
                            }
                            else
                            {
                                if (abonne)
                                {
                                    //ici, on modifie un abonné
                                    ClientAbonne ca = (ClientAbonne)c;
                                    ca.nom = (_nom == null ? ca.nom:_nom);
                                    ca.prenom = (_prenom == null ? ca.prenom : _prenom);
                                    ca.tel = (_tel == null ? ca.tel:_tel);
                                    ca.mail = (_mail == null ? ca.mail:_mail);
                                    ca.anneeAbonnement = (_anneeA == null ? ca.anneeAbonnement:int.Parse(_anneeA));
                                    ca.solde = (_solde == null ? ca.solde : double.Parse(_solde));
                                }
                                else
                                {
                                    //ici, on modifie un non abonné
                                    ClientNonAbonne cna = (ClientNonAbonne)c;
                                    cna.nom = (_nom == null ? cna.nom : _nom);
                                    cna.prenom = (_prenom == null ? cna.prenom : _prenom);
                                    cna.tel = (_tel == null ? cna.tel : _tel);
                                    cna.mail = (_mail == null ? cna.mail : _mail);
                                    cna.numCB = (_numCB==null ? cna.numCB:_numCB);
                                    cna.dateValide = (_dateValide==null ? cna.dateValide : DateTime.Parse(_dateValide));
                                    cna.cvc = (_cvc==null ? cna.cvc : int.Parse(_cvc));
                                }
                            }

                        }
                        catch (Exception)
                        {
                            Console.WriteLine("Erreur lors de la création/modification du client.");
                        }

                        break;
                }
            } while (choix != 'p' && choix != 'P' && choix != 'v' && choix != 'V');
            return choix;
        }
Exemplo n.º 4
0
        private static void Main(string[] args)
        {
            // Абстрактная фабрика № 1
            CarFactory bmwCar = new BmwFactory();
            var c1 = new Client(bmwCar);
            c1.Run();
            // Абстрактная фабрика № 2
            CarFactory audiFactory = new AudiFactory();
            var c2 = new Client(audiFactory);
            c2.Run();
            Console.WriteLine("");

            //пример TemplateMethod
            var c = new CustomAlgorithm();
            c.DoAlgorithm();
            Console.WriteLine("");

            //пример Bridge
            ClientForBridge.MainForBridge();

            Console.Read();
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            parseParse(File.ReadAllText(@"C:\shuffly\Shuffly-Brain\papa.txt"));

            string channel = "/consolepublisher";
            Console.WriteLine("Initializing...");

            Client client = new Client(new ClientArgs
            {
                // replace with your domain key
                DomainKey = "035d9877-9eaf-45ef-936f-80087fb3a221",
                // replace with your domain name
                DomainName = "localhost",
                Synchronous = true
            });

            Publisher publisher = new Publisher(new PublisherArgs
            {
                // replace with your domain key
                DomainKey = "035d9877-9eaf-45ef-936f-80087fb3a221",
                // replace with your domain name
                DomainName = "localhost",
                Synchronous = true
            });

            client.Connect(new ConnectArgs
            {
                OnSuccess = (successArgs) =>
                {
                    Console.WriteLine("The client connected and has ID " + successArgs.ClientId + ".");
                },
                OnFailure = (failureArgs) =>
                {
                    Console.WriteLine("The client could not connect... " + failureArgs.Exception.Message);
                },
                OnStreamFailure = (streamFailureArgs) =>
                {
                    Console.WriteLine("The client could not stream... " + streamFailureArgs.Exception.Message);
                    Console.WriteLine("The client " + (streamFailureArgs.WillReconnect ? "will" : "will not") + " automatically reconnect." + Environment.NewLine);
                }
            });

            // client is configured as "synchronous", so this
            // won't execute until the connect method is complete
            if (client.State != ClientState.Connected)
            {
                Console.WriteLine();
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("Connected");
            client.Subscribe(new SubscribeArgs
            {
                Channel = channel,
                OnSuccess = (successArgs) =>
                {
                    Console.WriteLine("The client " + (successArgs.IsResubscribe ? "re-" : "") + "subscribed to " + channel + ".");
                },
                OnFailure = (failureArgs) =>
                {
                    Console.WriteLine("The client could not subscribe to " + channel + "... " + failureArgs.Exception.Message);
                },
                OnReceive = (receiveArgs) =>
                {

                    Payload payload = JSON.Deserialize<Payload>(receiveArgs.DataJson);

                    Tuple<SpokeQuestion, string, GameBoard> vf = null;
                    switch (payload.Type)
                    {
                        case SpokeMessageType.AskQuestion:
                            return;
                        case SpokeMessageType.JoinGame:
                            playersInGame.Add(receiveArgs.PublishingClient.Id, payload.PlayerName);
                            if (playersInGame.Count == 2)
                            {
                                vf = RunGame.StartGame("sevens", playersInGame);
                                stackTrace = vf.Item2;
                            }
                            else return;
                            break;
                        case SpokeMessageType.AnswerQuestion:
                            vf = RunGame.ResumeGame("sevens", stackTrace, payload.AnswerIndex, playersInGame);
                            stackTrace = vf.Item2;
                            Console.WriteLine(vf.Item1);
                            break;
                        default:
                            throw new ArgumentOutOfRangeException();
                    }

                    PublicationArgs fc;
                    foreach (KeyValuePair<string, string> user in playersInGame)
                    {
                        fc = new PublicationArgs
                        {
                            Publication = new Publication
                            {
                                Channel = channel + "/gamedata/" + user.Value,
                                DataJson = JSON.Serialize(vf.Item3)
                            },
                            OnComplete = (completeArgs) =>
                            {
                                if (completeArgs.Publication.Successful == true)
                                {
                                    Console.WriteLine("The publisher published to " + channel +
                                                      ".");
                                }
                                else
                                {
                                    Console.WriteLine("The publisher could not publish to " +
                                                      channel + "... " +
                                                      completeArgs.Publication.Error);
                                }
                            },
                            OnException = (exceptionArgs) =>
                            {
                                Console.WriteLine("The publisher threw an exception... " +
                                                  exceptionArgs.Exception.Message);
                            }
                        };
                        publisher.Publish(fc);
                    }

                    SpokeQuestion q = vf.Item1;

                    fc = new PublicationArgs
                         {
                             Publication = new Publication
                                           {
                                               Channel = channel + "/" + q.User,
                                               DataJson = JSON.Serialize(new Payload(q.Question, q.Answers))
                                           },
                             OnComplete = (completeArgs) =>
                                          {
                                              if (completeArgs.Publication.Successful == true)
                                              {
                                                  Console.WriteLine("The publisher published to " + channel + ".");
                                              }
                                              else
                                              {
                                                  Console.WriteLine("The publisher could not publish to " + channel + "... " + completeArgs.Publication.Error);
                                              }
                                          },
                             OnException = (exceptionArgs) =>
                                           {
                                               Console.WriteLine("The publisher threw an exception... " + exceptionArgs.Exception.Message);
                                           }
                         };
                    publisher.Publish(fc);

                    //       Console.WriteLine("The client received data... (text: " + payload.Text + ", time: " + payload.Time + ")");
                }
            });

            Console.WriteLine();
            Console.WriteLine("Press Enter to publish text from the publisher.  Press Escape to quit.");

            while (true)
            {
                ConsoleKeyInfo consoleKeyInfo = Console.ReadKey();
                if (consoleKeyInfo.Key == ConsoleKey.Escape)
                {
                    return;
                }
                if (consoleKeyInfo.Key == ConsoleKey.Enter)
                {

                }
            }
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            // Abstract Factory #1
            var factory1 = Create("A");
            var c1 = new Client(factory1);
            c1.run();

            // Abstract Factory #2
            var factory2 = Create("B");
            var c2 = new Client(factory2);
            c2.run();

            Console.Read();
        }