/// <summary> /// This function is used to sort the list by spec requirement. /// Client is dumb so sorting is done on server /// </summary> /// <param name="obj">Right hand object to test</param> /// <returns>-1 if this is smaller than obj, otherwise 1</returns> public int CompareTo(object obj) { QueryResultFlight otherFlight = (QueryResultFlight)obj; if (price < otherFlight.price) { return(-1); } else if (price > otherFlight.price) { return(1); } else { if (seats > otherFlight.seats) { return(-1); } else if (seats < otherFlight.seats) { return(1); } else { return(flightNumber.CompareTo(otherFlight.flightNumber)); } } }
/// <summary> /// This function is delegated by the Client Query service. /// it will iterate all sellers and make the appropriate client requested /// query, returns search results to the client /// </summary> /// <param name="src">Source of flight</param> /// <param name="dst">Destination of flight</param> /// <param name="date">Date of flight</param> /// <returns>Flights from all sellers which match the input criterias</returns> public QueryResultFlights QueryFlights(string src, string dst, string date) { Console.WriteLine("FlightSearchServer: " + dst + " " + src + " " + date); try // Sanitize date { DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture); } catch (Exception) { throw new FlightSearchServerBadDate(); } QueryResultFlights flights = new QueryResultFlights(); foreach (var seller in sellers.Keys) { FlightQuery fq = new FlightQuery(); fq.src = src; fq.dst = dst; fq.date = DateTime.Parse(date); using (new OperationContextScope((IContextChannel)sellers[seller])) { try { Flights sellerFlights = sellers[seller].GetFlights(fq); // DEAL WITH EXCEPTIONS HERE foreach (var sellerFlight in sellerFlights) { QueryResultFlight f1 = (QueryResultFlight)sellerFlight; f1.name = seller; flights.Add(f1); } } catch (FaultException e) { Console.WriteLine("Seller {0} failed with {1}, ignoring.", seller, e.Reason.ToString()); } catch (Exception e) { Console.WriteLine("Seller {0} {1} malfunction: \n{2}", seller, "search", e.Message.ToString()); ITicketSellingQueryService victim; sellers.TryRemove(seller, out victim); } } } flights.Sort(); return(flights); }