Example #1
0
        public bool kickoutPeer(string hash, string kickerHash)
        {
            using (MemoDbConnection conn = new MemoDbConnection ()) {
                conn.BeginTransaction ();
                try {
                    using (MemoDbContext ctx = new MemoDbContext (conn.Connection, false)) {
                        var qp = from p in ctx.Peers
                            where
                            p.MAC_AddressHash.Equals (kickerHash)
                            select p;

                        if (qp.Count () != 1) {
                            conn.CommitTransaction ();
                            return false;
                        }

                        var qk = from p in ctx.Peers
                            where
                            p.MAC_AddressHash.Equals (kickerHash)
                            select p;

                        foreach(Peer p in qk){
                            ctx.Peers.Remove(p);
                        }
                        ctx.SaveChanges();
                    }
                    conn.CommitTransaction ();
                    return true;
                } catch {
                    conn.RollbackTransaction ();
                    return false;
                }
            }
        }
Example #2
0
        public void putMemo(string message, string to, string from, string hash, Uri clientUri)
        {
            Memo memo = new Memo(message, from);

            using (MemoDbConnection conn = new MemoDbConnection())
            {
                conn.BeginTransaction();
                try {
                    using (MemoDbContext ctx = new MemoDbContext(conn.Connection, false))
                    {
                        ctx.Database.UseTransaction(conn.Transaction);

                        var q = from u in ctx.Users where
                            u.Username.Equals (to)
                            select u;

                        if (q.Count() != 1) {
                            conn.CommitTransaction();
                            return;
                        }
                        User toUser = q.First();

                        ctx.Memos.Add(memo);
                        toUser.Memos.Add(memo);
                        ctx.SaveChanges();
                    }
                    conn.CommitTransaction();
                }
                catch
                {
                    conn.RollbackTransaction();
                    throw;
                }
            }
        }
Example #3
0
        public static void initAnon()
        {
            using (MemoDbConnection conn = new MemoDbConnection()) {
                using (MemoDbContext ctx = new MemoDbContext(conn.Connection, false))
                {
                    //var q = from u in ctx.Users where
                    //        u.Username.Equals (ANON_NAME)
                    //		select u;

                    var q = from u in ctx.Users	select u;

                    System.Console.WriteLine (q == null);
                    if (q.Count() < 1) {
                        var anon = new User ();
                        anon.Username = ANON_NAME;
                        anon.LastOnline = DateTime.Now;
                        anon.Password = "******";

                        conn.BeginTransaction ();
                        try {
                            ctx.Database.UseTransaction (conn.Transaction);
                            ctx.Users.Add (anon);
                            ctx.SaveChanges ();

                            conn.CommitTransaction ();
                        } catch {
                            conn.RollbackTransaction ();
                        }
                    }
                }
            }
        }
Example #4
0
        public bool registerNick(string nick, string hash, Uri clientUri)
        {
            using (MemoDbConnection conn = new MemoDbConnection())
            {
                conn.BeginTransaction();
                try {
                    using (MemoDbContext ctx = new MemoDbContext(conn.Connection, false))
                    {
                        var qu = from u in ctx.Users where
                            u.Username.Equals (nick)
                            select u;

                        if (qu.Count() > 0) {
                            foreach(User u in qu){
                                if(u.LastOnline.Add(timeout).CompareTo(DateTime.UtcNow) > 0){
                                    conn.CommitTransaction();
                                    return false;
                                } else {
                                    ctx.Users.Remove(u);
                                }
                            }
                        }

                        User nu = new User {
                            LastOnline = DateTime.UtcNow,
                            Username = nick,
                            Password = hash
                        };

                        ctx.Users.Add(nu);
                        ctx.SaveChanges();

                    }
                    conn.CommitTransaction();
                    return true;
                }
                catch
                {
                    conn.RollbackTransaction();
                    return false;
                }
            }
        }
Example #5
0
        public List<MemoData> getMyMemos(string nick, string hash, Uri clientUri)
        {
            List<MemoData>  memoData = new List<MemoData> ();
            using (MemoDbConnection conn = new MemoDbConnection())
            {
                conn.BeginTransaction();
                try {
                    using (MemoDbContext ctx = new MemoDbContext(conn.Connection, false))
                    {
                        ctx.Database.UseTransaction(conn.Transaction);

                        var q = from u in ctx.Users where
                            u.Username.Equals (nick)
                            select u;

                        if (q.Count() != 1) {
                            conn.CommitTransaction();
                            return null;
                        }
                        User toUser = q.First();

                        foreach(Memo m in toUser.Memos){
                            memoData.Add(
                                new MemoData {
                                    Message = m.Message,
                                    Sender = m.Sender,
                                    Timestamp = m.Timestamp
                                }
                            );
                        }
                    }
                    conn.CommitTransaction();
                    return memoData;
                }
                catch
                {
                    conn.RollbackTransaction();
                    throw;
                }
            }
        }
Example #6
0
        public bool registerPeer(string hash, Uri clientUri)
        {
            using (MemoDbConnection conn = new MemoDbConnection ()) {
                conn.BeginTransaction ();
                try {
                    using (MemoDbContext ctx = new MemoDbContext (conn.Connection, false)) {
                        var qp = from p in ctx.Peers
                                 where
                            p.MAC_AddressHash.Equals (hash)
                                 select p;

                        if (qp.Count () > 1) {
                            conn.CommitTransaction ();
                            return false;
                        }
                        Peer peer;
                        if (qp.Count () == 0) {
                            peer = new Peer {
                                Address = clientUri,
                                MAC_AddressHash = hash
                            };
                        } else {
                            peer = qp.First ();
                        }

                        ctx.Peers.Add(peer);
                        ctx.SaveChanges();
                    }
                    conn.CommitTransaction ();
                    return true;
                } catch {
                    conn.RollbackTransaction ();
                    return false;
                }
            }
        }
Example #7
0
        public List<Uri> getPeerList()
        {
            using (MemoDbConnection conn = new MemoDbConnection ()) {
                conn.BeginTransaction ();
                try {
                    List<Uri> list = new List<Uri>();
                    using (MemoDbContext ctx = new MemoDbContext (conn.Connection, false)) {

                        var qp = from p in ctx.Peers select p;

                        foreach(Peer p in qp){

                            list.Add(p.Address);
                        }

                    }
                    conn.CommitTransaction ();
                    return list;
                } catch {
                    conn.RollbackTransaction ();
                    return null;
                }
            }
        }
Example #8
0
        public void putPublicMemo(string message, string sender)
        {
            Memo memo = new Memo(message, sender);

            using (MemoDbConnection conn = new MemoDbConnection())
            {
                conn.BeginTransaction();
                try {
                    using (MemoDbContext ctx = new MemoDbContext(conn.Connection, false))
                    {
                        User Anon = AnonymousUser.getAnon(ctx);
                        //ctx.Database.Log = (string log) => { Console.WriteLine(log); };
                        ctx.Database.UseTransaction(conn.Transaction);
                        ctx.Memos.Add(memo);
                        Anon.Memos.Add(memo);
                        ctx.SaveChanges();
                    }
                    conn.CommitTransaction();
                }
                catch
                {
                    conn.RollbackTransaction();
                    throw;
                }
            }
        }