Advanced and distributed programming paradigms.docx
Document Details
Uploaded by Deleted User
Tags
Related
Full Transcript
[Advanced and distributed programming paradigms] [Notes for Paradigm 1: Client Server Architecture] Chapter 0 We've worked on standalone applications mostly, how can we develop separate apps written in different programming languages yet are connected and can interact without any hassle? MISCONC...
[Advanced and distributed programming paradigms] [Notes for Paradigm 1: Client Server Architecture] Chapter 0 We've worked on standalone applications mostly, how can we develop separate apps written in different programming languages yet are connected and can interact without any hassle? MISCONCEPTION: Server: a **[machine]** that serves other **[machines]** called clients. Chapter 1 The following is covered: Clients, Servers, Socket API, Connections. Two processes A and B with OS A and B, running on two separate machines can connect through establishing a connection. This allows them to exchange data. We can see that process A output is B's input, and vice versa. Thus, these exchanges are basically I/O operations. File operations as well are I/O operations, which allows us to draw the following analogy. If we'd like to write to a certain file like so: file.write('Hello World!') We need to look at the following: - File existence - Mode of operation - Authorization - Coordination and correlation We basically define a context and a state, a framework for managing file operations. Similarly, the OS defines a context and state for each process/endpoint. In the case of process A and B, their context and state are both bound together: logical binding through the sharing of a common identifier which allows to map them to each other. This binding is called a connection ![](media/image2.png) It is established by the Os through this logical binding to allow for data exchange by two different endpoints. It is an end-to-end concept: the underlying network is unaware of it. How does a process identify the endpoint to which it connects? - IP addresses: 32bit number that identifies a machine across the network. - Port: 16bit integer (range: 0 to 2^16^-1 = 65535), since a machine can have multiple different endpoints, this allows to distinguish them. WHAT ROLES DOES THE OS PLAY? It manages the end points as it discriminates between each based on the assigned ports. As it knows which are taken and which are not, it can assign them to processes while ensuring their uniqueness. Network abstraction, we are able to leverage system calls without worrying abnout the underlying details since the OS abstracts them alongside data traffic details away. It manages connections, the OS establishes connections, context, and state on behalf of the processes. System calls: ways to leverage services from the OS without worrying about the underlying details. Server and client are processes: a server is a process, which serves other processes (clients). Socket API: modern technology, which allows establishing a two-way connection between two processes First defined in C at Berkley university, release in 1983. API: interface of a library Application programming interface. Prototype of the function. MISCONCEPTIONS Client and servers are machine ///// Client and servers are processes A machine can be either a client or a server ///// a machine may run/host multiple servers or clients at the same time A server serves clients with data ///// Once a connection is established, data may flow in both directions. SOCKET API: first defined in C in 1984 at the Berkeley University. Since then has been adapted to several programming languages including java and python. A set of system calls enables communication between two different applications and provides developers with all related operations such as opening or closing a connection. Try with resources: declare resources to be used within the try block with the assurance that said resources will be blocked after execution of the block. Resource delegation. Protocol design: Communication between two processes isn't solely built on I/O operations. We must first set a purpose for the communication, as well as a set of rules to be followed. Errors should also be communicated. We need a contract between the client and the server to define them. The contract will allow: Synchronization between server and client (no deadlocks) Successful error handling Make sense out of exchanged data Metadata is also exchanged allow for better handling of data. Fx case study: what if file name has space? - Modify protocol to have file name be at the very end, so anything prior to a line feed is in the name - Have a delimiter like have the file name be inside quotes. - Have a protocol specific key word - Encode space, for instance with %20. Notes str.replaceAll("tobereplaced","tobereplacedwith") function Difference between: InputStream: abstract class that represents an input stream of bytes DataInputStream: for primitive data; has functions like readInt, readDouble... wraps inputstream to provide those methods InputStramReader: bridge to read bytes and decode them to characters. OutputStream: abstract class that represents an output stream of bytes Bufferedreader: efficiently read characters from a character stream. readLine() Bufferedwriter: efficiently write characters to a character output stream flush(),write(String s)..... FileInputStream: used to read bytes from a file. Part of java.io Try (FileInputStream fis = new FileInputStream("filename.txt"){ Int data; While ((data = fis.read)!=-1) { ///// read till EOF } \] FileOutputStream: used to write bytes to a file. Try (FileOutputStream fos = new FileOutputStream("filename.txt"){ Fos.write(data.getBytes()); //// convert string to bytes first. \] PrintWriter: class used to write formatted text to a character output stream. Automatic flushing. To check if a file exists: from java.nio.file.\* package Files.exists(path) deleteIfExists(takes path object of the file) Path path = Paths.get("/Frefge/Greger") Can get file name from path object path.getFileName() Path filename=path.getFileName() then to get the string: toString