Ejemplo n.º 1
0
 void ServerClientEntity_OnChange(ServerClientEntity o, EventArgs e)
 {
     foreach (var subscriber in subscribedUsers)
     {
         var op = new entityOperation() { operation = 'u', entity = this };
         Dem2Hub.sendItTo(op, subscriber.Value.connection);
     }
 }
Ejemplo n.º 2
0
        internal static void StoreToDB(ServerClientEntity entity)
        {
            using (Raven.Client.IDocumentSession session = Dem2Hub.docDB.OpenSession())
            {
                session.Store(entity);

                session.SaveChanges();
            }
        }
Ejemplo n.º 3
0
        public void respondToReadRequest(IWebSocketConnection socket)       //"r" operation, respond to it can be only "u" (update) or "n" (not found)
        {
            Vote vote = null;
            ServerClientEntity foundEntity = EntityRepository.GetEntityFromSetsByID(entity.Id);

            if (foundEntity != null)
            {
                try
                {
                    if (foundEntity is VotableItem)    //check if the entity we are responding with is a VotableItem or not, props to http://www.hanselman.com/blog/DoesATypeImplementAnInterface.aspx
                    {
                        vote = EntityRepository.allVotes.FirstOrDefault(x => x.subjectId == entity.Id && x.OwnerId == socket.ConnectionInfo.Cookies["user"]);
                    }
                    //var comments = new List<Comment>();

                    //comments = EntityRepository.allComments.Where(x => x.parentId == entity.Id).ToList();
                    //foreach (var comment in comments)
                    //{
                    //    entityOperation createCommentOp = new entityOperation { entity = comment, operation = 'u' };
                    //    createCommentOp.sendTo(socket);
                    //}
                }
                catch (Exception ex)
                {
                    if (ExceptionIsCriticalCheck.IsCritical(ex))
                    {
                        throw;
                    }
                }

                if (entity.version < foundEntity.version)
                {
                    //first possible outcome
                    entity = foundEntity;    //so if the version on client is current by any chance we send back the same as we received
                }
                //second possible outcome
                operation = 'u';
                //client must check whether the update he received has greater version of entity, if not, he knows he has the latest version of entity
                entity = foundEntity;
            }
            else
            {
                //third possible outcome
                operation = 'n';        //not found, nonexistent, in the entity field there is still the one that was in the request, so when it arrives back to the client he will know which request ended up not found
            }
            sendTo(socket);

            if (vote != null)   //votable Item needs to be sent to client before the vote on a votable itself
            {
                entityOperation sendVoteOp = new entityOperation {
                    entity = vote, operation = 'u'
                };
                sendVoteOp.sendTo(socket);
            }
        }
Ejemplo n.º 4
0
 void ServerClientEntity_OnChange(ServerClientEntity o, EventArgs e)
 {
     foreach (var subscriber in subscribedUsers)
     {
         var op = new entityOperation()
         {
             operation = 'u', entity = this
         };
         Dem2Hub.sendItTo(op, subscriber.Value.connection);
     }
 }
Ejemplo n.º 5
0
        public bool Equals(ServerClientEntity second)
        {
            // If parameter is null return false:
            if ((object)second == null)
            {
                return(false);
            }

            // Return true if the fields match:
            return(Id == second.Id);
        }
Ejemplo n.º 6
0
        internal static bool Remove(ServerClientEntity entity)
        {
            var success = all.Remove(entity);

            Console.WriteLine("Removing entity {0} with id {1} ended with succes:{2}", entity.ToString(), entity.Id, success);
            using (var session = Dem2Hub.docDB.OpenSession())
            {
                session.Advanced.Defer(new DeleteCommandData {
                    Key = entity.Id
                });
                session.SaveChanges();
            }
            return(success);
        }
Ejemplo n.º 7
0
        public static bool Add(ServerClientEntity entity)
        {
            var succes = all.Add(entity);

            if (succes)
            {
                StoreToDB(entity);
                entity.IncrementVersion();      //this should increment from 0 to 1
                Console.WriteLine("Adding entity {0} ended with succes:{1}", entity.ToString(), succes);
            }
            else
            {
                Console.WriteLine("Entity was not added because the same entity is already in hashset.");
            }
            return(succes);
        }
Ejemplo n.º 8
0
        public override bool Equals(System.Object obj)
        {
            // If parameter is null return false.
            if (obj == null)
            {
                return(false);
            }

            // If parameter cannot be cast to Point return false.
            ServerClientEntity second = obj as ServerClientEntity;

            if ((System.Object)second == null)
            {
                return(false);
            }

            // Return true if the fields match:
            return(Id == second.Id);
        }
Ejemplo n.º 9
0
        public static ServerClientEntity GetEntityFromSetsByID(string Id)
        {
            string             type           = Id.Split('/')[0];
            ServerClientEntity entityOnServer = null;

            try
            {
                entityOnServer = EntityRepository.entityNamesToSets[type].FirstOrDefault(x => x.Id == Id);
            }
            catch (Exception ex)
            {
                if (ExceptionIsCriticalCheck.IsCritical(ex))
                {
                    throw;
                }
                Console.WriteLine("Entity with ID {0} was not found", Id);
                return(null);
            }
            return(entityOnServer);
        }
Ejemplo n.º 10
0
        internal void resolveEntityRequest(IWebSocketConnection socket, JObject receivedObj)
        {
            switch (operation)
            {
            case 'c':       //create new user generated entity
                /*
                 * //Example shows json which creates new Vote for the present user
                 * {
                 *   "msgType": "entity",
                 *   "operation": "c",
                 *   "className": "Vote",
                 *   "entity": {"subjectID": "voting/215", "Agrees": true}
                 * }
                 *
                 * // TODO implement create spam check here
                 */
            {
                try
                {
                    var  className  = (string)receivedObj["className"];
                    Type type       = Type.GetType("Dem2UserCreated." + className);
                    var  sourceJSON = receivedObj["entity"].ToString();

                    var instance = JsonConvert.DeserializeObject(sourceJSON, type, new IsoDateTimeConverter());
                    entity         = (ServerClientEntity)instance;
                    entity.OwnerId = fromUser.Id;
                    entity.Subscribe(fromUser);
                    switch (className)
                    {
                    case "Vote":
                        var newVote = (Vote)instance;              //the serialized entity must be initialized
                        newVote.InitVote(fromUser);                //the after initialization, we return the entity back
                        Console.WriteLine("Created new vote with Id {0}", newVote.Id);
                        break;

                    case "Comment":
                        var newComment = (Comment)instance;
                        EntityRepository.Add(newComment);
                        Console.WriteLine("Created new comment with Id {0}", newComment.Id);
                        break;

                    case "Listing":
                        var newListing = (Listing)instance;
                        newListing.JSONQuery.sourceJSON = receivedObj["entity"]["JSONQuery"].ToString();;
                        var added = EntityRepository.Add(newListing);
                        if (added == false)
                        {
                            entity = EntityRepository.allListings.Single(x => x.JSONQuery.sourceJSON == sourceJSON);
                            entity.Subscribe(fromUser);
                            sendTo(socket);
                        }

                        Console.WriteLine("Created new listing with Id {0}", newListing.Id);
                        break;

                    default:
                        break;
                    }

                    //var op = new entityOperation() { operation = 'c', entity = instance as ServerClientEntity };
                    //
                    Console.WriteLine("Object {0} created", instance.ToString());
                }
                catch (Exception)
                {
                    throw;
                }
            }
            break;

            case 'r':       //read an entity
                /*
                 * Example shows json for this branch
                 * {
                 * "msgType": "entity",
                 * "operation": "r",
                 * "entity":{
                 *    "Id": "user/132"
                 * }
                 * }*/
                respondToReadRequest(socket);
                break;

            case 'u':       //read an entity
                /*
                 * Example shows json for this branch
                 * {
                 * "msgType": "entity",
                 * "operation": "r",
                 * "entity":{
                 *    "Id": "user/132"
                 * }
                 * }*/
                respondToUpdateRequest(receivedObj);
                break;

            case 'd':     //delete an entity
                /*
                 * {
                 *  "msgType": "entity",
                 *  "operation": "d",
                 *  "entity": {"Id": "voting/215"}
                 * }
                 *
                 */
            {
                string IdPrefixStr = entity.Id.Split('/')[0];
                Type   type        = EntityRepository.GetRepoTypeById(IdPrefixStr);

                var instance = JsonConvert.DeserializeObject(receivedObj["entity"].ToString(), type, new IsoDateTimeConverter());

                {
                    var Id = (string)receivedObj["entity"]["Id"];
                    ServerClientEntity toDelete = EntityRepository.GetEntityFromSetsByID(Id);
                    var deletor = socket.ConnectionInfo.Cookies["user"];
                    if (deletor == toDelete.OwnerId)
                    {
                        var success = toDelete.Delete();
                        if (success)
                        {
                            if (toDelete is Vote)
                            {           // update the subject
                                Vote aVote   = (Vote)toDelete;
                                var  subject = EntityRepository.GetEntityFromSetsByID(aVote.subjectId);
                                subject.IncrementVersion();
                            }
                            var op = new entityOperation()
                            {
                                operation = 'd', entity = new ServerClientEntity(toDelete.Id, toDelete.version)
                            };
                            Dem2Hub.sendItTo(op, socket);
                        }
                    }
                    else
                    {
                        var err = new ServerError()
                        {
                            message = "sorry, you can't delete the entity id " + Id
                        };
                        Dem2Hub.sendItTo(err, socket);
                    }
                }
            }
            break;

            default:
                Console.WriteLine("Unknown type of request: {0}", operation);
                break;
            }
        }
Ejemplo n.º 11
0
        public bool Equals(ServerClientEntity second)
        {
            // If parameter is null return false:
            if ((object)second == null)
            {
                return false;
            }

            // Return true if the fields match:
            return Id == second.Id;
        }