logo
down
shadow

Python - How to use adapter interfaces


Python - How to use adapter interfaces

By : user2949200
Date : November 16 2020, 06:23 AM
help you fix your problem Don't pass a string value of the interface type, pass an instance of the class (or, pass the string, and have the controller instantiate the interface). You can do this when you create the controller. Save the reference to the interface, then directly call methods on that interface:
code :
interface = ConsoleInterface()
StartController(interface).StartGame()
...
class StartController:
    def __init__(self, interface):
        self.interface = interface

    def StartGame(self, interface):
        self.interface.DisplayWelcome()
        return True


Share : facebook icon twitter icon
Using an adapter pattern without interfaces?

Using an adapter pattern without interfaces?


By : Kendall247
Date : March 29 2020, 07:55 AM
around this issue The way I see it, you have three options:
If your only need is conversion from B to A, you don't need the adapter. Just add the toClassA() method to class B. Use the adapter if you want to map class A interface to mimic that of class B so that you actually instantiate only the adapter (and implicitly A) when you need A functionality. If B adds functionality to A, derive B from A: classic inheritance or even multiple inheritance if you need interface compatibility to a class C that we don't know of.
Does INADDR_ANY interfaces include VMware Network Adapter?

Does INADDR_ANY interfaces include VMware Network Adapter?


By : PDF Prodigy
Date : March 29 2020, 07:55 AM
With these it helps The following code is the client side of a UDP connection.
This worked with ubuntu linux, over VM, over windows XP.
code :
/* *************** Included files************************************* */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>    // for system() and itoa() utilities
#include <stdarg.h>    // for va_list(), etc
#include <string.h>    // string handling utilities
#include <semaphore.h> // semaphores handling utilities

#include <fcntl.h>     // symbolic definitions for flags bits
#include <sys/types.h> // contains definitions of data types used in system calls
#include <sys/socket.h>// type definitions (I.E. struct sockaddr_in)
#include <sys/stat.h>  // symbolic definitions for permission bits and open()
#include <sys/ipc.h>   // ipc interface utilities

#include <sys/shm.h>   // shared memory utilities
#include <arpa/inet.h> // defines INADDR_ANY, etc

#include <errno.h>     // for accessing errno and related functions
#include <stdbool.h>   // define true, false, bool
#include <time.h>

    // contains common data type definitions
#include "localTypes.h"

    // contains data definitions for ON and OFF, enum enumReturnStatus
#include "Defines.h"

    // contains prototypes for writing to Log file
#include "CommonWriteLog.h"

    // contains prototypes, and data type definitions
#include "CommonConfiguration.h"

    //contains prototypes for globally visible functions in this file
#include "Common_UDP_Utilities.h"


/* *************** Global variables *********************************** */



/* *************** File Static Variables ****************************** */



/* *************** Code *********************************************** */



enum enumReturnStatus create_UDP_socket( INT32 taskSelector, INT32 *pUDPsocket_FD )
{
    enum enumReturnStatus returnStatus = eRS_Success; // indicate success
    INT32 LocalPort;
    INT32 socket_FD;
    struct sockaddr_in *pLocal_sockaddr;

char *txtAddress;

    if( is_taskSelector_Valid( taskSelector ) )
    { // then parameters valid

        socket_FD = get_UDPsocket_FD( eTask_Retrieve_GPS );

        if( 0 >= socket_FD )
        { // then socket not yet opened

            pLocal_sockaddr = get_pUDPsocket_sockaddr( taskSelector );

txtAddress = inet_ntoa((*pLocal_sockaddr).sin_addr);
fprintf(stdout, "OPENING SOCKET on socket addresss: %s \n", txtAddress );       

            if( NULL != pLocal_sockaddr )
            { // then, sockaddr configured

                LocalPort = get_UDPsocket_LocalPort( taskSelector );

fprintf(stdout, "OPENING SOCKET on PORT: %d \n", LocalPort );       

                if( 0 <= LocalPort )
                { // then port configured

                    socket_FD = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );

                    if( -1 != socket_FD )
                    { // then, socket creation successful

                        int TurnOn = 1;

                        returnStatus = setsockopt( socket_FD, SOL_SOCKET, SO_REUSEADDR, &TurnOn, sizeof( TurnOn ) );

                        if ( eRS_Success == returnStatus )
                        { // then setsockopt() successful

                            bzero((char *) pLocal_sockaddr, sizeof(struct sockaddr_in) );
                            pLocal_sockaddr->sin_family = AF_INET;
                            pLocal_sockaddr->sin_port = htons( LocalPort );
                            pLocal_sockaddr->sin_addr.s_addr = htonl(INADDR_ANY); // todo: change to specific GPS IP address

                            CommonWriteLog( eLL_Info, 
                                "INFO:File:%s: Line:%d:\n\t%s 0X%08X %d\n\n",
                                __FILE__, __LINE__, 
                                "addr.sin_port =",
                                pLocal_sockaddr->sin_port,
                                pLocal_sockaddr->sin_port );

                            returnStatus = bind( socket_FD, (struct sockaddr *)pLocal_sockaddr, sizeof(struct sockaddr) );

                            if( eRS_Success == returnStatus )
                            { // then bind successful

                                returnStatus = set_UDPsocket_FD( taskSelector, socket_FD );

                                if( eRS_Success != returnStatus )
                                { // then set_UDPsocket_FD failed

                                    CommonWriteLog( eLL_Error, 
                                        get_pFormatString( eFormat_EFFL_string ),
                                        __FILE__, __LINE__, 
                                        "Function:set_UDPsocket_FD() failed" );

                                    returnStatus = eRS_UDP_Create_Failure; // indicate error occurred

                                    close( socket_FD );
                                    socket_FD = -1;
                                }

                                // set callers data
                                *pUDPsocket_FD = socket_FD;
                            }

                            else
                            { // else, bind() failed

                                CommonWriteLog( eLL_Error, 
                                    get_pFormatString( eFormat_EFFL_taskSelector_dataIndex_string_string ),
                                    __FILE__, __LINE__,
                                    taskSelector, 0, 
                                    "LibFunc:bind() failed",
                                    strerror(errno) );
                            } // endif( bind )
                        }

                        else
                        { // else setsockopt() failed

                            CommonWriteLog( eLL_Error, 
                                get_pFormatString( eFormat_EFFL_taskSelector_dataIndex_string_string ),
                                __FILE__, __LINE__, 
                                taskSelector, 0,
                                "LibFunc:setsockopt( SO_REUSEADDR ) failed",
                                strerror(errno) );

                        } // endif( SO_REUSEADDR )
                    }

                    else
                    { // else failed to create socket

fprintf(stdout, "FAILED TO OPEN SOCKET: \n" );       
fflush(stdout);
system("sync;sync;");
exit(EXIT_FAILURE);


                        CommonWriteLog( eLL_Error, 
                            get_pFormatString( eFormat_EFFL_taskSelector_dataIndex_string_string ),
                            __FILE__, __LINE__, 
                            taskSelector, 0,
                            "LibFunc:socket() failed",
                            strerror(errno) );

                        returnStatus = eRS_SystemCall_Failure; // indicate error occured
                    } // endif( socket )
                }

                else
                { // else port not configured

                    CommonWriteLog( eLL_Error, 
                        get_pFormatString( eFormat_EFFL_taskSelector_dataIndex_string ),
                        __FILE__, __LINE__, 
                        taskSelector, 0,
                        "port not configured" );

                    returnStatus = eRS_Configuration_Failure;  // indicate error occurred
                }
            } 

            else
            { // else get pUDP sockaddr failed

                CommonWriteLog( eLL_Error, 
                    get_pFormatString( eFormat_EFFL_taskSelector_dataIndex_string ),
                    __FILE__, __LINE__,
                    taskSelector, 0, 
                    "Function:get_pUDPsocket_sockaddr() failed" );

                returnStatus = eRS_UDP_Create_Failure;  // indicate error occurred
            } // endif( get_pUDPsocket_sockaddr() successful )
        } // endif( socket already open )
    }

    else
    { // else, bad parameters

        returnStatus = eRS_UDP_Create_Failure;

        CommonWriteLog( eLL_Error, 
            get_pFormatString( eFormat_EFFL_digit_string ),
            __FILE__, __LINE__, 
            taskSelector,
            "Parameter:taskSelector out of range" );
    }

    return( returnStatus );
} // end create_UDP_socket()



enum enumReturnStatus write_UDP_socket( INT32 Socket_FD, char *pSocket_OutBuffer, INT32 Size, struct sockaddr *pTo_sockaddr, INT32 *pWriteCount )
{
    enum enumReturnStatus returnStatus = eRS_Success; // indicate success
    INT32 flags = 0;
    INT32 sendtoStatus;



    // do not call CommonWriteLog() to help avoid extranious 'recursion' messages in logs
    // and to avoid recrusive operation

    sendtoStatus = 
    sendto( Socket_FD, pSocket_OutBuffer, Size, flags,pTo_sockaddr, sizeof(struct sockaddr) );

    if( 0 <= sendtoStatus )
    { // then, sendto() successful

        *pWriteCount = sendtoStatus;

        if( sendtoStatus != Size )
        { // then not all char sent
            CommonWriteLog( eLL_Error, 
                "ERROR:File:%s: Line:%d:\n\t%s:num2Send:%d, numSent:%d\n\n",
                __FILE__, __LINE__, 
                "LibFunc:sendto() failed",
                 Size, sendtoStatus );

            returnStatus = eRS_UDP_Write_Failure;  // indicate error occurred
        }
    }

    else
    {
        CommonWriteLog( eLL_Error, 
            get_pFormatString( eFormat_EFFL_string_string ),
            __FILE__, __LINE__, 
            "LibFunc:sendto() failed",
            strerror(errno) );

        returnStatus = eRS_SystemCall_Failure; // indicate error occurred
        *pWriteCount = 0;
    }

    return( returnStatus );
} // end write_UDP_socket()



enum enumReturnStatus read_UDP_socket( INT32 Socket_FD, char *pSocket_InBuffer, INT32 Size, INT32 *pReadCount )
{
    enum enumReturnStatus returnStatus = eRS_Success; 
    INT32   readStatus  = 0; // result of call to recvfrom()
    INT32   flags       = 0; // option parameter for call to recvfrom()
    struct  sockaddr_in from_sockaddr;
    UINT32  slen        = 0; // sizeof struct sockaddr_in



    memset( &from_sockaddr, 0x00, sizeof(from_sockaddr) );
    slen = sizeof( struct sockaddr_in );
    readStatus = recvfrom( Socket_FD, pSocket_InBuffer,Size, flags,(struct sockaddr*)&from_sockaddr, &slen );          // filled during call


    if( 0 > readStatus )
    { // then, an I/O error occurred
        CommonWriteLog( eLL_Error, 
            get_pFormatString( eFormat_EFFL_string_string ),
            __FILE__, __LINE__, 
            "LibFunc:recvfrom() failed",
            strerror(errno) );

        *pReadCount = 0;
        returnStatus = eRS_SystemCall_Failure; // indicate error occurred
    }

    else
    { //else 0 or more bytes read

        // update callers' info
        *pReadCount = readStatus;
    }

    return( returnStatus );
} // end read_UDP_socket()



/* ******************************************************************** **
** End of source file: Common_UDP_Utilities.c
** ******************************************************************** */
Are we using the adapter design pattern everytime we extend a class and implements its interfaces methods?

Are we using the adapter design pattern everytime we extend a class and implements its interfaces methods?


By : R. Chanak
Date : March 29 2020, 07:55 AM
I hope this helps you . No -- inheritance is not an adapter pattern.
Say you have your Car with your drive() method (which really should implement an interface say Moveable with the method drive()). Now there is some other class that take a parameter of Moveable.
code :
public class MakeItSo
{
   public void easeOnDown(Moveable thing)
   { 
      thing.drive();
   }
}
public class MoveableSpaceship implements Moveable  
{  
    private Spaceship ship;

    public MoveableSpaceship(Spaceship s)
    {
       ship = s;
    } 

    @Override
    public void drive()
    {
       ship.warp();
    }
}
Spaceship enterprise = getSpaceship();  //from wherever
MoveableSpaceship ncc1701 = new MoveableSpaceship(enterprise);
MakeItSo doIt = new MakeItSo();
doIt.easeOnDown(ncc1701);
Why does VirtualBox create 2 interfaces on host-only adapter?

Why does VirtualBox create 2 interfaces on host-only adapter?


By : user160535
Date : March 29 2020, 07:55 AM
Any of those help I'm setting up minikube using VirtualBox as the VM driver using a NAT adapter and a host-only adapter. After the VM is created, I run a few pods one of which is Kafka (the messaging queue). I have issues using Kafka properly because the VM creates 2 network interfaces eth0 which points to 10.0.2.15 and eth1 which points to 192.168.99.100 which is the IP the host-only adapter is set up with. ,
| eth0 (10.0.2.15) | <--- why is this created?
Adapter pattern without the use of interfaces

Adapter pattern without the use of interfaces


By : Mayra Froy Garcia
Date : March 29 2020, 07:55 AM
Does that help Yes, essentially it's an Adapter if it implements an "external" interface or abstract API -- regardless of whether the "internal" connection is formalized as an interface or abstract API.
So it's still an Adapter when it hooks onto a specific internal implementation, which can fulfill/ answer the contract & methods of the external interface.
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org