Пример #1
0
func (la *Libairline) CreateFlight(flight airlineproto.FlightInfo) error {
	log.Printf("[%s:%s] Start to add flight: %s\n", la.agencyid, la.myhostport, flight.ID)

	conn, err := la.lib_conn.GetServer(tribproto.GetAirlineFromFlightId(flight.ID))
	if err != nil {
		log.Printf("[%s:%s] Fail to add flight: %s\n", la.agencyid, la.myhostport, flight.ID)
		return lsplog.MakeErr("Airline doesn't exist")
	}

	args := &airlineproto.PutArgs{flight.ID, flight}
	var reply airlineproto.PutReply
	err = conn.Call("AirlineRPC.CreateFlight", args, &reply)
	if lsplog.CheckReport(2, err) {
		log.Printf("[%s:%s] Fail to add flight: %s\n", la.agencyid, la.myhostport, flight.ID)
		return lsplog.MakeErr("Flight already exist")
	}

	if reply.Status != airlineproto.OK {
		log.Printf("[%s:%s] Fail to add flight: %s\n", la.agencyid, la.myhostport, flight.ID)
		return lsplog.MakeErr("Flight already exist")
	}

	log.Printf("[%s:%s] Succeed to add flight: %s\n", la.agencyid, la.myhostport, flight.ID)
	return nil
}
Пример #2
0
func (la *Libairline) MakeBooking(flightids []string, isRemove bool) error {
	//TODO: Handle remove
	orderQuantity := 1

	if isRemove {
		orderQuantity = -1
	}

	orderGrpMap := make(map[string]*bufi.Buf)

	for _, v := range flightids {
		airlineId := tribproto.GetAirlineFromFlightId(v)
		orderList, exist := orderGrpMap[airlineId]
		if exist == false {
			orderList = bufi.NewBuf()
		}
		orderList.Insert(v)
		orderGrpMap[airlineId] = orderList
	}

	orders := make([]tranlayerproto.Order, len(orderGrpMap))
	i := 0
	for airlineId, orderList := range orderGrpMap {
		flightIds := ""
		for !orderList.Empty() {
			flightId := orderList.Remove().(string)
			if flightIds == "" {
				flightIds = flightId
			} else {
				flightIds = fmt.Sprintf("%s:%s", flightIds, flightId)
			}
		}

		order := tranlayerproto.Order{airlineId, flightIds, orderQuantity}
		orders[i] = order
		i++
	}
	err := la.lib_tranlayer.BookingFlights(orders)
	if lsplog.CheckReport(2, err) {
		return err
	}

	return nil
}
Пример #3
0
func (la *Libairline) GetFlight(flightid string) (airlineproto.FlightInfo, error) {
	conn, err := la.lib_conn.GetServer(tribproto.GetAirlineFromFlightId(flightid))
	if lsplog.CheckReport(2, err) {
		return airlineproto.FlightInfo{}, err
	}

	args := &airlineproto.GetArgs{flightid, false, ""}
	var reply airlineproto.GetReply
	err = conn.Call("AirlineRPC.GetFlight", args, &reply)
	if lsplog.CheckReport(2, err) {
		return airlineproto.FlightInfo{}, err
	}

	if reply.Status != airlineproto.OK {
		return airlineproto.FlightInfo{}, lsplog.MakeErr("Fetch flight failed")
	}

	return reply.FlightInfo, nil
}