예제 #1
0
        private static IServerPM ConnectToServer(string serverID, RemotingAddress serverRA)
        {
            if (!serverList.Exists(x => x.Item2 == serverRA))
            {
                if (serverList.Exists(x => x.Item1 == serverID))
                {
                    throw new ApplicationException($"Server with ID '{serverID}' already exists.");
                }

                IServerPM server = (IServerPM)Activator.GetObject(typeof(IServerPM), serverRA.ToString());
                if (server == null)
                {
                    throw new ApplicationException("Could not locate Server: " + serverRA.ToString());
                }

                serverList.Add(new Tuple <string, RemotingAddress, IServerPM>(serverID, serverRA, server));
                //Add server to replication servers list
                remoteServerList.Add(new Tuple <string, RemotingAddress>(serverID, serverRA));

                return(server);
            }
            else
            {
                throw new ApplicationException("Already connected to server: " + serverRA.ToString());
            }
        }
예제 #2
0
        public PCSService()
        {
            string          PMAddress = CallContext.GetData("ClientIPAddress").ToString();
            RemotingAddress PMRA      = new RemotingAddress(PMAddress, 10001, "MSPM");

            PM = (IPM)Activator.GetObject(typeof(IPM), PMRA.ToString());
            Utilities.WriteDebug($"PCSServices constructed by {PMAddress}.");
        }
예제 #3
0
        public void RegisterServerReplica(string serverID, RemotingAddress serverRA)
        {
            IServerC newServerChannel = (IServerC)Activator.GetObject(typeof(IServerC), serverRA.ToString());

            Client.serverReplicasList.Add(newServerChannel);

            MessageBox.Show("Client: " + Client.Username + " Registered Server: " + serverRA.ToString());
        }
예제 #4
0
 private static IPCS ConnectToPCS(RemotingAddress PCSRemotingAddress)
 {
     if (!PCSList.Exists(x => x.Item1 == PCSRemotingAddress))
     {
         IPCS pcs = (IPCS)Activator.GetObject(typeof(IPCS), PCSRemotingAddress.ToString());
         if (pcs == null)
         {
             throw new ApplicationException("Could not locate PCS: " + PCSRemotingAddress.ToString());
         }
         PCSList.Add(new Tuple <RemotingAddress, IPCS>(PCSRemotingAddress, pcs));
         return(pcs);
     }
     else
     {
         return(PCSList.Find(x => x.Item1 == PCSRemotingAddress).Item2);
     }
 }
예제 #5
0
        public void StartServer(string serverId, RemotingAddress serverRA, uint maxFaults, uint minDelay, uint maxDelay)
        {
            if (Program.serverProcesses.ContainsKey(serverId))
            {
                throw new RemotingException($"PCS: Server with ID '{ serverId }' already exists.");
            }

            var procPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory() +
                                                         @"\..\..\..\Server\bin\Debug\Server.exe"));

            Console.WriteLine($"Starting server:\n\t" +
                              $"ID: {serverId}\n\t" +
                              $"URL: {serverRA.ToString()}\n\t" +
                              $"Max Faults: {maxFaults}\n\t" +
                              $"Min Delay: {minDelay}\n\t" +
                              $"Max Delay: {maxDelay}\n\t" +
                              $"Proc. path: {procPath}");

            Process server = RunProcess(procPath,
                                        $"{serverId} {serverRA.ToString()} {maxFaults} {minDelay} {maxDelay}");

            server.Exited += new EventHandler(delegate(Object o, EventArgs a)
            {
                Program.serverProcesses.Remove(serverId);
                Console.WriteLine($"Server '{serverId}' has exited.");
                try
                {
                    PM.InformServerExited(serverId);
                }
                catch (Exception ex)
                {
                    Utilities.WriteError($"Error informing PM that server exited: {ex.Message}.");
                }
            });

            Program.serverProcesses.Add(serverId, server);
        }
예제 #6
0
        public void StartClient(string username, RemotingAddress clientRA, RemotingAddress serverRA, string scriptFile)
        {
            if (Program.clientProcesses.ContainsKey(username))
            {
                throw new RemotingException($"PCS: Client with username '{ username }' already exists.");
            }

            var procPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory() +
                                                         @"\..\..\..\Client\bin\Debug\Client.exe"));

            Console.WriteLine($"Starting client:\n\t" +
                              $"Username: {username}\n\t" +
                              $"Client URL: {clientRA.ToString()}\n\t" +
                              $"Server URL: {serverRA.ToString()}\n\t" +
                              $"Script path: {scriptFile}\n\t" +
                              $"Proc. path: {procPath}");

            Process client = RunProcess(procPath,
                                        $"{username} {clientRA.ToString()} {serverRA.ToString()} {scriptFile}");

            client.Exited += new EventHandler(delegate(Object o, EventArgs a)
            {
                Program.clientProcesses.Remove(username);
                Console.WriteLine($"Client '{username}' has exited.");
                try
                {
                    PM.InformClientExited(username);
                }
                catch (Exception ex)
                {
                    Utilities.WriteError($"Error informing PM that client exited: {ex.Message}.");
                }
            });

            Program.clientProcesses.Add(username, client);
        }
예제 #7
0
 public static void ConnectToServer(RemotingAddress serverRA, string clientName, RemotingAddress clientRA)
 {
     Username = clientName;
     server   = (IServerC)Activator.GetObject(typeof(IServerC), serverRA.ToString());
     try
     {
         server.RegisterClient(clientName, clientRA);
     }
     catch (System.Net.Sockets.SocketException)
     {
         MessageBox.Show("Lost connection to the server.",
                         "Error",
                         MessageBoxButtons.OK,
                         MessageBoxIcon.Error);
     }
 }
예제 #8
0
        public static void CreateServer(string serverID, RemotingAddress serverRA,
                                        uint maxFaults, uint minDelay, uint maxDelay)
        {
            if (serverList.Exists(x => x.Item1 == serverID))
            {
                throw new ApplicationException($"Server with ID '{serverID}' already exists.");
            }

            RemotingAddress pcsRA = new RemotingAddress(serverRA.address, PCSPort, PCSChannel);

            IPCS pcs = GetPCS(pcsRA);

            try
            {
                pcs.StartServer(serverID, serverRA, maxFaults, minDelay, maxDelay);
            }
            catch (System.Net.Sockets.SocketException)
            {
                MessageBox.Show($"Connection with PCS on remoting address '{pcsRA.ToString()}' was lost.\n" +
                                "If the PCS has been restarted you may try again.");
                Program.RemovePCSFromList(pcsRA);
                return;
            }

            IServerPM server = ConnectToServer(serverID, serverRA);

            //Replication
            //Inform new server of all existing servers
            server.SendExistingServersList(remoteServerList);
            //Inform all existing servers of new server
            foreach (Tuple <string, RemotingAddress, IServerPM> serv in serverList)
            {
                serv.Item3.BroadcastNewServer(new Tuple <string, RemotingAddress>(serverID, serverRA));
            }


            // Fill location list
            if (serverList.Count == 1)
            {
                mainForm.manageServersPage.FillLocationCb(server.GetLocationsPM());
            }

            mainForm.manageServersPage.AddServerToList(serverID);
        }
예제 #9
0
        public void InformNewClient(string newClientUsername, RemotingAddress newClientRA)
        {
            bool clientExists = false;

            foreach (Client c in Server.clients)
            {
                if (c.Username == newClientUsername)
                {
                    clientExists = true;
                }
            }
            if (!clientExists)
            {
                IClient cliChannel = (IClient)Activator.GetObject(typeof(IClient), newClientRA.ToString());
                Client  newClient  = new Client(cliChannel, newClientUsername, newClientRA);
                Server.clients.Add(newClient);
                cliChannel.RegisterServerReplica(Server.serverID, Server.serverRAForClients);
            }
        }
예제 #10
0
        public static void CreateClient(string username, RemotingAddress clientRA,
                                        RemotingAddress serverRA, string scriptFile)
        {
            RemotingAddress pcsRA = new RemotingAddress(serverRA.address, PCSPort, PCSChannel);

            IPCS pcs = GetPCS(pcsRA);

            try
            {
                pcs.StartClient(username, clientRA, serverRA, scriptFile);
            }
            catch (System.Net.Sockets.SocketException)
            {
                MessageBox.Show($"Connection with PCS on remoting address '{pcsRA.ToString()}' was lost.\n" +
                                "If the PCS has been restarted you may try again.");
                Program.RemovePCSFromList(pcsRA);
                return;
            }
        }
예제 #11
0
파일: Form1.cs 프로젝트: MangaD/DAD-Project
        private void joinButton_Click(object sender, EventArgs e)
        {
            if (myClient != null)
            {
                return;
            }
            //TODO hardcoded for now
            clientRA = RemotingAddress.FromString("tcp://localhost:65001/MSClient");
            //TODO hardcoded for now
            serverRA = RemotingAddress.FromString("tcp://localhost:65000/MSServer");

            myClient.listenClient(clientRA.port, clientRA.channel);

            string clientName = nameTextBox.Text;

            if (clientName == null)
            {
                return;
            }
            myClient.connectToServer(serverRA.ToString(), clientName, clientRA.ToString());
        }
예제 #12
0
        public void RegisterClient(string username, RemotingAddress clientRA)
        {
            Server.freezeHandle.WaitOne(); // For Freeze command
            this.Delay();                  // For induced delay

            if (username == null || username == "")
            {
                throw new ApplicationException($"Username cannot be empty!");
            }
            if (clientRA == null || clientRA.address == null || clientRA.address == "")
            {
                throw new ApplicationException($"Your address cannot be empty!");
            }
            if (clientRA.channel == null || clientRA.channel == "")
            {
                throw new ApplicationException($"Your channel cannot be empty!");
            }
            if (Server.clients.Where(c => c.Username == username).Count() > 0)
            {
                throw new ApplicationException($"Someone with username '{username}' is already connected.");
            }

            IClient newClientChannel = (IClient)Activator.GetObject(typeof(IClient), clientRA.ToString());
            Client  newClient        = new Client(newClientChannel, username, clientRA);

            Server.clients.Add(newClient);

            //Send new clientToAllOtherServers
            Thread threadS = new Thread(() => Server.InformAllServersOfNewClient(newClient));

            threadS.Start();

            //Inforam all clients of new client
            Thread thread = new Thread(() => Server.InformAllClientsOfNewClient(newClient.Username));

            thread.Start();

            Console.WriteLine($"New client '{username}' listening at '{clientRA}'");
        }