3 * Revision 1.5 2000-03-17 12:47:02 adam
4 * Minor changes to admin client.
6 * Revision 1.4 2000/03/16 13:55:49 ian
7 * Added commands for sending shutdown and startup admin requests via the admin ES.
9 * Revision 1.3 2000/03/14 15:23:17 ian
10 * Removed unwanted ifdef and include of zes-admin.h
12 * Revision 1.2 2000/03/14 14:06:04 ian
13 * Minor change to order of debugging output for send_apdu,
14 * fixed encoding of admin request.
16 * Revision 1.1 2000/03/14 09:27:07 ian
17 * Added code to enable sending of admin extended service requests
26 #include <yaz/yaz-util.h>
28 #include <yaz/tcpip.h>
30 #include <yaz/xmosi.h>
33 #include <yaz/proto.h>
34 #include <yaz/marcdisp.h>
35 #include <yaz/diagbib1.h>
37 #include <yaz/pquery.h>
39 /* Helper functions to get to various statics in the client */
40 ODR getODROutputStream();
41 void send_apdu(Z_APDU *a);
45 int sendAdminES(int type, char* dbname, char* param1)
47 ODR out = getODROutputStream();
49 /* Type: 1=reindex, 2=truncate, 3=delete, 4=create, 5=import, 6=refresh, 7=commit */
50 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
51 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
54 Z_ESAdminOriginPartToKeep *toKeep;
55 Z_ESAdminOriginPartNotToKeep *notToKeep;
57 printf ("Admin request\n");
60 /* Set up the OID for the external */
61 update_oid.proto = PROTO_Z3950;
62 update_oid.oclass = CLASS_EXTSERV;
63 update_oid.value = VAL_ADMINSERVICE;
65 oid_ent_to_oid (&update_oid, oid);
66 req->packageType = odr_oiddup(out,oid);
67 req->packageName = "1.Extendedserveq";
69 /* Allocate the external */
70 r = req->taskSpecificParameters = (Z_External *) odr_malloc (out, sizeof(*r));
71 r->direct_reference = odr_oiddup(out,oid);
72 r->indirect_reference = 0;
74 r->which = Z_External_ESAdmin;
75 r->u.adminService = (Z_Admin *) odr_malloc(out, sizeof(*r->u.adminService));
76 r->u.adminService->which = Z_Admin_esRequest;
77 r->u.adminService->u.esRequest = (Z_AdminEsRequest *) odr_malloc(out, sizeof(*r->u.adminService->u.esRequest));
79 toKeep = r->u.adminService->u.esRequest->toKeep = (Z_ESAdminOriginPartToKeep *)
80 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->toKeep));
86 case Z_ESAdminOriginPartToKeep_reIndex:
87 toKeep->u.reIndex=odr_nullval();
88 toKeep->databaseName = dbname;
91 case Z_ESAdminOriginPartToKeep_truncate:
92 toKeep->u.truncate=odr_nullval();
93 toKeep->databaseName = dbname;
96 case Z_ESAdminOriginPartToKeep_delete:
97 toKeep->u.delete=odr_nullval();
98 toKeep->databaseName = dbname;
101 case Z_ESAdminOriginPartToKeep_create:
102 toKeep->u.create=odr_nullval();
103 toKeep->databaseName = dbname;
106 case Z_ESAdminOriginPartToKeep_import:
107 toKeep->u.import = (Z_ImportParameters*)odr_malloc(out, sizeof(*toKeep->u.import));
108 toKeep->u.import->recordType=param1;
109 toKeep->databaseName = dbname;
110 /* Need to add additional setup of records here */
113 case Z_ESAdminOriginPartToKeep_refresh:
114 toKeep->u.refresh=odr_nullval();
115 toKeep->databaseName = dbname;
118 case Z_ESAdminOriginPartToKeep_commit:
119 toKeep->u.commit=odr_nullval();
122 case Z_ESAdminOriginPartToKeep_shutdown:
123 toKeep->u.commit=odr_nullval();
126 case Z_ESAdminOriginPartToKeep_start:
127 toKeep->u.commit=odr_nullval();
131 /* Unknown admin service */
135 notToKeep = r->u.adminService->u.esRequest->notToKeep = (Z_ESAdminOriginPartNotToKeep *)
136 odr_malloc(out, sizeof(*r->u.adminService->u.esRequest->notToKeep));
137 notToKeep->which=Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
138 notToKeep->u.recordsWillFollow=odr_nullval();
145 /* cmd_adm_reindex <dbname>
146 Ask the specified database to fully reindex itself */
147 int cmd_adm_reindex(char* arg)
149 sendAdminES(Z_ESAdminOriginPartToKeep_reIndex,arg,NULL);
153 /* cmd_adm_truncate <dbname>
154 Truncate the specified database, removing all records and index entries, but leaving
155 the database & it's explain information intact ready for new records */
156 int cmd_adm_truncate(char* arg)
160 sendAdminES(Z_ESAdminOriginPartToKeep_truncate,arg,NULL);
166 /* cmd_adm_create <dbname>
167 Create a new database */
168 int cmd_adm_create(char* arg)
172 sendAdminES(Z_ESAdminOriginPartToKeep_create,arg,NULL);
178 /* cmd_adm_delete <dbname>
180 int cmd_adm_delete(char* arg)
184 sendAdminES(Z_ESAdminOriginPartToKeep_delete,arg,NULL);
190 /* cmd_adm_import <dbname> <rectype> <sourcefile>
191 Import the specified updated into the database
192 N.B. That in this case, the import may contain instructions to delete records as well as new or updates
193 to existing records */
194 int cmd_adm_import(char* arg)
196 /* Size of chunks we wish to read from import file */
197 size_t chunk_size = 8192;
199 /* Buffer for reading chunks of data from import file */
200 char chunk_buffer[chunk_size];
204 char dbname_buff[32];
205 char rectype_buff[32];
206 char filename_buff[32];
207 FILE* pImportFile = NULL;
209 if (sscanf (arg, "%s %s %s", dbname_buff, rectype_buff, filename_buff) != 3)
211 printf("Must specify database-name, record-type and filename for import\n");
215 /* Attempt to open the file */
217 pImportFile = fopen(filename_buff,"r");
219 /* This chunk of code should move into client.c sometime soon for sending files via the update es */
220 /* This function will then refer to the standard client.c one for uploading a file using es update */
223 int iTotalWritten = 0;
225 /* We opened the import file without problems... So no we send the es request, ready to
226 start sending fragments of the import file as segment messages */
227 sendAdminES(Z_ESAdminOriginPartToKeep_import,arg,rectype_buff);
229 while ( ! feof(pImportFile ) )
231 /* Read buffer_size bytes from the file */
232 size_t num_items = fread((void*)chunk_buffer, 1, sizeof(chunk_buffer), pImportFile);
234 /* Write num_bytes of data to */
236 if ( feof(pImportFile ) )
238 /* This is the last chunk... Write it as the last fragment */
239 printf("Last segment of %d bytes\n", num_items);
241 else if ( iTotalWritten == 0 )
243 printf("First segment of %d bytes\n",num_items);
247 printf("Writing %d bytes\n", num_items);
250 iTotalWritten += num_items;
258 /* "Freshen" the specified database, by checking metadata records against the sources from which they were
259 generated, and creating a new record if the source has been touched since the last extraction */
260 int cmd_adm_refresh(char* arg)
264 sendAdminES(Z_ESAdminOriginPartToKeep_refresh,arg,NULL);
271 Make imported records a permenant & visible to the live system */
272 int cmd_adm_commit(char* arg)
274 sendAdminES(Z_ESAdminOriginPartToKeep_commit,NULL,NULL);
278 int cmd_adm_shutdown(char* arg)
280 sendAdminES(Z_ESAdminOriginPartToKeep_shutdown,NULL,NULL);
284 int cmd_adm_startup(char* arg)
286 sendAdminES(Z_ESAdminOriginPartToKeep_start,NULL,NULL);