Ejemplo n.º 1
0
func (a *WaitAgent) Process(event ebus.Event) error {
	var rest int
	a.err = event.Payload(&rest)
	if a.err != nil {
		return a.err
	}
	applog.Infof("wait: %d orders remaining", rest)
	if rest == 0 {
		a.done <- true
	}
	return nil
}
Ejemplo n.º 2
0
func (a *ShopAgent) Process(event ebus.Event) error {
	var order Order
	a.err = event.Payload(&order)
	if a.err != nil {
		return a.err
	}
	a.err = StartOrderAgent(order)
	if a.err != nil {
		return a.err
	}
	return nil
}
Ejemplo n.º 3
0
func (a *DeliveryAgent) Process(event ebus.Event) error {
	switch event.Topic() {
	case a.receivedTopic:
		// Received a new order.
		var order Order
		a.err = event.Payload(&order)
		if a.err != nil {
			return a.err
		}
		a.openOrders[order.OrderNo] = true
	case a.packedTopic:
		// Deliver the packed order.
		var shipment Shipment
		a.err = event.Payload(&shipment)
		if a.err != nil {
			return a.err
		}
		delete(a.openOrders, shipment.OrderNo)
		ebus.Emit(len(a.openOrders), "OrderDelivered")
	}
	if len(a.openOrders)%10 == 0 {
		applog.Infof("delivery: topic %q open orders are %d", event.Topic(), len(a.openOrders))
	}
	return nil
}
Ejemplo n.º 4
0
func (a *OrderAgent) Process(event ebus.Event) error {
	var shipment Shipment
	a.err = event.Payload(&shipment)
	if a.err != nil {
		return a.err
	}
	for itemNo := range shipment.ShipmentItems {
		delete(a.openItems, itemNo)
	}
	if len(a.openItems) == 0 {
		// All done, pack for delivery.
		shipment := Shipment{a.order.OrderNo, a.order.OrderNo, make(map[int]ShipmentItem)}
		for itemNo, item := range a.order.OrderItems {
			shipment.ShipmentItems[itemNo] = ShipmentItem{a.order.OrderNo, itemNo, item.Quantity}
		}
		ebus.Emit(shipment, "OrderPacked")
	}
	return nil
}
Ejemplo n.º 5
0
func (a *ManufacturerAgent) Process(event ebus.Event) error {
	switch event.Topic() {
	case a.orderedTopic:
		// Store a new manufacturing order.
		var item OrderItem
		a.err = event.Payload(&item)
		if a.err != nil {
			return a.err
		}
		a.openOrders[item.ItemNo] += item.Quantity
	case a.manufacturedTopic:
		// Deliver random numbers of each item.
		a.shipmentNo++
		shipment := Shipment{a.shipmentNo, 0, make(map[int]ShipmentItem)}
		for itemNo, quantity := range a.openOrders {
			shipmentQuantity := rand.Intn(quantity) + 1
			if quantity == shipmentQuantity {
				delete(a.openOrders, itemNo)
			} else {
				a.openOrders[itemNo] = quantity - shipmentQuantity
			}
			shipment.ShipmentItems[itemNo] = ShipmentItem{a.shipmentNo, itemNo, shipmentQuantity}
		}
		ebus.Emit(shipment, "ManufacturingShipped")
	}
	if len(a.openOrders)%10 == 0 {
		applog.Infof("manufacturer: topic %q open orders are %d", event.Topic(), len(a.openOrders))
	}
	return nil
}
Ejemplo n.º 6
0
func (a *WarehouseAgent) Process(event ebus.Event) error {
	switch event.Topic() {
	case a.orderedTopic:
		var item OrderItem
		a.err = event.Payload(&item)
		if a.err != nil {
			return a.err
		}
		// Request item from manufacturer.
		if a.openOrders[item.ItemNo] == nil {
			a.openOrders[item.ItemNo] = make(map[int]OrderItem)
		}
		a.openOrders[item.ItemNo][item.OrderNo] = item
		ebus.Emit(item, "ManufacturerItemOrdered")
	case a.shippedTopic:
		// Manufactured items are shipped, add to inventory.
		var shipment Shipment
		a.err = event.Payload(&shipment)
		if a.err != nil {
			return a.err
		}
		for itemNo, item := range shipment.ShipmentItems {
			a.inventory[itemNo] += item.Quantity
		}
	case a.preparedTopic:
		// Order items are prepared and can now be shipped.
		for itemNo, orderItems := range a.openOrders {
			for orderNo, item := range orderItems {
				if a.inventory[itemNo] >= item.Quantity {
					a.shipmentNo++
					shipment := Shipment{a.shipmentNo, item.OrderNo, make(map[int]ShipmentItem)}
					shipment.ShipmentItems[itemNo] = ShipmentItem{a.shipmentNo, itemNo, item.Quantity}
					ebus.Emit(shipment, "WarehouseShipped", item.OrderNo)
					a.inventory[itemNo] -= item.Quantity
					delete(orderItems, orderNo)
				}
			}
		}
	}
	return nil
}