/*
 *****************************************************************************
 *                                                                           *
 *              IMPINJ CONFIDENTIAL AND PROPRIETARY                          *
 *                                                                           *
 * This source code is the sole property of Impinj, Inc. Reproduction or     *
 * utilization of this source code in whole or in part is forbidden without  *
 * the prior written consent of Impinj, Inc.                                 *
 *                                                                           *
 * (c) Copyright Impinj, Inc. 2014. All rights reserved.                     *
 *                                                                           *
 *****************************************************************************/

package com.impinj.stp;

import com.impinj.stp.StpApi.AddTIDTableEntriesCommand;
import com.impinj.stp.StpApi.Packet;
import com.impinj.stp.StpApi.TIDTableEntries;
import com.impinj.stp.StpApi.Update;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

/*
 * 
 * Class is used to send Tid table entries to various devices
 */

public class TidTableSender {
    private List<InetAddress> tidEntryListeners;
    private boolean debug = false;
    private DatagramSocket udpSocket;

    public TidTableSender() throws IOException {
         tidEntryListeners = new ArrayList<InetAddress>();
         udpSocket = new DatagramSocket(Device.PORT);
         udpSocket.setSoTimeout(1000);  // 1 second
    }
  /**
    * Adds a Tid table Listener
    *
    * @param  ipOrHost of the Speedway Device. Can be an IP address or host name.
    */
     public void addTidTableListener(String ipOrHost) throws UnknownHostException {
         tidEntryListeners.add(InetAddress.getByName(ipOrHost));
     }
  /** Adds a collection of Tid table entries
    */
     public void addTidTableEntries(TIDTableEntries table_entries) throws IOException {
         if (tidEntryListeners.size() == 0)
            return;
         
         Packet packet = Packet.newBuilder()
              .setUpdate(
                    Update.newBuilder()
                        .setAddTidTableEntries(
                            AddTIDTableEntriesCommand.newBuilder()
                                .setTableEntries(table_entries)
                        )
              ).build();
         if (debug)
             System.out.println("Update: "+packet.getUpdate());
         byte packetBuf[] = Device.buildPayloadBuffer(packet.toByteArray());
         try {
             for (InetAddress tidListener : tidEntryListeners)
                 udpSend(packetBuf, tidListener);
         } catch (java.net.UnknownHostException e) {
             e.printStackTrace();
         }            
     }
     void udpSend(byte buf[], InetAddress ip) throws IOException {
       udpSocket.send(new DatagramPacket(buf, buf.length, ip, Device.PORT));
   }
     public void setDebug(boolean debug) {
        this.debug = debug;
    }
}