|
|
1.1 root 1: /*
2: ******************************************************************************
3: *
4: * Module: bb_phase.c
5: *
6: * Functions:
7: * bb_read_phases() - Read or create the phases file.
8: * bb_phase_update() - Update the phases file on disk.
9: * bb_set_phase() - Add the specified flags to a phase.
10: * bb_unset_phase() - Remove the specified flags from a phase.
11: * bb_phase_ok() - Check if the phase is ok to make changes.
12: *
13: *
14: ******************************************************************************
15: */
16:
17: /*
18: ******************************************************************************
19: * Include Files
20: ******************************************************************************
21: */
22: #include <stdio.h>
23: #include <rpc/rpc.h>
24: #include "common.h"
25: #include "protocol.h"
26: #include "server.h"
27:
28:
29: /*************************************************************************
30: ** **
31: ** bb_read_phases - If the phases file exists read the entries into **
32: ** the phases array. Otherwise create the file with 0'ed entries. **
33: ** **
34: *************************************************************************/
35: BB_phase phases[BB_MAX_IMP]; /* The phases array. */
36:
37: int
38: bb_read_phases()
39: {
40: FILE *phase_file; /* Pointer to the phase file. */
41: int i = 0; /* Index into phase array. */
42: int num_imps; /* The number of implementations*/
43: char line[BB_MAX_LINE_LEN]; /* Space for the input line. */
44:
45: num_imps = bb_get_imp_cnt();
46:
47: /*
48: ** If the file exists read it into the phase array.
49: */
50: if ( (phase_file = fopen( BB_PHASE_FILE, "r")) != NULL )
51: {
52: for ( i = 0; i < num_imps; i++ )
53: {
54: if ( (phases[i] = getw( phase_file)) == (u_char)EOF )
55: {
56: fprintf( stderr, "ERROR: Phase data file incomplete.\n");
57: return BB_FAILURE;
58: }
59: }
60:
61: fclose( phase_file);
62: return BB_SUCCESS;
63: }
64:
65: /*
66: ** The file did not exist so create it.
67: */
68: if ( (phase_file = fopen( BB_PHASE_FILE, "w")) == NULL )
69: {
70: fprintf( stderr, "FAILED opening the phase data file '%s'.\n",
71: BB_PHASE_FILE);
72: fclose( phase_file);
73: return BB_FAILURE;
74: }
75:
76: /*
77: ** Fill the file with zeroed phases.
78: */
79: for( i = 0; i < num_imps; i++ )
80: {
81: if ( putw( (int)BB_BOARD_UNSET, phase_file) == 1 )
82: {
83: fprintf( stderr, "FAILED writting new phase file.\n");
84: fclose( phase_file);
85: return BB_FAILURE;
86: }
87: }
88:
89: fclose( phase_file);
90: return BB_SUCCESS;
91: }
92:
93:
94:
95: /*************************************************************************
96: ** **
97: ** bb_phase_update() - The phase of the client has changed and should **
98: ** be written to disk. **
99: ** **
100: *************************************************************************/
101: int
102: bb_phase_update( client)
103: int client; /* The client with the change of phase. */
104: {
105: int num_imps; /* The number of implementations. */
106: FILE *phase_file; /* Pointer to the phase file. */
107: long seek_to; /* The position to seek to in the file. */
108:
109: num_imps = bb_get_imp_cnt();
110:
111: if ( client >= num_imps )
112: {
113: return BB_FAILURE;
114: }
115:
116: /*
117: ** The word that we want to write is at the location client
118: ** integers into the file. Take the client index and multiply
119: ** that by the size of the integer and that is the offset.
120: */
121: seek_to = client * sizeof(int);
122:
123: /*
124: ** Open the phase file.
125: */
126: if ( (phase_file = fopen( BB_PHASE_FILE, "r+")) == NULL )
127: {
128: fprintf( stderr, "ERROR: Could not open phase file for update.\n");
129: return BB_FAILURE;
130: }
131:
132: if ( fseek( phase_file, seek_to, 0) != 0 )
133: {
134: fprintf( stderr, "ERROR: Could not seek in phase file.\n");
135: fclose( phase_file);
136: return BB_FAILURE;
137: }
138:
139: if ( putw( (int)phases[client], phase_file) == 1 )
140: {
141: fprintf( stderr, "ERROR: Could not update phase file.\n");
142: fclose( phase_file);
143: return BB_FAILURE;
144: }
145:
146: fclose( phase_file);
147: return BB_SUCCESS;
148: }
149:
150:
151: /*************************************************************************
152: ** **
153: ** bb_set_phase() - Change the phase of a client. This adds the **
154: ** flags specified to the phase in the phase list. **
155: ** **
156: *************************************************************************/
157: int
158: bb_set_phase( client, phase)
159: BB_id client; /* The name of the client to change. */
160: int phase; /* The phase to add to the client. */
161: {
162: int client_id; /* The index of the client. */
163:
164: /*
165: ** Get the client's id.
166: */
167: if ( (client_id = bb_get_hash( client)) == BB_HASH_ID_NOT_FOUND )
168: {
169: return BB_BAD_CLIENT;
170: }
171:
172: /*
173: ** Or the bits together to turn them on. The bits are saying OOO WEEE.
174: */
175: phases[client_id] |= phase;
176:
177: /*
178: ** If the phase cannot be written to disk, return an error.
179: */
180: return bb_phase_update( client_id);
181: }
182:
183:
184: /*************************************************************************
185: ** **
186: ** bb_unset_phase() - Change the phase of a client. This deletes the **
187: ** flags specified from the phase in the phase list. **
188: ** **
189: *************************************************************************/
190: int
191: bb_unset_phase( client, phase)
192: BB_id client; /* The name of the client to change. */
193: int phase; /* The phase to add to the client. */
194: {
195: int client_id; /* The index of the client. */
196:
197: /*
198: ** Get the client's id.
199: */
200: if ( (client_id = bb_get_hash( client)) == BB_HASH_ID_NOT_FOUND )
201: {
202: return BB_BAD_CLIENT;
203: }
204:
205: /*
206: ** Or the bits together to turn them on. The bits are saying OOO WEEE.
207: */
208: phases[client_id] &= ~phase;
209:
210: /*
211: ** If the phase cannot be written to disk, return an error.
212: */
213: return bb_phase_update( client_id);
214: }
215:
216:
217:
218: /*************************************************************************
219: ** **
220: ** bb_phase_ok() - Check to see if the phase is ok for the change to **
221: ** the board that will be made. **
222: ** **
223: *************************************************************************/
224: int
225: bb_phase_ok( client)
226: BB_id client; /* The client identifier. */
227: {
228: int client_id; /* The index of the client. */
229:
230: /*
231: ** Get the client's id.
232: */
233: if ( (client_id = bb_get_hash( client)) == BB_HASH_ID_NOT_FOUND )
234: {
235: return BB_BAD_CLIENT;
236: }
237:
238: if ( phases[client_id] & BB_SUN_PHASE )
239: return BB_SUCCESS;
240: else
241: return BB_FAILURE;
242: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.