State Design Pattern in C++

hi,
i got that question from a friend who interviewed for a job

the following code implements State protocol, explain how :
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
#include "stateMachine.h"
//#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define VALID 1
#define INVALID 0

#define D_A 0
#define D_B 1

#define ARR_SIZE 25

typedef struct{
	char cmdArr[ARR_SIZE];
	bool AorB;
}cmd_s;

typedef struct{
	char cmdArr[ARR_SIZE];
	int size;
}cmd3_s;


//Global Variables
cmd_s Cmd;
cmd3_s Cmd3;


void fillArr(char destArr[], const char sourceArr[], const int size){
	memset(destArr, 0, ARR_SIZE);
	memcpy(destArr, sourceArr, size);
}

bool isCmd(const char * cmd_t, const int size){
	if(strncmp(Cmd.cmdArr, cmd_t, size) == 0){
		return 1;
	}
	return 0;
}

int checkCmd(bool drction, char * cmd_t, int size, int *statePtr, int dest_state){
	if(Cmd.AorB == drction ){
		if(isCmd(cmd_t, size)){
			*statePtr = dest_state;
			return VALID;
		}
	}

	*statePtr = 1;
	return INVALID;
}

void saveCmd3(const char sourceArr[], const int size){
	fillArr(Cmd3.cmdArr, sourceArr, size);
	Cmd3.size = size - 1; //don't count the '/0'
}

int checkState3(int *statePtr){
	int status = (checkCmd(D_A,"LIST", sizeof("LIST"),statePtr, 4));

	saveCmd3("LIST", sizeof("LIST"));

	// other possible commands ...	

	if(status == INVALID){
		status = (checkCmd(D_A,"EXIT ", sizeof("EXIT "),statePtr, 5));
	}

return status;

}

int getCmdLen(){
	int i=0;
	int cnt=0;

	for(i=0; i<ARR_SIZE; i++){
		if(Cmd.cmdArr[i] != '\0')
			cnt++;
	}

return cnt;
}

int checkCh(char ch){
	if((ch >='a') && (ch <='z'))
		return VALID;

	if((ch >='0') && (ch <='9'))
		return VALID;

return INVALID;

}

int checkState4(int *statePtr){
	int cmdSize = getCmdLen();
	int cmd3Size = Cmd3.size;
	int SecondIdx = cmdSize - cmd3Size;
	int i;

	*statePtr = 1;

	if(cmdSize < cmd3Size*2 + 2)// check if the cmd msg is twice+2 space the cmd3 msg
		return INVALID;

	if(strncmp(&Cmd.cmdArr[0], Cmd3.cmdArr, cmd3Size) != 0)
		return INVALID;

	if(strncmp(&Cmd.cmdArr[SecondIdx], Cmd3.cmdArr, cmd3Size) != 0)
		return INVALID;

	// check the msg
	for(i = cmd3Size+1; i<= SecondIdx-2 ; i++ ){
		if(checkCh(Cmd.cmdArr[i]) == INVALID)
			return INVALID;
	}

	*statePtr = 3;
	return VALID;

}

int handleCmd(){
	static int stat_state = 1;

	switch(stat_state){
		case 1:
			return (checkCmd(D_A,"OPEN_CONNECTION", sizeof("OPEN_CONNECTION"),&stat_state, 2));
		case 2:
			return (checkCmd(D_B,"OPEN_CONNECTION_ACKED", sizeof("OPEN_CONNECTION_ACKED"),&stat_state, 3));
		case 3:
			return (checkState3(&stat_state));
		case 4:
			return (checkState4(&stat_state));
		case 5:
			return (checkCmd(D_B,"EXIT_ACKED", sizeof("EXIT_ACKED"),&stat_state, 1));

		default: 
			return INVALID;

	}
}

int stateMachine::main2(){
	// init

	memset(&Cmd, 0, sizeof(Cmd) );
	memset(&Cmd3, 0, sizeof(Cmd3));

	Cmd.AorB = D_A;
	fillArr(Cmd.cmdArr, "OPEN_CONNECTION", sizeof("OPEN_CONNECTION"));
	handleCmd();

	Cmd.AorB = D_B;
	fillArr(Cmd.cmdArr, "OPEN_CONNECTION_ACKED", sizeof("OPEN_CONNECTION_ACKED"));
	handleCmd();

	Cmd.AorB = D_A;
	fillArr(Cmd.cmdArr, "LIST", sizeof("LIST"));
	handleCmd();

	Cmd.AorB = D_B;
	fillArr(Cmd.cmdArr, "LIST 6575 LIST", sizeof("LIST 6575 LIST"));
	handleCmd();

	printf("YES");
	getchar();
	return 0;
}


its a protocol from A to B of exchanging messages, and every message has its own state, am i right?
Topic archived. No new replies allowed.