Index: trunk/pbs_drmaa/session.h
===================================================================
--- trunk/pbs_drmaa/session.h	(revision 12)
+++ trunk/pbs_drmaa/session.h	(revision 29)
@@ -35,18 +35,14 @@
 	fsd_drmaa_session_t super;
 
-	bool (*
-	do_drm_keeps_completed_jobs)( pbsdrmaa_session_t *self );
+	bool (*do_drm_keeps_completed_jobs)( pbsdrmaa_session_t *self );
 
-	void (*
-	super_destroy)( fsd_drmaa_session_t *self );
+	void (*super_destroy)( fsd_drmaa_session_t *self );
 
-	void (*
-	super_apply_configuration)(fsd_drmaa_session_t *self);
+	void (*super_apply_configuration)(fsd_drmaa_session_t *self);
 
 	/*
 	 * Pointer to standard wait_thread drmaa_utils function
 	 */
-        void* (*
-        super_wait_thread)( fsd_drmaa_session_t *self );
+	void* (*super_wait_thread)( fsd_drmaa_session_t *self );
 
 	/*
@@ -72,10 +68,10 @@
 
 	/*
-	 * Log file initial size - used by wait_thread which reads log files
+	 * Log file initial size - used by wait_thread which reads log files TODO: check if it can be safely moved to log_reader
  	 */
 	off_t log_file_initial_size;
 
 	/*
-	 * Time we checked log file initial size - used by wait_thread which reads log files
+	 * Time we checked log file initial size - used by wait_thread which reads log files TODO: check if it can be safely moved to log_reader
  	 */
 	time_t log_file_initial_time;
Index: trunk/pbs_drmaa/pbs_attrib.h
===================================================================
--- trunk/pbs_drmaa/pbs_attrib.h	(revision 26)
+++ trunk/pbs_drmaa/pbs_attrib.h	(revision 29)
@@ -81,4 +81,5 @@
 #define PBSDRMAA_EXTENSION              "extension"
 #define PBSDRMAA_SUBMIT_ARGS            "submit_args"
+#define PBSDRMAA_MTIME                  "mtime"
 
 
Index: trunk/pbs_drmaa/log_reader.c
===================================================================
--- trunk/pbs_drmaa/log_reader.c	(revision 28)
+++ trunk/pbs_drmaa/log_reader.c	(revision 29)
@@ -46,36 +46,37 @@
 #include <pbs_drmaa/submit.h>
 #include <pbs_drmaa/util.h>
+#include <pbs_drmaa/pbs_attrib.h>
 
 #include <errno.h>
 
-static bool
-pbsdrmaa_read_log();
-
-static void
-pbsdrmaa_select_file_wait_thread ( pbsdrmaa_log_reader_t * self);
-
-static ssize_t
-pbsdrmaa_read_line_wait_thread ( pbsdrmaa_log_reader_t * self, char * line, char * buffer, ssize_t size, int * idx, int * end_idx, int * line_idx );
-
-static void
-pbsdrmaa_select_file_job_on_missing ( pbsdrmaa_log_reader_t * self );
-
-static ssize_t
-pbsdrmaa_read_line_job_on_missing ( pbsdrmaa_log_reader_t * self, char * line, char * buffer, ssize_t size, int * idx, int * end_idx, int * line_idx );
-
-static void
-pbsdrmaa_select_file_accounting ( pbsdrmaa_log_reader_t * self );
-
-static ssize_t
-pbsdrmaa_read_line_accounting ( pbsdrmaa_log_reader_t * self, char * line, char * buffer, ssize_t size, int * idx, int * end_idx, int * line_idx );
-
-static bool 
-pbsdrmaa_read_log_accounting( pbsdrmaa_log_reader_t * self );
-
-int 
-fsd_job_id_cmp(const char *s1, const char *s2);
-
-int 
-pbsdrmaa_date_compare(const void *a, const void *b) ;
+enum pbsdrmaa_field_id
+{
+	PBSDRMAA_FLD_ID_DATE = 0,
+	PBSDRMAA_FLD_ID_EVENT = 1,
+	PBSDRMAA_FLD_ID_SRC = 2,
+	PBSDRMAA_FLD_ID_OBJ_TYPE = 3,
+	PBSDRMAA_FLD_ID_OBJ_ID = 4,
+	PBSDRMAA_FLD_ID_MSG = 5
+};
+
+
+#define PBSDRMAA_FLD_MSG_0008 "0008"
+#define PBSDRMAA_FLD_MSG_0010 "0010"
+
+enum pbsdrmaa_event_type
+{
+	pbsdrmaa_event_0008 = 8,
+	pbsdrmaa_event_0010 = 10
+};
+
+static void pbsdrmaa_read_log();
+
+static void pbsdrmaa_select_file_wait_thread( pbsdrmaa_log_reader_t * self);
+
+char *pbsdrmaa_read_line_wait_thread( pbsdrmaa_log_reader_t * self);
+
+static time_t pbsdrmaa_parse_log_timestamp(const char *timestamp, char *unixtime_str, size_t size);
+
+static char *pbsdrmaa_get_exec_host_from_accountig(pbsdrmaa_log_reader_t * log_reader, const char *job_id);
 
 /*
@@ -97,11 +98,41 @@
 10/11/2011 14:48:24;0010;PBS_Server;Job;15545337.batch.grid.cyf-kr.edu.pl;Exit_status=0 resources_used.cput=00:00:00 resources_used.mem=720kb resources_used.vmem=13308kb resources_used.walltime=00:00:00
 
+deleting job:
+I . PBS Pro
+a) in Q state
+10/16/2011 09:49:25;0008;Server@grass1;Job;2178.grass1.man.poznan.pl;Job Queued at request of mmamonski@grass1.man.poznan.pl, owner = mmamonski@grass1.man.poznan.pl, job name = STDIN, queue = workq
+10/16/2011 09:49:25;0008;Server@grass1;Job;2178.grass1.man.poznan.pl;Job Modified at request of Scheduler@grass1.man.poznan.pl
+10/16/2011 09:49:37;0008;Server@grass1;Job;2178.grass1.man.poznan.pl;Job to be deleted at request of mmamonski@grass1.man.poznan.pl
+10/16/2011 09:49:37;0100;Server@grass1;Job;2178.grass1.man.poznan.pl;dequeuing from workq, state 5
+
+
+b) in R state
+10/16/2011 09:45:12;0080;Server@grass1;Job;2177.grass1.man.poznan.pl;delete job request received
+10/16/2011 09:45:12;0008;Server@grass1;Job;2177.grass1.man.poznan.pl;Job sent signal TermJob on delete
+10/16/2011 09:45:12;0008;Server@grass1;Job;2177.grass1.man.poznan.pl;Job to be deleted at request of mmamonski@grass1.man.poznan.pl
+10/16/2011 09:45:12;0010;Server@grass1;Job;2177.grass1.man.poznan.pl;Exit_status=271 resources_used.cpupercent=0 resources_used.cput=00:00:00 resources_used.mem=2772kb resources_used.ncpus=1 resources_used.vmem=199288kb resources_used.walltime=00:00:26
+10/16/2011 09:45:12;0100;Server@grass1;Job;2177.grass1.man.poznan.pl;dequeuing from workq, state 5
+
+II. Torque
+a) in Q state
+10/15/2011 21:19:25;0008;PBS_Server;Job;113045.grass1.man.poznan.pl;Job deleted at request of mmamonski@grass1.man.poznan.pl
+10/15/2011 21:19:25;0100;PBS_Server;Job;113045.grass1.man.poznan.pl;dequeuing from batch, state EXITING
+
+b) in R state
+10/15/2011 21:19:47;0008;PBS_Server;Job;113046.grass1.man.poznan.pl;Job deleted at request of mmamonski@grass1.man.poznan.pl
+10/15/2011 21:19:47;0008;PBS_Server;Job;113046.grass1.man.poznan.pl;Job sent signal SIGTERM on delete
+10/15/2011 21:19:47;0010;PBS_Server;Job;113046.grass1.man.poznan.pl;Exit_status=271 resources_used.cput=00:00:00 resources_used.mem=0kb resources_used.vmem=0kb resources_used.walltime=00:00:10
+
+Log closed:
+10/16/2011 00:00:17;0002;PBS_Server;Svr;Log;Log closed
+
  */
 pbsdrmaa_log_reader_t * 
-pbsdrmaa_log_reader_new ( fsd_drmaa_session_t *session, fsd_job_t *job )
+pbsdrmaa_log_reader_new( fsd_drmaa_session_t *session )
 {
 	pbsdrmaa_log_reader_t *volatile self = NULL;
 
 	fsd_log_enter((""));
+
 	TRY
 	{
@@ -109,32 +140,13 @@
 		
 		self->session = session;
-		
-		/* ~templete method pattern */
-		if(job != NULL) /* job on missing */
-		{
-			self->job = job;
-			self->name = "Job_on_missing";
-			self->select_file = pbsdrmaa_select_file_job_on_missing;
-			self->read_line = pbsdrmaa_read_line_job_on_missing;
-		}
-		else /* wait thread */
-		{
-			self->job = NULL;
-			self->name = "WT";
-			self->select_file = pbsdrmaa_select_file_wait_thread;
-			self->read_line = pbsdrmaa_read_line_wait_thread;
-		}		
+
+		self->select_file = pbsdrmaa_select_file_wait_thread;
 		self->read_log = pbsdrmaa_read_log;	
 		
-		self->log_files = NULL;
-		self->log_files_number = 0;
-		
 		self->run_flag = true;
-		self->fd = -1;
+		self->fhandle = NULL;
 		self->date_changed = true;
 		self->first_open = true;
 		
-		self->log_file_initial_size = 0;
-		self->log_file_read_size = 0;
 	}
 	EXCEPT_DEFAULT
@@ -146,49 +158,10 @@
 	}
 	END_TRY
+
 	fsd_log_return((""));
+
 	return self;
 }
 
-pbsdrmaa_log_reader_t * 
-pbsdrmaa_log_reader_accounting_new ( fsd_drmaa_session_t *session, fsd_job_t *job )
-{
-	pbsdrmaa_log_reader_t *volatile self = NULL;
-
-	fsd_log_enter((""));
-	TRY
-	{
-		fsd_malloc(self, pbsdrmaa_log_reader_t );
-		
-		self->session = session;
-		
-		self->job = job;
-		self->name = "Accounting";
-		self->select_file = pbsdrmaa_select_file_accounting;
-		self->read_line = pbsdrmaa_read_line_accounting;
-				
-		self->read_log = pbsdrmaa_read_log_accounting;	
-		
-		self->log_files = NULL;
-		self->log_files_number = 0;
-		
-		self->run_flag = true;
-		self->fd = -1;
-		self->date_changed = true;
-		self->first_open = true;
-		
-		self->log_file_initial_size = 0;
-		self->log_file_read_size = 0;
-	}
-	EXCEPT_DEFAULT
-	{
-		if( self != NULL)
-			fsd_free(self);
-			
-		fsd_exc_reraise();
-	}
-	END_TRY
-	fsd_log_return((""));
-	return self;
-}
 
 void
@@ -200,10 +173,6 @@
 		if(self != NULL)
 		{
-			int i = -1;
-			for(i = 0; i < self->log_files_number ; i++)
-				fsd_free(self->log_files[i]);
-			fsd_free(self->log_files);
 			fsd_free(self);	
-		}			
+		}
 	}
 	EXCEPT_DEFAULT
@@ -216,225 +185,99 @@
 }
 
-enum field
-{ 
-	FLD_DATE = 0,
-	FLD_EVENT = 1,
-	FLD_OBJ = 2,
-	FLD_TYPE = 3,
-	FLD_ID = 4,
-	FLD_MSG = 5
-};
-
-enum field_msg
-{
-	FLD_MSG_EXIT_STATUS = 0,
-	FLD_MSG_CPUT = 1,
-	FLD_MSG_MEM = 2,
-	FLD_MSG_VMEM = 3,
-	FLD_MSG_WALLTIME = 4
-};
-
-enum field_msg_accounting
-{
-	FLD_MSG_ACC_USER = 0,
-	FLD_MSG_ACC_GROUP = 1,
-	FLD_MSG_ACC_JOBNAME = 2,
-	FLD_MSG_ACC_QUEUE = 3,
-	FLD_MSG_ACC_CTIME = 4,
-	FLD_MSG_ACC_QTIME = 5,
-	FLD_MSG_ACC_ETIME = 6,
-	FLD_MSG_ACC_START = 7,
-	FLD_MSG_ACC_OWNER = 8,
-	FLD_MSG_ACC_EXEC_HOST = 9,
-	FLD_MSG_ACC_RES_NEEDNODES = 10,
-	FLD_MSG_ACC_RES_NODECT = 11,
-	FLD_MSG_ACC_RES_NODES = 12,
-	FLD_MSG_ACC_RES_WALLTIME = 13
-};
-
-#define FLD_MSG_STATUS "0010"
-#define FLD_MSG_STATE "0008"
-#define FLD_MSG_LOG "0002"
-
-bool 
+
+void
 pbsdrmaa_read_log( pbsdrmaa_log_reader_t * self )
 {
-	pbsdrmaa_job_t *pbsjob = (pbsdrmaa_job_t*) self->job;
-	fsd_job_t *volatile temp_job = NULL;
-		
 	fsd_log_enter((""));
 	
-	if(self->job == NULL)
-		fsd_mutex_lock( &self->session->mutex );
+	fsd_mutex_lock( &self->session->mutex );
 
 	TRY
-	{		
+	 {
 		while( self->run_flag )
-		TRY
-		{
-			char line[4096] = "";
-			char buffer[4096] = "";
-			int idx = 0, end_idx = 0, line_idx = 0;
-			
-			self->select_file(self);
-
-			while ((self->read_line(self, line,buffer, sizeof(line), &idx,&end_idx,&line_idx)) > 0)
+		 {
+			TRY
 			{
-				const char *volatile ptr = line;
-				char field[256] = "";
-				char job_id[256] = "";
-				char event[256] = "";
-				int volatile field_n = 0;
- 				int n;
+				char *line = NULL;
 				
-				bool volatile job_id_match = false;
-				bool volatile event_match = false;
-				bool volatile log_event = false;
-				bool volatile log_match = false;
-				bool volatile older_job_found = false;
-				bool volatile job_found = false;
-				char * temp_date = NULL;
-				
-				struct batch_status status;
-				status.next = NULL;
-
-				while ( sscanf(ptr, "%255[^;]%n", field, &n) == 1 ) /* split current line into fields */
-				{
-					if(field_n == FLD_DATE)
-					{
-						temp_date = fsd_strdup(field);
-					}
-					else if(field_n == FLD_EVENT && (strcmp(field,FLD_MSG_STATUS) == 0 || strcmp(field,FLD_MSG_STATE) == 0 ))
-					{
-						/* event described by log line*/
-						if(strlcpy(event, field,sizeof(event)) > sizeof(event))
-						{
-							fsd_log_error(("%s - strlcpy error",self->name));
-						}
-						event_match = true;
-					}
-					else if(event_match && field_n == FLD_ID)
-					{	
-						TRY
-						{	
-							if(self->job == NULL) /* wait_thread */
-							{
-								temp_job = self->session->get_job( self->session, field );
-								pbsjob = (pbsdrmaa_job_t*) temp_job;
-
-								if( temp_job )
-								{
-									if(strlcpy(job_id, field,sizeof(job_id)) > sizeof(job_id)) {
-										fsd_log_error(("%s - strlcpy error",self->name));
-									}
-									fsd_log_debug(("%s - job_id: %s",self->name,job_id));
-									status.name = fsd_strdup(job_id);
-									job_id_match = true; /* job_id is in drmaa */	
-								}
-								else 
-								{
-									fsd_log_debug(("%s - Unknown job: %s", self->name,field));
-								}
-							}
-							else /* job_on_missing */
-							{
-								int diff = -1;
-								diff = fsd_job_id_cmp(self->job->job_id,field);
-								if( diff == 0)
-								{
-									/* read this file to the place we started and exit*/
-									fsd_log_debug(("Job_on_missing found job: %s",self->job->job_id));
-									job_found = true;
-									older_job_found = false;
-									self->run_flag = false;
-									job_id_match = true; 
-									status.name = fsd_strdup(self->job->job_id);
-								}
-								else if ( !job_found && diff >= 1)
-								{
-									/* older job, find its beginning */
-									fsd_log_debug(("Job_on_missing found older job than %s : %s",self->job->job_id,field));
-									older_job_found = true;
-									job_id_match = true; 
-									status.name = fsd_strdup(self->job->job_id);
-								}
-								else  if( !job_found )
-								{
-									fsd_log_debug(("Job_on_missing found newer job than %s : %s",self->job->job_id,field));
-								}								
-							}
-						}
-						END_TRY	
-					}
-					else if(job_id_match && field_n == FLD_MSG)
-					{						
-						/* parse msg - depends on FLD_EVENT */
-						struct attrl struct_resource_cput,
-							struct_resource_mem,
-							struct_resource_vmem,
-							struct_resource_walltime,
-							struct_status,
-							struct_state,
-							struct_start_time,
-							struct_mtime,
-							struct_queue,
-							struct_account_name,
-							struct_exec_vnode;
-						struct attrl *last_attr = NULL;
-						
-						bool state_running = false;
-
-						memset(&struct_status,0,sizeof(struct attrl));
-						memset(&struct_state,0,sizeof(struct attrl));
-						memset(&struct_resource_cput,0,sizeof(struct attrl));
-						memset(&struct_resource_mem,0,sizeof(struct attrl));
-						memset(&struct_resource_vmem,0,sizeof(struct attrl));
-						memset(&struct_resource_walltime,0,sizeof(struct attrl));
-						memset(&struct_start_time,0,sizeof(struct attrl));
-						memset(&struct_mtime,0,sizeof(struct attrl));
-						memset(&struct_queue,0,sizeof(struct attrl));
-						memset(&struct_account_name,0,sizeof(struct attrl));
-						memset(&struct_exec_vnode,0,sizeof(struct attrl));
-								
-						if (strcmp(event,FLD_MSG_STATE) == 0) 
-						{
-							/* job run, modified, queued etc */
-							int n = 0;
-							status.attribs = &struct_state;
-							struct_state.next = NULL;
-							struct_state.name = "job_state";
-							last_attr = &struct_state;
-
-							if(field[0] == 'J') /* Job Queued, Job Modified, Job Run*/
-							 {
-								n = 4;
-								if(older_job_found) /* job_on_missing - older job beginning - read this file and end */
+				self->select_file(self);
+
+				while ((line = fsd_readline(self->fhandle)) != NULL)
+				 {
+					int field_id = PBSDRMAA_FLD_ID_DATE;
+					char *tok_ctx = NULL;
+					char *field_token = NULL;
+					char *event_timestamp = NULL;
+					int event_type = -1;
+					fsd_job_t *job = NULL;
+
+					/* at first detect if this not the end of log file */
+					if (strstr(line, "Log;Log closed")) /*TODO try to be more effective and safe */
+					 {
+						fsd_log_debug(("WT - Date changed. Closing log file"));
+						self->date_changed = true;
+					 }
+
+					for (field_token = strtok_r(line, ";", &tok_ctx); field_token; field_token = strtok_r(NULL, ";", &tok_ctx), field_id++)
+					 {
+						if ( field_id == PBSDRMAA_FLD_ID_DATE)
+						 {
+							event_timestamp = field_token;
+						 }
+						else if ( field_id == PBSDRMAA_FLD_ID_EVENT)
+						 {
+							if (strncmp(field_token, PBSDRMAA_FLD_MSG_0008, 4) == 0)
+								event_type = pbsdrmaa_event_0008;
+							else if (strncmp(field_token, PBSDRMAA_FLD_MSG_0010, 4) == 0)
+								event_type = pbsdrmaa_event_0010;
+							else
+								break; /*we are interested only in the above log messages */
+						 }
+						else if ( field_id == PBSDRMAA_FLD_ID_SRC)
+						 {
+							/* not used ignore */
+						 }
+						else if (field_id  == PBSDRMAA_FLD_ID_OBJ_TYPE)
+						 {
+							if (strncmp(field_token, "Job", 3) != 0)
+								break; /* we are interested only in job events */
+						 }
+						else if (field_id == PBSDRMAA_FLD_ID_OBJ_ID)
+						 {
+							const char *event_jobid = field_token;
+
+							TRY
+							 {
+								job = self->session->get_job( self->session, event_jobid );
+
+								if( job )
 								 {
-									self->run_flag = false;
-									fsd_log_debug(("Job_on_missing found older job beginning"));
-									fsd_free(status.name);
+									fsd_log_debug(("WT - Found job event: %s", event_jobid));
+								 }
+								else
+								 {
+									fsd_log_debug(("WT - Unknown job: %s", event_jobid)); /* Not a DRMAA job */
 									break;
 								 }
-
-								 { /* modified */
-									struct tm temp_time_tm;
-									memset(&temp_time_tm, 0, sizeof(temp_time_tm));
-									temp_time_tm.tm_isdst = -1;
-
-									if (strptime(temp_date, "%m/%d/%Y %H:%M:%S", &temp_time_tm) == NULL)
-									 {
-										fsd_log_error(("failed to parse mtime: %s (line = %s)", temp_date, line));
-									 }
-									else
-									 {
-										time_t temp_time = mktime(&temp_time_tm);
-										last_attr->next = &struct_mtime;
-										last_attr = &struct_mtime;
-										struct_mtime.name = "mtime";
-										struct_mtime.next = NULL;
-										struct_mtime.value = fsd_asprintf("%lu",temp_time);
-									 }
-								 }
 							 }
+							END_TRY
+						 }
+						else if (field_id == PBSDRMAA_FLD_ID_MSG)
+						 {
+							char *msg = field_token;
+							struct batch_status status;
+							struct attrl *attribs = NULL;
+							bool in_running_state = false;
+
+							if (event_type == pbsdrmaa_event_0008 && strncmp(msg, "Job Queued", 10) == 0)
+							 {
+								/* Queued
+								 * PBS Pro: 10/11/2011 14:43:29;0008;Server@nova;Job;2127218.nova;Job Queued at request of mamonski@endor.wcss.wroc.pl, owner = mamonski@endor.wcss.wroc.pl, job name = STDIN, queue = normal
+								 * Torque:  10/11/2011 14:47:59;0008;PBS_Server;Job;15545337.batch.grid.cyf-kr.edu.pl;Job Queued at request of plgmamonski@ui.cyf-kr.edu.pl, owner = plgmamonski@ui.cyf-kr.edu.pl, job name = STDIN, queue = l_short
+								 */
+								char *p_queue = NULL;
+
+								if ((p_queue = strstr(msg,"queue =")) == NULL)
+									fsd_exc_raise_fmt(FSD_ERRNO_INTERNAL_ERROR,"No queue attribute found in log line = %s", line);
 
 							/* != Job deleted and Job to be deleted*/
@@ -447,7 +290,10 @@
 #endif
 								struct_state.value = fsd_asprintf("%c",field[n]);
-								if(struct_state.value[0] == 'R')
-								 {
-									state_running = true;
+								(void)pbsdrmaa_parse_log_timestamp(event_timestamp, timestamp_unix, sizeof(timestamp_unix));
+
+								in_running_state = true;
+
+								attribs = pbsdrmaa_add_attr(attribs, PBSDRMAA_JOB_STATE, "R");
+								attribs = pbsdrmaa_add_attr(attribs, PBSDRMAA_START_TIME, timestamp_unix);
 #ifdef PBS_PROFESSIONAL
 									{
@@ -463,199 +309,174 @@
 									}
 #endif
+							 }
+#ifndef PBS_PBS_PROFESSIONAL
+							else if (event_type == pbsdrmaa_event_0008 && strncmp(msg, "Job deleted", 11))
+#else
+							else if (event_type == pbsdrmaa_event_0008 && strncmp(msg, "Job to be deleted", 17))
+#endif
+							 {
+							/* Deleted
+							 * PBS Pro: 10/16/2011 09:45:12;0008;Server@grass1;Job;2177.grass1.man.poznan.pl;Job to be deleted at request of mmamonski@grass1.man.poznan.pl
+							 * Torque: 10/15/2011 21:19:25;0008;PBS_Server;Job;113045.grass1.man.poznan.pl;Job deleted at request of mmamonski@grass1.man.poznan.pl
+							 */
+								char timestamp_unix[64];
+
+								(void)pbsdrmaa_parse_log_timestamp(event_timestamp, timestamp_unix, sizeof(timestamp_unix));
+
+								if (job->state < DRMAA_PS_RUNNING)
+								 {
+									fsd_log_info(("Job %s killed before entering running state (%d).", job->job_id, job->state));
+									attribs = pbsdrmaa_add_attr(attribs, PBSDRMAA_JOB_STATE, "C");
+									attribs = pbsdrmaa_add_attr(attribs, PBSDRMAA_MTIME, timestamp_unix);
+									attribs = pbsdrmaa_add_attr(attribs, PBSDRMAA_EXIT_STATUS, "-2");
+								 }
+								else
+								 {
+									break; /* job was started, ignore, wait for Exit_status message */
+								 }
+							 }
+							else if (event_type == pbsdrmaa_event_0010 && strncmp(msg, "Exit_status=", 12))
+							 {
+							/* Completed:
+							 * PBS Pro: 10/11/2011 14:43:32;0010;Server@nova;Job;2127218.nova;Exit_status=0 resources_used.cpupercent=0 resources_used.cput=00:00:00 resources_used.mem=1768kb resources_used.ncpus=6 resources_used.vmem=19228kb resources_used.walltime=00:00:01
+							 * Torque: 10/11/2011 14:48:24;0010;PBS_Server;Job;15545337.batch.grid.cyf-kr.edu.pl;Exit_status=0 resources_used.cput=00:00:00 resources_used.mem=720kb resources_used.vmem=13308kb resources_used.walltime=00:00:00
+							 */
+								char timestamp_unix[64];
+								time_t timestamp_time_t = pbsdrmaa_parse_log_timestamp(event_timestamp, timestamp_unix, sizeof(timestamp_unix));
+								char *tok_ctx2 = NULL;
+								char *token = NULL;
+
+								attribs = pbsdrmaa_add_attr(attribs, PBSDRMAA_JOB_STATE, "C");
+								attribs = pbsdrmaa_add_attr(attribs, PBSDRMAA_MTIME, timestamp_unix);
+
+								/* tokenize !!! */
+								for (token = strtok_r(msg, " ", &tok_ctx2); token; token = strtok_r(NULL, " ", &tok_ctx2))
+								 {
+									if (strncmp(token, "Exit_status=", 12) == 0)
+									 {
+										token[12] = '\0';
+										attribs = pbsdrmaa_add_attr(attribs, token, token + 12);
+										fsd_log_info(("WT - Completion of job %s (Exit_status=%s) detected after %d seconds", job->job_id, token+12, (int)(time(NULL) - timestamp_time_t) ));
+									 }
+									else if (strncmp(token, "resources_used.cput=", 20) == 0)
+									 {
+										token[20] = '\0';
+										attribs = pbsdrmaa_add_attr(attribs, token, token + 20);
+									 }
+									else if (strncmp(token, "resources_used.mem=", 19) == 0)
+									 {
+										token[19] = '\0';
+										attribs = pbsdrmaa_add_attr(attribs, token, token + 19);
+									 }
+									else if (strncmp(token, "resources_used.vmem=", 20) == 0)
+									 {
+										token[20] = '\0';
+										attribs = pbsdrmaa_add_attr(attribs, token, token + 20);
+									 }
+									else if (strncmp(token, "resources_used.walltime=", 24) == 0)
+									 {
+										token[24] = '\0';
+										attribs = pbsdrmaa_add_attr(attribs, token, token + 24);
+									 }
+								 }
+
+								if (!job->execution_hosts)
+								 {
+									char *exec_host = NULL;
+									fsd_log_info(("WT - No execution host information for job %s. Reading accounting logs...", job->job_id));
+									exec_host = pbsdrmaa_get_exec_host_from_accountig(self, job->job_id);
+									attribs = pbsdrmaa_add_attr(attribs, PBSDRMAA_EXECUTION_HOST, exec_host);
+									fsd_free(exec_host);
 								 }
 							 }
 							else
-							 { /* job terminated - pbs drmaa detects failed as completed with exit_status !=0, aborted with status -1*/
-								struct_status.name = "exit_status";
-								struct_status.value = fsd_strdup("-1");
-								struct_status.next = NULL;
-								struct_state.next = &struct_status;
-								struct_state.value = fsd_strdup("C");
-							 }
-						}
-						else /*if (strcmp(event,FLD_MSG_STATUS) == 0 )*/
-						{
-							/* exit status and rusage */
-							const char *ptr2 = field;
-							char  msg[ 256 ] = "";
-							int n2;
-							int msg_field_n = 0;
-							
-							struct_resource_cput.name = "resources_used";
-							struct_resource_mem.name = "resources_used";
-							struct_resource_vmem.name = "resources_used";
-							struct_resource_walltime.name = "resources_used";
-							struct_status.name = "exit_status";
-							struct_state.name = "job_state";
-				
-							status.attribs = &struct_resource_cput;
-							struct_resource_cput.next = &struct_resource_mem;
-							struct_resource_mem.next = &struct_resource_vmem;
-							struct_resource_vmem.next = &struct_resource_walltime;
-							struct_resource_walltime.next =  &struct_status;
-							struct_status.next = &struct_state;
-							struct_state.next = NULL;
-
-							while ( sscanf(ptr2, "%255[^ ]%n", msg, &n2) == 1 )
-							 {						
-								switch(msg_field_n) 
-								{
-									case FLD_MSG_EXIT_STATUS:
-										struct_status.value = fsd_strdup(strchr(msg,'=')+1);
-										break;
-
-									case FLD_MSG_CPUT:
-										struct_resource_cput.resource = "cput";
-										struct_resource_cput.value = fsd_strdup(strchr(msg,'=')+1);
-										break;
-
-									case FLD_MSG_MEM:
-										struct_resource_mem.resource = "mem";
-										struct_resource_mem.value  = fsd_strdup(strchr(msg,'=')+1);
-										break;
-
-									case FLD_MSG_VMEM:
-										struct_resource_vmem.resource = "vmem";
-										struct_resource_vmem.value  = fsd_strdup(strchr(msg,'=')+1);
-										break; 
-
-									case FLD_MSG_WALLTIME:
-										struct_resource_walltime.resource = "walltime";
-										struct_resource_walltime.value  = fsd_strdup(strchr(msg,'=')+1);
-										break; 
-								}
-
-								ptr2 += n2; 
-								msg_field_n++;
-								if ( *ptr2 != ' ' )
-									 break; 
-								++ptr2;
+							{
+								break; /* ignore other job events*/
 							}
-							struct_state.value = fsd_strdup("C");	/* we got exit_status so we say that it has completed */
-							fsd_log_info(("WT - job %s found as finished on %u", temp_job->job_id, (unsigned int)time(NULL)));
-						}						
-						 
-						if(self->job == NULL) /* wait_thread */
-						{
-							if ( state_running )
-							{
-								fsd_log_debug(("WT - forcing update of job: %s", temp_job->job_id ));
+
+							if ( in_running_state )
+							 {
+								fsd_log_debug(("WT - forcing update of job: %s", job->job_id ));
 								TRY
 								{
-									temp_job->update_status( temp_job );
+									job->update_status( job );
 								}
 								EXCEPT_DEFAULT
 								{
 									/*TODO: distinguish between invalid job and internal errors */
-									fsd_log_debug(("Job finished just after entering running state: %s", temp_job->job_id));
+									fsd_log_debug(("Job finished just after entering running state: %s", job->job_id));
 								}
 								END_TRY
-							}
+							 }
 							else
-							{
-								fsd_log_debug(("%s - updating job: %s",self->name, temp_job->job_id ));
-								pbsjob->update( temp_job, &status );
-							}
-						}
-						else if( job_found ) /* job_on_missing */
-						{
-							fsd_log_debug(("Job_on_missing - updating job: %s", self->job->job_id ));
-							pbsjob->update( self->job, &status );
-						}
-						
-						if(self->job == NULL)
-						{
-							fsd_cond_broadcast( &temp_job->status_cond);
+							 {
+								fsd_log_debug(("WT - updating job: %s", job->job_id ));
+								status.name = job->job_id;
+								status.attribs = attribs;
+
+								((pbsdrmaa_job_t *)job)->update( job, &status );
+
+								pbsdrmaa_free_attrl(attribs); /* TODO free on exception */
+							 }
+
+							fsd_cond_broadcast( &job->status_cond);
 							fsd_cond_broadcast( &self->session->wait_condition );
-						}
-						if ( temp_job )
-							temp_job->release( temp_job );
-	
-						fsd_free(struct_resource_cput.value);
-						fsd_free(struct_resource_mem.value);
-						fsd_free(struct_resource_vmem.value);
-						fsd_free(struct_resource_walltime.value);
-						fsd_free(struct_status.value);
-						fsd_free(struct_state.value);
-						fsd_free(struct_start_time.value);
-						fsd_free(struct_mtime.value);
-						fsd_free(struct_queue.value);
-						fsd_free(struct_account_name.value);
-
-						if ( status.name!=NULL ) 
-							fsd_free(status.name);
-					}
-					else if(field_n == FLD_EVENT && strcmp(field,FLD_MSG_LOG) == 0)
-					{
-						log_event = true;
-					}
-					else if (log_event && field_n == FLD_ID && strcmp(field,"Log") == 0 )
-					{
-						log_match = true;
-						log_event = false;
-					}
-					else if( self->job == NULL && log_match && field_n == FLD_MSG && strncmp(field,"Log closed",10) == 0)
-					{
-						fsd_log_debug(("%s - Date changed. Closing log file",self->name));
-						self->date_changed = true;
-						log_match = false;
-					}
+
+							if ( job )
+								job->release( job );
+
+							fsd_free(line); /* TODO free on exception */
+						 }
+						else
+						 {
+							fsd_assert(0); /*not reached */
+						 }
+					 }
+
+				 } /* end of while getline loop */
+
+				 { /* poll on log file */
+					struct timeval timeout_tv;
+					fd_set log_fds;
+
+					fsd_mutex_unlock( &self->session->mutex );
 					
-					ptr += n; 
-					if ( *ptr != ';' )
-					{
-						break; /* end of line */
-					}
-					field_n++;
-					++ptr;
-				}		
-
-				fsd_free(temp_date);
-			} /* end of while getline loop */
-			
-			if(self->job == NULL)
+					FD_ZERO(&log_fds);
+					FD_SET(fileno(self->fhandle), &log_fds);
+
+					timeout_tv.tv_sec = 1;
+					timeout_tv.tv_usec = 0;
+
+					/* ignore return value - the next get line call will handle IO errors */
+					(void)select(1, &log_fds, NULL, NULL, &timeout_tv);
+
+					fsd_mutex_lock( &self->session->mutex );
+
+					self->run_flag = self->session->wait_thread_run_flag;
+				 }
+			}
+			EXCEPT_DEFAULT
 			{
-				struct timeval timeout_tv;
-				fd_set log_fds;
-	
-				fsd_mutex_unlock( &self->session->mutex );
-				
-				FD_ZERO(&log_fds);
-				FD_SET(self->fd, &log_fds);
-
-				timeout_tv.tv_sec = 1;
-				timeout_tv.tv_usec = 0;
-
-				/* ignore return value - the next get line call will handle IO errors */
-				(void)select(1, &log_fds, NULL, NULL, &timeout_tv);
-
-				fsd_mutex_lock( &self->session->mutex );	
-
-				self->run_flag = self->session->wait_thread_run_flag;
+				const fsd_exc_t *e = fsd_exc_get();
+				/* Its better to exit and communicate error rather then let the application to hang */
+				fsd_log_fatal(( "Exception in wait thread: <%d:%s>. Exiting !!!", e->code(e), e->message(e) ));
+				exit(1);
 			}
-		}		
-		EXCEPT_DEFAULT
-		{
-			const fsd_exc_t *e = fsd_exc_get();
-			/* Its better to exit and communicate error rather then let the application to hang */
-			fsd_log_fatal(( "Exception in wait thread %s: <%d:%s>. Exiting !!!", self->name, e->code(e), e->message(e) ));
-			exit(1);
-		}
-		END_TRY
-
-		if(self->fd != -1)
-			close(self->fd);
-		fsd_log_debug(("%s - Log file closed",self->name));	
+			END_TRY
+		 }
+
+		if(self->fhandle)
+			fclose(self->fhandle);
+
+		fsd_log_debug(("WT - Log file closed"));
 	}
 	FINALLY
 	{
-	 	fsd_log_debug(("%s - Terminated.",self->name));	
-		if(self->job == NULL)
-			fsd_mutex_unlock( &self->session->mutex ); /**/
+	 	fsd_log_debug(("WT - Terminated."));
+		fsd_mutex_unlock( &self->session->mutex ); /**/
 	}
 	END_TRY
 	
 	fsd_log_return((""));
-	return true;
 }
 
@@ -665,6 +486,6 @@
 	pbsdrmaa_session_t *pbssession = (pbsdrmaa_session_t*) self->session;
 	
-	if(self->date_changed)
-	{
+	if (self->date_changed)
+	 {
 		char * log_path = NULL;
 		int num_tries = 0;
@@ -682,32 +503,28 @@
 		#define DRMAA_WAIT_THREAD_MAX_TRIES (12)
 		/* generate new date, close file and open new */
-		if((log_path = fsd_asprintf("%s/server_logs/%04d%02d%02d",
-					pbssession->pbs_home,	 
-					tm.tm_year + 1900,
-					tm.tm_mon + 1,
-					tm.tm_mday)) == NULL) {
-			fsd_exc_raise_fmt(FSD_ERRNO_INTERNAL_ERROR,"WT - Memory allocation wasn't possible");
-		}
-
-		if(self->fd != -1)
-			close(self->fd);
-
-		fsd_log_debug(("Log file: %s",log_path));
+		log_path = fsd_asprintf("%s/server_logs/%04d%02d%02d", pbssession->pbs_home, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
+
+		if(self->fhandle)
+			fclose(self->fhandle);
+
+		fsd_log_info(("Opening log file: %s",log_path));
 				
 	retry:
-		if((self->fd = open(log_path,O_RDONLY) ) == -1 && num_tries > DRMAA_WAIT_THREAD_MAX_TRIES )
-		{
+		if ((self->fhandle = fopen(log_path,"")) == NULL && (num_tries > DRMAA_WAIT_THREAD_MAX_TRIES || self->first_open))
+		 {
 			fsd_log_error(("Can't open log file. Verify pbs_home. Running standard wait_thread."));
-			fsd_log_error(("Remember that without keep_completed set standard wait_thread won't run correctly"));
+			fsd_log_error(("Remember that without keep_completed set the standard wait_thread won't provide information about job exit status"));
 			/*pbssession->super.enable_wait_thread = false;*/ /* run not wait_thread */
 			pbssession->wait_thread_log = false;
 			pbssession->super.wait_thread = pbssession->super_wait_thread;
 			pbssession->super.wait_thread(self->session);
-		} else if ( self->fd == -1 ) {
+		 }
+		else if ( self->fhandle == NULL )
+		 { /* Torque seems not to create a new file immediately after the old one is closed */
 			fsd_log_warning(("Can't open log file: %s. Retries count: %d", log_path, num_tries));
 			num_tries++;
-			sleep(5);
+			sleep(2 * num_tries);
 			goto retry;
-		}
+		 }
 
 		fsd_free(log_path);
@@ -715,13 +532,14 @@
 		fsd_log_debug(("Log file opened"));
 
-		if(self->first_open) {
+		if(self->first_open)
+		 {
 			fsd_log_debug(("Log file lseek"));
-			if(lseek(self->fd,pbssession->log_file_initial_size,SEEK_SET) == (off_t) -1) {
-				char errbuf[256] = "InternalError";
-				(void)strerror_r(errno, errbuf, 256);
-				fsd_exc_raise_fmt(FSD_ERRNO_INTERNAL_ERROR,"lseek error: %s",errbuf);
-			}
+
+			if(fseek(self->fhandle, pbssession->log_file_initial_size, SEEK_SET) == (off_t) -1)
+			 {
+				fsd_exc_raise_fmt(FSD_ERRNO_INTERNAL_ERROR,"fseek error");
+			 }
 			self->first_open = false;
-		}
+		 }
 
 		self->date_changed = false;
@@ -731,434 +549,29 @@
 }
 
-ssize_t
-pbsdrmaa_read_line_wait_thread ( pbsdrmaa_log_reader_t * self, char * line, char * buffer, ssize_t size, int * idx, int * end_idx, int * line_idx )
-{
-	return fsd_getline_buffered(line,buffer,size,self->fd,idx,end_idx,line_idx);
+time_t
+pbsdrmaa_parse_log_timestamp(const char *timestamp, char *unixtime_str, size_t size)
+{
+	struct tm temp_time_tm;
+	memset(&temp_time_tm, 0, sizeof(temp_time_tm));
+	temp_time_tm.tm_isdst = -1;
+
+	if (strptime(timestamp, "%m/%d/%Y %H:%M:%S", &temp_time_tm) == NULL)
+	 {
+		fsd_exc_raise_fmt(FSD_ERRNO_INTERNAL_ERROR,"WT - failed to parse log timestamp: %s", timestamp);
+	 }
+	else
+	 {
+		time_t temp_time = mktime(&temp_time_tm);
+		snprintf(unixtime_str, size, "%lu", temp_time);
+		return temp_time;
+	 }
 }
 
-/* reverse date compare*/
-int 
-pbsdrmaa_date_compare(const void *a, const void *b) 
-{
-	const char *ia = *(const char **) a;
-	const char *ib = *(const char **) b;
-	return strcmp(ib, ia);
+char *
+pbsdrmaa_get_exec_host_from_accountig(pbsdrmaa_log_reader_t * log_reader, const char *job_id)
+{
+	/* TODO: implement */
+	return NULL;
 }
 
-void
-pbsdrmaa_select_file_job_on_missing( pbsdrmaa_log_reader_t * self )
-{
-	pbsdrmaa_session_t *pbssession = (pbsdrmaa_session_t*) self->session;	
-	
-	char * log_path = NULL;
-	int num_tries = 0;
-	static int file_number = 0;
-	fsd_log_enter((""));
-		
-	if(self->first_open) 
-	{			
-		DIR *dp = NULL;		
-		char * path = NULL;
-		struct dirent *ep = NULL;
-		
-		if((path = fsd_asprintf("%s/server_logs/",pbssession->pbs_home)) == NULL)
-			fsd_exc_raise_fmt(FSD_ERRNO_INTERNAL_ERROR,"Job_on_missing - Memory allocation wasn't possible");
-		
-		self->log_files_number = 0;     
-		dp = opendir (path);
-
-		fsd_calloc(self->log_files,2,char*);
-  	
-		if (dp != NULL)
-		{
-			while ((ep = readdir (dp)))
-			{
-				self->log_files_number++;
-				if(self->log_files_number > 2)
-					fsd_realloc(self->log_files,self->log_files_number,char *);
-				
-				self->log_files[self->log_files_number-1] = fsd_strdup(ep->d_name);
-			}
-			(void) closedir (dp);
-		}
-		else
-			fsd_exc_raise_fmt(FSD_ERRNO_INTERNAL_ERROR,"Job_on_missing - Couldn't open the directory");
-
-		qsort(self->log_files,self->log_files_number,sizeof(char *),pbsdrmaa_date_compare);
-		
-		if(self->log_files_number <= 2)
-		{
-			self->run_flag = false;
-			fsd_log_error(("Job_on_missing - No log files available"));
-		}
-		
-		self->first_open = false;
-		fsd_free(path);
-	}	
-	else /* check previous day*/
-	{
-		if(++file_number > self->log_files_number - 2)
-			fsd_log_error(("Job_on_missing - All available log files checked"));
-		else
-			fsd_log_debug(("Job_on_missing checking previous day"));
-		
-		self->run_flag = false;
-		pbsdrmaa_job_on_missing_standard( self->job );				
-	}
-	
-	#define DRMAA_WAIT_THREAD_MAX_TRIES (12)
-	if((log_path = fsd_asprintf("%s/server_logs/%s",
-				pbssession->pbs_home,	 
-				self->log_files[file_number])) == NULL) {
-		fsd_exc_raise_fmt(FSD_ERRNO_INTERNAL_ERROR,"Job_on_missing - Memory allocation wasn't possible");
-	}
-
-	if(self->fd != -1)
-		close(self->fd);
-
-	fsd_log_debug(("Log file: %s",log_path));
-				
-retry:
-	if((self->fd = open(log_path,O_RDONLY) ) == -1 && num_tries > DRMAA_WAIT_THREAD_MAX_TRIES )
-	{
-		fsd_log_error(("Can't open log file. Verify pbs_home. Running standard job_on_missing"));
-		fsd_log_error(("Remember that without keep_completed set standard job_on_missing won't run correctly"));
-		self->run_flag = false;
-		pbsdrmaa_job_on_missing_standard( self->job );			
-	} else if ( self->fd == -1 ) {
-		fsd_log_warning(("Can't open log file: %s. Retries count: %d", log_path, num_tries));
-		num_tries++;
-		sleep(5);
-		goto retry;
-	}
-	else
-	{
-		struct stat statbuf;
-		if(stat(log_path,&statbuf) == -1) {
-				char errbuf[256] = "InternalError";
-				(void)strerror_r(errno, errbuf, 256);
-				fsd_exc_raise_fmt(FSD_ERRNO_INTERNAL_ERROR,"stat error: %s",errbuf);
-		}
-		self->log_file_read_size = 0;
-		self->log_file_initial_size = statbuf.st_size;
-		fsd_log_debug(("Set log_file_initial_size %ld",self->log_file_initial_size));
-	}
-
-	fsd_free(log_path);
-
-	fsd_log_debug(("Log file opened"));
-	
-	fsd_log_return((""));
-}
-
-ssize_t
-pbsdrmaa_read_line_job_on_missing ( pbsdrmaa_log_reader_t * self, char * line, char * buffer, ssize_t size, int * idx, int * end_idx, int * line_idx )
-{
-	int n = fsd_getline_buffered(line,buffer,size,self->fd, idx, end_idx, line_idx);
-	
-	if(n >= 0)
-		self->log_file_read_size += n;
-		
-	if(self->log_file_read_size >= self->log_file_initial_size)
-		return -1; 
-
-	return n; 
-}
-
-void
-pbsdrmaa_select_file_accounting ( pbsdrmaa_log_reader_t * self )
-{
-	pbsdrmaa_session_t *pbssession = (pbsdrmaa_session_t*) self->session;
-		
-	char * log_path = NULL;
-
-	struct tm tm; 
-		
-	fsd_log_enter((""));
-		
-	time(&self->t);	
-			
-	localtime_r(&self->t,&tm);
-				
-	#define DRMAA_ACCOUNTING_MAX_TRIES (12)
-	/* generate new date, close file and open new */
-	if((log_path = fsd_asprintf("%s/server_priv/accounting/%04d%02d%02d",
-				pbssession->pbs_home,	 
-				tm.tm_year + 1900,
-				tm.tm_mon + 1,
-				tm.tm_mday)) == NULL) {
-		fsd_exc_raise_fmt(FSD_ERRNO_INTERNAL_ERROR,"Read accounting file - Memory allocation wasn't possible");
-	}
-
-	if(self->fd != -1)
-		close(self->fd);
-
-	fsd_log_debug(("Accounting Log file: %s",log_path));
-
-	if((self->fd = open(log_path,O_RDONLY) ) == -1 )
-	{
-		fsd_log_error(("Can't open accounting log file. Change directory chmod and verify pbs_home."));
-	} 
-
-	fsd_free(log_path);
-
-	fsd_log_debug(("Accounting Log file opened"));
-
-	fsd_log_return((""));	
-}
-
-ssize_t
-pbsdrmaa_read_line_accounting ( pbsdrmaa_log_reader_t * self, char * line, char * buffer, ssize_t size, int * idx, int * end_idx, int * line_idx )
-{
-	return fsd_getline_buffered(line,buffer,size,self->fd,idx,end_idx,line_idx);
-}
-
-enum field_acc
-{ 
-	FLD_ACC_DATE = 0,
-	FLD_ACC_EVENT = 1,
-	FLD_ACC_ID = 2,
-	FLD_ACC_MSG = 3
-};
-
-bool 
-pbsdrmaa_read_log_accounting( pbsdrmaa_log_reader_t * self )
-{
-	pbsdrmaa_job_t *pbsjob = (pbsdrmaa_job_t*) self->job;	
-	bool res = false;
-	
-	fsd_job_t *volatile temp_job = NULL;
-		
-	fsd_log_enter((""));
-	fsd_log_debug(("Accounting Log file opened"));
-	if(self->job == NULL)
-		fsd_mutex_lock( &self->session->mutex );
-
-	TRY
-	{		
-		TRY
-		{
-			char line[4096] = "";
-			char buffer[4096] = "";
-			int idx = 0, end_idx = 0, line_idx = 0;
-			
-			self->select_file(self);
-			
-			if(self->fd != -1) 					
-			while ((self->read_line(self, line,buffer, sizeof(line), &idx,&end_idx,&line_idx)) > 0)
-			{
-				const char *volatile ptr = line;
-  				char field[256] = "";
-				int volatile field_n = 0;
- 				int n;
-				
-				bool volatile job_id_match = false;	
-			
-				bool volatile job_found = false;
-  				char *  temp_date = NULL;
-				
-				struct batch_status status;
-				
-				while ( sscanf(ptr, "%255[^;]%n", field, &n) == 1 ) /* split current line into fields */
-				{
-					status.next = NULL;
-					status.attribs = NULL;
-				
-					if(field_n == FLD_ACC_DATE)
-					{
-						temp_date = fsd_strdup(field);
-					}
-					else if(field_n == FLD_ACC_EVENT)
-					{
-							
-					}
-					else if(field_n == FLD_ACC_ID)
-					{							
-						TRY
-						{								
-								int diff = -1;
-								diff = fsd_job_id_cmp(self->job->job_id,field);
-								if( diff == 0)
-								{
-									/* read this file to the place we started and exit*/
-									fsd_log_debug(("Accounting found job: %s",self->job->job_id));
-									job_found = true;
-									job_id_match = true; 
-									status.name = fsd_strdup(self->job->job_id);
-								}	
-						}
-						END_TRY	
-					}
-					else if(job_id_match && field_n == FLD_ACC_MSG)
-					{					
-						struct attrl * struct_attrl = calloc(10,sizeof(struct attrl));
-						int i;
-
-						if(field[0] == 'q')
-						{
-							status.attribs = &struct_attrl[0];
-							struct_attrl[0].name =  ATTR_queue;
-							struct_attrl[0].value = fsd_strdup(strchr(field,'=')+1);
-							struct_attrl[0].next = NULL;
-						}
-						else if(field[0] == 'u')
-						{
-							/* rusage */
-							const char *ptr2 = field;
-							char  msg[ 256 ] = "";
-							int n2 = 0;
-							int msg_field_n = 0;
-				
-							status.attribs = &struct_attrl[0];
-
-							while ( sscanf(ptr2, "%255[^ ]%n", msg, &n2) == 1 )
-							 {						
-								switch(msg_field_n) 
-								{
-									case FLD_MSG_ACC_USER:
-										struct_attrl[msg_field_n].name = ATTR_euser;
-										break;
-
-									case FLD_MSG_ACC_GROUP:
-										struct_attrl[msg_field_n].name = ATTR_egroup;
-										break;
-
-									case FLD_MSG_ACC_JOBNAME:
-										struct_attrl[msg_field_n].name = ATTR_name;
-										break;
-
-									case FLD_MSG_ACC_QUEUE:
-										struct_attrl[msg_field_n].name = ATTR_queue;
-										break; 
-
-									case FLD_MSG_ACC_CTIME:
-										struct_attrl[msg_field_n].name = ATTR_ctime;
-										break; 
-										
-									case FLD_MSG_ACC_QTIME:
-										struct_attrl[msg_field_n].name = ATTR_qtime;
-										break; 
-										
-									case FLD_MSG_ACC_ETIME:
-										struct_attrl[msg_field_n].name = ATTR_etime;
-										break; 
-#ifndef PBS_PROFESSIONAL		
-									case FLD_MSG_ACC_START:
-										struct_attrl[msg_field_n].name = ATTR_start_time;
-#else
-									case FLD_MSG_ACC_START:
-										struct_attrl[msg_field_n].name = ATTR_stime;
-#endif
-										
-									case FLD_MSG_ACC_OWNER:
-										struct_attrl[msg_field_n].name = ATTR_owner;
-										break; 
-										
-									case FLD_MSG_ACC_EXEC_HOST:
-										struct_attrl[msg_field_n].name = ATTR_exechost;
-										break; 										
-								}
-								
-								struct_attrl[msg_field_n].value  = fsd_strdup(strchr(msg,'=')+1);
-								if(msg_field_n!=9)
-								{
-									struct_attrl[msg_field_n].next = &struct_attrl[msg_field_n+1];
-								}
-								else
-								{
-									struct_attrl[msg_field_n].next = NULL;
-									break;
-								}
-
-								ptr2 += n2; 
-								msg_field_n++;
-								if ( *ptr2 != ' ' )
-									break;
-
-								++ptr2;
-							}
-						}						
-
-						if( job_found && status.attribs != NULL)
-						{
-							fsd_log_debug(("Accounting file - updating job: %s", self->job->job_id ));
-							pbsjob->update( self->job, &status );
-							res = true;
-						}
-						
-						if(self->job == NULL)
-						{
-							fsd_cond_broadcast( &temp_job->status_cond);
-							fsd_cond_broadcast( &self->session->wait_condition );
-						}
-						if ( temp_job )
-							temp_job->release( temp_job );
-	
-						for(i = 0; i < 10; i++)
-						{
-							fsd_free(struct_attrl[i].value);
-						}
-						fsd_free(struct_attrl);
-						fsd_free(status.name);
-					}
-					
-					
-					ptr += n; 
-					if ( *ptr != ';' )
-					{
-						break; /* end of line */
-					}
-					field_n++;
-					++ptr;
-				}		
-
-				fsd_free(temp_date);			
-			} /* end of while getline loop */	
-			
-		}		
-		EXCEPT_DEFAULT
-		 {
-			const fsd_exc_t *e = fsd_exc_get();
-			/* Its better to exit and communicate error rather then let the application to hang */
-			fsd_log_fatal(( "Exception in reading accounting file %s: <%d:%s>. Exiting !!!", self->name, e->code(e), e->message(e) ));
-			exit(1);
-		 }
-		END_TRY
-
-		if(self->fd != -1)
-			close(self->fd);
-		fsd_log_debug(("%s - Accounting log file closed",self->name));	
-	}
-	FINALLY
-	{
-	 	fsd_log_debug(("%s - Terminated.",self->name));	
-		if(self->job == NULL)
-			fsd_mutex_unlock( &self->session->mutex ); /**/
-	}
-	END_TRY
-	
-	fsd_log_return((""));
-	return res;
-}
-
-int 
-fsd_job_id_cmp(const char *s1, const char *s2) /* maybe move to drmaa_utils? */
-{
-	int job1;
-	int job2;
-	char *rest = NULL;
-	char *token = NULL;
-	char *ptr = fsd_strdup(s1);
-	token = strtok_r(ptr, ".", &rest);
-	job1 = atoi(token);
-	
-	fsd_free(token);
-	
-	ptr = fsd_strdup(s2);
-	token = strtok_r(ptr,".",&rest);
-	job2 = atoi(token);
-	
-	fsd_free(token);
-	return job1 - job2;
-}
-
+
Index: trunk/pbs_drmaa/submit.c
===================================================================
--- trunk/pbs_drmaa/submit.c	(revision 24)
+++ trunk/pbs_drmaa/submit.c	(revision 29)
@@ -151,23 +151,8 @@
 			if( name  &&  name[0] != '!' && pbs_tmpl->get_attr( pbs_tmpl, name ) )
 			 {
-				struct attrl *p;
-				const char *resource;
 				const char *value;
+
 				value = pbs_tmpl->get_attr( pbs_tmpl, name );
-				fsd_malloc( p, struct attrl );
-				memset( p, 0, sizeof(struct attrl) );
-				p->next = pbs_attr;
-				pbs_attr = p;
-				resource = strchr( name, '.' );
-				if( resource )
-				 {
-					p->name = fsd_strndup( name, resource-name );
-					p->resource = fsd_strdup( resource+1 );
-				 }
-				else
-					p->name = fsd_strdup( name );
-				fsd_log_debug(("set attr: %s = %s", name, value));
-				p->value = fsd_strdup( value );
-				p->op = SET;
+				pbs_attr = pbsdrmaa_add_attr( pbs_attr, name, value );
 			 }
 		 }
Index: trunk/pbs_drmaa/log_reader.h
===================================================================
--- trunk/pbs_drmaa/log_reader.h	(revision 21)
+++ trunk/pbs_drmaa/log_reader.h	(revision 29)
@@ -25,4 +25,6 @@
 #endif
 
+#include <stdio.h>
+
 #include <drmaa_utils/job.h>
 #include <drmaa_utils/session.h>
@@ -31,8 +33,5 @@
 
 pbsdrmaa_log_reader_t * 
-pbsdrmaa_log_reader_new ( fsd_drmaa_session_t * session, fsd_job_t * job );
-
-pbsdrmaa_log_reader_t * 
-pbsdrmaa_log_reader_accounting_new ( fsd_drmaa_session_t * session, fsd_job_t * job );
+pbsdrmaa_log_reader_new ( fsd_drmaa_session_t * session);
 
 void
@@ -41,17 +40,10 @@
 struct pbsdrmaa_log_reader_s {
 	fsd_drmaa_session_t *volatile session ;
-	fsd_job_t *volatile job;
 	
-	bool (*
-	read_log) ( pbsdrmaa_log_reader_t * self );
+	void (*read_log) ( pbsdrmaa_log_reader_t * self );
 	
-	void (*
-	select_file) ( pbsdrmaa_log_reader_t * self );
+	void (*select_file) ( pbsdrmaa_log_reader_t * self );
 	
-	/* line - read line, buffer - keeps read but not returned lines, idx, end_idx and line_idx values needed to be kept outside the function */
-	ssize_t (*
-	read_line) ( pbsdrmaa_log_reader_t * self, char * line, char * buffer, ssize_t size, int * idx, int * end_idx, int * line_idx );
-	
-	/* specifies if function should run */
+	/* determines if function should run */
 	bool run_flag;
 	
@@ -59,18 +51,6 @@
 	time_t t;	
 	
-	/* for job_on_missing  - available log files */
-	char ** log_files;
-	
-	/* for job_on_missing - number of log files */
-	int log_files_number;
-	
-	/* log file descriptor */
-	int volatile fd;
-	
-	/* for job_on_missing - log file size when function was ran */
-	off_t log_file_initial_size;
-	
-	/* for job_on_missing - read lines size */
-	off_t log_file_read_size;
+	/* log file handle */
+	FILE *fhandle;
 	
 	/* for wait_thread - day changed */
@@ -79,6 +59,4 @@
 	/* for wait_thread - log file first open */
 	bool volatile first_open;	
-	
-	char * name;
 };
 
Index: trunk/pbs_drmaa/job.c
===================================================================
--- trunk/pbs_drmaa/job.c	(revision 26)
+++ trunk/pbs_drmaa/job.c	(revision 29)
@@ -59,12 +59,6 @@
 pbsdrmaa_job_on_missing_standard( fsd_job_t *self );
 
-void
-pbsdrmaa_job_on_missing_log_based( fsd_job_t *self );
-
 static void
 pbsdrmaa_job_update( fsd_job_t *self, struct batch_status* );
-
-bool
-pbsdrmaa_job_update_status_accounting( fsd_job_t *self );
 
 
@@ -254,11 +248,5 @@
 		else if( self->state < DRMAA_PS_DONE )
 		 {
-#ifndef PBS_PROFESSIONAL
-			/*best effort call*/
-			if (pbsdrmaa_job_update_status_accounting(self) == false)
-				self->on_missing( self );
-#else
 			self->on_missing( self );
-#endif
 		 }
 	 }
@@ -305,5 +293,5 @@
 				break;
 			case PBSDRMAA_ATTR_EXIT_STATUS:
-				exit_status = atoi( i->value );
+				exit_status = fsd_atoi( i->value );
 				break;
 			case PBSDRMAA_ATTR_RESOURCES_USED:
@@ -453,5 +441,5 @@
 		pbsdrmaa_job_on_missing_standard( self );	
 	else
-		pbsdrmaa_job_on_missing_log_based( self );	
+		pbsdrmaa_job_on_missing_standard( self ); /* TODO: try to provide implementation that uses accounting/server log files */
 }
 
@@ -506,50 +494,2 @@
 }
 
-void
-pbsdrmaa_job_on_missing_log_based( fsd_job_t *self )
-{
-	fsd_drmaa_session_t *session = self->session;
-	pbsdrmaa_log_reader_t *log_reader = NULL;
-	
-	fsd_log_enter(( "({job_id=%s})", self->job_id ));
-	fsd_log_info(( "Job %s missing from DRM queue", self->job_id ));
-	
-	TRY
-	{	
-		log_reader = pbsdrmaa_log_reader_new( session, self);
-		log_reader->read_log( log_reader ); 
-	}
-	FINALLY
-	{
-		pbsdrmaa_log_reader_destroy( log_reader );
-	}
-	END_TRY
-
-	fsd_log_return(( "; job_ps=%s, exit_status=%d",
-				drmaa_job_ps_to_str(self->state), self->exit_status ));	
-}
-
-bool
-pbsdrmaa_job_update_status_accounting( fsd_job_t *self )
-{
-	fsd_drmaa_session_t *session = self->session;
-	pbsdrmaa_log_reader_t *log_reader = NULL;
-	bool res = false;
-	
-	fsd_log_enter(( "({job_id=%s})", self->job_id ));
-	fsd_log_info(( "Reading job %s info from accounting file", self->job_id ));
-	
-	TRY
-	{	
-		log_reader = pbsdrmaa_log_reader_accounting_new( session, self);
-		bool res = log_reader->read_log( log_reader ); 
-	}
-	FINALLY
-	{
-		pbsdrmaa_log_reader_destroy( log_reader );
-	}
-	END_TRY
-
-	fsd_log_return((""));
-	return res;
-}
Index: trunk/pbs_drmaa/util.c
===================================================================
--- trunk/pbs_drmaa/util.c	(revision 16)
+++ trunk/pbs_drmaa/util.c	(revision 29)
@@ -73,4 +73,38 @@
 		fsd_free( p );
 	 }
+}
+
+struct attrl *
+pbsdrmaa_add_attr( struct attrl *head, const char *name, const char *value)
+{
+	struct attrl *p = NULL;
+	char *resource = NULL;
+
+	fsd_malloc( p, struct attrl );
+	memset( p, 0, sizeof(struct attrl) );
+
+	resource = strchr( name, '.' );
+
+	if( resource )
+	 {
+		p->name = fsd_strndup( name, resource - name );
+		p->resource = fsd_strdup( resource+1 );
+	 }
+	else
+	 {
+		p->name = fsd_strdup( name );
+	 }
+
+	p->value = fsd_strdup(value);
+	p->op = SET;
+
+	fsd_log_debug(("set attr: %s = %s", name, value));
+
+	if (head)
+		p->next = head;
+	else
+		p->next = NULL;
+
+	return p;
 }
 
@@ -247,85 +281,3 @@
 }
 
-ssize_t fsd_getline(char * line,ssize_t size, int fd)
-{
-	char buf;
-	char * ptr = NULL;
-	ssize_t n = 0, rc;
-	ptr = line;
-	for(n = 1; n< size; n++)
-	{		
-		if( (rc = read(fd,&buf,1 )) == 1) {
-			*ptr++ = buf;
-			if(buf == '\n')
-			{
-				break;
-			}
-		}
-		else if (rc == 0) {
-			if (n == 1)
-				return 0;
-			else
-				break;
-		}		
-		else
-			return -1; 
-	}
-
-	return n;
-} 
-
-ssize_t fsd_getline_buffered(char * line,char * buf, ssize_t size, int fd, int * idx, int * end_idx, int * line_idx)
-{
-	int i = -1;
-	int rc = -1;
-
-	memset(line,0,size);
-	
-start:
-	/* idx - start of data to parse (in buffer)
-	   end_idx - end of data read from log (in buffer)
-	   line_idx - place to write data in output line */
-	if(*idx < *end_idx)
-	{
-		/* take line from buffer */
-		for(i = *idx; i<= *end_idx;i++)
-		{		
-			if(buf[i] == '\n')
-			{
-				int tmp = i - *idx;
-				strncpy(line + *line_idx,buf + *idx,tmp);				
-				*idx = i + 1;
-			
-				tmp+= *line_idx;
-				*line_idx = 0;
-				
-				return tmp;
-			}
-		}
-		
-		/* there was no '\n' so next part of log needs to be read. save lines beginning */
-		if(*line_idx + i - *idx > size )
-			fsd_exc_raise_fmt(FSD_ERRNO_INTERNAL_ERROR,"Line longer than %d unsupported",size);
-		
-		strncpy(line + *line_idx,buf + *idx,i - *idx);
-		*line_idx += i - *idx;
-		*idx = 0;
-		*end_idx = 0;
-		goto start;
-	}
-	else
-	{		
-		/* read log */
-		if((rc = read(fd,buf,size)) > 0)
-		{		
-			*end_idx = rc - 1;
-			*idx = 0;
-			goto start;
-		}
-		else if (rc == 0) 
-			return 0;
-		else
-			return -1;
-	}
-} 
-
+
Index: trunk/pbs_drmaa/util.h
===================================================================
--- trunk/pbs_drmaa/util.h	(revision 12)
+++ trunk/pbs_drmaa/util.h	(revision 29)
@@ -31,6 +31,7 @@
 
 void pbsdrmaa_free_attrl( struct attrl *list );
-void pbsdrmaa_dump_attrl(
-		const struct attrl *attribute_list, const char *prefix );
+void pbsdrmaa_dump_attrl( const struct attrl *attribute_list, const char *prefix );
+
+struct attrl *pbsdrmaa_add_attr( struct attrl *head, const char *name, const char *value);
 
 /**
@@ -43,9 +44,4 @@
 pbsdrmaa_write_tmpfile( const char *content, size_t len );
 
-ssize_t
-fsd_getline(char * line,ssize_t size, int fd);
-
-ssize_t 
-fsd_getline_buffered(char * line,char * buf, ssize_t size, int fd, int * idx, int * end_idx, int * line_idx);
 
 #endif /* __PBS_DRMAA__UTIL_H */
Index: trunk/pbs_drmaa/session.c
===================================================================
--- trunk/pbs_drmaa/session.c	(revision 12)
+++ trunk/pbs_drmaa/session.c	(revision 29)
@@ -520,5 +520,5 @@
 	TRY
 	{	
-		log_reader = pbsdrmaa_log_reader_new( self, NULL);
+		log_reader = pbsdrmaa_log_reader_new( self );
 		log_reader->read_log( log_reader );
 	}
Index: trunk/pbs_drmaa/pbs_drmaa.h
===================================================================
--- trunk/pbs_drmaa/pbs_drmaa.h	(revision 12)
+++ 	(revision )
@@ -1,120 +1,0 @@
-/* $Id$ */
-/*
- *  FedStage DRMAA for PBS Pro
- *  Copyright (C) 2006-2007  FedStage Systems
- *
- *  This program is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  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, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef __DRMAA__PBS_DRMAA_H
-#define __DRMAA__PBS_DRMAA_H
-
-#ifdef HAVE_CONFIG_H
-#	include <config.h>
-#endif
-
-#include <sys/types.h>
-#include <stdbool.h>
-#include <time.h>
-#include <pbs_ifl.h>
-
-#include <drmaa_utils/common.h>
-#include <pbs_drmaa/pbs_attrib.h>
-
-
-/** PBS DRMAA specific session data. */
-struct drmaa_session_impl_s {
-	int                 pbs_conn;    /**< PBS connection (or -1). */
-	drmaa_mutex_t       conn_mutex;  /**< Mutex for PBS connection. */
-	drmaa_thread_t      wait_thread;
-	bool                wait_thread_started;
-	struct attrl       *status_attrl;
-};
-
-
-enum { PBS_ATTRIBS_BITSET_SIZE = (N_PBS_DRMAA_ATTRIBS+31) / 32 };
-struct drmaa_submit_impl_s {
-	struct attrl *pbs_attribs;
-	uint32_t pbs_attribs_bitset[ PBS_ATTRIBS_BITSET_SIZE ];
-	char *script_filename;
-};
-
-
-void *
-drmaa_wait_thread( void *arg );
-
-drmaa_submit_ctx_t *
-drmaa_create_submission_context(
-		const drmaa_job_template_t *jt, int bulk_no,
-		drmaa_err_ctx_t *err
-		);
-
-void
-drmaa_free_submission_context( drmaa_submit_ctx_t *c, drmaa_err_ctx_t* );
-
-void drmaa_set_job_defaults        ( drmaa_submit_ctx_t*, drmaa_err_ctx_t* );
-void drmaa_create_job_script       ( drmaa_submit_ctx_t*, drmaa_err_ctx_t* );
-void drmaa_set_job_submit_state    ( drmaa_submit_ctx_t*, drmaa_err_ctx_t* );
-void drmaa_set_job_files           ( drmaa_submit_ctx_t*, drmaa_err_ctx_t* );
-void drmaa_set_file_staging        ( drmaa_submit_ctx_t*, drmaa_err_ctx_t* );
-void drmaa_set_job_resources       ( drmaa_submit_ctx_t*, drmaa_err_ctx_t* );
-void drmaa_set_job_environment     ( drmaa_submit_ctx_t*, drmaa_err_ctx_t* );
-void drmaa_set_job_email_notif     ( drmaa_submit_ctx_t*, drmaa_err_ctx_t* );
-void drmaa_apply_native_spec       ( drmaa_submit_ctx_t*, drmaa_err_ctx_t* );
-void drmaa_apply_job_category      ( drmaa_submit_ctx_t*, drmaa_err_ctx_t* );
-char *drmaa_translate_staging( const char *stage, drmaa_err_ctx_t* );
-
-void
-drmaa_parse_qsub_args(
-		drmaa_submit_ctx_t *ctx, const char *string,
-		drmaa_err_ctx_t *err
-		);
-
-void
-drmaa_job_missing( drmaa_job_t *job, drmaa_err_ctx_t *err );
-
-void
-drmaa_add_pbs_attr(
-		drmaa_submit_ctx_t *c,
-		int attr, char *resource, char *value,
-		unsigned set,
-		drmaa_err_ctx_t *err
-		);
-
-char *
-drmaa_write_tmpfile(
-	 const char *content, size_t len,
-	 drmaa_err_ctx_t *err
-	 );
-
-struct attrl *
-drmaa_create_pbs_attrl( drmaa_err_ctx_t *err );
-
-void
-drmaa_log_attrl( const struct attrl *attribute_list, const char *prefix );
-
-void
-drmaa_free_attrl( struct attrl *attr );
-
-void
-drmaa_err_pbs_error( drmaa_err_ctx_t *err );
-
-int
-drmaa_err_map_pbs_error( int pbs_errcode );
-
-#define RAISE_PBS() \
-	drmaa_err_pbs_error( err )
-
-#endif /* __DRMAA__PBS_DRMAA_H */
-
