Download | Plain Text | No Line Numbers


  1. /*
  2.  * Copyright (c) 2010, Manuel Mausz. All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * - Redistributions of source code must retain the above copyright
  9.  * notice, this list of conditions and the following disclaimer.
  10.  *
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  * notice, this list of conditions and the following disclaimer in the
  13.  * documentation and/or other materials provided with the distribution.
  14.  *
  15.  * - The names of the authors may not be used to endorse or promote products
  16.  * derived from this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  20.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  */
  30.  
  31. import java.rmi.server.UnicastRemoteObject;
  32. import java.rmi.RemoteException;
  33. import java.util.Date;
  34.  
  35. /*
  36.  * client --> server interface implementation
  37.  *
  38.  * @author Manuel Mausz (0728348)
  39.  */
  40. public class C2SInterfaceImpl
  41. extends UnicastRemoteObject
  42. implements C2SInterface
  43. {
  44. private Server server;
  45. private Server.UserRecord user = null;
  46.  
  47. /*--------------------------------------------------------------------------*/
  48.  
  49. C2SInterfaceImpl(Server server)
  50. throws RemoteException
  51. {
  52. this.server = server;
  53. }
  54.  
  55. /*--------------------------------------------------------------------------*/
  56.  
  57. public boolean register(String username, String pass)
  58. throws RemoteException
  59. {
  60. if (user != null)
  61. throw new RemoteException("Unable to register. Already logged in.");
  62. return server.users.register(username, pass);
  63. }
  64.  
  65. /*--------------------------------------------------------------------------*/
  66.  
  67. public boolean login(String username, String pass, S2CInterface s2ciface)
  68. throws RemoteException
  69. {
  70. if (user != null)
  71. throw new RemoteException("Unable to log in. Already logged in.");
  72. user = server.users.login(username, pass, s2ciface);
  73. if (user == null)
  74. return false;
  75. return true;
  76. }
  77.  
  78. /*--------------------------------------------------------------------------*/
  79.  
  80. public void checkLoggedIn()
  81. throws RemoteException
  82. {
  83. if (user == null)
  84. throw new RemoteException("Not logged in.");
  85. }
  86.  
  87. /*--------------------------------------------------------------------------*/
  88.  
  89. public boolean create(String event, String location, int duration)
  90. throws RemoteException
  91. {
  92. checkLoggedIn();
  93. return server.events.create(user, event, location, duration);
  94. }
  95.  
  96. /*--------------------------------------------------------------------------*/
  97.  
  98. public void invite(String event, String invitee)
  99. throws RemoteException
  100. {
  101. checkLoggedIn();
  102. server.events.invite(user, event, invitee);
  103. }
  104.  
  105. /*--------------------------------------------------------------------------*/
  106.  
  107. public void addDate(String event, Date date)
  108. throws RemoteException
  109. {
  110. checkLoggedIn();
  111. server.events.addDate(user, event, date);
  112. }
  113.  
  114. /*--------------------------------------------------------------------------*/
  115.  
  116. public String event2String(String event)
  117. throws RemoteException
  118. {
  119. checkLoggedIn();
  120. return server.events.toString(event);
  121. }
  122.  
  123. /*--------------------------------------------------------------------------*/
  124.  
  125. public void vote(String event, Date[] dates)
  126. throws RemoteException
  127. {
  128. checkLoggedIn();
  129. synchronized(user)
  130. {
  131. server.events.vote(event, user.name, dates);
  132. }
  133. }
  134.  
  135. /*--------------------------------------------------------------------------*/
  136.  
  137. public void finalize(String event)
  138. throws RemoteException
  139. {
  140. checkLoggedIn();
  141. server.events.finalize(user, event);
  142. }
  143.  
  144. /*--------------------------------------------------------------------------*/
  145.  
  146. public boolean logout()
  147. throws RemoteException
  148. {
  149. checkLoggedIn();
  150. server.users.logout(user);
  151. user = null;
  152. return true;
  153. }
  154. }
  155.