3 * Revision 1.4 2000-03-16 13:55:49 ian
4 * Added commands for sending shutdown and startup admin requests via the admin ES.
6 * Revision 1.3 2000/03/14 15:23:17 ian
7 * Removed unwanted ifdef and include of zes-admin.h
9 * Revision 1.2 2000/03/14 14:06:04 ian
10 * Minor change to order of debugging output for send_apdu,
11 * fixed encoding of admin request.
13 * Revision 1.1 2000/03/14 09:27:07 ian
14 * Added code to enable sending of admin extended service requests
23 #include <yaz/yaz-util.h>
25 #include <yaz/tcpip.h>
27 #include <yaz/xmosi.h>
30 #include <yaz/proto.h>
31 #include <yaz/marcdisp.h>
32 #include <yaz/diagbib1.h>
34 #include <yaz/pquery.h>
36 /* Helper functions to get to various statics in the client */
37 ODR getODROutputStream();
38 void send_apdu(Z_APDU *a);
42 int sendAdminES(int type, char* dbname, char* param1)
44 ODR out = getODROutputStream();
46 /* Type: 1=reindex, 2=truncate, 3=delete, 4=create, 5=import, 6=refresh, 7=commit */
47 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
48 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
51 Z_ESAdminOriginPartToKeep *toKeep;
52 Z_ESAdminOriginPartNotToKeep *notToKeep;
54 printf ("Admin request\n");
57 /* Set up the OID for the external */
58 update_oid.proto = PROTO_Z3950;
59 update_oid.oclass = CLASS_EXTSERV;
60 update_oid.value = VAL_ADMINSERVICE;
62 oid_ent_to_oid (&update_oid, oid);
63 req->packageType = odr_oiddup(out,oid);
64 req->packageName = "1.Extendedserveq";
66 /* Allocate the external */
67 r = req->taskSpecificParameters = (Z_External *) odr_malloc (out, sizeof(*r));
68 r->direct_reference = odr_oiddup(out,oid);
69 r->indirect_reference = 0;
71 r->which = Z_External_ESAdmin;
72 r->u.adminService = (Z_Admin *) odr_malloc(out, sizeof(*r->u.adminService));
73 r->u.adminService->which = Z_Admin_esRequest;
74 r->u.adminService->u.esRequest = (Z_AdminEsRequest *) odr_malloc(out, sizeof(*r->u.adminService->u.esRequest));
76 toKeep = r->u.adminService->u.esRequest->toKeep = (Z_ESAdminOriginPartToKeep *)
77 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->toKeep));
83 case Z_ESAdminOriginPartToKeep_reIndex:
84 toKeep->u.reIndex=odr_nullval();
85 toKeep->databaseName = dbname;
88 case Z_ESAdminOriginPartToKeep_truncate:
89 toKeep->u.truncate=odr_nullval();
90 toKeep->databaseName = dbname;
93 case Z_ESAdminOriginPartToKeep_delete:
94 toKeep->u.delete=odr_nullval();
95 toKeep->databaseName = dbname;
98 case Z_ESAdminOriginPartToKeep_create:
99 toKeep->u.create=odr_nullval();
100 toKeep->databaseName = dbname;
103 case Z_ESAdminOriginPartToKeep_import:
104 toKeep->u.import = (Z_ImportParameters*)odr_malloc(out, sizeof(*toKeep->u.import));
105 toKeep->u.import->recordType=param1;
106 toKeep->databaseName = dbname;
107 /* Need to add additional setup of records here */
110 case Z_ESAdminOriginPartToKeep_refresh:
111 toKeep->u.refresh=odr_nullval();
112 toKeep->databaseName = dbname;
115 case Z_ESAdminOriginPartToKeep_commit:
116 toKeep->u.commit=odr_nullval();
119 case Z_ESAdminOriginPartToKeep_shutdown:
120 toKeep->u.commit=odr_nullval();
123 case Z_ESAdminOriginPartToKeep_start:
124 toKeep->u.commit=odr_nullval();
128 /* Unknown admin service */
132 notToKeep = r->u.adminService->u.esRequest->notToKeep = (Z_ESAdminOriginPartNotToKeep *)
133 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->notToKeep));
134 notToKeep->which=Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
135 notToKeep->u.recordsWillFollow=odr_nullval();
142 /* cmd_adm_reindex <dbname>
143 Ask the specified database to fully reindex itself */
144 int cmd_adm_reindex(char* arg)
146 sendAdminES(Z_ESAdminOriginPartToKeep_reIndex,arg,NULL);
149 /* cmd_adm_truncate <dbname>
150 Truncate the specified database, removing all records and index entries, but leaving
151 the database & it's explain information intact ready for new records */
152 int cmd_adm_truncate(char* arg)
155 sendAdminES(Z_ESAdminOriginPartToKeep_truncate,arg,NULL);
158 /* cmd_adm_create <dbname>
159 Create a new database */
160 int cmd_adm_create(char* arg)
163 sendAdminES(Z_ESAdminOriginPartToKeep_create,arg,NULL);
166 /* cmd_adm_delete <dbname>
168 int cmd_adm_delete(char* arg)
171 sendAdminES(Z_ESAdminOriginPartToKeep_delete,arg,NULL);
174 /* cmd_adm_import <dbname> <rectype> <sourcefile>
175 Import the specified updated into the database
176 N.B. That in this case, the import may contain instructions to delete records as well as new or updates
177 to existing records */
178 int cmd_adm_import(char* arg)
180 /* Size of chunks we wish to read from import file */
181 size_t chunk_size = 8192;
183 /* Buffer for reading chunks of data from import file */
184 char chunk_buffer[chunk_size];
188 char dbname_buff[32];
189 char rectype_buff[32];
190 char filename_buff[32];
191 FILE* pImportFile = NULL;
193 if (sscanf (arg, "%s %s %s", dbname_buff, rectype_buff, filename_buff) != 3)
195 printf("Must specify database-name, record-type and filename for import\n");
199 /* Attempt to open the file */
201 pImportFile = fopen(filename_buff,"r");
203 /* This chunk of code should move into client.c sometime soon for sending files via the update es */
204 /* This function will then refer to the standard client.c one for uploading a file using es update */
207 int iTotalWritten = 0;
209 /* We opened the import file without problems... So no we send the es request, ready to
210 start sending fragments of the import file as segment messages */
211 sendAdminES(Z_ESAdminOriginPartToKeep_import,arg,rectype_buff);
213 while ( ! feof(pImportFile ) )
215 /* Read buffer_size bytes from the file */
216 size_t num_items = fread((void*)chunk_buffer, 1, sizeof(chunk_buffer), pImportFile);
218 /* Write num_bytes of data to */
220 if ( feof(pImportFile ) )
222 /* This is the last chunk... Write it as the last fragment */
223 printf("Last segment of %d bytes\n", num_items);
225 else if ( iTotalWritten == 0 )
227 printf("First segment of %d bytes\n",num_items);
231 printf("Writing %d bytes\n", num_items);
234 iTotalWritten += num_items;
240 /* "Freshen" the specified database, by checking metadata records against the sources from which they were
241 generated, and creating a new record if the source has been touched since the last extraction */
242 int cmd_adm_refresh(char* arg)
245 sendAdminES(Z_ESAdminOriginPartToKeep_refresh,arg,NULL);
249 Make imported records a permenant & visible to the live system */
250 int cmd_adm_commit(char* arg)
252 sendAdminES(Z_ESAdminOriginPartToKeep_commit,NULL,NULL);
255 int cmd_adm_shutdown(char* arg)
257 sendAdminES(Z_ESAdminOriginPartToKeep_shutdown,NULL,NULL);
260 int cmd_adm_startup(char* arg)
262 sendAdminES(Z_ESAdminOriginPartToKeep_start,NULL,NULL);