示例#1
0
        public override void Execute(CommandData data)
        {
            var serverMessage = data as ServerCommandData;

            if (serverMessage == null || serverMessage.Connection == null)
            {
                throw new CoflnetException("connection_invalid", "Nothing connected");
            }

            var options = serverMessage.GetAs <LoginParams>();


            var user = data.CoreInstance.EntityManager.GetEntity <CoflnetUser>(options.id);

            if (user.Secret == null || options.secret == null || !user.Secret.SequenceEqual(options.secret))
            {
                throw new CoflnetException("secret_invalid", "The users secret is incorrect");
            }
            serverMessage.Connection.User = user;

            var response = CommandData.CreateCommandData <LoginUserResponse, EntityId>(user.Id, user.Id);

            response.SenderId = data.CoreInstance.Id;
            data.SendBack(response);
        }
示例#2
0
    public IEnumerator CreateUserTest()
    {
        // tell the server his id
        ConfigController.ApplicationSettings.id = new SourceReference(1, 1, 1, 0);
        ServerCore.Init();
        ClientSocket.Instance.Reconnect();
        CommandData response = null;

        ClientSocket.Instance.AddCallback(data => {
            response = data;
        });

        yield return(new UnityEngine.WaitForSeconds(1));

        // set data
        var request = new RegisterUserRequest();

        request.captchaToken = "";
        request.clientId     = ConfigController.ApplicationSettings.id;

        // send the command
        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <RegisterUser, RegisterUserRequest> (ConfigController.ApplicationSettings.id, request));

        // await response
        yield return(new UnityEngine.WaitForSeconds(0.5f));

        Logger.Log(response.GetAs <RegisterUserResponse> ().id);

        // user was created in the last second
        Assert.IsTrue(response.GetAs <RegisterUserResponse> ().id.ResourceId > ThreadSaveIdGenerator.NextId - 10000000);

        ServerCore.Stop();
    }
示例#3
0
    public void FirstStartupStoreValue()
    {
        FirstStartSetupController.Instance.RedoSetup(true);
        PrivacyService.Instance.privacyScreen = new AcceptAllScreen();
        var serverId = new EntityId(1, 1, 1, 0);

        DevCore.Init(serverId, true);

        Logger.Log(ConfigController.UserSettings.userId);

        var valueToStore = "abc123Hellou :D";

        // sometimes it comes to errors when the test is run alone and the static constructor
        // of CoflnetUser didn't register the SetUserKeyValue Command

        var data = CommandData.CreateCommandData <SetUserKeyValue, KeyValuePair <string, string> > (
            ConfigController.ActiveUserId,
            new KeyValuePair <string, string> ("mykey", valueToStore));

        data.SenderId = ConfigController.ActiveUserId;
        CoflnetCore.Instance
        .SendCommand(data);

        foreach (var item in DevCore.DevInstance.simulationInstances.Keys)
        {
            Logger.Log($"having {item}");
        }

        CoflnetCore.Instance.SendCommand <GetUserKeyValue, string> (ConfigController.ActiveUserId, "mykey", ConfigController.ActiveUserId, (d) => {
            Assert.AreEqual(valueToStore, d.GetAs <string>());
        });
    }
示例#4
0
    public void Remove(T element)
    {
        var data = new CommandData(Access.Owner);

        data.SerializeAndSet(CommandData.CreateCommandData <RemoteListRemoveCommand <T>, T>(this.Id, element));

        CoflnetCore.Instance.SendCommand(data);
    }
示例#5
0
    public IEnumerator MessageTransitPersitenceCrossServerTest()
    {
        // tell the server his id
        ConfigController.ApplicationSettings.id = new SourceReference(1, 1, 1, 0);
        ServerCore.Init();
        ClientSocket.Instance.Reconnect();
        CommandData response = null;

        ClientSocket.Instance.AddCallback(data => {
            response = data;
        });
        ConfigController.ActiveUserId = SourceReference.Default;

        yield return(new UnityEngine.WaitForSeconds(0.5f));

        // set data
        var request = new RegisterUserRequest();

        request.captchaToken = "";
        request.clientId     = ConfigController.ApplicationSettings.id;

        // send the command
        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <RegisterUser, RegisterUserRequest> (ConfigController.ApplicationSettings.id, request));

        // await response
        yield return(new UnityEngine.WaitForSeconds(0.5f));

        var login = response.GetAs <RegisterUserResponse> ();

        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <Coflnet.LoginUser, LoginParams> (
                ConfigController.ApplicationSettings.id,
                new LoginParams()
        {
            id     = login.id,
            secret = login.secret
        }));

        yield return(new WaitForSeconds(0.5f));

        // register second user on virtual other server
        var receiverUser = CoflnetUser.Generate(new SourceReference(1, 1, 2, 0));

        receiverUser.Id = new SourceReference(3, 1, 2, ThreadSaveIdGenerator.NextId);
        receiverUser.GetCommandController().OverwriteCommand <ChatMessageCommand> ();
        Logger.Log(receiverUser.Id);

        (new CoflnetUser()).GetCommandController().OverwriteCommand <TestCommandWithPermission> ();

        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <ChatMessageCommand, string> (receiverUser.Id, "hi"));

        yield return(new WaitForSeconds(0.5f));

        // message is saved
        Assert.IsTrue(MessagePersistence.ServerInstance.GetMessagesFor(receiverUser.Id).ToList().Any());
    }
示例#6
0
    public IEnumerator CreateAndLoginUserTest()
    {
        // tell the server his id
        ConfigController.ApplicationSettings.id = new SourceReference(1, 1, 1, 0);
        ServerCore.Init();
        CommandData response = null;

        ClientSocket.Instance.AddCallback(data => {
            response = data;
        });

        yield return(new UnityEngine.WaitForSeconds(1));

        // set data
        var request = new RegisterUserRequest();

        request.captchaToken = "";
        request.clientId     = ConfigController.ApplicationSettings.id;

        // send the command
        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <RegisterUser, RegisterUserRequest> (ConfigController.ApplicationSettings.id, request));

        // await response
        yield return(new UnityEngine.WaitForSeconds(0.5f));

        var login = response.GetAs <RegisterUserResponse> ();

        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <Coflnet.LoginUser, LoginParams> (ConfigController.ApplicationSettings.id,
                                                                            new LoginParams()
        {
            id     = login.id,
            secret = login.secret
        }));

        // tell the client that we are logged in
        ConfigController.ActiveUserId = login.id;

        yield return(new WaitForSeconds(0.5f));

        // register test command on user
        (new CoflnetUser()).GetCommandController().OverwriteCommand <TestCommandWithPermission> ();

        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <TestCommandWithPermission, int> (login.id, 1), true);

        yield return(new WaitForSeconds(0.5f));

        // user was created in the last second
        Assert.IsTrue(response.GetAs <int> () == 5);
    }
示例#7
0
    public void MessageDeliveryMultipleServerConnectedTest()
    {
        var alice = new EntityId(1, 123);
        var bob   = new EntityId(2, 555);

        DevCore.Init(alice);
        DevCore.DevInstance.AddClientCore(bob).OnMessage = m => {
            // when bob receives the message make sure it is valid
            Assert.AreEqual("msg", m.Type);
            return(false);
        };

        CoflnetCore.Instance.SendCommand(
            CommandData.CreateCommandData <ChatMessageCommand, string>(
                bob, "hi", 0, alice));
    }
示例#8
0
        /// <summary>
        /// Generates a new Resources On the server and returns a temporary proxy resource.
        /// When the server created the Resource it will be replaced locally.
        /// </summary>
        /// <param name="options">Options to pass along</param>
        /// <typeparam name="C"></typeparam>
        /// <returns>Temporary proxy object storing executed commands</returns>
        public Entity CreateEntity <C, T>(T options, EntityId sender = default(EntityId))
            where C : CreationCommand where T : CreationCommand.CreationParamsBase
        {
            options.options.OldId = EntityId.NextLocalId;

            var ownerId = this.Id;

            if (ownerId == default(EntityId))
            {
                ownerId = ConfigController.ManagingServer;
            }

            // create it locally
            // first craft CommandData
            var normaldata = CommandData.CreateCommandData <C, T>(ownerId, options, 0, this.Id);

            // wrap it in a special message data that captures the id
            var data = new CreationCommandData(normaldata);
            var core = this;            //new CreationCore(){ReferenceManager=this.ReferenceManager};

            //core.SetCommandsLive();

            data.CoreInstance = this;

            // execute it on the owner resource if possible
            if (EntityManager.Exists(sender))
            {
                EntityManager.GetResource(sender).ExecuteCommand(data);
            }
            else
            {
                Logger.Log("oh shot");
                core.ExecuteCommand(data);
            }

            // exeute it
            //core.ExecuteCommand(data);

            // remove the Redirect<see cref="Entity"/> again (todo)

            options.options.OldId = data.createdId;

            // create it on the server
            SendCommand <C, T>(ownerId, options, 0, sender);

            return(EntityManager.GetEntity <Entity>(data.createdId));
        }
示例#9
0
    public void MessageDeliverySameServerConnectedTest()
    {
        var alice = new EntityId(1, 123);
        var bob   = new EntityId(1, 555);

        DevCore.Init(alice);


        DevCore.DevInstance.AddClientCore(bob).OnMessage = m => {
            // received data is of type msg
            Assert.AreEqual("msg", m.Type);
            return(false);
        };


        CoflnetCore.Instance.SendCommand(
            CommandData.CreateCommandData <ChatMessageCommand, string>(
                bob, "hi", 0, alice));
    }
示例#10
0
    public IEnumerator MessageTransitPersitenceTest()
    {
        // tell the server his id
        ConfigController.ApplicationSettings.id = new SourceReference(1, 1, 1, 0);
        ServerCore.Init();
        CommandData response = null;

        ClientSocket.Instance.Reconnect();
        ClientSocket.Instance.AddCallback(data => {
            response = data;
        });

        ConfigController.ActiveUserId = SourceReference.Default;

        yield return(new UnityEngine.WaitForSeconds(0.5f));

        // set data
        var request = new RegisterUserRequest();

        request.captchaToken = "";
        request.clientId     = ConfigController.ApplicationSettings.id;

        // send the command
        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <RegisterUser, RegisterUserRequest> (ConfigController.ApplicationSettings.id, request));

        // await response
        yield return(new UnityEngine.WaitForSeconds(0.5f));

        var login = response.GetAs <RegisterUserResponse> ();

        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <Coflnet.LoginUser, LoginParams> (
                ConfigController.ApplicationSettings.id,
                new LoginParams()
        {
            id     = login.id,
            secret = login.secret
        }));

        yield return(new WaitForSeconds(0.5f));

        // register second user
        var receiverUser = CoflnetUser.Generate(ConfigController.ApplicationSettings.id);

        receiverUser.GetCommandController().OverwriteCommand <ChatMessageCommand> ();

        //register the command
        (new CoflnetUser()).GetCommandController().OverwriteCommand <TestCommandWithPermission> ();
        //tell the client what user we are
        ConfigController.ActiveUserId = login.id;
        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <ChatMessageCommand, string> (receiverUser.Id, "hi"));

        yield return(new WaitForSeconds(0.5f));

        // message is saved
        Assert.IsTrue(MessagePersistence.ServerInstance.GetMessagesFor(receiverUser.Id).ToList().Any());

        // connect with the receiver
        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <Coflnet.LoginUser, LoginParams> (
                ConfigController.ApplicationSettings.id,
                new LoginParams()
        {
            id     = receiverUser.Id,
            secret = receiverUser.Secret
        }));

        yield return(new WaitForSeconds(0.5f));

        // tell the Client socket the identity
        ConfigController.ActiveUserId = receiverUser.Id;

        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <Coflnet.ReceiveableResource.GetMessages, int> (receiverUser.Id, 0));

        yield return(new WaitForSeconds(0.5f));

        Logger.Log(response.type);
        // message is delivered
        Assert.AreEqual(response.GetAs <string> (), "hi");

        // message is deleted
        Assert.IsFalse(MessagePersistence.ServerInstance.GetMessagesFor(receiverUser.Id).ToList().Any());

        // clean up
        ServerCore.Stop();
    }