Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            /// Define the expected service certificate. It is required to establish cmmunication using certificates.
            string srvCertCN = "sbesserver";

            NetTcpBinding binding = new NetTcpBinding();

            binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate;
            /// Use CertManager class to obtain the certificate based on the "srvCertCN" representing the expected service identity.
            X509Certificate2 srvCert = CertManager.GetCertificateFromStorage(StoreName.TrustedPeople, StoreLocation.LocalMachine, srvCertCN);

            EndpointAddress address = new EndpointAddress(new Uri("net.tcp://localhost:9999/Receiver"),
                                                          new X509CertificateEndpointIdentity(srvCert));

            int      id;
            string   name           = "";
            string   dateInput      = "";
            DateTime date           = DateTime.Now;
            int      room           = -1;
            double   price          = -1;
            int      discount       = -1;
            int      ticketQuantity = -1;

            string[] tokens;
            int      input = 0;

            using (WCFClient proxy = new WCFClient(binding, address))
            {
                Console.WriteLine("Connection established.");
                do
                {
                    Console.WriteLine("\nMenu:\n\t1. Add Performance\n\t2. Modify Performance\n\t3. Modify Discount\n\t" +
                                      "4. Make Reservation\n\t5. Pay Reservation\n\t6. Show all performances\n\t" +
                                      "7. Show all users\n\t8. Show all reservations\n\t9. Show discount\n\t10. Show my informations\n\t" +
                                      "11. Exit\n\t\n======================\n");
                    try
                    {
                        input = int.Parse(Console.ReadLine());
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"Invalid input: {e.Message}.");
                        input = 0;
                    }

                    switch (input)
                    {
                    case 1:

                        Console.Write("\nNew Performance\n\tname: ");
                        try
                        {
                            name = Console.ReadLine();
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Input not valid {e.Message}. Please try again.");
                            break;
                        }

                        Console.Write("\tdate (format dd/mm/yyyy): ");
                        try
                        {
                            dateInput = Console.ReadLine();
                            tokens    = dateInput.Split('/');
                            date      = new DateTime(int.Parse(tokens[2]), int.Parse(tokens[1]), int.Parse(tokens[0]));
                        }
                        catch (Exception)
                        {
                            Console.WriteLine($"Input not valid. Please try again and enter date in format DD/MM/YYYY.");
                            break;
                        }

                        Console.Write("\troom: ");
                        try
                        {
                            room = int.Parse(Console.ReadLine());
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Input not valid {e.Message}. Please try again.");
                            break;
                        }

                        Console.Write("\tticket price: ");
                        try
                        {
                            price = double.Parse(Console.ReadLine());
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Input not valid {e.Message}. Please try again.");
                            break;
                        }

                        proxy.AddPerformance(name, date, room, price, out id);
                        break;

                    case 2:

                        Console.WriteLine("\nEnter id of the performance you want to modify: ");
                        try
                        {
                            id = int.Parse(Console.ReadLine());
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Input not valid {e.Message}. Please try again.");
                            break;
                        }

                        if (!proxy.CheckIfPerformanceExists(id, 2))
                        {
                            break;
                        }

                        Console.Write("\tname: ");
                        try
                        {
                            name = Console.ReadLine();
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Input not valid {e.Message}. Please try again.");
                            break;
                        }

                        Console.Write("\tdate (format dd/mm/yyyy): ");
                        try
                        {
                            dateInput = Console.ReadLine();
                            tokens    = dateInput.Split('/');
                            date      = new DateTime(int.Parse(tokens[2]), int.Parse(tokens[1]), int.Parse(tokens[0]));
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Input not valid. Please try again and enter date in format DD/MM/YYYY.");
                            break;
                        }

                        Console.Write("\troom: ");
                        try
                        {
                            room = int.Parse(Console.ReadLine());
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Input not valid {e.Message}. Please try again.");
                            break;
                        }

                        Console.Write("\tticket price: ");
                        try
                        {
                            price = double.Parse(Console.ReadLine());
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Input not valid {e.Message}. Please try again.");
                            break;
                        }

                        proxy.ModifyPerformance(id, name, date, room, price);
                        break;

                    case 3:

                        Console.Write("\nEnter new discount: ");
                        try
                        {
                            discount = int.Parse(Console.ReadLine());
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Input not valid {e.Message}. Please try again.");
                            break;
                        }

                        proxy.ModifyDiscount(discount);
                        break;

                    case 4:

                        Console.WriteLine("\nEnter id of the performance you want to reserve: ");
                        try
                        {
                            id = int.Parse(Console.ReadLine());
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Input not valid {e.Message}. Please try again.");
                            break;
                        }

                        if (!proxy.CheckIfPerformanceExists(id, 4))
                        {
                            break;
                        }

                        Console.Write("\n\tticketQuantity: ");
                        try
                        {
                            ticketQuantity = int.Parse(Console.ReadLine());
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Input not valid {e.Message}. Please try again.");
                            break;
                        }

                        proxy.MakeReservation(id, DateTime.Now, ticketQuantity, out int reservationId);
                        break;

                    case 5:

                        Console.WriteLine("\nEnter id of the reservation you want to pay: ");
                        try
                        {
                            id = int.Parse(Console.ReadLine());
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Input not valid {e.Message}. Please try again.");
                            break;
                        }

                        if (!proxy.CheckIfReservationCanBePaied(id))
                        {
                            break;
                        }

                        proxy.PayReservation(id);
                        break;

                    case 6:
                        proxy.ListAllPerformances();
                        break;

                    case 7:
                        proxy.ListAllUsers();
                        break;

                    case 8:
                        proxy.ListAllReservations();
                        break;

                    case 9:
                        proxy.ListDiscount();
                        break;

                    case 10:
                        proxy.ListUser();
                        break;

                    case 11:
                        break;

                    default:
                        Console.WriteLine("Entered invalid number. Valid 1-11. Please try again.");
                        break;
                    }
                }while (input != 11);

                proxy.Dispose();
                proxy.Close();
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            int            localdb        = 0;
            CallbackClient callbackclient = new CallbackClient();

            callbackclient.Proxy = null;
            WCFClient     proxy   = null;
            NetTcpBinding binding = new NetTcpBinding();

            Console.WriteLine("Na koji localDB zelis da se konektujes[0, 1, 2, 3 ... x]:");
            while (true)
            {
                localdb = Int32.Parse(Console.ReadLine());
                string address = "net.tcp://localhost:" + (8888 + localdb).ToString() + "/localdb";
                proxy = new WCFClient(callbackclient, binding, new EndpointAddress(new Uri(address)));

                try { proxy.testServerMessage("Hello from client to server."); }
                catch (Exception e) { Console.WriteLine("nesto je poslo po zlu probaj opet"); continue; }

                break;
            }

            callbackclient.Proxy = proxy;
            Database database = new Database();

            int opt;


            while (true)
            {
                // Ispis menija i unos zeljene opcije..
                opt = Meni();

                switch (opt)
                {
                case 1:
                    database.InterestRegions = Odaberiregione();
                    if (database.InterestRegions != null)
                    {
                        proxy.GetEntitiesForRegions(database.InterestRegions);
                    }
                    break;

                case 2:
                    Region ch = OdaberiRegion();
                    if (ch != Region.Nazad)
                    {
                        proxy.GetAverageConsumptionForRegion(ch);
                    }
                    break;

                case 3:
                    string grad = OdaberiGrad();
                    if (grad != null)
                    {
                        proxy.GetAverageConsumptionForCity(grad);
                    }
                    break;

                case 4:
                    string package = OdaberiEntitet();
                    if (package != null)
                    {
                        string[] input = package.Split(',');
                        proxy.UpdateConsumption(input[0], int.Parse(input[1]), float.Parse(input[2]));
                    }
                    break;

                case 5:
                    LogEntity kreiranEntitet = DodajEntitet();
                    if (kreiranEntitet != null)
                    {
                        proxy.AddLogEntity(kreiranEntitet);
                    }
                    break;

                case 6:
                    string identification = IzbrisiEntitet();
                    if (identification != null)
                    {
                        proxy.DeleteLogEntity(identification);
                    }
                    break;

                case 7:
                    IzlistajEntitete();
                    break;

                case 8:
                    goto labela;

                default:
                    Console.WriteLine("Nepostojeća opcija je odabrana, molimo pokušajte ponovo.");
                    break;
                }
            }

labela:
            proxy.Close();

            Console.ReadLine();
        }
Ejemplo n.º 3
0
        static int Main(string[] args)
        {
            string users = String.Empty;
            string clientId;
            int    otherClient;
            string encryptedSecretKey = String.Empty;
            string decryptedSecretKey;
            RSA_Asimm_Algorithm_C rsa = new RSA_Asimm_Algorithm_C();

            string publicKey = rsa.GenerateKeys();

            WCFClient proxy = BindToCentralServer();

            Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            IIdentity       identity        = Thread.CurrentPrincipal.Identity;
            WindowsIdentity winIdentity     = identity as WindowsIdentity;
            string          senderName      = Formatter.ParseName(identity.Name);
            int             peerServicePort = 6000;

            try
            {
                clientId = proxy.Connect(identity.Name, winIdentity.User.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("Connection failed. \nDetails: " + e.Message);
                Console.WriteLine("Press any key to close connection. ");
                Console.ReadKey();
                proxy.Abort();
                return(0);
            }

            peerServicePort += Int32.Parse(clientId);
            Console.WriteLine("Current: peer_" + clientId);

            ServiceHost       host            = OpenPeerService(peerServicePort);
            MonitoringChannel proxyMonitoring = OpenMonitoringChannel();    //open channel for logging all messages

            try
            {
                encryptedSecretKey = proxy.GenerateSecretKey(publicKey); //klijent dobija kriptovan tajni kljuc
            }
            catch (Exception e)
            {
                Console.WriteLine("Encryption of secret key failed. \nDetails: " + e.Message);
                Console.WriteLine("Press any key to close connection. ");
                Console.ReadKey();
                proxy.Abort();
                return(0);
            }
            decryptedSecretKey = rsa.DecryptData(encryptedSecretKey);

            string publicKeyFromMonitoring = String.Empty;

            try
            {
                publicKeyFromMonitoring = proxyMonitoring.GenerateRSAKeys();
            }
            catch (Exception e)
            {
                Console.WriteLine("Getting public RSA key from monitoring failed. \nDetails: " + e.Message);
            }

            string encryptedSecretKeyWithMonitoringKey = rsa.EncryptData(publicKeyFromMonitoring, decryptedSecretKey);

            try
            {
                proxyMonitoring.SendSecretKey(encryptedSecretKeyWithMonitoringKey);
            }
            catch (FaultException e)
            {
                Console.WriteLine("Sending secret key to monitoring failed. \nDetails: " + e.Message);
            }

            while (true)
            {
                int m = Menu();
                if (m == 1)
                {
                    try
                    {
                        users = proxy.GetConnectedClients();
                    }
                    catch (FaultException e)
                    {
                        Console.WriteLine("Getting list of connected clients from central server failed. \nDetails: " + e.Message);
                    }
                    DeserializeJson(users);
                    PrintConnectedClients();
                    otherClient = ChooseClient();//otherUser i otherClient se ne podudaraju ali otherUser = dict vrednost sa keyom otherClient

                    if (otherClient == 0)
                    {
                        continue;
                    }
                    User otherUser = DBClients.connectedClients[otherClient];
                    if (otherUser.Counter == Int32.Parse(clientId))
                    {
                        Console.WriteLine("This is your account. Please choose again.");
                        continue;
                    }
                    Peer proxyPeerClient = OpenPeerClient(otherUser.Counter);
                    while (true)
                    {
                        Console.WriteLine("Enter message to send [type 'x' for disconection]:");
                        string messageToSend = Console.ReadLine();
                        if (messageToSend.Equals("x") || messageToSend.Equals("X"))
                        {
                            break;
                        }
                        try
                        {
                            proxyPeerClient.SendMessage(messageToSend);
                        }catch (Exception e)
                        {
                            Console.WriteLine("Sending message to client failed. \nDetails: " + e.Message);
                            break;
                        }
                        byte[] encryptedMessage      = AES_ENCRYPTION.EncryptFile(messageToSend, decryptedSecretKey);
                        byte[] encryptedSenderName   = AES_ENCRYPTION.EncryptFile(senderName, decryptedSecretKey);
                        byte[] encryptedRecieverName = AES_ENCRYPTION.EncryptFile(otherUser.Name, decryptedSecretKey);
                        try
                        {
                            proxyMonitoring.LogMessage(encryptedMessage, encryptedSenderName, encryptedRecieverName);
                        }
                        catch (FaultException e)
                        {
                            Console.WriteLine("Logging message on server failed. \nDetails: " + e.Message);
                            break;
                        }
                    }
                    proxyPeerClient.Abort();
                }
                if (m == 0)
                {
                    break;
                }
            }

            Console.WriteLine("Press any key to close..");
            Console.ReadKey();
            try
            {
                proxy.Disconnect(winIdentity.User.ToString());
            } catch (FaultException e)
            {
                Console.WriteLine("Disconnection failed. \nDetails: " + e.Message);
            }
            host.Close();
            proxy.Close();
            return(0);
        }