MASA-Core
URLCellsReader.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 "URLCellsReader.hpp"
00023 
00024 #include "FileCellsReader.hpp"
00025 #include "DummyCellsReader.hpp"
00026 #include "SocketCellsReader.hpp"
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 
00031 URLCellsReader::URLCellsReader(string url) {
00032         int pos1 = url.find_first_of("://");
00033         if (pos1 == -1) {
00034                 fprintf(stderr, "URLCellsReader: Wrong URL format: %s\n", url.c_str());
00035                 exit(1);
00036         }
00037         string type = url.substr(0, pos1);
00038         string param = url.substr(pos1+3);
00039 
00040 
00041         fprintf(stderr, "%s:   %s - %s\n", url.c_str(), type.c_str(), param.c_str());
00042         if (type == "socket") {
00043                 int port;
00044                 string hostname;
00045                 int pos2 = param.find_first_of(":");
00046                 if (pos2 > 0) {
00047                         port = atoi(param.substr(pos2+1).c_str());
00048                         hostname = param.substr(0, pos2);
00049                 } else {
00050                         hostname = param;
00051                 }
00052                 reader = new SocketCellsReader(hostname, port);
00053         } else if (type == "file") {
00054                 reader = new FileCellsReader(param);
00055         } else if (type == "null") {
00056                 int size = atoi(param.c_str());
00057                 reader = new DummyCellsReader(size);
00058         } else {
00059                 fprintf(stderr, "URLCellsReader: Unknown reader type: %s\n", type.c_str());
00060                 exit(1);
00061         }
00062 }
00063 
00064 URLCellsReader::~URLCellsReader() {
00065         close();
00066 }
00067 
00068 void URLCellsReader::close() {
00069         if (reader != NULL) {
00070                 reader->close();
00071                 reader = NULL;
00072         }
00073 }
00074 
00075 int URLCellsReader::getType() {
00076         return INIT_WITH_CUSTOM_DATA;
00077 }
00078 
00079 int URLCellsReader::read(cell_t* buf, int len) {
00080         return reader->read(buf, len);
00081 }