
/*
 * router.hh
 * Copyright (C) 2002 by John Heidemann <johnh@isi.edu>
 * $Id: router.hh,v 1.9 2002/02/20 02:09:17 johnh Exp $
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 */

/*
 * router.hh implements the router functionality except for the actual
 * routing algorithm
 */

#include <map>
#include <sys/types.h>  // socklen_t
#include <sys/socket.h>
#include <fstream>

#include "gen.hh"
#include "msg.hh"
#include "timers/timers.hh"
#include "linkstate.hh"

class RealRouter;

class ActivityCallback : public TimerCallback {
	// finish when no activity for QUIESCENT_TIME_SECS
protected:
	RealRouter *router_;
	bool active_;
	enum { QUIESCENT_TIME_SECS = 20 };

public:
	ActivityCallback(RealRouter* router) : router_(router), active_(true) {}
	virtual int expire();
	void active() { active_ = true; } // xxx: could cancel and remove it, too
	
};


class LSAFloodCallback : public TimerCallback {
	RealRouter *router_;
	int source_socket_;
	int origin_id_;
	int dest_id_;
	int seqno_;
	LSA *lsa_;
	/*
	 * outdated_ indicates a callback that's no longer relevant,
	 * either because it's been acked, or because this
	 * message has been superceeded by another with more 
	 * recent info.
	 */
	bool outdated_;
	int tx_count_;

public:
	LSAFloodCallback(RealRouter* router, int source_socket, int origin_id, int dest_id, LSA *lsa) : router_(router), source_socket_(source_socket), origin_id_(origin_id), dest_id_(dest_id), seqno_(lsa->seqno_), lsa_(lsa), outdated_(false), tx_count_(0) {}
	virtual int expire();

	int seqno() { return seqno_; }
	int origin_id() { return origin_id_; }
	int dest_id() { return dest_id_; }

	void outdate() { outdated_ = true; } // xxx: could cancel and remove it, too
	
};

class RealRouter : public BaseRouter {
protected:
	ofstream outstr_;

	int master_port_;
	int master_socket_;

	int port_;
	int socket_;

	int id_;
	int stage_;
	int loss_rate_;
	list<int> neighbor_ids_;
	list<int> neighbor_ports_;
	map<int,int> latest_lsa_seqno_;  // map id -> seqno for all routers
	LSA *last_lsa_;

	int next_seqno_;
	enum { DEFAULT_TTL = 15 };

	Timers *timers_;
	list<LSAFloodCallback*> cbs_;
	ActivityCallback act_cb_;

	LinkStateRouting *lsa_routing_;

	map<int,int> neighbor_id_to_port_;

        void init();
        void loop();

	void send_our_lsa();
	void handle_lsa(LSA * lsa, struct sockaddr *source_sa, int source_sa_len);
	void handle_lsa_ack(LSA *lsa, struct sockaddr *source_sap, int source_sa_len);
	void reliably_flood(LSA *lsa, int msec_delay);
public:
	RouterStats stats_;
	RealRouter(int master_port);

	int id() { return id_; }
	void assign(int stage, int id, int loss_rate);
	void add_neighbor_id_port(int id, int port);
	int neighbor_id_to_port(int id);

	void active() { act_cb_.active(); }
	void quiescent();
};

