servlib/session/Session.cc
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2007 Intel Corporation
       
     3  * 
       
     4  *    Licensed under the Apache License, Version 2.0 (the "License");
       
     5  *    you may not use this file except in compliance with the License.
       
     6  *    You may obtain a copy of the License at
       
     7  * 
       
     8  *        http://www.apache.org/licenses/LICENSE-2.0
       
     9  * 
       
    10  *    Unless required by applicable law or agreed to in writing, software
       
    11  *    distributed under the License is distributed on an "AS IS" BASIS,
       
    12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  *    See the License for the specific language governing permissions and
       
    14  *    limitations under the License.
       
    15  */
       
    16 
       
    17 #ifdef HAVE_CONFIG_H
       
    18 #include <dtn-config.h>
       
    19 #endif
       
    20 
       
    21 #include <oasys/thread/Atomic.h>
       
    22 
       
    23 #include "Session.h"
       
    24 
       
    25 namespace dtn {
       
    26 
       
    27 //----------------------------------------------------------------------
       
    28 Session::Session(const EndpointID& eid)
       
    29     : Logger("Session", "/dtn/session"),
       
    30       eid_(eid),
       
    31       bundles_(std::string("session-") + eid.str()),
       
    32       resubscribe_timer_(NULL)
       
    33 {
       
    34     static oasys::atomic_t next_session_id = 0;
       
    35     id_ = oasys::atomic_incr_ret(&next_session_id);
       
    36 
       
    37     logpath_appendf("/%d", id_);
       
    38     log_info("created new session %u for eid %s", id_, eid.c_str());
       
    39 }
       
    40 
       
    41 //----------------------------------------------------------------------
       
    42 Session::~Session()
       
    43 {
       
    44 }
       
    45 
       
    46 //----------------------------------------------------------------------
       
    47 int
       
    48 Session::format(char* buf, size_t sz) const
       
    49 {
       
    50     return snprintf(buf, sz, "session id %u [%s]", id(), eid().c_str());
       
    51 }
       
    52 
       
    53 //----------------------------------------------------------------------
       
    54 const char*
       
    55 Session::flag_str(u_int flags)
       
    56 {
       
    57     // XXX/demmer not really flags, but you know...
       
    58     switch (flags) {
       
    59     case 0:           return "Session::NONE";
       
    60     case SUBSCRIBE:   return "Session::SUBSCRIBE";
       
    61     case RESUBSCRIBE: return "Session::RESUBSCRIBE";
       
    62     case UNSUBSCRIBE: return "Session::UNSUBSCRIBE";
       
    63     case PUBLISH:     return "Session::PUBLISH";
       
    64     case CUSTODY:     return "Session::CUSTODY";
       
    65     default: {
       
    66         static char buf[256];
       
    67         snprintf(buf, sizeof(buf), "Session::UNKNOWN(0x%x)", flags);
       
    68         return buf;
       
    69     }
       
    70     }
       
    71 }
       
    72 
       
    73 //----------------------------------------------------------------------
       
    74 void
       
    75 Session::set_upstream(const Subscriber& upstream)
       
    76 {
       
    77     upstream_ = upstream;
       
    78     log_info("set_upstream(*%p)", &upstream_);
       
    79 
       
    80     add_subscriber(upstream);
       
    81 }
       
    82 
       
    83 //----------------------------------------------------------------------
       
    84 void
       
    85 Session::add_subscriber(const Subscriber& subscriber)
       
    86 {
       
    87     if (std::find(subscribers_.begin(), subscribers_.end(), subscriber) !=
       
    88         subscribers_.end())
       
    89     {
       
    90         log_info("add_subscriber(*%p): already subscribed",
       
    91                  &subscriber);
       
    92         return;
       
    93     }
       
    94     
       
    95     subscribers_.push_back(subscriber);
       
    96     log_info("add_subscriber(*%p)", &subscriber);
       
    97 }
       
    98 
       
    99 } // namespace dtn