help in designing class

Discussion in 'C++' started by Naren, May 26, 2004.

  1. Naren

    Naren Guest

    Victor Bazarov <> wrote:
    >Naren wrote:
    >> MyServer class conatins a list of Mysock class.
    >>
    >> class Myserver
    >> {
    >> private:
    >> list<Mysock> L1;
    >> };
    >>
    >> MySock is a class containing the socket information
    >>
    >> class MySock
    >> {
    >> private:
    >> int _socketdesc;
    >> Type *pType;
    >> };
    >>
    >> Type is a class which conatins the device type and an ID of the

    device
    >> which connects to the server
    >>
    >> class Type
    >> {
    >> private:
    >> eType e;
    >> int ID;
    >> };
    >>
    >> Now when a command has to be sent to some device. I get its ID,

    eType
    >
    >How does the server know what combination of 'ID' and 'eType' to use?
    >
    >> and the command.
    >> This has to be sent to the unique device.

    >
    >What if there is no such "device" connected?
    >
    >>
    >> SO here you see that I have to do a linear search for the correct
    >> device to send the command.

    >
    >It doesn't have to be linear. You could cache the information in,

    say,
    >a map<>, it would speed up the search. Or you could simply keep the
    >list of your 'Mysock' objects _sorted_. That should speed up the

    search
    >too.
    >
    >>
    >> The type class is filled by the Mysock class

    >
    >Don't you mean "the Type class"?
    >
    > > after recieving the first
    >> packet containing the information. so a type object is created by

    the
    >
    >Don't you mean "a Type object"?
    >
    >> Mysock class.
    >>
    >> Now if Myserver had a list of Newtype
    >> class Newtype
    >> {
    >> private:
    >> etype e;

    >
    >What's "etype"? Is it the same as "eType"?
    >
    >> list<NewID> L;

    >
    >What's the meaning of a list of IDs in the "Newtype"?
    >
    >> };
    >>
    >> class NewID
    >> {
    >> private:
    >> int ID;
    >> int _socketdesc;
    >> };
    >>
    >> This helps in searching the device fast but since type information

    is
    >> obtained after recieving the first paket I am not able to use this
    >> approach.

    >
    >Why not?
    >
    >>
    >> Could anyone help me?

    >
    >Sure. As soon as you explain what you expect us to help you with.
    >
    >Designing a type or types requires understanding of the problem that
    >you're trying to solve. You showed us some implementation details,
    >which even according to you don't do the job, and you think we can
    >figure it out what you want, from them? You give our mind-reading
    >ability way too much credit.
    >
    >Now, let's just reiterate what you said, and we can probably get to
    >mutual understanding a bit sooner.
    >
    >- A connection is established by the client, and never by the server.
    > Or is that not so?
    >
    >- Once a connection is established, its parameters (ID and eType) are
    > figured out. Or not. You didn't say how ID is assigned.
    >
    >- To communicate to clients, the server sends commands identified by
    > command ID, and 'Type'. How does the server know what 'Type' to
    > use?
    >
    >You see, perhaps you need to state your problem just a bit clearer...
    >
    >V


    Thank you for the advice
    I think I should have been clear.
    Let us assume there is a unit which gives ID,Type and command.
    The server should have a list of cleint sockets(with their ID and
    Type) and it should search through them and
    send the command to the corresponding socket(device)

    This is the transmitting part.
    Coming to recieving part, the cleint connects to the server and sends
    the first packet as ID and Type of itself(device). This should be
    stored by the server for transmission of output packet.
    multiple cleints(devices) can connect to the server

    Is this pretty clear?

    Can you kindly help again?

    Thanks in advance.

    Regards,
    Naren.

    -------------------------------------------
    This message has been posted from News2Web
    Your free, web based, news reading service
    Visit us at http://www.news2web.com
    (c) 1997 SoftCom Technology Consulting Inc.
    -------------------------------------------
     
    Naren, May 26, 2004
    #1
    1. Advertising

  2. Naren wrote:
    > Let us assume there is a unit which gives ID,Type and command.
    > The server should have a list of cleint sockets(with their ID and
    > Type) and it should search through them and
    > send the command to the corresponding socket(device)
    >
    > This is the transmitting part.
    > Coming to recieving part, the cleint connects to the server and sends
    > the first packet as ID and Type of itself(device). This should be
    > stored by the server for transmission of output packet.
    > multiple cleints(devices) can connect to the server
    >
    > Is this pretty clear?
    >
    > Can you kindly help again?


    To be entirely honest with you, I am not sure I see any problem
    and therefore am puzzled why you think you can't solve this yourself.
    But since you asked, I'll try to explain what I'd do if I needed to
    implement something like that.

    First off, you do realise that if an independent "unit" gives the
    {ID,Type,command} combination, it's not defined what happens if
    there is no such connection presently. I'll assume that nothing
    happens. So, during establishing the connection, the client gives
    its connection parameters to the server, which stores them in some
    kind of table for future reference when it's time to send commands
    to the client.

    struct ConnectionID {
    ID_t ID;
    Type_t Type;

    bool operator < (ConnectionID const&) const; // for sorting
    };

    struct Connection {
    ConnectionID id;
    void * some_other_parameters;

    virtual ~Connection(); // should destroy 'some_other_parameters'
    // if you suppose that Connection owns it

    bool operator < (Connection const& c) const { return id < c.id; }
    };

    class Server {
    std::multiset<Connection> my_open_connections;
    public:
    virtual ~Server(); // will close all open connections gracefully
    void answer();
    bool send_command(ConnectionID const&, command_t);
    bool communicate_with_client(const Connection&, command_t);
    };

    ------------------------- implementation details

    void Server::answer() {
    // The connection has been requested -- open the port
    ...
    // Receive the connection data from the client
    ...
    // got tempID, tempType (and some other parameters in tempParams
    Connection freshConnection = { tempID, tempType, tempParams };
    my_open_connections.insert(freshConnection);
    }

    bool Server::send_command(ConnectionID const& id, command_t cmd) {
    Connection template = { id, 0 };
    std::multiset<Connection>::iterator first =
    my_open_connections.lower_bound(template);
    bool sent = false;
    while (first != my_open_connections.upper_bound(template)) {
    sent = sent | communicate_with_client(*first++, cmd);
    }
    return sent;
    }

    bool Server::communicate_with_client(const Connection & c,
    command_t cmd) {
    // do what needs to be done with...
    c.ID;
    c.Type;
    c.some_other_parameters;

    return rand() > 10000; // whatever is the indicator of success
    }
    ----------------------------------------------------------------------

    If there is something here that you consider outrageously incorrect or
    against your understanding of the problem you're having, then either I
    didn't understand your explanation well, or you didn't explain it well,
    or your problem needs to be thought through a bit more.

    Good luck!

    Victor
     
    Victor Bazarov, May 26, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dilip Bajaj via .NET 247
    Replies:
    2
    Views:
    501
    Peter Blum
    Mar 21, 2005
  2. E11
    Replies:
    1
    Views:
    4,788
    Thomas Weidenfeller
    Oct 12, 2005
  3. E G
    Replies:
    6
    Views:
    348
    Jumbo
    Jan 23, 2004
  4. alexhong2001

    Questions of designing a class

    alexhong2001, Feb 18, 2004, in forum: C++
    Replies:
    3
    Views:
    402
    Cy Edmunds
    Feb 18, 2004
  5. Naren

    help in designing class

    Naren, May 25, 2004, in forum: C++
    Replies:
    1
    Views:
    298
    Victor Bazarov
    May 25, 2004
Loading...

Share This Page