1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Copyright (c) the dbgp contributors. All rights reserved.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation. This file is also subject
 * to the Linking exception provided in the LICENSE file that
 * accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#![deny(missing_docs)]
//#![deny(warnings)]
#![doc(test(attr(allow(unused_variables), deny(warnings))))]

//! This library implements the dbgp protocol

#[macro_use]
extern crate lazy_static;
extern crate tokio_io;
extern crate tokio_service;
extern crate tokio_proto;
extern crate futures;
extern crate bytes;

pub mod escape;
mod codec;
mod protocol;
mod error_codes;
mod command;
mod transaction;

use tokio_service::Service;
use futures::{future, Future, BoxFuture};
use std::io;
use tokio_proto::TcpServer;
use std::net::{IpAddr, SocketAddr, Ipv4Addr};
use protocol::DbgpProto;

lazy_static! {
    static ref DEFAULT_IP_ADDR: Ipv4Addr = {
        Ipv4Addr::new(127,0,0,1)
    };
}

static DEFAULT_PORT: u16 = 9000;

enum BreakReason {
    Ok,
    Error,
    Aborted,
    Exception,
}

enum SessionStatus {
    Starting,
    Stopping,
    Stopped,
    Running,
    Break(BreakReason),
}

/// Represents a session with a debugger
pub struct Session {
    /// Address to listen to
    address: SocketAddr,

    /// Represents the debugger status, None if there is no connection yet
    status: Option<SessionStatus>,
}

impl Session {
    /// Creates a new session with the default parameters
    pub fn new() -> Session {
        Session {
            address: SocketAddr::new(IpAddr::V4(*DEFAULT_IP_ADDR), DEFAULT_PORT),
            status: None,
        }
    }

    /// Sets the adress for the server to listen to
    pub fn address(mut self, addr: IpAddr) -> Self {
        self.address.set_ip(addr);
        self
    }

    /// Sets the port for the server to listen to
    pub fn port(mut self, port: u16) -> Self {
        self.address.set_port(port);
        self
    }

    /// Initializes the server for this session, this function
    /// is not expected to return until an error occurs
    pub fn run(self) {
        let server = TcpServer::new(DbgpProto, self.address);

        server.serve(|| Ok(Echo));
    }
}

/// 
pub struct Echo;

impl Service for Echo {
    // These types must match the corresponding protocol types:
    type Request = String;
    type Response = String;

    // For non-streaming protocols, service errors are always io::Error
    type Error = io::Error;

    // The future for computing the response; box it for simplicity.
    type Future = BoxFuture<Self::Response, Self::Error>;

    // Produce a future for computing a response from a request.
    fn call(&self, req: Self::Request) -> Self::Future {
        // In this case, the response is immediate.
        future::ok(req).boxed()
    }
}