/*
 * Copyright (C) 1994-2018 Altair Engineering, Inc.
 * For more information, contact Altair at www.altair.com.
 *
 * This file is part of the PBS Professional ("PBS Pro") software.
 *
 * Open Source License Information:
 *
 * PBS Pro is free software. You can redistribute it and/or modify it under the
 * terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * PBS Pro 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Commercial License Information:
 *
 * For a copy of the commercial license terms and conditions,
 * go to: (http://www.pbspro.com/UserArea/agreement.html)
 * or contact the Altair Legal Department.
 *
 * Altair’s dual-license business model allows companies, individuals, and
 * organizations to create proprietary derivative works of PBS Pro and
 * distribute them - whether embedded or bundled with other software -
 * under a commercial license agreement.
 *
 * Use of Altair’s trademarks, including but not limited to "PBS™",
 * "PBS Professional®", and "PBS Pro™" and Altair’s logos is subject to Altair's
 * trademark licensing policies.
 *
 */


/**
 * @file	enc_reply.c
 * @brief
 * encode_DIS_reply() - encode a Batch Protocol Reply Structure
 *
 * 	batch_reply structure defined in libpbs.h, it must be allocated
 *	by the caller.
 */

#include <pbs_config.h>   /* the master config generated by configure */

#include "libpbs.h"
#include "list_link.h"
#include "attribute.h"
#include "dis.h"
#include "net_connect.h"

int encode_DIS_svrattrl(int sock, svrattrl *psattl);


/**
 * @brief-
 *      encode a Batch Protocol Reply Structure for a Command
 *
 * Note: batch_reply structure defined in libpbs.h, it must be allocated
 *       by the caller.
 *
 * @param[in] sock - socket descriptor
 * @param[in] reply - pointer to batch_reply structure
 *
 * @return      int
 * @retval      -1      error
 * @retval      0       Success
 *
 */

static int
encode_DIS_reply_inner(int sock, struct batch_reply *reply)
{
	int		    ct;
	int		    i;
	struct brp_select  *psel;
	struct brp_status  *pstat;
	svrattrl	   *psvrl;

	int rc;


	/* next encode code, auxcode and choice (union type identifier) */

	if ((rc = diswsi(sock, reply->brp_code)) 	||
		(rc = diswsi(sock, reply->brp_auxcode))	||
		(rc = diswui(sock, reply->brp_choice)))
			return rc;

	switch (reply->brp_choice) {

		case BATCH_REPLY_CHOICE_NULL:
			break;	/* no more to do */

		case BATCH_REPLY_CHOICE_Queue:
		case BATCH_REPLY_CHOICE_RdytoCom:
		case BATCH_REPLY_CHOICE_Commit:
			if ((rc = diswst(sock, reply->brp_un.brp_jid)) != 0)
				return (rc);
			break;

		case BATCH_REPLY_CHOICE_Select:

			/* have to send count of number of strings first */

			ct = 0;
			psel = reply->brp_un.brp_select;
			while (psel) {
				++ct;
				psel = psel->brp_next;
			}
			if ((rc = diswui(sock, ct)) != 0)
				return rc;

			psel = reply->brp_un.brp_select;
			while (psel) {
				/* now encode each string */
				if ((rc = diswst(sock, psel->brp_jobid)) != 0)
					return rc;
				psel = psel->brp_next;
			}
			break;

		case BATCH_REPLY_CHOICE_Status:

			/* encode "server version" of status structure.
			 *
			 * Server always uses svrattrl form.
			 * Commands decode into their form.
			 *
			 * First need to encode number of status objects and then
			 * the object itself.
			 */

			ct = 0;
			pstat = (struct brp_status *)GET_NEXT(reply->brp_un.brp_status);
			while (pstat) {
				++ct;
				pstat =(struct brp_status *)GET_NEXT(pstat->brp_stlink);
			}
			if ((rc = diswui(sock, ct)) != 0)
				return rc;
			pstat = (struct brp_status *)GET_NEXT(reply->brp_un.brp_status);
			while (pstat) {
				if ((rc = diswui(sock, pstat->brp_objtype))	||
					(rc = diswst(sock, pstat->brp_objname)))
						return rc;

				psvrl = (svrattrl *)GET_NEXT(pstat->brp_attr);
				if ((rc = encode_DIS_svrattrl(sock, psvrl)) != 0)
					return rc;
				pstat =(struct brp_status *)GET_NEXT(pstat->brp_stlink);
			}
			break;

		case BATCH_REPLY_CHOICE_Text:

			/* text reply */

			rc = diswcs(sock, reply->brp_un.brp_txt.brp_str,
				(size_t)reply->brp_un.brp_txt.brp_txtlen);
			if (rc)
				return rc;
			break;

		case BATCH_REPLY_CHOICE_Locate:

			/* Locate Job Reply */

			if ((rc = diswst(sock, reply->brp_un.brp_locate)) != 0)
				return rc;
			break;

		case BATCH_REPLY_CHOICE_RescQuery:

			/* Query Resources Reply */

			ct = reply->brp_un.brp_rescq.brq_number;
			if ((rc = diswui(sock, ct)) != 0)
				return rc;
			for (i=0; (i<ct) && (rc == 0); ++i) {
				rc =diswui(sock, *(reply->brp_un.brp_rescq.brq_avail+i));
			}
			if (rc) return rc;
			for (i=0; (i<ct) && (rc == 0); ++i) {
				rc =diswui(sock, *(reply->brp_un.brp_rescq.brq_alloc+i));
			}
			if (rc) return rc;
			for (i=0; (i<ct) && (rc == 0); ++i) {
				rc =diswui(sock, *(reply->brp_un.brp_rescq.brq_resvd+i));
			}
			if (rc) return rc;
			for (i=0; (i<ct) && (rc == 0); ++i) {
				rc =diswui(sock, *(reply->brp_un.brp_rescq.brq_down+i));
			}
			if (rc) return rc;
			break;

		default:
			return -1;
	}

	return 0;
}

int
encode_DIS_reply(int sock, struct batch_reply *reply)
{
	int rc;
	/* first encode "header" consisting of protocol type and version */

	if ((rc = diswui(sock, PBS_BATCH_PROT_TYPE))   ||
		(rc = diswui(sock, PBS_BATCH_PROT_VER)))
			return rc;

	return (encode_DIS_reply_inner(sock, reply));
}

int
encode_DIS_replyRPP(int sock, char *rppcmd_msgid, struct batch_reply *reply)
{
	int rc;
	/* first encode "header" consisting of protocol type and version */

	/* since it RPP, write a header */
	if ((rc = is_compose(sock, IS_CMD_REPLY)) != DIS_SUCCESS)
		return rc;

	/* for IS_CMD_REPLY, also send across the rppcmd_msgid, so that
	 * server can match the reply with the request it had sent earlier
	 */
	if ((rc = diswst(sock, rppcmd_msgid)) != DIS_SUCCESS)
		return rc;

	return (encode_DIS_reply_inner(sock, reply));
}
