Пример #1
0
        public static void registerPeer(string hash, Uri clientUri)
        {
            using (MemoDbConnection conn = new MemoDbConnection())
            {
                conn.BeginTransaction();
                try {
                    using (MemoDbContext ctx = new MemoDbContext(conn.Connection, false))
                    {
                        ctx.Database.UseTransaction(conn.Transaction);

                        var qp = from p in ctx.Peers
                            where
                            p.MAC_AddressHash.Equals (hash)
                            select p;

                        if (qp.Count () > 1) {
                            throw new PeerAlreadyRegistratedException();
                        }
                        Peer peer;
                        if (qp.Count () == 0) {
                            peer = new Peer {
                                Address = clientUri.ToString(),
                                MAC_AddressHash = hash
                            };
                        } else {
                            peer = qp.First ();
                        }

                        ctx.Peers.Add(peer);
                        ctx.SaveChanges();
                    }
                    conn.CommitTransaction ();
                }
                catch (PeerAlreadyRegistratedException pare){
                    conn.RollbackTransaction();
                    throw pare;
                }
                catch (Exception e)
                {
                    conn.RollbackTransaction();

                    System.Console.Error.WriteLine (e.Message);
                    System.Console.Error.WriteLine (e.StackTrace);
                    throw new PeerRegistrationFailedException(e.Message, e);
                }
            }
        }
Пример #2
0
        static void Debug()
        {
            Console.WriteLine ("Debug");

            using (MemoDbConnection conn = new MemoDbConnection())
            {
                conn.BeginTransaction();
                try {
                    using (MemoDbContext ctx = new MemoDbContext(conn.Connection, false))
                    {
                        ctx.Database.Log = (string message) => { Console.WriteLine(message); };
                        ctx.Database.UseTransaction(conn.Transaction);

                        Memo testMemo = new Memo("Hello", "d");
                        User anon = new User {
                            Username = "******",
                            Password = "******",
                            LastOnline = DateTime.UtcNow
                        };
                        Peer peer = new Peer {
                            Address = new Uri("http://localhost:8088").ToString(),
                            MAC_AddressHash = "pass"
                        };

            //						ctx.Memos.Add(testMemo);
            //						ctx.Users.Add(anon);
            //						ctx.Peers.Add(peer);
            //						anon.Memos.Add(testMemo);
            //						ctx.SaveChanges();
                    }

                    conn.CommitTransaction();
                }
                catch
                {
                    conn.RollbackTransaction();
                    throw;
                }
            }

            try {
                NickManager.registerNick ("nickkk", "MAChashMD5");
                System.Console.WriteLine ("registration OK");
            } catch (Exception ex) {
                System.Console.WriteLine (ex.Message);
            }

            try {
                NickManager.refreshNick ("nickkk", "MAChashMD5");
                System.Console.WriteLine ("refresh OK");
            } catch (Exception ex) {
                System.Console.WriteLine (ex.Message);
            }

            try {
                NickManager.refreshNick ("nickkkkk", "MAChashMD5");
                System.Console.WriteLine ("refresh OK");
            } catch (Exception ex) {
                System.Console.WriteLine (ex.Message);
            }
            System.Console.WriteLine ();

            try {
                PeerManager.registerPeer("ddsgsgsdffcdsfg", new Uri ("http://localhost:53215") );
                System.Console.WriteLine ("peer registration OK");
            } catch (Exception ex) {
                System.Console.WriteLine (ex.Message);
            }
            System.Console.WriteLine ();

            try {
                MemoManager.putMemo("hali", "Anonymous", "d");
                MemoManager.putMemo("hali", "nickkk", "d");
                MemoManager.putMemo("hali", "nickkkkkkkkkk", "d");
                System.Console.WriteLine ("Memo added!");
            } catch (Exception ex) {
                System.Console.WriteLine (ex.Message);
            }
            System.Console.WriteLine ();
        }