/*
 * libsyncml - A syncml protocol implementation
 * Copyright (C) 2005  Armin Bauer <armin.bauer@opensync.org>
 * Copyright (C) 2008-2009  Michael Bell <michael.bell@opensync.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 */

/**
 * @defgroup SmlObjectDatastoreServerPrivate SyncML Datastore Server Object Internals
 * @ingroup PrivateObjectAPI
 * @brief The public part
 * 
 */
/*@{*/

#ifndef _SML_DS_SERVER_INTERNALS_H_
#define _SML_DS_SERVER_INTERNALS_H_

#include "sml_ds_server.h"

typedef struct SmlWriteContext {
	SmlDsSessionChangeStatusCb callback;
	SmlDataSyncChangeItem *item;
	SmlStatus *status;
	void *userdata;
	SmlDsSession *session;
} SmlWriteContext;

struct SmlDsSession {
	gint refCount;
	
	SmlDsServer *server;
	SmlSession *session;
	
	SmlLocation *target;
	SmlLocation *location;
	
	/** Callback that will receive the alert once it is received */
	SmlDsSessionAlertCb recvAlertCallback;
	void *recvAlertCallbackUserdata;
	SmlCommand *alertCommand;
	
	/** Callback that will receive the reply to the alert */
	SmlStatusReplyCb sentAlertCallback;
	void *sentAlertCallbackUserdata;
	
	/** Callback that will receive the sync command */
	SmlDsSessionSyncCb recvSyncCallback;
	void *recvSyncCallbackUserdata;
	SmlDsSessionChangeCb recvChangeCallback;
	void *recvChangeCallbackUserdata;
	/** List of received sync commands. We need a list here
	 * since sync commands might get split due to size limitations
	 * and the answer to a sync command might arrive after several
	 * sync commands have been received */
	GList *recvSync;
	GList *recvChanges;
	SmlErrorType syncReply;
	gboolean finalLock;
	gboolean emptySync;
	
	/** Callback that will receive the reply to the sync command */
	SmlStatusReplyCb sentSyncCallback;
	void *sentSyncCallbackUserdata;
	
	/** The temporary storage for the sync command to send */
	SmlCommand *syncCommand;
	
	GList *pendingChanges;
	GMutex *pendingChangesLock;
	GList *mapItems;

	/** Callback that signals mappings */
	SmlDsSessionMapCb recvMappingCallback;
	void *recvMappingCallbackUserdata;
	GList *recvMap;

	GMutex *write_lock;
	GMutex *lock;
};

struct SmlDsServer {
	SmlLocation *location;
	/** The target if the ds server is in client mode */
	SmlLocation *target;
	gchar *contenttype;
	SmlDsServerType servertype;
	SmlManager *manager;
	
	/** Callback that will get called once a new client connects */
	SmlDsSessionConnectCb connectCallback;
	void *connectCallbackUserdata;
	
	SmlDsServerSanSessionCb sanSessionCallback;
	void *sanSessionCallbackUserdata;
};

void          smlDsSessionRecvAlert  (SmlSession *session, SmlCommand *cmd, void *userdata);
void          smlDsSessionRecvSync   (SmlSession *session, SmlCommand *cmd, void *userdata);
void          smlDsSessionRecvChange (SmlSession *session, SmlCommand *cmd, void *userdata);
void          smlDsSessionRecvMap    (SmlSession *session, SmlCommand *cmd, void *userdata);
SmlDsSession* smlDsServerRecvAlert   (SmlDsServer *server, SmlSession *session, SmlCommand *cmd);

SmlDsSession* smlDsSessionNew  (SmlDsServer *server, SmlSession *session, GError **error);
void          smlDsSessionFree (SmlDsSession *dsession);

#endif //_SML_DS_SERVER_INTERNALS_H_
/*@}*/
