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; } } }
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; } } }
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 (); } } } } }
public MemoDbConnection() { if (DbServer == null) { DbServer = ConfigurationManager.AppSettings ["db.server"]; } if (DbPort == null) { DbPort = ConfigurationManager.AppSettings ["db.port"]; } if (DbUser == null) { DbUser = ConfigurationManager.AppSettings ["db.user"]; } if (DbDb == null) { DbDb = ConfigurationManager.AppSettings ["db.db"]; } string connectionString = "server="+ DbServer +";port="+ DbPort +";database=" + DbDb +";"+ "uid=" + DbUser +";password="******";Persist Security Info=True;"; Connection = new MySqlConnection (connectionString); if (!creationAttempt) { using (MemoDbContext contextDB = new MemoDbContext(Connection, false)) { contextDB.Database.CreateIfNotExists (); creationAttempt = true; } } Connection.Open (); }
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; } } }
public static User getAnon(MemoDbContext ctx) { bool loop = true; int threshold = 3; while (loop) { var q = from u in ctx.Users where u.Username.Equals (ANON_NAME) select u; if (q.Count () < 1) { initAnon (); threshold--; } else { return q.First (); } if (threshold < 1) { break; } } return null; }
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; } } }
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; } } }
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; } } }