Ejemplo n.º 1
0
        /// <summary>
        /// Starts when an incomming connection was requested
        /// </summary>            
        public void Accept(IAsyncResult result)
        {
            Status status = new Status();
            try
            {
                allDone.Set();

                status.Socket = ((Socket)result.AsyncState).EndAccept(result);
                WriteLog(status.Socket.RemoteEndPoint.ToString() + " connected");

                status.Socket.BeginReceive(status.buffer, 0, status.buffer.Length, SocketFlags.None, Receive, status);
            }
            catch (Exception e)
            {
                WriteLog(e.Message);
                Done(status);
                allDone.Set();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Deserializes and outputs the received object
        /// </summary>            
        public void Done(Status status)
        {
            Status send = status.DeSerialize();
            WriteLog("Received: " + send.msg + " from " + status.Socket.RemoteEndPoint.ToString());

            string[] commands = send.msg.Split(new char[] { ' ' });

            var uow = context.CreateUnitOfWork();

            List<Agreement> agreements = new List<Agreement>(uow.Agreements);
            List<Controller> controllers = new List<Controller>(uow.Controllers);
            List<Buyer> buyers = new List<Buyer>(uow.Buyers);
            List<Organization> organizations = new List<Organization>(uow.Organizations);

            var ser = new BinaryFormatter();
            byte[] bytes = null;

            try
            {
                switch (commands[0])
                {
                    case "select":
                        {
                            if (commands[1] == "agreements")
                            {
                                List<Agreement> r = new List<Agreement>(agreements);
                                using (var ms = new MemoryStream())
                                {
                                    ser.Serialize(ms, r);
                                    bytes = ms.ToArray();
                                }
                                WriteLog(send.msg + " OK");
                                send.msg = "request agreements";
                            }

                            if (commands[1] == "buyers")
                            {
                                List<Buyer> r = new List<Buyer>(buyers);

                                using (var ms = new MemoryStream())
                                {
                                    ser.Serialize(ms, r);
                                    bytes = ms.ToArray();
                                }
                                WriteLog(send.msg + " OK");
                                send.msg = "request buyers";

                            }

                            if (commands[1] == "controllers")
                            {
                                List<Controller> r = new List<Controller>(controllers);

                                using (var ms = new MemoryStream())
                                {
                                    ser.Serialize(ms, r);
                                    bytes = ms.ToArray();
                                }
                                WriteLog(send.msg + " OK");
                                send.msg = "request controllers";
                            }

                            if (commands[1] == "organizations")
                            {

                                List<Organization> r = new List<Organization>(organizations);

                                using (var ms = new MemoryStream())
                                {
                                    ser.Serialize(ms, r);
                                    bytes = ms.ToArray();
                                }
                                WriteLog(send.msg + " OK");
                                send.msg = "request organizations";
                            }

                            break;
                        }
                    case "insert":
                        {
                            if (commands[1] == "agreement")
                            {
                                Agreement a;
                                var serq = new BinaryFormatter();

                                using (var ms = new MemoryStream(send.content))
                                {
                                    a = (Agreement)serq.Deserialize(ms);
                                }

                                uow.Context.

                                uow.Add(a);
                                uow.SaveChanges();

                                using (var ms = new MemoryStream())
                                {
                                    ser.Serialize(ms, a);
                                    bytes = ms.ToArray();
                                    send.content = bytes;
                                }
                                WriteLog(send.msg + " OK");
                                send.msg = "OK";

                            }
                            if (commands[1] == "controller")
                            {
                                Controller c;
                                var serq = new BinaryFormatter();

                                using (var ms = new MemoryStream(send.content))
                                {
                                    c = (Controller)serq.Deserialize(ms);
                                }

                                uow.Add(c);
                                uow.SaveChanges();

                                using (var ms = new MemoryStream())
                                {
                                    ser.Serialize(ms, c);
                                    bytes = ms.ToArray();
                                    send.content = bytes;
                                }
                                WriteLog(send.msg + " OK");
                                send.msg = "OK";
                            }
                            if (commands[1] == "organization")
                            {
                                Organization o;
                                var serq = new BinaryFormatter();

                                using (var ms = new MemoryStream(send.content))
                                {
                                    o = (Organization)serq.Deserialize(ms);
                                }

                                uow.Add(o);
                                uow.SaveChanges();
                                using (var ms = new MemoryStream())
                                {
                                    ser.Serialize(ms, o);
                                    bytes = ms.ToArray();
                                    send.content = bytes;
                                }
                                WriteLog(send.msg + " OK");
                                send.msg = "OK";
                            }

                            if (commands[1] == "buyer")
                            {
                                Buyer b;
                                var serq = new BinaryFormatter();

                                using (var ms = new MemoryStream(send.content))
                                {
                                    b = (Buyer)serq.Deserialize(ms);
                                }

                                uow.Add(b);
                                uow.SaveChanges();
                                using (var ms = new MemoryStream())
                                {
                                    ser.Serialize(ms, b);
                                    bytes = ms.ToArray();
                                    send.content = bytes;
                                }
                                WriteLog(send.msg + " OK");
                                send.msg = "OK";
                            }
                        }
                        break;
                    //}//insert
                    //case "update":
                    //	{
                    //		if (commands[1] == "road")
                    //		{
                    //			Road r;
                    //			var serq = new BinaryFormatter();

                    //			using (var ms = new MemoryStream(send.content))
                    //			{
                    //				r = (Road)serq.Deserialize(ms);
                    //			}
                    //			var rts = from rt in _ctx.Routes
                    //					  where rt.RoadId == r.Id
                    //					  select rt;
                    //			foreach (var ro in rts)
                    //			{
                    //				_ctx.Routes.Remove(ro);
                    //			}
                    //			List<Route> query = new List<Route>(r.Routes.Where(x => x.Town != null));
                    //			int n = query.Count();
                    //			for (int j = 0; j < n; j++)
                    //			{
                    //				r.Routes.Remove(query[0] as Route);
                    //				query.RemoveAt(0);
                    //			}

                    //			_ctx.SaveChanges();
                    //			Road ur = _ctx.Roads.First(x => x.Id == r.Id);

                    //			ur.Index = r.Index;
                    //			ur.Length = r.Length;
                    //			ur.Name = r.Name;
                    //			foreach (var rt in r.Routes)
                    //			{
                    //				ur.Routes.Add(new Route { TownId = rt.TownId, RoadId = rt.RoadId });
                    //			}

                    //			_ctx.SaveChanges();
                    //			using (var ms = new MemoryStream())
                    //			{
                    //				ser.Serialize(ms, ur);
                    //				bytes = ms.ToArray();
                    //				send.content = bytes;
                    //			}
                    //			WriteLog(send.msg + " OK");
                    //			send.msg = "OK";
                    //		}
                    //		if (commands[1] == "town")
                    //		{
                    //			Town t;
                    //			var serq = new BinaryFormatter();

                    //			using (var ms = new MemoryStream(send.content))
                    //			{
                    //				t = (Town)serq.Deserialize(ms);
                    //			}

                    //			Town ut = _ctx.Towns.First(x => x.Id == t.Id);
                    //			ut.Inhabitants = t.Inhabitants;
                    //			ut.Name = t.Name;
                    //			ut.Photo = t.Photo;
                    //			_ctx.SaveChanges();

                    //			using (var ms = new MemoryStream())
                    //			{
                    //				ser.Serialize(ms, ut);
                    //				bytes = ms.ToArray();
                    //				send.content = bytes;
                    //			}
                    //			WriteLog(send.msg + " OK");
                    //			send.msg = "OK";
                    //		}
                    //		break;
                    //	}
                    //case "delete":
                    //	{
                    //		if (commands[1] == "road")
                    //		{
                    //			Road r;
                    //			var serq = new BinaryFormatter();

                    //			using (var ms = new MemoryStream(send.content))
                    //			{
                    //				r = (Road)serq.Deserialize(ms);
                    //			}

                    //			var query = from rd in _ctx.Routes
                    //						where rd.RoadId == r.Id
                    //						select rd;
                    //			foreach (var rd in query)
                    //			{
                    //				_ctx.Routes.Remove(rd);
                    //			}
                    //			var removed = _ctx.Roads.First(x => x.Id == r.Id);
                    //			_ctx.Roads.Remove(removed);
                    //			_ctx.SaveChanges();
                    //			WriteLog(send.msg + " OK");
                    //			send.msg = "OK";
                    //		}
                    //		if (commands[1] == "town")
                    //		{
                    //			Town t;
                    //			var serq = new BinaryFormatter();

                    //			using (var ms = new MemoryStream(send.content))
                    //			{
                    //				t = (Town)serq.Deserialize(ms);
                    //			}
                    //			var query = from r in _ctx.Routes
                    //						where r.TownId == t.Id
                    //						select r;
                    //			foreach (var r in query)
                    //			{
                    //				_ctx.Routes.Remove(r);
                    //			}
                    //			var removed = _ctx.Towns.First(x => x.Id == t.Id);
                    //			_ctx.Towns.Remove(removed);
                    //			_ctx.SaveChanges();
                    //			WriteLog(send.msg + " OK");
                    //			send.msg = "OK";
                    //		}
                    //		break;
                    //	}
                }
            }
            catch (Exception ex)
            {
                WriteLog(send.msg + " ERROR " + ex.Message);
                send.msg = send.msg + " ERROR " + ex.Message;
            }
            finally
            {
                send.Socket = status.Socket;
                send.content = bytes;

                byte[] data = send.Serialize();
                status.Socket.BeginSend(data, 0, data.Length, 0, new AsyncCallback(SendCallback), send);
            }
        }