Copyright 2012, 2013 & 2015 – Noah Mendelsohn COMP 150-IDS Ping Demonstration Programs & Makefile / C++ Hints Noah Mendelsohn Tufts University

Slides:



Advertisements
Similar presentations
C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Advertisements

Yoshi
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
C/c++ 4 Yeting Ge.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
1 Chapter 17-2 Templates and Exceptions Dale/Weems.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Exceptions OO Software Design and Construction Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ 1 Exceptions exceptiona program.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
CSCI 4550/8556 Computer Networks Comer, Chapter 3: Network Programming and Applications.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Web:
CS203 Java Object Oriented Programming Errors and Exception Handling.
1 Web Based Programming Section 6 James King 12 August 2003.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
Handling ErrorstMyn1 Handling Errors Up to this point we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions.
1 CSC241: Object Oriented Programming Lecture No 27.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Working with Strings Lecture 2 Hartmut Kaiser
Chapter 9: Exceptions For error/problem situations Exception classes –ArithmeticException, IOException, etc. –checked exceptions try blocks –catch statements.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
“In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Exceptions Handling Exceptionally Sticky Problems.
VB.Net - Exceptions Copyright © Martin Schray
COMP Exception Handling Yi Hong June 10, 2015.
More on Abstract Data Types Noah Mendelsohn Tufts University Web: COMP 40: Machine.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Namespaces & Exceptions Adapted from Ch. 3 slides of Data Abstraction:
CSC 270 – Survey of Programming Languages C++ Lecture 6 – Exceptions.
Copyright 2012 & 2015 – Noah Mendelsohn A Brief Intro to the RPC Project Framework Noah Mendelsohn Tufts University
Chapter 9 Questions 1. What are the difference between constructors and member functions? 2. Design and implement a simple class as you want, with constructors.
HANDLING EXCEPTIONS Chapter 9. Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Big Endian vs. Little Endian Storage of Numeric Data Noah Mendelsohn Tufts University Web:
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
LECTURE LECTURE 14 Exception Handling Textbook p
A Quick Look at C for C++ Programmers Noah Mendelsohn Tufts University Web: COMP.
CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1.
Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
CHAPTER 18 C – C++ Section 1: Exceptions. Error Handling with Exceptions Forces you to defend yourself Separates error handling code from the source.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Chapter 6 CS 3370 – C++ Functions.
A Brief Intro to the RPC Project Framework
Compilation and Debugging
Compilation and Debugging
Java Programming Language
Lessons from The File Copy Assignment
COMP 117 Ping Demonstration Programs & Makefile / C++ Hints
COMP 117 Ping Demonstration Programs & Makefile / C++ Hints
Data Abstraction: The Walls
Part B – Structured Exception Handling
CSC 270 – Survey of Programming Languages
COMP 150-IDS Ping Demonstration Programs & Makefile / C++ Hints
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Exceptions and networking
Presentation transcript:

Copyright 2012, 2013 & 2015 – Noah Mendelsohn COMP 150-IDS Ping Demonstration Programs & Makefile / C++ Hints Noah Mendelsohn Tufts University Web: COMP 150-IDS: Internet Scale Distributed Systems (Spring 2015)

© 2010 Noah Mendelsohn 2 What you should get from today’s session  A quick look at some details you’ll need to do our programming assignment, including:  The framework we’re using  C++ Exceptions  A tiny bit about inheritance  Makefiles  C/C++ tips and tricks

© 2010 Noah Mendelsohn 3 Working through the Demo Client

© 2010 Noah Mendelsohn Preamble 4 #include "c150dgmsocket.h" #include "c150debug.h" #include using namespace std; // for C++ std library using namespace C150NETWORK; Include Framework and Debug code

© 2010 Noah Mendelsohn Preamble 5 #include "c150dgmsocket.h" #include "c150debug.h" #include using namespace std; // for C++ std library using namespace C150NETWORK; IMPORTANT! Needed for COMP150IDS Framework!

© 2010 Noah Mendelsohn 6 Main pingclient Logic

© 2010 Noah Mendelsohn Client logic 7 try { C150DgmSocket *sock = new C150DgmSocket(); sock -> setServerName(argv[serverArg]); sock -> write(argv[msgArg], strlen(argv[msgArg])+1); readlen = sock -> read(incomingMessage, sizeof(incomingMessage)); checkAndPrintMessage(readlen, incomingMessage, sizeof(incomingMessage)); } catch (C150NetworkException e) { cerr << argv[0] << ": caught C150NetworkException: " << e.formattedExplanation() << endl; } This is not an ordinary socket…it’s a smart wrapper around a socket Establishes us as a client…and identifies the server…ports set based on login id

© 2010 Noah Mendelsohn Client logic 8 try { C150DgmSocket *sock = new C150DgmSocket(); sock -> setServerName(argv[serverArg]); sock -> write(argv[msgArg], strlen(argv[msgArg])+1); readlen = sock -> read(incomingMessage, sizeof(incomingMessage)); checkAndPrintMessage(readlen, incomingMessage, sizeof(incomingMessage)); } catch (C150NetworkException e) { cerr << argv[0] << ": caught C150NetworkException: " << e.formattedExplanation() << endl; } Clients write, then read

© 2010 Noah Mendelsohn 9 Demo Server

© 2010 Noah Mendelsohn Server logic 10 try { C150DgmSocket *sock = new C150DgmSocket(); c150debug->printf(C150APPLICATION,"Ready to accept messages"); while(1){ readlen = sock -> read(incomingMessage, sizeof(incomingMessage)-1); // … WORK WITH MESSAGE HERE string response = “SOME RESPONSE HERE”; sock -> write(response.c_str(), response.length()+1); } catch (C150NetworkException e) { c150debug->printf(C150ALWAYSLOG,"Caught C150NetworkException: %s\n", e.formattedExplanation().c_str()); } Servers read, then write

© 2010 Noah Mendelsohn Server logic 11 try { C150DgmSocket *sock = new C150DgmSocket(); c150debug->printf(C150APPLICATION,"Ready to accept messages"); while(1){ readlen = sock -> read(incomingMessage, sizeof(incomingMessage)-1); // … WORK WITH MESSAGE HERE string response = “SOME RESPONSE HERE”; sock -> write(response.c_str(), response.length()+1); } catch (C150NetworkException e) { c150debug->printf(C150ALWAYSLOG,"Caught C150NetworkException: %s\n", e.formattedExplanation().c_str()); } Framework assumes responses go to server/port from which we read

© 2010 Noah Mendelsohn Inferring who is a server and who is a client 12 try { C150DgmSocket *sock = new C150NastyDgmSocket(nastiness); c150debug->printf(C150APPLICATION,"Ready to accept messages"); while(1){ readlen = sock -> read(incomingMessage, sizeof(incomingMessage)-1); // … WORK WITH MESSAGE HERE string response = “SOME RESPONSE HERE”; sock -> write(response.c_str(), response.length()+1); } catch (C150NetworkException e) { c150debug->printf(C150ALWAYSLOG,"Caught C150NetworkException: %s\n", e.formattedExplanation().c_str()); } NOTE: The socket class imposes a simple notion of client/server on UDP… It decides whether you’re a server or client based on which methods you call first 1) client calls setServer name then writes 2) server starts by doing a read. Note a very robust approach for production code, but handy for these simple programs.

© 2010 Noah Mendelsohn 13 C++ Inheritance

© 2010 Noah Mendelsohn A super-simple look at C++ Inheritance 14 class Shape { private: Point position; public: Point getPosition(); virtual void draw() = 0; }; Base class “Shape” has draw() method with no implementation (=0)

© 2010 Noah Mendelsohn Class Circle : public Shape { public: virtual void draw(); }; Class Square : public Shape { public: virtual void draw(); }; A super-simple look at C++ Inheritance 15 class Shape { private: Point position; public: Point getPosition(); virtual void draw() = 0; }; Each subclass provides its own implementation of draw()

© 2010 Noah Mendelsohn A super-simple look at C++ Inheritance 16 Class shape { private: Point position; public: Point getPosition(); virtual void draw() = 0; }; Class Circle : public Shape { public: virtual void draw(); }; Class Square : public Shape { public: virtual void draw(); }; Shape *shapeArray[2]; int i; shapeArray[0] = new Circle(); shapeArray[1] = new Square(); for(i=0; i<2; i++) { cout << shapeArray[i] -> position; shapeArray[i] -> draw(); } Both classes inherit postion() method from parent

© 2010 Noah Mendelsohn A super-simple look at C++ Inheritance 17 Class shape { private: Point position; public: Point getPosition(); virtual void draw() = 0; }; Class Circle : public Shape { public: virtual void draw(); }; Class Square : public Shape { public: virtual void draw(); }; Shape *shapeArray[2]; int i; shapeArray[0] = new Circle(); shapeArray[1] = new Square(); for(i=0; i<2; i++) { cout << shapeArray[i] -> position; shapeArray[i] -> draw(); } First time calls Circle::draw, second time calls Square::draw

© 2010 Noah Mendelsohn Inheritance and nasty sockets logic 18 try { C150DgmSocket *sock = new C150NastyDgmSocket(nastiness); c150debug->printf(C150APPLICATION,"Ready to accept messages"); while(1){ readlen = sock -> read(incomingMessage, sizeof(incomingMessage)-1); // … WORK WITH MESSAGE HERE string response = “SOME RESPONSE HERE”; sock -> write(response.c_str(), response.length()+1); } catch (C150NetworkException e) { c150debug->printf(C150ALWAYSLOG,"Caught C150NetworkException: %s\n", e.formattedExplanation().c_str()); } The C150NastyDgmSocket class inherits from… … C150DgmSocket. You can use either type here, unless you want to call methods specific to the “nasty” class.

© 2010 Noah Mendelsohn 19 C++ Exceptions

© 2010 Noah Mendelsohn C++ Exceptions 20 try { C150DgmSocket *sock = new C150DgmSocket(); sock -> setServerName(argv[serverArg]); sock -> write(argv[msgArg], strlen(argv[msgArg])+1); readlen = sock -> read(incomingMessage, sizeof(incomingMessage)); checkAndPrintMessage(readlen, incomingMessage, sizeof(incomingMessage)); } catch (C150NetworkException e) { cerr << argv[0] << ": caught C150NetworkException: " << e.formattedExplanation() << endl; } C++ has try/catch/throw for Exceptions try clause runs first Any network exception in try block or methods called by try block takes us here e is of whatever type was “thrown”

© 2010 Noah Mendelsohn C++ Exceptions  Exceptions are particularly useful for network code…  …no need to “percolate” return codes through layers of method calls  Standard COMP 150IDS Exception Class: throw throw C150NetworkException("Client received message that was not null terminated"); When an error occurs, throw an Exception (same as “raise” in other langs): throw C150NetworkException (or other class)  Exception classes form a hierarchy…based on class inheritance (no need for you to worry about that if you don’t know C++ inheritance) 21

© 2010 Noah Mendelsohn 22 Makefiles Dependency –based Command Execution

© 2010 Noah Mendelsohn Makefile variables 23 # Do all C++ compies with g++ CPP = g++ CPPFLAGS = -g -Wall -Werror -I$(C150LIB) # Where the COMP 150 shared utilities live, including c150ids.a and userports.csv # Note that environment variable COMP150IDS must be set for this to work! C150LIB = $(COMP150IDS)/files/c150Utils/ [… several lines skipped…] pingclient: pingclient.o $(C150AR) $(INCLUDES) $(CPP) -o pingclient pingclient.o Variables defined this way… …used this way …or from environment… (this is one reason you setenv COMP150IDS /comp/150IDS)

© 2010 Noah Mendelsohn Targets and dependencies 24 # Do all C++ compies with g++ CPP = g++ CPPFLAGS = -g -Wall -Werror -I$(C150LIB) # Where the COMP 150 shared utilities live, including c150ids.a and userports.csv # Note that environment variable COMP150IDS must be set for this to work! C150LIB = $(COMP150IDS)/files/c150Utils/ [… several lines skipped…] pingclient: pingclient.o $(C150AR) $(INCLUDES) $(CPP) -o pingclient pingclient.o The pingclient target …depends on pingclient.o (and include files, etc.)

© 2010 Noah Mendelsohn What gets run 25 # Do all C++ compies with g++ CPP = g++ CPPFLAGS = -g -Wall -Werror -I$(C150LIB) # Where the COMP 150 shared utilities live, including c150ids.a and userports.csv # Note that environment variable COMP150IDS must be set for this to work! C150LIB = $(COMP150IDS)/files/c150Utils/ [… several lines skipped…] pingclient: pingclient.o $(C150AR) $(INCLUDES) $(CPP) -o pingclient pingclient.o When pingclient is older than pingclient.o, etc. …..use g++ to relink it

© 2010 Noah Mendelsohn Fancier dependencies 26 %.o:%.cpp $(INCLUDES) $(CPP) -c $(CPPFLAGS) $< Each xxx.o file …depends on xxx.cpp …and is compiled from that.cpp file

© 2010 Noah Mendelsohn 27 C cs. C++ Strings

© 2010 Noah Mendelsohn C vs C++ Strings – we use both!  C++ provides automatic allocation and useful concatenation operations  C char[] arrays needed for formatting message packets  File and socket APIs defined in terms of C byte arrays  Also…preference –For some purposes, printf/scanf are handier than C++ << –Etc. 28

© 2010 Noah Mendelsohn Some hints on strings  Won’t try a full tutorial here but, remember that you can convert: char cstring[4] = “abc”; // remember the null! string newstring(cstring); // initialize C++ string // from C string char *fromCPlusPlus = newstring.c_str(); // IMPORTANT: fromCPlusPlus // is stable ONLY until // next change to newstring  Our focus is on the distributed system design…performance matters some, but do what’s easy and clean 29

© 2010 Noah Mendelsohn Stringstreams: useful for formatting and conversions  C++ strings do not support // for stringstream include // for cout stringstream ss; // empty stringstream int answer = 25; ss << “The answer is “<< answer << “ pounds” << endl; cout << ss.str(); // get string from stringstream 30

© 2010 Noah Mendelsohn What I Do (mostly)  I mostly use C++ strings: automatic allocation  I use or convert to char[] if I’m using APIs that need them  I use either printf or stringstreams for formatting or…  …concatenate strings with “+” (slower, and edge cases where it doesn’t work)  Not all the framework code makes good choices internally…you’ll find some stuff that probably should be cleaned up (e.g. excess conversions) 31