Пример #1
0
        /// <summary>
        /// Create the TcpLister listening at the IP adress 127.0.0.1 and at the port choose at the initilisation of the server.
        /// Create all TopicServer for each Topic in the database.
        /// Then listen for new TcpClient and give it to a new ServerClientListener.
        /// </summary>
        public void Start()
        {
            TcpListener _listener = new TcpListener(new System.Net.IPAddress(new byte[] { 127, 0, 0, 1 }), _port);

            _listener.Start();

            Console.WriteLine("lauching server");

            Dictionary <string, Topic> topics = TopicService.getAll();

            foreach (KeyValuePair <string, Topic> topic in topics)
            {
                ServerTopic topicServer = new ServerTopic(topic.Value);
                this._serverTopics.Add(topic.Key, topicServer);
                new Thread(topicServer.start).Start();
            }

            while (!terminated)
            {
                TcpClient connection = _listener.AcceptTcpClient();

                Console.WriteLine("connection etablie avec : " + connection.Client.RemoteEndPoint);
                ServerClientListener listener = new ServerClientListener(this, connection);
                this._serverListeners.Add(listener);

                new Thread(listener.HandlingConnection).Start();
            }
        }
        public ServerClientTopicListener(Topic topic, TcpClient connection, ServerTopic topicServer)
        {
            this._topic = topic;
            this._user  = null;

            this._connection   = connection;
            this._serverSource = topicServer;
        }
        private void HandlingCreation(Creation c)
        {
            Console.WriteLine(c);

            Topic new_topic = TopicService.add(c);

            if (new_topic == null)
            {
                Console.WriteLine("Failed to create new Topic !\n");
                Net.SendServerCommunication(this._connection.GetStream(), new Response(c, new Error(new Exception("Failed to create new Topic !"))));
            }
            else
            {
                Console.WriteLine("New Topic `" + new_topic.Topic_name + "` created !\n");
                Net.SendServerCommunication(this._connection.GetStream(), new Response(c, new_topic));


                Console.WriteLine("Creating new TopicServer for `" + new_topic.Topic_name + "` !\n");
                ServerTopic topicServer = new ServerTopic(new_topic);
                this._server.serverTopics.Add(new_topic.Topic_name, topicServer);
                new Thread(topicServer.start).Start();
            }
        }