Exemple #1
0
        public void HandleConnection()
        {
            // int recv;
            TcpClient client = threadListner.AcceptTcpClient();

            byte[] data = new byte[client.ReceiveBufferSize];
            NetworkStream stream = client.GetStream();

            try
            {
                /* NegotiateStream nStream = new NegotiateStream(stream);

                nStream.AuthenticateAsServer((NetworkCredential)CredentialCache.DefaultCredentials,
                                             ProtectionLevel.EncryptAndSign,
                                             TokenImpersonationLevel.Identification);
                                             */
            }
            catch (IOException e)
            {
                Console.WriteLine(e);
            }
            catch (AuthenticationException e)
            {
                Console.WriteLine(e);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }



            StreamReader reader = new StreamReader(stream);
            StreamWriter writer = new StreamWriter(stream);
            writer.AutoFlush = true;
            writer.NewLine = "\n";
            connections++;

            Console.WriteLine("New client accepted. Connected clients: {0}", connections);

            string line;
            string [] command;
            do
            {
                try
                {
                    line = reader.ReadLine();

                    if (line != null)
                    {
                        command = line.Split(';');

                        switch (command[0])
                        {
                            case "GETALL":
                                {
                                    Console.WriteLine("Received {0} command", command[0]);

                                    foreach (ServerElement s in servers)
                                    {
                                        writer.WriteLine(s.command);
                                    }
                                    
                                    Console.WriteLine("Sent {0} servers", servers.Count);

                                    foreach (SiteElement s in sites)
                                    {
                                        writer.WriteLine(s.command);
                                    }
                                    
                                    Console.WriteLine("Sent {0} sites", sites.Count);

                                    foreach (CityElement c in cities)
                                    {
                                        writer.WriteLine(c.command);
                                    }
                                    Console.WriteLine("Sent {0} cities", cities.Count);

                                    foreach (GalaxyElement g in galaxies)
                                    {
                                        writer.WriteLine(g.command);
                                    }
                                    
                                    Console.WriteLine("Sent {0} galaxies", galaxies.Count);
                                    foreach (DomainElement d in domains)
                                    {
                                        writer.WriteLine(d.command);
                                    }
                                    
                                    Console.WriteLine("Sent {0} domains", domains.Count);
                                    foreach (CloudElement v in VMs)
                                    {
                                        writer.WriteLine(v.command);
                                    }
                                    

                                    Console.WriteLine("Sent {0} VMs", VMs.Count);
                                    foreach (CloudSiteElement c in cloudSites)
                                    {
                                        writer.WriteLine(c.command);
                                    }
                                    Console.WriteLine("Sent {0} Cloud sites", cloudSites.Count);
                                    writer.WriteLine("<END>");

                                    break;
                                }
                            case "SERVER_1.0":
                                {
                                    Console.WriteLine("Received {0} command", command[0]);
                                    ServerElement server = new ServerElement();
                                    server.data.x = command[1];
                                    server.data.y = command[2];
                                    server.data.rotation = command[3];
                                    server.data.name = command[4];
                                    server.data.role = command[5];
                                    server.data.location = command[6];
                                    server.data.status = command[7];
                                    server.data.type = command[8];
                                    server.data.description = command[9];
                                    server.command = line;
                                    bool found = false;

                                    foreach (ServerElement s in servers)
                                    {
                                        if (s.data.name == server.data.name)
                                        {
                                            servers.Remove(s);
                                            servers.Add(server);
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (!found)
                                    {
                                        servers.Add(server);
                                    }
                                    break;
                                }
                            case "SITE_1.0":
                                {
                                    Console.WriteLine("Received {0} command", command[0]);
                                    SiteElement site = new SiteElement();
                                    site.data.x = command[1];
                                    site.data.y = command[2];
                                    site.data.width = command[3];
                                    site.data.height = command[4];
                                    site.data.rotation = command[5];
                                    site.data.name = command[6];
                                    site.data.location = command[7];
                                    site.data.description = command[8];
                                    site.command = line;
                                    bool found = false;

                                    foreach (SiteElement s in sites)
                                    {
                                        if (s.data.name == site.data.name)
                                        {
                                            sites.Remove(s);
                                            sites.Add(site);
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (!found)
                                    {
                                        sites.Add(site);
                                    }
                                    break;
                                }

                            case "CITY_1.0":
                                {
                                    Console.WriteLine("Received {0} command", command[0]);
                                    CityElement city = new CityElement();
                                    city.data.name = command[1];
                                    city.data.status = command[2];
                                    city.data.altname = command[3];
                                    city.data.description = command[4];
                                    city.command = line;
                                    bool found = false;

                                    foreach (CityElement c in cities)
                                    {
                                        if (c.data.name == city.data.name)
                                        {
                                            cities.Remove(c);
                                            cities.Add(city);
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (!found)
                                    {
                                        cities.Add(city);
                                    }
                                    break;
                                }

                            case "DOMAIN_1.0":
                                {
                                    Console.WriteLine("Received {0} command", command[0]);
                                    DomainElement domain = new DomainElement();
                                    domain.data.name = command[1];
                                    domain.data.status = command[2];
                                    domain.data.description = command[3];
                                    domain.command = line;
                                    bool found = false;

                                    foreach (DomainElement d in domains)
                                    {
                                        if (d.data.name == domain.data.name)
                                        {
                                            domains.Remove(d);
                                            domains.Add(domain);
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (!found)
                                    {
                                        domains.Add(domain);
                                    }
                                    break;
                                }
                            case "CLOUD_1.0":
                                {
                                    Console.WriteLine("Received {0} command", command[0]);
                                    CloudElement vm = new CloudElement();
                                    vm.data.name = command[1];
                                    vm.data.host = command[2];
                                    vm.data.cluster = command[3];
                                    vm.data.site = command[4];
                                    vm.data.status = command[5];
                                    vm.data.description = command[6];
                                    vm.command = line;
                                    bool found = false;

                                    foreach (CloudElement v in VMs)
                                    {
                                        if (v.data.name == vm.data.name)
                                        {
                                            VMs.Remove(v);
                                            VMs.Add(vm);
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (!found)
                                    {
                                        VMs.Add(vm);
                                    }
                                    break;
                                }
                            case "CLOUDINFO_1.0":
                                {
                                    Console.WriteLine("Received {0} command", command[0]);
                                    CloudSiteElement site = new CloudSiteElement();
                                    site.data.name = command[1];
                                    site.data.role = command[2];
                                    site.data.location = command[3];
                                    site.data.site = command[4];
                                    site.data.status = command[5];
                                    site.data.description = command[6];
                                    site.command = line;
                                    bool found = false;

                                    foreach (CloudSiteElement c in cloudSites)
                                    {
                                        if (c.data.site == site.data.site)
                                        {
                                            cloudSites.Remove(c);
                                            cloudSites.Add(site);
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (!found)
                                    {
                                        cloudSites.Add(site);
                                    }
                                    break;
                                }

                            case "GALAXY_1.0":
                                {
                                    Console.WriteLine("Received {0} command", command[0]);

                                    GalaxyElement galaxy = new GalaxyElement();
                                    galaxy.data.name = command[1];
                                    galaxy.data.stars = command[2];
                                    galaxy.data.dust = command[3];
                                    galaxy.data.description = command[4];
                                    galaxy.data.galaxyRadius = command[5];
                                    galaxy.data.coreRadius = command[6];
                                    galaxy.data.angularOffset = command[7];
                                    galaxy.data.coreExcentricity = command[8];
                                    galaxy.data.edgeExcentricity = command[9];

                                    galaxy.command = line;
                                    bool found = false;

                                    foreach (GalaxyElement g in galaxies)
                                    {
                                        if (g.data.name == galaxy.data.name)
                                        {
                                            galaxies.Remove(g);
                                            galaxies.Add(galaxy);
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (!found)
                                    {
                                        galaxies.Add(galaxy);
                                    }
                                    break;
                                }


                            default:
                                break;
                        }
                    }
                }
                catch (SocketException e)
                {
                    Console.WriteLine(e);
                    break;
                }
                catch (IOException e)
                {
                    Console.WriteLine(e);
                    break;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    break;
                }

            } while (line != null);

            Thread.Sleep(300);

            reader.Dispose();
            writer.Dispose();
            stream.Close();
            client.Close();

            if (connections > 0)
                connections--;
            Console.WriteLine("Client disconnected. Connected clients: {0}", connections);
        }
Exemple #2
0
        public void HandleConnection()
        {
            // int recv;
            TcpClient client = threadListner.AcceptTcpClient();

            byte[]        data   = new byte[client.ReceiveBufferSize];
            NetworkStream stream = client.GetStream();

            try
            {
                /* NegotiateStream nStream = new NegotiateStream(stream);
                 *
                 * nStream.AuthenticateAsServer((NetworkCredential)CredentialCache.DefaultCredentials,
                 *                           ProtectionLevel.EncryptAndSign,
                 *                           TokenImpersonationLevel.Identification);
                 */
            }
            catch (IOException e)
            {
                Console.WriteLine(e);
            }
            catch (AuthenticationException e)
            {
                Console.WriteLine(e);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }



            StreamReader reader = new StreamReader(stream);
            StreamWriter writer = new StreamWriter(stream);

            writer.AutoFlush = true;
            writer.NewLine   = "\n";
            connections++;

            Console.WriteLine("New client accepted. Connected clients: {0}", connections);

            string line;

            string [] command;
            do
            {
                try
                {
                    line = reader.ReadLine();

                    if (line != null)
                    {
                        command = line.Split(';');

                        switch (command[0])
                        {
                        case "GETALL":
                        {
                            Console.WriteLine("Received {0} command", command[0]);

                            foreach (ServerElement s in servers)
                            {
                                writer.WriteLine(s.command);
                            }

                            Console.WriteLine("Sent {0} servers", servers.Count);

                            foreach (SiteElement s in sites)
                            {
                                writer.WriteLine(s.command);
                            }

                            Console.WriteLine("Sent {0} sites", sites.Count);

                            foreach (CityElement c in cities)
                            {
                                writer.WriteLine(c.command);
                            }
                            Console.WriteLine("Sent {0} cities", cities.Count);

                            foreach (GalaxyElement g in galaxies)
                            {
                                writer.WriteLine(g.command);
                            }

                            Console.WriteLine("Sent {0} galaxies", galaxies.Count);
                            foreach (DomainElement d in domains)
                            {
                                writer.WriteLine(d.command);
                            }

                            Console.WriteLine("Sent {0} domains", domains.Count);
                            foreach (CloudElement v in VMs)
                            {
                                writer.WriteLine(v.command);
                            }

                            Console.WriteLine("Sent {0} VMs", VMs.Count);
                            foreach (CloudSiteElement c in cloudSites)
                            {
                                writer.WriteLine(c.command);
                            }
                            Console.WriteLine("Sent {0} Cloud sites", cloudSites.Count);

                            // Send events
                            foreach (EventElement c in events)
                            {
                                writer.WriteLine(c.command);
                            }
                            foreach (EventsByServer c in byserver)
                            {
                                writer.WriteLine(c.command);
                            }
                            foreach (EventsByAccount c in byaccount)
                            {
                                writer.WriteLine(c.command);
                            }
                            // Remove all processed events
                            events.Clear();
                            byaccount.Clear();
                            byserver.Clear();

                            Console.WriteLine("Sent {0} Events", events.Count);
                            writer.WriteLine("<END>");

                            break;
                        }

                        case "SERVER_1.0":
                        {
                            Console.WriteLine("Received {0} command", command[0]);
                            ServerElement server = new ServerElement();
                            server.data.x           = command[1];
                            server.data.y           = command[2];
                            server.data.rotation    = command[3];
                            server.data.name        = command[4];
                            server.data.role        = command[5];
                            server.data.location    = command[6];
                            server.data.status      = command[7];
                            server.data.type        = command[8];
                            server.data.description = command[9];
                            server.command          = line;
                            bool found = false;

                            foreach (ServerElement s in servers)
                            {
                                if (s.data.name == server.data.name)
                                {
                                    servers.Remove(s);
                                    servers.Add(server);
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                servers.Add(server);
                            }
                            break;
                        }

                        case "SITE_1.0":
                        {
                            Console.WriteLine("Received {0} command", command[0]);
                            SiteElement site = new SiteElement();
                            site.data.x           = command[1];
                            site.data.y           = command[2];
                            site.data.width       = command[3];
                            site.data.height      = command[4];
                            site.data.rotation    = command[5];
                            site.data.name        = command[6];
                            site.data.location    = command[7];
                            site.data.description = command[8];
                            site.command          = line;
                            bool found = false;

                            foreach (SiteElement s in sites)
                            {
                                if (s.data.name == site.data.name)
                                {
                                    sites.Remove(s);
                                    sites.Add(site);
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                sites.Add(site);
                            }
                            break;
                        }

                        case "CITY_1.0":
                        {
                            Console.WriteLine("Received {0} command", command[0]);
                            CityElement city = new CityElement();
                            city.data.name        = command[1];
                            city.data.status      = command[2];
                            city.data.altname     = command[3];
                            city.data.description = command[4];
                            city.command          = line;
                            bool found = false;

                            foreach (CityElement c in cities)
                            {
                                if (c.data.name == city.data.name)
                                {
                                    cities.Remove(c);
                                    cities.Add(city);
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                cities.Add(city);
                            }
                            break;
                        }

                        case "DOMAIN_1.0":
                        {
                            Console.WriteLine("Received {0} command", command[0]);
                            DomainElement domain = new DomainElement();
                            domain.data.name        = command[1];
                            domain.data.status      = command[2];
                            domain.data.description = command[3];
                            domain.command          = line;
                            bool found = false;

                            foreach (DomainElement d in domains)
                            {
                                if (d.data.name == domain.data.name)
                                {
                                    domains.Remove(d);
                                    domains.Add(domain);
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                domains.Add(domain);
                            }
                            break;
                        }

                        case "EVENT_1.0":
                        {
                            Console.WriteLine("Received {0} command", command[0]);
                            EventElement e = new EventElement();
                            e.data.name        = command[1];
                            e.data.status      = command[2];
                            e.data.description = command[3];
                            e.command          = line;
                            events.Add(e);
                            break;
                        }

                        case "EVENT_BYACCOUNT1.0":
                        {
                            Console.WriteLine("Received {0} command", command[0]);
                            EventsByAccount e = new EventsByAccount();
                            e.account = command[1];
                            e.number  = command[2];
                            e.command = line;
                            byaccount.Add(e);
                            break;
                        }

                        case "EVENT_BYSERVER1.0":
                        {
                            Console.WriteLine("Received {0} command", command[0]);
                            EventsByServer e = new EventsByServer();
                            e.server  = command[1];
                            e.number  = command[2];
                            e.command = line;
                            byserver.Add(e);
                            break;
                        }

                        case "CLOUD_1.0":
                        {
                            Console.WriteLine("Received {0} command", command[0]);
                            CloudElement vm = new CloudElement();
                            vm.data.name        = command[1];
                            vm.data.host        = command[2];
                            vm.data.cluster     = command[3];
                            vm.data.site        = command[4];
                            vm.data.status      = command[5];
                            vm.data.description = command[6];
                            vm.command          = line;
                            bool found = false;

                            foreach (CloudElement v in VMs)
                            {
                                if (v.data.name == vm.data.name)
                                {
                                    VMs.Remove(v);
                                    VMs.Add(vm);
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                VMs.Add(vm);
                            }
                            break;
                        }

                        case "CLOUDINFO_1.0":
                        {
                            Console.WriteLine("Received {0} command", command[0]);
                            CloudSiteElement site = new CloudSiteElement();
                            site.data.name        = command[1];
                            site.data.role        = command[2];
                            site.data.location    = command[3];
                            site.data.site        = command[4];
                            site.data.status      = command[5];
                            site.data.description = command[6];
                            site.command          = line;
                            bool found = false;

                            foreach (CloudSiteElement c in cloudSites)
                            {
                                if (c.data.site == site.data.site)
                                {
                                    cloudSites.Remove(c);
                                    cloudSites.Add(site);
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                cloudSites.Add(site);
                            }
                            break;
                        }

                        case "GALAXY_1.0":
                        {
                            Console.WriteLine("Received {0} command", command[0]);

                            GalaxyElement galaxy = new GalaxyElement();
                            galaxy.data.name             = command[1];
                            galaxy.data.stars            = command[2];
                            galaxy.data.dust             = command[3];
                            galaxy.data.description      = command[4];
                            galaxy.data.galaxyRadius     = command[5];
                            galaxy.data.coreRadius       = command[6];
                            galaxy.data.angularOffset    = command[7];
                            galaxy.data.coreExcentricity = command[8];
                            galaxy.data.edgeExcentricity = command[9];

                            galaxy.command = line;
                            bool found = false;

                            foreach (GalaxyElement g in galaxies)
                            {
                                if (g.data.name == galaxy.data.name)
                                {
                                    galaxies.Remove(g);
                                    galaxies.Add(galaxy);
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                galaxies.Add(galaxy);
                            }
                            break;
                        }


                        default:
                            break;
                        }
                    }
                }
                catch (SocketException e)
                {
                    Console.WriteLine(e);
                    break;
                }
                catch (IOException e)
                {
                    Console.WriteLine(e);
                    break;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    break;
                }
            } while (line != null);

            Thread.Sleep(300);

            reader.Dispose();
            writer.Dispose();
            stream.Close();
            client.Close();

            if (connections > 0)
            {
                connections--;
            }
            Console.WriteLine("Client disconnected. Connected clients: {0}", connections);
        }