Chapter 14
Accessing Files on a Network
If your system supports DECnet facilities, and your computer is one of the nodes in a DECnet network, you can communicate with other nodes in the network by means of standard PL/I I/O statements. These statements provide two distinct types of network operations:
Remote file access lets you read and write files on a remote node as if the files were on your local system.
Task-to-task communication lets you exchange data directly with a job that is being executed at a remote location.
Examples of both remote file access and task-to-task communication using PL/I statements are given in this chapter. For details on using DECnet, see the DECnet-VAX or DECnet/OSI documentation.
14.1. Remote File Access
To access a file on a remote system, you include the node name in the file specification of the external file you identify for the execution of the program. For example:
BOSTON::DBA0:[MALCOLM]TEMPS.TST
This file specification identifies the file TEMPS.TST in the directory [MALCOLM] on the device DBA0: on the node BOSTON.
You can specify a node name in a file specification in either of the following contexts:
In the file specification in the TITLE option of an OPEN statement
In the equivalence name you assign to a logical name before running a program that refers to a file by logical name
For example:
OPEN FILE (NETFILE) SEQUENTIAL INPUT RECORD TITLE ('TULSA::DBB0:[MALCOLM]PLITEST.DAT');
This OPEN statement specifies the name of a file to be read from the node named TULSA.
If no file specification is present in the TITLE option, or if the TITLE option specifies a logical name, you can define a remote file. For example:
OPEN FILE(INFILE) INPUT RECORD SEQUENTIAL;
This OPEN statement refers to the logical name INFILE. The following DEFINE command equates this logical name with a remote file:
$ DEFINE INFILE TULSA::DBB0:[MALCOLM]PL1TEST.DATWhen the OPEN statement is executed, the run-time system associates the remote file with the PL/I file INFILE.
When you run a program that modifies a file on a remote node in a protected account, the file specification must contain an access control string. Each VSI operating system defines the format of an access control string. For an OpenVMS system, you specify the user name and password of the account whose file you are modifying. For example:
$DEFINE INFILE -$_TULSA“"MALCOLM YES"""::DBB0:[ENERGY]PL1TEST.DAT”
The user name MALCOLM and the password for this account (YES) are enclosed in quotation marks following the node name in the file specification. The extra quotation marks are required because the DCL command interpreter removes single pairs of quotation marks from lines. On the system at the node TULSA, the user MALCOLM must have access privileges to the account ENERGY.
The following file-system functions are not available for processing remote files: keyed statements (DELETE, WRITE, REWRITE FILE_ID_TO, FILE_ID_FROM, RECORD_ID_TO, and RECORD_ID_FROM) cannot be followed by a sequential operation; that is, an operation that does not specify a key.
14.2. Task-to-Task Communication
Network task-to-task communication lets a program running on one network node interact with a program running on another network node. The interaction is accomplished with PL/I I/O statements, but network connections themselves are transparent to the cooperating programs.
PL/I programs at remote locations can communicate over the network by the following mechanism:
The program that initiates the communication is called the source task. It requests a network connection to a target task by specifying a task name in a file specification that contains a node name. This OPEN statement initiates the request and associates a PL/I file with a network logical link created by DECnet. For example:
OPEN FILE (TASKFILE) RECORD OUTPUT TITLE ('HSTN"MALCOLM YES"::"TASK=LOGGER" ');
This OPEN statement initiates task-to-task communication with the target node by specifying the task name LOGGER. The network program uses the default directory of user MALCOLM to locate the command file LOGGER.COM on the remote target node.
DECnet locates the command file LOGGER.COM on the remote node specified in the OPEN statement. The name of the command file is specified by the task specification string, TASK=LOGGER. DECnet submits this command file for execution by the remote system. When the OPEN statement completes, communication between the two tasks can begin.
The command file LOGGER.COM must contain the command necessary to initiate the execution of the cooperating program, COPYTASK.
$
RUN COPYTASKThe network program submits the specified command file to the batch job queue on the target system.
The cooperating target task must complete the connection to the source task by executing an OPEN statement to open the file SYS$NET.
OPEN FILE (NETFILE) RECORD SEQUENTIAL INPUT TITLE ('SYS$NET');
SYS$NET is a logical name assigned by DECnet to the network job that identifies the source task's node and process.
After the logical link is established, the cooperating programs, or tasks, read and write data using the PL/I files associated with the logical link.
When either program executes a CLOSE statement for the file, the logical link is broken and an end-of-file record is written to the cooperating task.
Examples 14.1 and 14.2 illustrate PL/I programs that communicate across the network using synchronous I/O. The following notes are keyed to Example 14.1, “A PL/I Network Source Task”:
- ❶
The procedure SOURCE_TASK is the program that initiates the request.
- ❷
The UNDEFINEDFILE condition will be signaled if any error occurs that is associated with the logical link or connection. In the ON-unit, the procedure uses the ONCODE built-in function to obtain the error code, display the status value, and stop the program.
- ❸
The OPEN statement associates the PL/I file TASKNAME with the task named LOGGER on the node named BOSTON. The network program uses the default directory for the account BEANS on the node and locates the command file LOGGER.COM. The file LOGGER.COM contains this command:
$ RUN TARGET- ❹
The procedure writes three messages from the structure TASK_MESSAGE. The first field is a binary value, and the second field a character-string text.
- ❺
When the three messages have been written, the CLOSE statement closes the file to terminate the network connection.
SOURCE_TASK: PROCEDURE; /* ❶ */ DECLARE TASKNAME FILE; DECLARE 1 TASK_MESSAGE, 2 NUMBER FIXED BINARY(31), 2 TEXT CHARACTER(40) VARYING; ON UNDEFINEDFILE(TASKNAME) BEGIN; /* ❷ */ PUT SKIP LIST('File error',ONCODE()); STOP; END; OPEN FILE(TASKNAME) SEQUENTIAL OUTPUT RECORD /* ❸ */ TITLE('BOSTON"BEANS BAKED"::"TASK=LOGGER"'); NUMBER = 1; /* ❹ */ TEXT = 'first message'; WRITE FILE (TASKNAME) FROM (TASK_MESSAGE); NUMBER = 2; TEXT = 'second message'; WRITE FILE (TASKNAME) FROM (TASK_MESSAGE); NUMBER = 3; TEXT = 'third and last message'; WRITE FILE(TASKNAME) FROM (TASK_MESSAGE); CLOSE FILE(TASKNAME); /* ❺ */ RETURN; END;
The following notes are keyed to Example 14.2, “A PL/I Target Task”:
- ❶
The image file TARGET.EXE contains the compiled and linked code for the procedure TARGET_TASK. The declarations in the procedure TARGET_TASK include the files INFILE and OUTFILE, a structure into which messages will be read across the logical link, and a message field from which data will be written to the output file.
- ❷
The procedure establishes an UNDEFINEDFILE ON-unit for any error conditions that occur in creating the logical link; at the label FILE_ERROR, the status code is reported and the procedure exits.
- ❸
The ENDFILE condition provides for a normal termination of the logical link. When the program SOURCE_TASK closes the file TASKNAME, an end-of-file condition is returned on the next read attempted in TARGET_TASK.
- ❹
The first OPEN statement opens the file SYS$NET; if the file is opened successfully, the network connection is established. The second OPEN statement opens the file TASK.DAT, the output file that will be created at the target node, in the default directory for the user named BEANS.
- ❺
The read loop in this procedure reads a message from the logical link, edits the data, and places the record in the output file.
TARGET_TASK: PROCEDURE; /* ❶ */ DECLARE (INFILE,OUTFILE) FILE; /* Files */ DECLARE 1 LOG_MESSAGE, /* Structure to read in messages */ 2 STATUS FIXED BIN(31), 2 TEXT CHARACTER(40) VARYING; DECLARE MESSAGE CHARACTER(80); /* Variable to convert message */ PUT STRING(MESSAGE) EDIT(' ') (A(80)); ON UNDEFINEDFILE(INFILE) GOTO FILE_ERROR; /* Network errors */ /* ❷ */ ON ENDFILE(INFILE) GOTO FINISH; /* Normal completion */ /* ❸ */ OPEN FILE (INFILE) RECORD SEQUENTIAL INPUT TITLE ('SYS$NET'); /* Open SYS$NET */ /* ❹ */ OPEN FILE(OUTFILE) RECORD SEQUENTIAL OUTPUT TITLE('TASK.DAT'); /* Open output log file */ LOOP: /* ❺ */ READ FILE(INFILE) INTO (LOG_MESSAGE); PUT STRING(MESSAGE) EDIT(STATUS,TEXT) (F(6),X,A); WRITE FILE(OUTFILE) FROM (MESSAGE); GOTO LOOP; FINISH: CLOSE FILE(INFILE); CLOSE FILE(OUTFILE); RETURN; FILE_ERROR: PUT SKIP LIST('Input file error',ONCODE()); RETURN; END;