MASA-Core
BufferFactory.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2010-2015   Edans Sandes
00004  *
00005  * This file is part of MASA-Core.
00006  * 
00007  * MASA-Core is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * MASA-Core is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with MASA-Core.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  ******************************************************************************/
00021 
00022 #include "BufferFactory.hpp"
00023 
00024 #include <stdlib.h>
00025 
00026 #include "SocketBuffer.hpp"
00027 #include "FileBuffer.hpp"
00028 #include "NullBuffer.hpp"
00029 
00030 
00031 Buffer* BufferFactory::createBufferFromURL(string url) {
00032         int pos1 = url.find_first_of("://");
00033         if (pos1 == -1) return NULL;
00034         string type = url.substr(0, pos1);
00035         string param = url.substr(pos1+3);
00036 
00037 
00038         fprintf(stderr, "%s:   %s - %s\n", url.c_str(), type.c_str(), param.c_str());
00039         if (type == "socket") {
00040                 int port;
00041                 string hostname;
00042                 int pos2 = param.find_first_of(":");
00043                 if (pos2 > 0) {
00044                         port = atoi(param.substr(pos2+1).c_str());
00045                         hostname = param.substr(0, pos2);
00046                 } else {
00047                         hostname = param;
00048                 }
00049                 return new SocketBuffer(hostname, port);
00050         } else if (type == "file") {
00051                 return new FileBuffer(param);
00052         } else if (type == "null") {
00053                 int size = atoi(param.c_str());
00054                 return new NullBuffer(size);
00055         } else {
00056                 return NULL;
00057         }
00058 }