High Quality Work!

Self-Motivation!

Despite the chaos:

Standup Friday 3/24!

C++ Singleton


class SingletonConverter
{
   public:
       static SingletonConverter* getInstance( ) {
       
       	    
            return instance;
       
       };
       ~SingletonConverter( );
        float fromFtoC(float temp) {
            return ((temp-32)/1.8);
        };
   private:
       SingletonConverter( );
       inline static SingletonConverter* instance;
};
  //... in main
  SingletonConverter *c = SingletonConverter::getInstance()
  float convertedTemp = c->fromFtC(temp);
  //...

C++ Singleton


class SingletonConverter
{
   public:
       static SingletonConverter* getInstance( ) {
       
       	    static SingletonConverter* instance;
            return instance;
       
       };
       ~SingletonConverter( );
        float fromFtoC(float temp) {
            return ((temp-32)/1.8);
        };
   private:
       SingletonConverter( );
};
  //... in main
  SingletonConverter *c = SingletonConverter::getInstance()
  float convertedTemp = c->fromFtC(temp);
  //...

Lazy Loading!

But does it work?

#include <iostream>

//
// Singleton Converter w/ Lazy Loading
//
class SingletonConverter
{
   public:
       static SingletonConverter* getInstance( ) {
            static SingletonConverter* instance;
            return instance;
       };
       ~SingletonConverter( );
        float fromFtoC(float temp) {
            return ((temp-32)/1.8);
        };
        
   private:
       SingletonConverter( );
};

int main()
{

    SingletonConverter* i1 = SingletonConverter::getInstance();
    SingletonConverter* i2 = SingletonConverter::getInstance();
    SingletonConverter* i3 = SingletonConverter::getInstance();
    
    if (i1 == i2) {
        std::cout << "yes";
    }
    
    if (i1 == i3) {
        std::cout << "yes";
    }

}

Pointer arithmetic!

#include <iostream>

class B {};

class A
{
public:
    // This is a declaration, but needs a definition
    static A* inst_1;
    
    // This is an inline decl/def from c++17
    // but is invalid because A has not been completed
    //inline static A* inst_2 = new A();
    
    inline static B* inst_2 = new B();
    
    static A* get_1()
    {
        return inst_1;
    }
    
    static B* get_2()
    {
        return inst_2;
    }
    
    static A* get_3()
    {
        // This is a static lazy init, the right side of the = sign will only be called once
        static A* a = A();
        return &a;
    }
    
    template<int t>
    static A* get_t()
    {
        // A useful trick to create something once per template specialization
        static A a;
        return &a;
    }
};

A* A::inst_1 = new A();

int main()
{
    std::cout
        << "Inst 1\t\t" << A::get_1() << std::endl
        << "Inst 2\t\t" << A::get_2() << std::endl
        << "Inst 3\t\t" << A::get_3() << std::endl
        << "Inst t=0\t" << A::get_t<0>() << std::endl
        << "Inst t=0\t" << A::get_t<0>() << std::endl
        << "Inst t=1\t" << A::get_t<1>() << std::endl;
}
int carton[10];
int carton[10] = { };
int carton[10] = { 1, 3, 7 };

Array

#include <vector>

// ...

std::vector<float> allTemps;

std::cout << allTemps.size();

while(1) {

	std::string temp;
	std::cout << "Please enter a temperature or x to stop.";
	std::cin >> temp;

	if (temp == "x") {
		break;
	}

	allTemps.push_back( std::stof(temp) );

}

Vector

Dynamic containers..

Templates!

Developer

IT Operations

Software Developer

SysAdmins / IT Ops

should know a little bit of both!

Deployment

Delivery to Client or Customer

Make Software Accessible

Containers

Virtual Machines

Docker

All other platforms..

$ docker run -p 8080:80 --detach httpd
$ docker ps
$ docker inspect ID
$ docker pause ID
$ docker unpause ID
$ docker kill ID
$ docker run -it python:2.7

We can never know it all.

But we can be curious.

alias docker=podman

MicroVMs for AWS

No daemon required

# A basic apache server. To use either add or bind mount content under /var/www
FROM ubuntu:12.04

MAINTAINER Kimbro Staken version: 0.1

RUN apt-get update && apt-get install -y apache2 && apt-get clean && rm -rf /var/lib/apt/lists/*

ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2

EXPOSE 80

CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]

Dockerfile

Base Docker

Author and version

Install Commands

Environment Variables

Open Ports

Executable Commands

This command can be overwritten when starting the container!

ENTRYPOINT ["/bin/bash"]

Fixed Command!

We will now create our own

Dockerfile

to specify a container

that compiles C++ code,

and then runs it!

We will deploy the container using

This time just watch!

We will do it later together..

1. Create                   repository and clone it.

2. Add C++ code.

3. Create                  Dockerfile.

4. Build and tag Docker container.

5. Push container to                        !

6. Modify C++ code.

7. And see what happens...

#include <iostream>
#include <vector>


class SingletonConverter
{
   public:
       static SingletonConverter* getInstance( ) {
            static SingletonConverter* instance;
            return instance;
       
       };
       ~SingletonConverter( );
        float fromFtoC(float temp) {
            return ((temp-32)/1.8);
        };
   private:
       SingletonConverter( );
       
};

int main()
{

  std::vector<float> allTemps;

  while(1) {

      std::string temp;
      std::cout << "Please enter a temperature or x to stop entering temperatures.\n";
      std::cin >> temp;
      
      if (temp == "x") {
          break;
      }
      
      std::cout << "Stored " << temp << "!\n";

      allTemps.push_back(std::stof(temp));
      
  }

  std::cout << allTemps.size() << " temperatures stored!\n";

  float averageTemp = 0;
  for (int i=0; i<allTemps.size(); i++) {
    averageTemp += allTemps[i];
  }

  averageTemp /= allTemps.size();

  std::cout << "The average temperature is: " << averageTemp << "\n";

  std::cout << "Converted to Celsius, this temperature is " << SingletonConverter::getInstance()->fromFtoC(averageTemp) << "!\n";
  
}

converter.cc

FROM ubuntu:22.04

MAINTAINER CS410.net version: 0.1


ADD converter.cc converter.cc

RUN apt-get update
RUN apt-get install -y g++
RUN g++ -o converter converter.cc

ENTRYPOINT ["./converter"]

Dockerfile

Docker commands we used today:

# build the container from the Dockerfile
docker build -t TAG .

# run the container interactively
docker run -it TAG

# login to DockerHub
docker login

# deploy the container
docker push TAG:latest

Container Orchestration

Run many containers on a cluster

Scalability

Docker Swarm

Kubernetes

easy

complex

CS410 Lecture 20

By Daniel Haehn

CS410 Lecture 20

Slides for CS410 Software Engineering at UMass Boston. See https://cs410.net!

  • 325