summaryrefslogtreecommitdiff
path: root/tbl_qcmd.c
blob: bc609b604fd59e2fc6722833aea175c267243151 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
#include "tbl_qcmd.h"

tble_exec *tbl_exec_c()
{
	MVAR_ALLOC_STRC(ret,tble_exec,{
		return NULL;});
	return ret;
}


tbl_exec *tbl_exec_list_c(int size)
{
	MVAR_ALLOC_STRC(ret,tbl_exec,{
		return NULL;});

	ret->id = uniq_id();
	ret->size = 0;
	ret->max_size = size;

	MVAR_ALLOC_ARR(cmd,tble_exec*,size,{
		free(ret);
		return NULL;
	})

	ret->cmd = cmd;

	return ret;
}

//TODO check if command allready excists
int tbl_exec_add(tbl_exec *tbl, tble_exec *cmd)
{
	if (tbl == NULL || cmd == NULL)
	{
		return -1;
	}

	tbl->cmd[tbl->size] = cmd;
	if (tbl->size+1<tbl->max_size)
	{
		tbl->size += 1;
	} else 
	{
		return -1;
	}

	return 0;
}

int tbl_exec_in_s(tbl_exec *tbl, char *cmd)
{
	int i;
	tble_exec *el=NULL;

	if ((tbl == NULL) || (cmd == NULL))
	{
		PERM();
		return -1;
	}

	for (i=0; i<tbl->size; i++)
	{
		el = tbl->cmd[i];
		if (strncmp(el->cmd, cmd, strlen(el->cmd))==0)
		{
			return i;
		}
	}

	return -1;
}

int tbl_exec_run(tbl_exec *tbl, char *cmd, void **resp)
{
	int ret=-1;

	int i;
	tble_exec *el=NULL;

	if ((tbl == NULL) || (cmd == NULL) || (resp==NULL) || (*resp != NULL))
	{
		PERM();
		return -1;
	}

	for (i=0;i<tbl->size;i++)
	{
		el = tbl->cmd[i];
		if (strncmp(el->cmd, cmd, strlen(el->cmd))==0)
		{
			void *ret_resp=NULL;

			//execute callback
			PRINT("EXEC CLB ID %d\n",i);
			ret_resp = el->callback(NULL); //no params for now

			//return 
			if (ret_resp != NULL)
			{
				*resp = ret_resp;
				ret = i;
			}
		}
	}

	return ret;
}

tble_exec *tbl_exec_search_cmd(tbl_exec *tbl, tble_qcmd *cmd)
{
	tble_exec *ret = NULL;
	tble_exec *cur_exec = NULL;
	int i;

	if ((tbl == NULL)||(cmd == NULL))
	{
		PERM();
		return NULL;
	}

	if (tbl->id != cmd->tid) 
	{
		PERM();
		return NULL;
	}

	for (i=0;i<tbl->max_size;i++)
	{
		cur_exec = tbl->cmd[i];
		//command match
		if ((strncmp(cur_exec->cmd, cmd->cmd, strlen(cmd->cmd)) == 0) && 
		    (strlen(cur_exec->cmd) == strlen(cmd->cmd)))
		// command id and exec id match
		if (cur_exec->id == cmd->tidx)
		{
			ret = cur_exec;
			break;
		}
	}

	return ret;
}


int tbl_exec_print(tble_exec *tble, int flags)
{
	if (tble == NULL)
	{
		return -1;
	}

	if (flags&TBL_PF_EXEC_ID)
	{
		printf("ID:[%d] ", tble->id);
	}

	if (flags&TBL_PF_EXEC_NAME)
	{
		printf("NAME:[%s] ", tble->name);
	}

	if (flags&TBL_PF_EXEC_CMD)
	{
		printf("CMD:[%s] ", tble->cmd);
	}

	if (flags&TBL_PF_EXEC_TYPE)
	{
		printf("TYPE:[%d] ", tble->type);
	}

	printf("\n");

	return 0;
}

int tbl_exec_tbl_free(tbl_exec *tbl)
{
	int i;

	if (tbl == NULL)
	{
		return -1;
	}

	if (tbl->cmd != NULL)
	{
		for (i=0;i<tbl->size;i++)
		{
			tbl_exec_tel_free(tbl->cmd[i]);
		}
		free(tbl->cmd);
	}

	free(tbl);

	return 0;
}


int tbl_exec_tel_free(tble_exec *tble)
{
	int i;

	if (tble == NULL)
	{
		return -1;
	}

	free(tble->name);
	tble->name = NULL;

	free(tble->cmd);
	tble->cmd = NULL;

	free(tble);
	tble = NULL;

	return 0;
}


int tbl_exec_print_tbl(tbl_exec *tbl, int flags)
{
	int i;
	tble_exec *el=NULL;

	if (tbl == NULL)
	{
		return -1;
	}

	for (i=0; i<tbl->size; i++)
	{
		el = tbl->cmd[i];
		if (el != NULL)
		{
			printf(">%02d< ",i);
			tbl_exec_print(el, flags);
		}
	}
	return 0;
}

tble_qcmd *tbl_qcmd_cmd_c()
{
	MVAR_ALLOC_STRC(ret,tble_qcmd,{
		return NULL;});
	return ret;
}


tbl_qcmd* tbl_qcmd_c(int size)
{
	MVAR_ALLOC_STRC(ret,tbl_qcmd,{
		return NULL;});

	ret->id = uniq_id();
	ret->size = 0;
	ret->max_size = size;

	//alloc array of (tble_qcmd*) pointers
	MVAR_ALLOC_ARR(cmd,tble_qcmd*,size,{
		free(ret);
		return NULL;
	})

	ret->cmd = cmd;

	return ret;
}


int tbl_qcmd_exec(tbl_qcmd *tcmd, tbl_exec *texec)
{
	int ret=-1;
	int i,j;

	if (tcmd == NULL)
	{
		PERM();
		return -1;
	}

	if (texec == NULL)
	{
		PERM();
		return -1;
	}

	//EFFICIENT CODE MATE
	//search for commands that are in NONE state
	for (i=0; i<tcmd->size; i++)
	{
		tble_qcmd *tc = tcmd->cmd[i];
		if (tc->state == QCMD_INIT)
		for (j=0;j<texec->size;j++)
		{
			tble_exec *e = texec->cmd[j];
			if (strncmp(e->cmd, tc->cmd, strlen(e->cmd)) == 0)
			{
				tc->tid   = texec->id;  //save table id
				tc->tidx  = e->id;      //set executor id
				tc->state = QCMD_READY; //change state, that we ready to be executed
			}
		}
	}

	return 0;
}


int tbl_qcmd_add(tbl_qcmd *tbl, tble_qcmd *cmd)
{
	if (tbl == NULL || cmd == NULL)
		return -1;
	cmd->id = uniq_id();
	//all command that are just added will be in zero state
	//should match to some executot then can change state
	cmd->state = QCMD_INIT;
	tbl->cmd[tbl->size] = cmd;
	if (tbl->size+1<tbl->max_size)
	{
		tbl->size += 1;
	} else 
	{
		return -1;
	}

	return 0;
}


int tbl_qcmd_chk(tbl_qcmd *tbl)
{
	int ret=-1;
	int i;

	if (tbl == NULL)
	{
		return -1;
	}

	for (i=0;i<tbl->max_size;i++)
	{
		//if command in some state like segfaulted or 
		// finished running then lets check
		if ((tbl->cmd[i]->state == QCMD_TIMEOUT)||
		    (tbl->cmd[i]->state == QCMD_DONE))
		{
			return i;
		}
	}

	return ret;
}

//delete but dont free from list? well lets free
int tbl_qcmd_del_by_idx(tbl_qcmd *tbl, int idx)
{
	int ret=-1;
	tble_qcmd *replace_cmd=NULL;
	tble_qcmd *remove_cmd=NULL;

	if (tbl == NULL || idx < 0)
	{
		return -1;
	}

	if (idx >= tbl->size)
	{
		return -1;
	}

	if (tbl->size == 1)
	{
		tbl->size = 0;
		return 0;
	}

	//if last idx = size then will overwrite itself and decrease command counter
	replace_cmd = tbl->cmd[tbl->size]; //if tbl->size changed you get into troubles pal
	tbl->size -= 1;

	//free before replace
	remove_cmd = tbl->cmd[idx];
	tbl_qcmd_destroy_cmd(remove_cmd);

	tbl->cmd[idx] = replace_cmd;

	//looks like no errors
	ret = 0;

	return ret;
}


/*
1 - command found and have been deleted
0 - nothing have been deleted
-1 - someking of error happened
*/
int tbl_qcmd_del_by_id(tbl_qcmd *tbl, int id)
{
	int i;
	tble_qcmd *replace_cmd = NULL;
	tble_qcmd *remove_cmd = NULL;


	if (tbl == NULL)
	{
		PERM();
		return -1;
	}

	if (id < 0)
	{
		PERM();
		return -1;
	}

	for (i=0;i<tbl->size;i++)
	{
		tble_qcmd *iter = tbl->cmd[i];

		if (iter->id == id)
		{
			// if its just last element reduce size and no other logic
			if (i == tbl->size-1)
			{
				tbl_qcmd_destroy_cmd(tbl->cmd[i]);
				tbl->size -= 1;
				return 1;
			} else
			//if element not last one
			{
				replace_cmd = tbl->cmd[tbl->size-1];
				tbl->size -= 1;

				//free before replace
				remove_cmd = iter;
				tbl_qcmd_destroy_cmd(remove_cmd);
				
				iter = replace_cmd;
				return 1; //yea yea i know
			}	
		}
	}

	return 0;
}


int tbl_qcmd_add_tok(tble_qcmd *tbl, irc_token *tk)
{
	if (tbl == NULL)
	{
		PERM();
		return -1;
	}

	if (tk == NULL)
	{
		PERM();
		return -1;
	}

	//just copy inside new token
	tbl->tok = token_cpy_new(tk);

	return 0;
}

tble_qcmd* tbl_qcmd_match_id(tbl_qcmd *tbl, int idx)
{
	int i;

	if (tbl == NULL)
	{
		PERM();
		return NULL;
	}

	if (idx < 0)
	{
		PERM();
		return NULL;
	}

	for (i=0;i<tbl->size;i++)
	{
		tble_qcmd *iter = tbl->cmd[i];

		if (iter->id == idx)
		{
			return iter;
		}
	}

	return NULL;
}

int tbl_qcmd_resp(tbl_qcmd *tbl, tble_cmd_resp *resp)
{
	int ret = -1;
	tble_qcmd *single_cmd=NULL;

	int i;

	if ((tbl == NULL) || (resp == NULL))
	{
		return -1;
	}

	for (i=0;i<tbl->size;i++)
	{
		single_cmd = tbl->cmd[i];

		//check this for particular checks
		if (single_cmd->cid == resp->qid)
		{
			return single_cmd->id;
		}
	}

	return ret;
}

tble_cmd_param* tbl_cmd_param(tble_qcmd *cmd)
{
	if (cmd == NULL)
	{
		return NULL;
	}

	if (cmd->state != QCMD_READY)
	{
		PERM();
		return NULL;
	}


	MVAR_ALLOC_STRC(ret,tble_cmd_param,{
		PERM();
		return NULL;});

	ret->id = uniq_id();
	ret->qid = cmd->id;
	ret->cmd = alloc_new_str(cmd->cmd);
	ret->param = alloc_new_str(cmd->param);
	ret->out_q = cmd->out_q;

	return ret;
}

int tbl_cmd_param_free(tble_cmd_param *param)
{
	if (param == NULL)
	{
		return -1;
	}

	PNL();
	free(param->cmd);
	PNL();
	free(param->param);
	PNL();
	free(param);

	return 0;
}

int tbl_cmd_param_print(tble_cmd_param *param)
{
	if (param == NULL)
		return -1;
	
	printf("PARAM ");
	printf("ID:[%d] ",    param->id);
	printf("QID:[%d] ",   param->qid);
	printf("OUT_Q:[%d] ", param->out_q);
	printf("CMD:[%s] ",   param->cmd);
	printf("PARAM:[%s] ", param->param);
	printf("\n");

	return 0;
}

tble_cmd_resp* tbl_cmd_resp_c(tble_cmd_param *param)
{
	if (param == NULL)
	{
		PERM();
		return NULL;
	}

	MVAR_ALLOC_STRC(ret, tble_cmd_resp,
	{
		PERM();
		return NULL;
	});

	ret->id = uniq_id();
	ret->qid = param->qid;

	return ret;
}

int tbl_cmd_resp_free(tble_cmd_resp *resp)
{
	if (resp == NULL)
	{
		return -1;
	}

	free(resp->resp);
	resp->resp = NULL;

	free(resp);
	resp = NULL;

	return 0;
}

int tbl_cmd_resp_print(tble_cmd_resp *resp)
{
	if (resp == NULL)
		return -1;

	printf("RESP ");
	printf("ID:[%d] ",       resp->id);
	printf("QID:[%d] ",      resp->qid);
	printf("TYPE:[%d] ",     resp->type);
	printf("RET_CODE:[%d] ", resp->ret_code);
	printf("RESP:[%s] ",     resp->resp);
	printf("\n");

	return 0;
}

//add to print token if its there
int tbl_qcmd_print_cmd(tble_qcmd *tbl, int flags)
{
	if (tbl == NULL)
	{
		return -1;
	}

	if (flags&TBL_PF_QCMD_ID)
	{
		printf("ID:[%d] ",tbl->id);
	}
	if (flags&TBL_PF_QCMD_CID)
	{
		printf("CID:[%d] ",tbl->cid);
	}
	if (flags&TBL_PF_QCMD_TIMESTAMP)
	{
		printf("TIMESTAMP:[%d] ", tbl->timestamp);
	}
	if (flags&TBL_PF_QCMD_STATE)
	{
		printf("STATE:[%d] ", tbl->state);
	}
	if (flags&TBL_PF_QCMD_TIMEOUT)
	{
		printf("TIMEOUT:[%d] ", tbl->timeout);
	}
	if (flags&TBL_PF_QCMD_CMD)
	{
		printf("CMD:[%s] ", tbl->cmd);
	}
	if (flags&TBL_PF_QCMD_PARAM)
	{
		printf("PARAM:[%s] ", tbl->param);
	}
	if (flags&TBL_PF_QCMD_OUT_Q)
	{
		printf("OUT_Q:[%d] ", tbl->out_q);
	}
	if (flags&TBL_PF_QCMD_IN_Q)
	{
		printf("IN_Q:[%d] ", tbl->in_q);
	}
	if (flags&TBL_PF_QCMD_TID)
	{
		printf("TID:[%d] ", tbl->tid);
	}
	if (flags&TBL_PF_QCMD_TIDX)
	{
		printf("TIDX:[%d] ", tbl->tidx);
	}
	printf("\n");

	return 0;
}

int tbl_qcmd_print_tbl(tbl_qcmd *tbl, int flags)
{
	int i = -1;
	tble_qcmd *el = NULL;

	if (tbl == NULL)
	{
		PERM();
		return -1;
	}

	if (tbl->cmd == NULL)
	{
		PERM();
		return -1;
	}

	for (i=0; i<tbl->size; i++)
	{
		el = tbl->cmd[i];
		printf(">%02d< ", i);
		tbl_qcmd_print_cmd(el, flags);
	}

	return 0;
}


int tbl_qcmd_destroy_cmd(tble_qcmd *tble)
{
	if (tble == NULL )
	{
		PERM();
		return -1;
	}

	//?
	//Free token list TODO

	free(tble->cmd);
	tble->cmd = NULL;

	free(tble->param);
	tble->param = NULL;
	
	free(tble);
	tble=NULL;

	return 0;
}


int tbl_qcmd_destroy_table(tbl_qcmd *tbl)
{
	int i;

	if (tbl == NULL)
	{
		PERM();
		return -1;
	}

	for (i=0; i<tbl->size;i++)
	{
		if (tbl->cmd[i] != NULL)
			tbl_qcmd_destroy_cmd(tbl->cmd[i]);
	}

	free(tbl->cmd);
	tbl->cmd = NULL;

	free(tbl);
	tbl = NULL;

	return 0;
}

int tbl_qcmd_mng_states(tbl_qcmd *tbl)
{
	int i;

	if (tbl == NULL)
	{
		PERM();
		return -1;
	}
	

	for (i=0;i<tbl->size;i++)
	{
		tble_qcmd *iter = tbl->cmd[i];
		//now there is no states used, so just ignore state

		if ((time(NULL) - iter->timestamp)>QCMD_DEFAULT_TIMEOUT)
		{
			iter->state = QCMD_TIMEOUT;
		}
	}

	//remove all timed out commands
	for (i=tbl->size-1; i>=0;i--)
	{
		tble_qcmd *iter = tbl->cmd[i];
		if (iter->state == QCMD_TIMEOUT)
		{
			//can blow your balls
			tbl_qcmd_del_by_id(tbl, iter->id); //now iter becomes invalid
			iter = NULL;
		}
	}

	//add more complicated state machine execution
	//add more logging and more cases to log

}