Beispiel #1
0
        /// <summary>
        /// Diese Methode dient zur Anbindung der P2P Topology an die Hierarchische.
        /// Es wird ein entsprechendes Paket in das hierarchische Netz gesendet.
        /// </summary>
        /// <param name="p2pRequest"></param>
        /// <param name="username"></param>
        /// <param name="kaEvent"></param>
        /// <param name="writeResult">übergebe die Methode writeResult</param>
        void ConnectP2P(P2PRequest p2pRequest, string username, KaEvent kaEvent, Action <Request, string> writeResult)
        {
            //P2P Teil
            P2PPackage p2p = new P2PPackage(username, kaEvent)
            {
                P2Prequest = p2pRequest
            };

            //Sende Teil
            Package sendPackage = new Package(p2p);
            Package recievePackage;

            recievePackage = Send(sendPackage, KnownServers.GetRandomWellKnownPeer());

            if (recievePackage != null)
            {
                switch (recievePackage.p2p.P2PAnswer)
                {
                case P2PAnswer.Success:
                    writeResult(Request.Success, InviteSuccess);
                    break;

                case P2PAnswer.Failure:
                    writeResult(Request.Failure, InviteFail);
                    break;

                default:
                    writeResult(Request.Failure, InviteFail);
                    break;
                }
            }
            else
            {
                writeResult(Request.Error, Error);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Settings für P2PServer. Zugehöriigkeit WellKnownPeers usw.
        /// </summary>
        /// <param name="isWellKnown"></param>
        public static void P2PSettings(bool isWellKnown)
        {
            while (CheckWellKnownPeers())
            {
                ;
            }

            if (isWellKnown)
            {
                //Data.ServerConfig.ListofWellKnown.Add(Data.ServerConfig.host);
                foreach (IPAddress ipAddress in KnownServers.ListofWellKnownPeers)
                {
                    if (ipAddress.ToString() != ServerConfig.host.ToString())
                    {
                        Package package = new Package
                        {
                            p2p = new P2PPackage()
                        };
                        package.p2p.P2Prequest = P2PRequest.RegisterServer;
                        package.p2p.SetOriginIPAddress(ServerConfig.host.ToString());
                        Package receive = ServerLogic.Send(package, ipAddress);

                        //Nur hinzufügen wenn noch nicht existent
                        if (receive != null && !ServerConfig.neighbours.Exists(x => x.ToString() == ipAddress.ToString()))
                        {
                            ServerConfig.neighbours.Add(ipAddress);
                        }
                    }
                }
            }
            else
            {                               //This only functions if we have two or more Well Known Peers
                for (int i = 2; i > 0; i--) //'i' represents number of connections to create.
                {
                    Package package = new Package
                    {
                        p2p = new P2PPackage
                        {
                            P2Prequest = P2PRequest.NewServer
                        },
                        sourceServer = ServerConfig.host.ToString()
                    };
                    package.p2p.SetOriginIPAddress(ServerConfig.host.ToString());
                    package = ServerLogic.Send(package, KnownServers.GetRandomWellKnownPeer());

                    if (package != null)
                    {
                        //2 Server mit denen ich mich verbinde und bei denen Registriere
                        switch (package.p2p.P2PAnswer)
                        {
                        case P2PAnswer.Error:
                            Console.WriteLine(package.p2p.ErrorMsg);
                            break;

                        case P2PAnswer.Failure:
                            Console.WriteLine(NewServerFail);
                            break;

                        default:
                            Package registerPackage = new Package
                            {
                                p2p = new P2PPackage
                                {
                                    P2Prequest = P2PRequest.RegisterServer
                                },
                                sourceServer = ServerConfig.host.ToString()
                            };
                            registerPackage.p2p.SetOriginIPAddress(ServerConfig.host.ToString());
                            registerPackage = ServerLogic.Send(registerPackage, IPAddress.Parse(package.p2p.lastIP));

                            if (registerPackage != null)
                            {
                                switch (registerPackage.p2p.P2PAnswer)
                                {
                                case P2PAnswer.Success:
                                    ServerConfig.neighbours.Add(IPAddress.Parse(package.p2p.lastIP));
                                    Console.WriteLine(RegisterServerSuccess);
                                    break;

                                case P2PAnswer.Visited:
                                    break;

                                default:
                                    Console.WriteLine(RegisterServerFail);
                                    break;
                                }
                            }
                            else
                            {
                                Console.WriteLine(SendError);
                            }
                            break;
                        }
                    }
                    else
                    {
                        Console.WriteLine(SendError);
                    }
                }
            }

            if (ServerConfig.neighbours.Count == 0)
            {
                Console.WriteLine(SetupFailed);
            }
            else if (ServerConfig.neighbours.Count < 2)
            {
                Console.WriteLine(SetupIncomplete);
            }
            else
            {
                Console.WriteLine(SetupComplete);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Resolve Acquired Packages and trigger corresponding requests. For Client
        /// </summary>
        /// <param name="package"></param>
        public Package ResolvePackage(Package package)
        {
            switch (package.request)
            {
            /// In case of Login Request try to login to the server database and set Request accordingly
            case Request.Login:
                Console.WriteLine(LoginRequest);
                if (package.serverSwitched)
                {
                    if (database.Login(package.user))
                    {
                        List <KaEvent> kaEvents;
                        kaEvents         = database.Read(package.user.name);
                        package.kaEvents = kaEvents;
                        try
                        {
                            package.invites = database.ReadInvites(package.user.name);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("ReadInvites failed.\n{0}\n{1}", ex.Message, ex.StackTrace);
                        }
                        writeResult(Request.Success, LoginSuccess);
                    }
                    else
                    {
                        writeResult(Request.Failure, LoginFail);
                    }
                }
                else
                {
                    if (ServerConfig.structure == Structure.HIERARCHY)
                    {
                        ///if serverID > 0 => Hierarchy
                        if (package.user.serverID > 0)
                        {
                            //Hierarchie Login
                            HierarchiePackage hierarchie = new HierarchiePackage
                            {
                                HierarchieRequest = HierarchieRequest.UserLogin,
                                login             = package.user.name,
                                destinationID     = package.user.serverID
                            };
                            hierarchie = HierarchyLogic.ResolveHierarchie(hierarchie);

                            switch (hierarchie.HierarchieAnswer)
                            {
                            case HierarchieAnswer.Success:
                                package.sourceServer = hierarchie.destinationAdress;
                                writeResult(Request.ChangeServer, ChangeServer);
                                break;

                            case HierarchieAnswer.Failure:
                                writeResult(Request.Failure, LoginFail);
                                break;

                            case HierarchieAnswer.Error:
                                writeResult(Request.Error, Error);
                                break;

                            default:
                                writeResult(Request.Failure, Error);
                                break;
                            }
                        }
                        ///else => Send to P2P
                        else
                        {
                            //P2P Teil
                            P2PPackage p2p = new P2PPackage(package.user.name)
                            {
                                P2Prequest = P2PRequest.Login
                            };

                            //Sende Teil
                            Package sendPackage = new Package(p2p);
                            Package recievePackage;
                            recievePackage = Send(sendPackage, KnownServers.GetRandomWellKnownPeer());

                            if (recievePackage != null)
                            {
                                switch (recievePackage.p2p.P2PAnswer)
                                {
                                case P2PAnswer.Success:
                                    package.sourceServer = recievePackage.p2p.lastIP;
                                    writeResult(Request.ChangeServer, ChangeServer);
                                    break;

                                case P2PAnswer.Failure:
                                    writeResult(Request.Failure, LoginFail);
                                    break;

                                default:
                                    writeResult(Request.Failure, LoginFail);
                                    break;
                                }
                            }
                            else
                            {
                                writeResult(Request.Error, Error);
                            }
                        }
                    }
                    else if (ServerConfig.structure == Structure.P2P)
                    {
                        ///if serverID == 0 => P2P
                        if (package.user.serverID == 0)
                        {
                            //P2P Teil
                            P2PPackage p2p = new P2PPackage(package.user.name)
                            {
                                P2Prequest = P2PRequest.Login
                            };
                            p2p = P2PLogic.ResolveP2P(p2p);

                            switch (p2p.P2PAnswer)
                            {
                            case P2PAnswer.Success:
                                package.sourceServer = p2p.lastIP;
                                writeResult(Request.ChangeServer, ChangeServer);
                                break;

                            case P2PAnswer.Failure:
                                writeResult(Request.Failure, LoginFail);
                                break;

                            default:
                                writeResult(Request.Failure, LoginFail);
                                break;
                            }
                        }
                        ///else =>  Send to Hierarchy
                        else
                        {
                            //Hierarchie Login
                            HierarchiePackage hierarchie = new HierarchiePackage
                            {
                                HierarchieRequest = HierarchieRequest.UserLogin,
                                login             = package.user.name,
                                destinationID     = package.user.serverID
                            };

                            //Sende Teil
                            Package sendPackage = new Package(hierarchie);
                            Package recievePackage;
                            recievePackage = Send(sendPackage, KnownServers.Root);

                            if (recievePackage != null)
                            {
                                switch (hierarchie.HierarchieAnswer)
                                {
                                case HierarchieAnswer.Success:
                                    package.sourceServer = hierarchie.destinationAdress;
                                    writeResult(Request.ChangeServer, ChangeServer);
                                    break;

                                case HierarchieAnswer.Failure:
                                    writeResult(Request.Failure, LoginFail);
                                    break;

                                case HierarchieAnswer.Error:
                                    writeResult(Request.Error, Error);
                                    break;

                                default:
                                    writeResult(Request.Failure, Error);
                                    break;
                                }
                            }
                            else
                            {
                                writeResult(Request.Error, Error);
                            }
                        }
                    }
                }
                break;

            /// In case of Register Request try to login to the server database and set Request accordingly
            case Request.Register:
                Console.WriteLine(RegisterRequest);
                try
                {
                    if (package.serverSwitched)
                    {
                        if (database.RegisterUser(package.user, package.passwordConfirm))
                        {
                            writeResult(Request.Success, RegisterSuccess);
                            package.user.serverID = ServerConfig.serverID;
                        }
                        else
                        {
                            writeResult(Request.Failure, RegisterFail);
                        }
                    }
                    else
                    {
                        if (ServerConfig.structure == Structure.HIERARCHY)
                        {
                            //Hierarchie Teil
                            HierarchiePackage hierarchie = new HierarchiePackage
                            {
                                HierarchieRequest = HierarchieRequest.RegisterUser
                            };
                            hierarchie = HierarchyLogic.ResolveHierarchie(hierarchie);

                            if (hierarchie.HierarchieAnswer == HierarchieAnswer.UserExistent)
                            {
                                writeResult(Request.UserExistent, UserExistent);
                                break;
                            }

                            //P2P Teil
                            //ConnectP2P(P2PRequest.NewUser, package.user.name, writeResult);

                            //P2P Teil
                            P2PPackage p2p = new P2PPackage(package.user.name)
                            {
                                P2Prequest = P2PRequest.NewUser
                            };

                            //Sende Teil
                            Package sendPackage = new Package(p2p);
                            Package recievePackage;
                            recievePackage = Send(sendPackage, KnownServers.GetRandomWellKnownPeer());

                            if (recievePackage != null)
                            {
                                switch (recievePackage.p2p.P2PAnswer)
                                {
                                case P2PAnswer.Success:
                                    if (hierarchie.anzUser < recievePackage.p2p.anzUser)
                                    {
                                        package.sourceServer = hierarchie.destinationAdress;
                                    }
                                    else
                                    {
                                        package.sourceServer = recievePackage.p2p.lastIP;
                                    }
                                    writeResult(Request.ChangeServer, ChangeServer);
                                    break;

                                ///user already exists
                                case P2PAnswer.UserExistent:
                                    writeResult(Request.UserExistent, UserExistent);
                                    break;

                                case P2PAnswer.Error:
                                    Console.WriteLine(recievePackage.p2p.ErrorMsg);
                                    writeResult(Request.Error, Error);
                                    break;

                                default:
                                    package.sourceServer = hierarchie.destinationAdress;
                                    writeResult(Request.ChangeServer, ChangeServer);
                                    break;
                                }
                            }
                            else
                            {
                                package.sourceServer = hierarchie.destinationAdress;
                                writeResult(Request.ChangeServer, ChangeServer);
                            }
                        }
                        else if (ServerConfig.structure == Structure.P2P)
                        {
                            //P2P Teil
                            P2PPackage p2p = new P2PPackage
                            {
                                P2Prequest = P2PRequest.NewUser
                            };
                            p2p = P2PLogic.ResolveP2P(p2p);

                            ///user is already existent
                            if (p2p.P2PAnswer == P2PAnswer.Visited)
                            {
                                writeResult(Request.UserExistent, UserExistent);
                                break;
                            }

                            //Hierarchie Teil
                            HierarchiePackage hierarchie = new HierarchiePackage
                            {
                                HierarchieRequest = HierarchieRequest.RegisterUser
                            };

                            //Sende Teil
                            Package sendPackage = new Package(hierarchie);
                            Package recievePackage;
                            recievePackage = Send(sendPackage, KnownServers.Root);
                            if (recievePackage != null)
                            {
                                switch (recievePackage.hierarchie.HierarchieAnswer)
                                {
                                case HierarchieAnswer.Success:
                                    if (p2p.anzUser < recievePackage.hierarchie.anzUser)
                                    {
                                        package.sourceServer = p2p.lastIP;
                                    }
                                    else
                                    {
                                        package.sourceServer = recievePackage.hierarchie.destinationAdress;
                                    }
                                    writeResult(Request.ChangeServer, RegisterSuccess);
                                    break;

                                ///user already exists
                                case HierarchieAnswer.UserExistent:
                                    writeResult(Request.UserExistent, UserExistent);
                                    break;

                                default:
                                    package.sourceServer = p2p.lastIP;
                                    writeResult(Request.ChangeServer, ChangeServer);
                                    break;
                                }
                            }
                            else
                            {
                                package.sourceServer = p2p.lastIP;
                                writeResult(Request.ChangeServer, ChangeServer);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.GetType().FullName);
                    Console.WriteLine(e.Message);
                    writeResult(Request.Failure, RegisterFail);
                }
                break;

            case Request.Save:
                Console.WriteLine(SaveRequest);
                try
                {
                    database.SaveEvent(package.kaEvents[0]);
                    writeResult(Request.Success, SaveSuccess);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.GetType().FullName);
                    Console.WriteLine(e.Message);
                    writeResult(Request.Failure, SaveFail);
                }
                break;

            case Request.Delete:
                Console.WriteLine(DeleteRequest);
                try
                {
                    database.DeleteEvent(package.kaEvents[0].TerminID);
                    writeResult(Request.Success, DeleteSuccess);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.GetType().FullName);
                    Console.WriteLine(e.Message);
                    writeResult(Request.Failure, DeleteFail);
                }
                break;

            case Request.Load:
                Console.WriteLine(LoadRequest);
                try
                {
                    List <KaEvent> kaEvents;
                    //kaEvents = database.LoadEvents(package.user, package.kaEvents[0].Beginn);
                    kaEvents         = database.Read(package.user.name);
                    package.kaEvents = kaEvents;
                    writeResult(Request.Success, LoadSuccess);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.GetType().FullName);
                    Console.WriteLine(e.Message);
                    writeResult(Request.Failure, LoadFail);
                }
                break;

            case Request.Test:
                Console.WriteLine(TestRequest);
                break;

            case Request.Invite:
                //Prüfung ServerID
                Console.WriteLine(InviteRequest);
                if (ServerConfig.structure == Structure.HIERARCHY)
                {
                    /*
                     * Hier ist nur Client-Server.
                     * Wenn Server-Server Invite message ist, wird es bei resolveHierarchie erledigt
                     * Das hier sollte wenn möglich als Asynchron, bzw. nach art Fire und Forget.
                     * Wenn mehrere User eingetragen wird, hat der Server eine lange Anschreibezeit
                     * */

                    foreach (User member in package.kaEvents[0].members)
                    {
                        ///member is user in current server
                        if (member.serverID == ServerConfig.serverID)
                        {
                            if (database.UserExist(member.name))
                            {
                                database.SaveInvites(member.name, package.kaEvents[0]);
                                writeResult(Request.Success, InviteSuccess);
                            }
                            else
                            {
                                writeResult(Request.Failure, UserNotFound);
                            }
                        }
                        ///member is user in hierarchy topology
                        else if (member.serverID > 0)
                        {
                            HierarchiePackage hierarchie = new HierarchiePackage
                            {
                                HierarchieRequest = HierarchieRequest.Invite,
                                invite            = package.kaEvents[0],
                                login             = member.name,
                                destinationID     = member.serverID
                            };
                            HierarchyLogic.ResolveHierarchie(hierarchie);

                            switch (hierarchie.HierarchieAnswer)
                            {
                            case HierarchieAnswer.Success:
                                writeResult(Request.Success, InviteSuccess);
                                break;

                            case HierarchieAnswer.Failure:
                                writeResult(Request.Failure, InviteFail);
                                break;

                            default:
                                break;
                            }
                        }
                        ///member is user in p2p topology
                        else
                        {
                            //P2P Teil
                            P2PPackage p2p = new P2PPackage(member.name, package.kaEvents[0])
                            {
                                P2Prequest = P2PRequest.Invite
                            };

                            //Sende Teil
                            Package sendPackage = new Package(p2p);
                            Package recievePackage;
                            recievePackage = Send(sendPackage, KnownServers.GetRandomWellKnownPeer());

                            if (recievePackage != null)
                            {
                                switch (recievePackage.p2p.P2PAnswer)
                                {
                                case P2PAnswer.Success:
                                    writeResult(Request.Success, InviteSuccess);
                                    break;

                                case P2PAnswer.Failure:
                                    writeResult(Request.Failure, InviteFail);
                                    break;

                                default:
                                    writeResult(Request.Failure, InviteFail);
                                    break;
                                }
                            }
                            else
                            {
                                writeResult(Request.Error, Error);
                            }
                        }
                    }
                }
                else if (ServerConfig.structure == Structure.P2P)
                {
                    //Logik P2P Invite
                    List <User> list = package.kaEvents[0].members;

                    foreach (User member in list)
                    {
                        ///member is user in p2p topology
                        if (member.serverID == 0)
                        {
                            if (database.UserExist(member.name))
                            {
                                database.SaveInvites(member.name, package.kaEvents[0]);
                                writeResult(Request.Success, InviteSuccess);
                            }
                            else
                            {
                                P2PPackage p2p = new P2PPackage(member.name, package.kaEvents[0])
                                {
                                    P2Prequest = P2PRequest.Invite
                                };
                                p2p = P2PLogic.ResolveP2P(p2p);

                                switch (p2p.P2PAnswer)
                                {
                                case P2PAnswer.Success:
                                    writeResult(Request.Success, InviteSuccess);
                                    break;

                                case P2PAnswer.Failure:
                                    writeResult(Request.Failure, InviteFail);
                                    break;

                                default:
                                    writeResult(Request.Error, Error);
                                    break;
                                }
                            }
                        }
                        ///member is user in hierarchy topology
                        else
                        {
                            //Hierarchie Teil
                            HierarchiePackage hierarchie = new HierarchiePackage
                            {
                                HierarchieRequest = HierarchieRequest.Invite,
                                invite            = package.kaEvents[0],
                                login             = member.name,
                                destinationID     = member.serverID
                            };

                            //SendeTeil
                            Package sendPackage = new Package(hierarchie);
                            Package recievePackage;
                            recievePackage = Send(sendPackage, KnownServers.Root);

                            if (recievePackage != null)
                            {
                                switch (recievePackage.hierarchie.HierarchieAnswer)
                                {
                                case HierarchieAnswer.Success:
                                    writeResult(Request.Success, InviteSuccess);
                                    break;

                                case HierarchieAnswer.Failure:
                                    writeResult(Request.Failure, InviteFail);
                                    break;

                                default:
                                    writeResult(Request.Failure, InviteFail);
                                    break;
                                }
                            }
                            else
                            {
                                writeResult(Request.Error, Error);
                            }
                        }
                    }
                }
                break;

            case Request.AnswerInvite:
                //TODO für P2P und Hierarchisch
                database.AnswerInvite(package.kaEvents[0], package.user.name, package.answerInvite);
                break;

            default:
                Console.WriteLine(RequestUnknown);
                break;
            }

            return(package);

            void writeResult(Request request, string line)
            {
                Console.WriteLine(line);
                package.request = request;
            }
        }
Beispiel #4
0
 public void ChangeP2P()
 {
     ip = KnownServers.GetRandomWellKnownPeer();
 }