00001 /***************************** License ********************************** 00002 00003 Copyright (C) 2008 by Communicative Machines 00004 http://www.cmlabs.com All rights reserved. 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 00020 ************************************************************************/ 00021 // NetProtocol.cpp: implementation of the NetProtocol class. 00022 // 00023 ////////////////////////////////////////////////////////////////////// 00024 00025 #include "NetProtocol.h" 00026 00027 namespace cmlabs { 00028 00029 ////////////////////////////////////////////////////////////////////// 00030 // Construction/Destruction 00031 ////////////////////////////////////////////////////////////////////// 00032 00033 NetProtocol::NetProtocol(JString pname) 00034 { 00035 currentUnsentBufferSize = 0; 00036 currentSentBufferSize = 0; 00037 currentUnreceivedBufferSize = 0; 00038 currentReceivedBufferSize = 0; 00039 00040 name = pname; 00041 netTimeout = 10000; 00042 prev = next = NULL; 00043 isLocalCallback = false; 00044 isRemoteCallback = false; 00045 } 00046 00047 NetProtocol::~NetProtocol() 00048 { 00049 00050 } 00051 00052 bool NetProtocol::initializeAsReceiver(JSocket* socket, JString from) { 00053 return false; 00054 } 00055 00056 bool NetProtocol::equals(NetProtocol* protocol) { 00057 return ( (protocol != NULL) && (protocol->name.equals(name)) ); 00058 } 00059 00060 bool NetProtocol::checkBufferForCompatibility(char* buffer, int length) { 00061 return false; 00062 } 00063 00064 bool NetProtocol::sendObject(JSocket* socket, Object* obj, bool isReply) { 00065 Message* msg = new Message(); 00066 msg->setObject(obj); 00067 bool res = sendObject(socket, msg, isReply); 00068 delete(msg); 00069 return res; 00070 } 00071 00072 bool NetProtocol::sendObject(JSocket* socket, Message* msg, bool isReply) { 00073 return false; 00074 } 00075 00076 Message* NetProtocol::receiveObject(JSocket* socket, int timeout) { 00077 wait(timeout); 00078 return NULL; 00079 } 00080 00081 double NetProtocol::getReceiveProgress() { 00082 if (currentUnreceivedBufferSize == 0) 00083 return 1; 00084 else if (currentUnreceivedBufferSize == currentReceivedBufferSize) 00085 return 1; 00086 else if (currentUnreceivedBufferSize > currentReceivedBufferSize) 00087 return ((double)currentReceivedBufferSize)/((double)currentUnreceivedBufferSize); 00088 else 00089 return 1; 00090 } 00091 00092 double NetProtocol::getSendProgress() { 00093 if (currentUnsentBufferSize == 0) 00094 return 1; 00095 else if (currentUnsentBufferSize == currentSentBufferSize) 00096 return 1; 00097 else if (currentUnsentBufferSize > currentSentBufferSize) 00098 return ((double)currentSentBufferSize)/((double)currentUnsentBufferSize); 00099 else 00100 return 1; 00101 } 00102 00103 00104 } // namespace cmlabs